Tests
This commit is contained in:
@@ -0,0 +1,17 @@
|
|||||||
|
using ApplicationLayer.Services.Applicants.Models;
|
||||||
|
using Bogus;
|
||||||
|
|
||||||
|
namespace VisaApi.Fakers.Applicants.Requests
|
||||||
|
{
|
||||||
|
public sealed class NameModelFaker : Faker<NameModel>
|
||||||
|
{
|
||||||
|
public NameModelFaker()
|
||||||
|
{
|
||||||
|
RuleFor(m => m.FirstName, f => f.Name.FirstName());
|
||||||
|
|
||||||
|
RuleFor(m => m.Surname, f => f.Name.LastName());
|
||||||
|
|
||||||
|
RuleFor(m => m.Patronymic, f => f.Name.FirstName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
15
SchengenVisaApi/VisaApiTests/Fakers/Auth/AuthDataFaker.cs
Normal file
15
SchengenVisaApi/VisaApiTests/Fakers/Auth/AuthDataFaker.cs
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
using ApplicationLayer.Services.AuthServices.Common;
|
||||||
|
using Bogus;
|
||||||
|
|
||||||
|
namespace VisaApi.Fakers.Auth
|
||||||
|
{
|
||||||
|
public sealed class AuthDataFaker : Faker<AuthData>
|
||||||
|
{
|
||||||
|
public AuthDataFaker()
|
||||||
|
{
|
||||||
|
RuleFor(a => a.Email, f => f.Internet.Email());
|
||||||
|
|
||||||
|
RuleFor(a => a.Password, f => f.Internet.Password());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
using ApplicationLayer.Services.AuthServices.Requests;
|
||||||
|
using Bogus;
|
||||||
|
|
||||||
|
namespace VisaApi.Fakers.Auth
|
||||||
|
{
|
||||||
|
public sealed class RegisterRequestFaker : Faker<RegisterRequest>
|
||||||
|
{
|
||||||
|
private static AuthDataFaker authDataFaker = new();
|
||||||
|
|
||||||
|
public RegisterRequestFaker()
|
||||||
|
{
|
||||||
|
RuleFor(r => r.AuthData, () => authDataFaker.Generate());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,171 @@
|
|||||||
|
using System.Text;
|
||||||
|
using ApplicationLayer.Services.Applicants.Models;
|
||||||
|
using ApplicationLayer.Services.Applicants.Models.Validation;
|
||||||
|
using Domains;
|
||||||
|
using FluentAssertions;
|
||||||
|
using FluentValidation;
|
||||||
|
using VisaApi.Fakers.Applicants.Requests;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace VisaApi.Tests.Application.Validation.Applicants
|
||||||
|
{
|
||||||
|
public class NameValidatorTests
|
||||||
|
{
|
||||||
|
private static IValidator<NameModel> validator = new NameModelValidator();
|
||||||
|
private static NameModelFaker faker = new();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Test for <see cref="NameModel"/> validator that should throw for empty first name
|
||||||
|
/// </summary>
|
||||||
|
[Fact]
|
||||||
|
private async Task ValidateForEmptyFirstNameShouldThrow()
|
||||||
|
{
|
||||||
|
var name = faker.Generate();
|
||||||
|
name.FirstName = null!;
|
||||||
|
|
||||||
|
var result = await validator.ValidateAsync(name);
|
||||||
|
|
||||||
|
result.Errors.Where(error => error.PropertyName == nameof(name.FirstName))
|
||||||
|
.Should().HaveCount(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Test for <see cref="NameModel"/> validator that should throw for empty surname
|
||||||
|
/// </summary>
|
||||||
|
[Fact]
|
||||||
|
private async Task ValidateForEmptySurnameShouldThrow()
|
||||||
|
{
|
||||||
|
var name = faker.Generate();
|
||||||
|
name.Surname = null!;
|
||||||
|
|
||||||
|
var result = await validator.ValidateAsync(name);
|
||||||
|
|
||||||
|
result.Errors.Where(error => error.PropertyName == nameof(name.Surname))
|
||||||
|
.Should().HaveCount(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Test for <see cref="NameModel"/> validator that should return no errors for empty patronymic
|
||||||
|
/// </summary>
|
||||||
|
[Fact]
|
||||||
|
private async Task ValidateForEmptyPatronymicShouldReturnNoErrors()
|
||||||
|
{
|
||||||
|
var name = faker.Generate();
|
||||||
|
name.Patronymic = null;
|
||||||
|
|
||||||
|
var result = await validator.ValidateAsync(name);
|
||||||
|
|
||||||
|
result.Errors.Where(error => error.PropertyName == nameof(name.Patronymic))
|
||||||
|
.Should().BeEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Test for <see cref="NameModel"/> validator that should return error for too long first name
|
||||||
|
/// </summary>
|
||||||
|
[Fact]
|
||||||
|
private async Task ValidateForLongFirstNameShouldReturnError()
|
||||||
|
{
|
||||||
|
var name = faker.Generate();
|
||||||
|
var stringBuilder = new StringBuilder();
|
||||||
|
stringBuilder.Append('h', ConfigurationConstraints.NameLength + 1);
|
||||||
|
name.FirstName = stringBuilder.ToString();
|
||||||
|
|
||||||
|
var result = await validator.ValidateAsync(name);
|
||||||
|
|
||||||
|
result.Errors.Where(error => error.PropertyName == nameof(name.FirstName))
|
||||||
|
.Should().HaveCount(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Test for <see cref="NameModel"/> validator that should return error for too long surname
|
||||||
|
/// </summary>
|
||||||
|
[Fact]
|
||||||
|
private async Task ValidateForLongSurnameShouldReturnError()
|
||||||
|
{
|
||||||
|
var name = faker.Generate();
|
||||||
|
var stringBuilder = new StringBuilder();
|
||||||
|
stringBuilder.Append('h', ConfigurationConstraints.NameLength + 1);
|
||||||
|
name.Surname = stringBuilder.ToString();
|
||||||
|
|
||||||
|
var result = await validator.ValidateAsync(name);
|
||||||
|
|
||||||
|
result.Errors.Where(error => error.PropertyName == nameof(name.Surname))
|
||||||
|
.Should().HaveCount(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Test for <see cref="NameModel"/> validator that should return error for too long patronymic
|
||||||
|
/// </summary>
|
||||||
|
[Fact]
|
||||||
|
private async Task ValidateForLongPatronymicShouldReturnError()
|
||||||
|
{
|
||||||
|
var name = faker.Generate();
|
||||||
|
var stringBuilder = new StringBuilder();
|
||||||
|
stringBuilder.Append('h', ConfigurationConstraints.NameLength + 1);
|
||||||
|
name.Patronymic = stringBuilder.ToString();
|
||||||
|
|
||||||
|
var result = await validator.ValidateAsync(name);
|
||||||
|
|
||||||
|
result.Errors.Where(error => error.PropertyName == nameof(name.Patronymic))
|
||||||
|
.Should().HaveCount(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Test for <see cref="NameModel"/> validator that should return error for not valid firstname
|
||||||
|
/// </summary>
|
||||||
|
[Fact]
|
||||||
|
private async Task ValidateForNotValidFirstNameShouldReturnError()
|
||||||
|
{
|
||||||
|
var name = faker.Generate();
|
||||||
|
name.FirstName = "&&7!**|";
|
||||||
|
|
||||||
|
var result = await validator.ValidateAsync(name);
|
||||||
|
|
||||||
|
result.Errors.Where(error => error.PropertyName == nameof(name.FirstName))
|
||||||
|
.Should().HaveCount(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Test for <see cref="NameModel"/> validator that should return error for not valid surname
|
||||||
|
/// </summary>
|
||||||
|
[Fact]
|
||||||
|
private async Task ValidateForNotValidSurnameShouldReturnError()
|
||||||
|
{
|
||||||
|
var name = faker.Generate();
|
||||||
|
name.Surname = "&&7!**|";
|
||||||
|
|
||||||
|
var result = await validator.ValidateAsync(name);
|
||||||
|
|
||||||
|
result.Errors.Where(error => error.PropertyName == nameof(name.Surname))
|
||||||
|
.Should().HaveCount(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Test for <see cref="NameModel"/> validator that should return error for not valid patronymic
|
||||||
|
/// </summary>
|
||||||
|
[Fact]
|
||||||
|
private async Task ValidateForNotValidPatronymicShouldReturnError()
|
||||||
|
{
|
||||||
|
var name = faker.Generate();
|
||||||
|
name.Patronymic = "&&7!**|";
|
||||||
|
|
||||||
|
var result = await validator.ValidateAsync(name);
|
||||||
|
|
||||||
|
result.Errors.Where(error => error.PropertyName == nameof(name.Patronymic))
|
||||||
|
.Should().HaveCount(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Test for <see cref="NameModel"/> validator that should return no errors for valid name
|
||||||
|
/// </summary>
|
||||||
|
[Fact]
|
||||||
|
private async Task ValidateForValidNameShouldReturnNoErrors()
|
||||||
|
{
|
||||||
|
var name = faker.Generate();
|
||||||
|
|
||||||
|
var result = await validator.ValidateAsync(name);
|
||||||
|
|
||||||
|
result.Errors.Should().BeEmpty();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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