Tests
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
using System.Text;
|
||||
using ApplicationLayer.Services.AuthServices.Common;
|
||||
using ApplicationLayer.Services.AuthServices.Requests.Validation;
|
||||
using Domains;
|
||||
using FluentAssertions;
|
||||
using FluentValidation;
|
||||
using VisaApi.Fakers.Auth;
|
||||
using Xunit;
|
||||
|
||||
namespace VisaApi.Tests.Application.Validation.Auth
|
||||
{
|
||||
public class AuthDataValidatorTests
|
||||
{
|
||||
private readonly static IValidator<AuthData> validator = new AuthDataValidator();
|
||||
private readonly static AuthDataFaker faker = new();
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="AuthData"/> validator that should return validation error for invalid email
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForInvalidEmailShouldReturnError()
|
||||
{
|
||||
var authData = faker.Generate();
|
||||
authData.Email = "alsdas'dsa";
|
||||
|
||||
var result = await validator.ValidateAsync(authData);
|
||||
|
||||
result.Errors.Should()
|
||||
.HaveCount(1)
|
||||
.And.Contain(error => error.PropertyName == nameof(authData.Email));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="AuthData"/> validator that should return validation error for too long email
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForLongEmailShouldReturnError()
|
||||
{
|
||||
var authData = faker.Generate();
|
||||
var stringBuilder = new StringBuilder();
|
||||
stringBuilder.Append('d', ConfigurationConstraints.EmailLength);
|
||||
stringBuilder.Append("@mail.ru");
|
||||
authData.Email = stringBuilder.ToString();
|
||||
|
||||
var result = await validator.ValidateAsync(authData);
|
||||
|
||||
result.Errors.Should()
|
||||
.HaveCount(1)
|
||||
.And.Contain(error => error.PropertyName == nameof(authData.Email));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="AuthData"/> validator that should return no errors for valid email
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForValidEmailShouldReturnNoError()
|
||||
{
|
||||
var authData = faker.Generate();
|
||||
|
||||
var result = await validator.ValidateAsync(authData);
|
||||
|
||||
result.Errors.Where(error => error.PropertyName == nameof(authData.Email))
|
||||
.Should().BeEmpty();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="AuthData"/> validator that should return validation error for empty password
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForEmptyPasswordShouldReturnError()
|
||||
{
|
||||
var authData = faker.Generate();
|
||||
authData.Password = string.Empty;
|
||||
|
||||
var result = await validator.ValidateAsync(authData);
|
||||
|
||||
result.Errors.Should()
|
||||
.HaveCount(1)
|
||||
.And.Contain(error => error.PropertyName == nameof(authData.Password));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="AuthData"/> validator that should return validation error for too long password
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForLongPasswordShouldReturnError()
|
||||
{
|
||||
var authData = faker.Generate();
|
||||
var stringBuilder = new StringBuilder();
|
||||
stringBuilder.Append('d', ConfigurationConstraints.PasswordLength + 1);
|
||||
authData.Password = stringBuilder.ToString();
|
||||
|
||||
var result = await validator.ValidateAsync(authData);
|
||||
|
||||
result.Errors.Should()
|
||||
.HaveCount(1)
|
||||
.And.Contain(error => error.PropertyName == nameof(authData.Password));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="AuthData"/> validator that should return no errors for valid password
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForValidPasswordShouldReturnNoError()
|
||||
{
|
||||
var authData = faker.Generate();
|
||||
|
||||
var result = await validator.ValidateAsync(authData);
|
||||
|
||||
result.Errors.Where(error => error.PropertyName == nameof(authData.Password))
|
||||
.Should().BeEmpty();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
using ApplicationLayer.InfrastructureServicesInterfaces;
|
||||
using ApplicationLayer.Services.AuthServices.Common;
|
||||
using ApplicationLayer.Services.AuthServices.Requests;
|
||||
using ApplicationLayer.Services.AuthServices.Requests.Validation;
|
||||
using FluentAssertions;
|
||||
using FluentValidation;
|
||||
using Infrastructure.Database;
|
||||
using Infrastructure.Database.Users.Repositories;
|
||||
using VisaApi.Fakers.Auth;
|
||||
using VisaApi.Fakers.Users;
|
||||
using VisaApi.Tests.Infrastructure.Database;
|
||||
using Xunit;
|
||||
|
||||
namespace VisaApi.Tests.Application.Validation.Auth
|
||||
{
|
||||
[Collection(Collections.ContextUsingTestCollection)]
|
||||
public class RegisterRequestValidatorTests
|
||||
{
|
||||
private readonly static IValidator<AuthData> authDataValidator = new AuthDataValidator();
|
||||
private readonly static RegisterRequestFaker requestFaker = new();
|
||||
private readonly static UserFaker userFaker = new();
|
||||
|
||||
/// <summary>
|
||||
/// Creates validator from context
|
||||
/// </summary>
|
||||
/// <param name="context">db context</param>
|
||||
/// <returns>RegisterRequest validator</returns>
|
||||
private static IValidator<RegisterRequest> GetValidator(DbContext context)
|
||||
{
|
||||
var repository = new UsersRepository(context, context);
|
||||
return new RegisterRequestValidator(repository, authDataValidator);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="RegisterRequest"/> validator that should throw for empty auth data
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForEmptyAuthDataShouldThrow()
|
||||
{
|
||||
var context = InMemoryContextProvider.GetDbContext();
|
||||
var validator = GetValidator(context);
|
||||
var request = requestFaker.Generate();
|
||||
request.AuthData = null!;
|
||||
NullReferenceException? result = null;
|
||||
|
||||
try
|
||||
{
|
||||
await validator.ValidateAsync(request);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
result = e as NullReferenceException;
|
||||
}
|
||||
|
||||
result.Should().NotBeNull();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="RegisterRequest"/> validator that should return error for used email
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForUsedEmailShouldReturnError()
|
||||
{
|
||||
var context = InMemoryContextProvider.GetDbContext();
|
||||
var validator = GetValidator(context);
|
||||
var user = userFaker.Generate();
|
||||
await context.AddAsync(user);
|
||||
await context.SaveChangesAsync();
|
||||
var request = requestFaker.Generate();
|
||||
request.AuthData.Email = user.Email;
|
||||
|
||||
var result = await validator.ValidateAsync(request);
|
||||
|
||||
result.Errors.Should()
|
||||
.Contain(error => error.PropertyName == nameof(request.AuthData))
|
||||
.And.HaveCount(1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="RegisterRequest"/> validator that should return o errors for valid requests
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForValidRequestShouldReturnNoErrors()
|
||||
{
|
||||
var context = InMemoryContextProvider.GetDbContext();
|
||||
var validator = GetValidator(context);
|
||||
var request = requestFaker.Generate();
|
||||
|
||||
var result = await validator.ValidateAsync(request);
|
||||
|
||||
result.Errors.Should().BeEmpty();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user