Вытащил солюшен на уровень выше, чтобы прощё было дотнетить
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
@@ -0,0 +1,170 @@
|
||||
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 NameModelValidatorTests
|
||||
{
|
||||
private readonly static IValidator<NameModel> validator = new NameModelValidator();
|
||||
private readonly 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,174 @@
|
||||
using System.Text;
|
||||
using ApplicationLayer.InfrastructureServicesInterfaces;
|
||||
using ApplicationLayer.Services.Applicants.Models;
|
||||
using ApplicationLayer.Services.Applicants.Models.Validation;
|
||||
using Domains;
|
||||
using FluentAssertions;
|
||||
using FluentValidation;
|
||||
using VisaApi.Fakers.Applicants.Requests;
|
||||
using VisaApi.Services;
|
||||
using Xunit;
|
||||
|
||||
namespace VisaApi.Tests.Application.Validation.Applicants;
|
||||
|
||||
public class PassportModelValidatorTests
|
||||
{
|
||||
private readonly static IDateTimeProvider dateTimeProvider = new TestDateTimeProvider();
|
||||
private readonly static IValidator<PassportModel> validator = new PassportModelValidator(dateTimeProvider);
|
||||
private readonly static PassportModelFaker faker = new();
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="PassportModel"/> validator that should return error for empty number
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForEmptyNumberShouldReturnError()
|
||||
{
|
||||
var model = faker.Generate();
|
||||
model.Number = string.Empty;
|
||||
|
||||
var result = await validator.ValidateAsync(model);
|
||||
|
||||
result.Errors.Where(error => error.PropertyName == nameof(model.Number))
|
||||
.Should().HaveCount(1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="PassportModel"/> validator that should return error for too long number
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForLongNumberShouldReturnError()
|
||||
{
|
||||
var model = faker.Generate();
|
||||
var stringBuilder = new StringBuilder();
|
||||
stringBuilder.Append('d', ConfigurationConstraints.PassportNumberLength + 1);
|
||||
model.Number = stringBuilder.ToString();
|
||||
|
||||
var result = await validator.ValidateAsync(model);
|
||||
|
||||
result.Errors.Where(error => error.PropertyName == nameof(model.Number))
|
||||
.Should().HaveCount(1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="PassportModel"/> validator that should return error for not valid number
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForNotValidNumberShouldReturnError()
|
||||
{
|
||||
var model = faker.Generate();
|
||||
model.Number = "&?%$24asd\\]|";
|
||||
|
||||
var result = await validator.ValidateAsync(model);
|
||||
|
||||
result.Errors.Where(error => error.PropertyName == nameof(model.Number))
|
||||
.Should().HaveCount(1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="PassportModel"/> validator that should return error for empty issuer
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForEmptyIssuerShouldReturnError()
|
||||
{
|
||||
var model = faker.Generate();
|
||||
model.Issuer = string.Empty;
|
||||
|
||||
var result = await validator.ValidateAsync(model);
|
||||
|
||||
result.Errors.Where(error => error.PropertyName == nameof(model.Issuer))
|
||||
.Should().HaveCount(1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="PassportModel"/> validator that should return error for too long issuer
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForLongIssuerShouldReturnError()
|
||||
{
|
||||
var model = faker.Generate();
|
||||
var stringBuilder = new StringBuilder();
|
||||
stringBuilder.Append('d', ConfigurationConstraints.IssuerNameLength + 1);
|
||||
model.Issuer = stringBuilder.ToString();
|
||||
|
||||
var result = await validator.ValidateAsync(model);
|
||||
|
||||
result.Errors.Where(error => error.PropertyName == nameof(model.Issuer))
|
||||
.Should().HaveCount(1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="PassportModel"/> validator that should return error for not valid issuer
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForNotValidIssuerShouldReturnError()
|
||||
{
|
||||
var model = faker.Generate();
|
||||
model.Issuer = "&?%$24asd\\]|";
|
||||
|
||||
var result = await validator.ValidateAsync(model);
|
||||
|
||||
result.Errors.Where(error => error.PropertyName == nameof(model.Issuer))
|
||||
.Should().HaveCount(1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="PassportModel"/> validator that should return error for expired passport
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForExpiredPassportShouldReturnError()
|
||||
{
|
||||
var model = faker.Generate();
|
||||
model.ExpirationDate = dateTimeProvider.Now().AddDays(-10);
|
||||
model.IssueDate = model.ExpirationDate.AddDays(-10);
|
||||
|
||||
var result = await validator.ValidateAsync(model);
|
||||
|
||||
result.Errors.Where(error => error.PropertyName == nameof(model.ExpirationDate))
|
||||
.Should().HaveCount(1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="PassportModel"/> validator that should return error for passport from future
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForPassportFromFutureShouldReturnError()
|
||||
{
|
||||
var model = faker.Generate();
|
||||
model.ExpirationDate = dateTimeProvider.Now().AddDays(10);
|
||||
model.IssueDate = model.ExpirationDate.AddDays(-3);
|
||||
|
||||
var result = await validator.ValidateAsync(model);
|
||||
|
||||
result.Errors.Where(error => error.PropertyName == nameof(model.IssueDate))
|
||||
.Should().HaveCount(1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="PassportModel"/> validator that should return error for passport that expired before issue
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForPassportExpiredBeforeIssueShouldReturnError()
|
||||
{
|
||||
var model = faker.Generate();
|
||||
model.ExpirationDate = dateTimeProvider.Now().AddDays(10);
|
||||
model.IssueDate = model.ExpirationDate.AddDays(3);
|
||||
|
||||
var result = await validator.ValidateAsync(model);
|
||||
|
||||
result.Errors.Where(error => error.PropertyName == nameof(model.IssueDate))
|
||||
.Should().HaveCount(1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="PassportModel"/> validator that should return no errors for valid passport
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForValidPassportShouldReturnNoErrors()
|
||||
{
|
||||
var model = faker.Generate();
|
||||
|
||||
var result = await validator.ValidateAsync(model);
|
||||
|
||||
result.Errors.Should().BeEmpty();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,353 @@
|
||||
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 PlaceOfWorkModelValidatorTests
|
||||
{
|
||||
private readonly static IValidator<PlaceOfWorkModel> validator = new PlaceOfWorkModelValidator();
|
||||
private readonly static PlaceOfWorkModelFaker faker = new();
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="PlaceOfWorkModel"/> validator that should return error for empty phone num
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForEmptyPhoneNumShouldReturnError()
|
||||
{
|
||||
var model = faker.Generate();
|
||||
model.PhoneNum = string.Empty;
|
||||
|
||||
var result = await validator.ValidateAsync(model);
|
||||
|
||||
result.Errors.Where(error => error.PropertyName == nameof(model.PhoneNum))
|
||||
.Should().NotBeEmpty();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="PlaceOfWorkModel"/> validator that should return error for long phone num
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForLongPhoneNumShouldReturnError()
|
||||
{
|
||||
var model = faker.Generate();
|
||||
var stringBuilder = new StringBuilder();
|
||||
stringBuilder.Append('8', ConfigurationConstraints.PhoneNumberLength + 1);
|
||||
model.PhoneNum = stringBuilder.ToString();
|
||||
|
||||
var result = await validator.ValidateAsync(model);
|
||||
|
||||
result.Errors.Where(error => error.PropertyName == nameof(model.PhoneNum))
|
||||
.Should().NotBeEmpty();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="PlaceOfWorkModel"/> validator that should return error for short phone num
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForShortPhoneNumShouldReturnError()
|
||||
{
|
||||
var model = faker.Generate();
|
||||
var stringBuilder = new StringBuilder();
|
||||
stringBuilder.Append('8', ConfigurationConstraints.PhoneNumberMinLength - 1);
|
||||
model.PhoneNum = stringBuilder.ToString();
|
||||
|
||||
var result = await validator.ValidateAsync(model);
|
||||
|
||||
result.Errors.Where(error => error.PropertyName == nameof(model.PhoneNum))
|
||||
.Should()
|
||||
.HaveCount(1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="PlaceOfWorkModel"/> validator that should return error for not valid phone num
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForNotValidPhoneNumShouldReturnError()
|
||||
{
|
||||
var model = faker.Generate();
|
||||
var stringBuilder = new StringBuilder();
|
||||
stringBuilder.Append('a', ConfigurationConstraints.PhoneNumberMinLength);
|
||||
model.PhoneNum = stringBuilder.ToString();
|
||||
|
||||
var result = await validator.ValidateAsync(model);
|
||||
|
||||
result.Errors.Where(error => error.PropertyName == nameof(model.PhoneNum))
|
||||
.Should()
|
||||
.HaveCount(1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="PlaceOfWorkModel"/> validator that should throw exception for null address
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForEmptyAddressShouldThrow()
|
||||
{
|
||||
var model = faker.Generate();
|
||||
model.Address = null!;
|
||||
NullReferenceException? result = null;
|
||||
|
||||
try
|
||||
{
|
||||
await validator.ValidateAsync(model);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
result = e as NullReferenceException;
|
||||
}
|
||||
|
||||
result.Should().NotBeNull();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="PlaceOfWorkModel"/> validator that should return error for empty Country
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForEmptyCountryShouldReturnError()
|
||||
{
|
||||
var model = faker.Generate();
|
||||
model.Address.Country = "";
|
||||
|
||||
var result = await validator.ValidateAsync(model);
|
||||
|
||||
result.Errors.Where(error => error.PropertyName == "Address.Country")
|
||||
.Should().HaveCount(1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="PlaceOfWorkModel"/> validator that should return error for too long Country
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForLongCountryShouldReturnError()
|
||||
{
|
||||
var model = faker.Generate();
|
||||
var stringBuilder = new StringBuilder();
|
||||
stringBuilder.Append('a', ConfigurationConstraints.CountryNameLength + 1);
|
||||
model.Address.Country = stringBuilder.ToString();
|
||||
|
||||
var result = await validator.ValidateAsync(model);
|
||||
|
||||
result.Errors.Where(error => error.PropertyName == "Address.Country")
|
||||
.Should().HaveCount(1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="PlaceOfWorkModel"/> validator that should return error for not valid Country
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForNotValidCountryShouldReturnError()
|
||||
{
|
||||
var model = faker.Generate();
|
||||
model.Address.Country = "|&%";
|
||||
|
||||
var result = await validator.ValidateAsync(model);
|
||||
|
||||
result.Errors.Where(error => error.PropertyName == "Address.Country")
|
||||
.Should().HaveCount(1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="PlaceOfWorkModel"/> validator that should return error for empty City
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForEmptyCityShouldReturnError()
|
||||
{
|
||||
var model = faker.Generate();
|
||||
model.Address.City = "";
|
||||
|
||||
var result = await validator.ValidateAsync(model);
|
||||
|
||||
result.Errors.Where(error => error.PropertyName == "Address.City")
|
||||
.Should().HaveCount(1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="PlaceOfWorkModel"/> validator that should return error for too long city
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForLongCityShouldReturnError()
|
||||
{
|
||||
var model = faker.Generate();
|
||||
var stringBuilder = new StringBuilder();
|
||||
stringBuilder.Append('a', ConfigurationConstraints.CityNameLength + 1);
|
||||
model.Address.City = stringBuilder.ToString();
|
||||
|
||||
var result = await validator.ValidateAsync(model);
|
||||
|
||||
result.Errors.Where(error => error.PropertyName == "Address.City")
|
||||
.Should().HaveCount(1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="PlaceOfWorkModel"/> validator that should return error for not valid city
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForNotValidCityShouldReturnError()
|
||||
{
|
||||
var model = faker.Generate();
|
||||
model.Address.City = "|&%";
|
||||
|
||||
var result = await validator.ValidateAsync(model);
|
||||
|
||||
result.Errors.Where(error => error.PropertyName == "Address.City")
|
||||
.Should().HaveCount(1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="PlaceOfWorkModel"/> validator that should return error for empty street
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForEmptyStreetShouldReturnError()
|
||||
{
|
||||
var model = faker.Generate();
|
||||
model.Address.Street = "";
|
||||
|
||||
var result = await validator.ValidateAsync(model);
|
||||
|
||||
result.Errors.Where(error => error.PropertyName == "Address.Street")
|
||||
.Should().HaveCount(1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="PlaceOfWorkModel"/> validator that should return error for too long street
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForLongStreetShouldReturnError()
|
||||
{
|
||||
var model = faker.Generate();
|
||||
var stringBuilder = new StringBuilder();
|
||||
stringBuilder.Append('a', ConfigurationConstraints.StreetNameLength + 1);
|
||||
model.Address.Street = stringBuilder.ToString();
|
||||
|
||||
var result = await validator.ValidateAsync(model);
|
||||
|
||||
result.Errors.Where(error => error.PropertyName == "Address.Street")
|
||||
.Should().HaveCount(1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="PlaceOfWorkModel"/> validator that should return error for not valid street
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForNotValidStreetShouldReturnError()
|
||||
{
|
||||
var model = faker.Generate();
|
||||
model.Address.Street = "|&%";
|
||||
|
||||
var result = await validator.ValidateAsync(model);
|
||||
|
||||
result.Errors.Where(error => error.PropertyName == "Address.Street")
|
||||
.Should().HaveCount(1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="PlaceOfWorkModel"/> validator that should return error for empty building /// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForEmptyBuildingShouldReturnError()
|
||||
{
|
||||
var model = faker.Generate();
|
||||
model.Address.Building = "";
|
||||
|
||||
var result = await validator.ValidateAsync(model);
|
||||
|
||||
result.Errors.Where(error => error.PropertyName == "Address.Building")
|
||||
.Should().HaveCount(1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="PlaceOfWorkModel"/> validator that should return error for too long building
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForLongBuildingShouldReturnError()
|
||||
{
|
||||
var model = faker.Generate();
|
||||
var stringBuilder = new StringBuilder();
|
||||
stringBuilder.Append('a', ConfigurationConstraints.BuildingNumberLength + 1);
|
||||
model.Address.Building = stringBuilder.ToString();
|
||||
|
||||
var result = await validator.ValidateAsync(model);
|
||||
|
||||
result.Errors.Where(error => error.PropertyName == "Address.Building")
|
||||
.Should().HaveCount(1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="PlaceOfWorkModel"/> validator that should return error for not valid building
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForNotValidBuildingShouldReturnError()
|
||||
{
|
||||
var model = faker.Generate();
|
||||
model.Address.Building = "|&%";
|
||||
|
||||
var result = await validator.ValidateAsync(model);
|
||||
|
||||
result.Errors.Where(error => error.PropertyName == "Address.Building")
|
||||
.Should().HaveCount(1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="PlaceOfWorkModel"/> validator that should return error for empty name
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForEmptyNameShouldReturnError()
|
||||
{
|
||||
var model = faker.Generate();
|
||||
model.Name = "";
|
||||
|
||||
var result = await validator.ValidateAsync(model);
|
||||
|
||||
result.Errors.Where(error => error.PropertyName == nameof(model.Name))
|
||||
.Should().HaveCount(1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="PlaceOfWorkModel"/> validator that should return error for too long name
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForTooLongNameShouldReturnError()
|
||||
{
|
||||
var model = faker.Generate();
|
||||
var stringBuilder = new StringBuilder();
|
||||
stringBuilder.Append('g', ConfigurationConstraints.PlaceOfWorkNameLength + 1);
|
||||
model.Name = stringBuilder.ToString();
|
||||
|
||||
var result = await validator.ValidateAsync(model);
|
||||
|
||||
result.Errors.Where(error => error.PropertyName == nameof(model.Name))
|
||||
.Should().HaveCount(1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="PlaceOfWorkModel"/> validator that should return error for not valid name
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForNotValidNameShouldReturnError()
|
||||
{
|
||||
var model = faker.Generate();
|
||||
model.Name = "@$%&|";
|
||||
|
||||
var result = await validator.ValidateAsync(model);
|
||||
|
||||
result.Errors.Where(error => error.PropertyName == nameof(model.Name))
|
||||
.Should().HaveCount(1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="PlaceOfWorkModel"/> validator that should return no errors for valid model
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForValidShouldReturnNoErrors()
|
||||
{
|
||||
var model = faker.Generate();
|
||||
|
||||
var result = await validator.ValidateAsync(model);
|
||||
|
||||
result.Errors.Should().BeEmpty();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
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,73 @@
|
||||
using ApplicationLayer.Services.Applicants.Models.Validation;
|
||||
using ApplicationLayer.Services.AuthServices.Requests;
|
||||
using ApplicationLayer.Services.AuthServices.Requests.Validation;
|
||||
using Domains.ApplicantDomain;
|
||||
using FluentValidation.TestHelper;
|
||||
using Infrastructure.Database.Users.Repositories;
|
||||
using VisaApi.Fakers.Auth;
|
||||
using VisaApi.Services;
|
||||
using VisaApi.Tests.Infrastructure.Database;
|
||||
using Xunit;
|
||||
|
||||
namespace VisaApi.Tests.Application.Validation.Auth
|
||||
{
|
||||
public class RegisterApplicantRequestValidatorTests
|
||||
{
|
||||
private readonly RegisterApplicantRequestValidator validator;
|
||||
private RegisterApplicantRequestFaker requestFaker;
|
||||
|
||||
public RegisterApplicantRequestValidatorTests()
|
||||
{
|
||||
var context = InMemoryContextProvider.GetDbContext();
|
||||
var dateTimeProvider = new TestDateTimeProvider();
|
||||
requestFaker = new(dateTimeProvider);
|
||||
validator = new(
|
||||
dateTimeProvider,
|
||||
new NameModelValidator(),
|
||||
new RegisterRequestValidator(new UsersRepository(context, context), new AuthDataValidator()),
|
||||
new PassportModelValidator(dateTimeProvider),
|
||||
new PlaceOfWorkModelValidator()
|
||||
);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Validation should return no errors
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task ValidateShouldWork()
|
||||
{
|
||||
var request = requestFaker.Generate();
|
||||
|
||||
var result = await validator.TestValidateAsync(request);
|
||||
|
||||
result.ShouldNotHaveAnyValidationErrors();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Validation should return errors
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task ValidateShouldFail()
|
||||
{
|
||||
var request = new RegisterApplicantRequest
|
||||
{
|
||||
BirthDate = DateTime.Now,
|
||||
Citizenship = string.Empty,
|
||||
CitizenshipByBirth = string.Empty,
|
||||
CityOfBirth = string.Empty,
|
||||
CountryOfBirth = string.Empty,
|
||||
Gender = (Gender)123123,
|
||||
JobTitle = string.Empty,
|
||||
MaritalStatus = (MaritalStatus)123123,
|
||||
};
|
||||
|
||||
var result = await validator.TestValidateAsync(request);
|
||||
|
||||
var properties = request.GetType().GetProperties().Select(x => x.Name).Except([nameof(RegisterApplicantRequest.IsNonResident)]);
|
||||
foreach (var property in properties)
|
||||
{
|
||||
result.ShouldHaveValidationErrorFor(property);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
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(DatabaseContext 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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
using ApplicationLayer.Services.Users.Requests;
|
||||
using ApplicationLayer.Services.Users.Requests.Validation;
|
||||
using FluentAssertions;
|
||||
using FluentValidation;
|
||||
using VisaApi.Fakers.Users.Requests;
|
||||
using Xunit;
|
||||
|
||||
namespace VisaApi.Tests.Application.Validation.Users;
|
||||
|
||||
public class ChangeUserAuthDataRequestValidationTests
|
||||
{
|
||||
private readonly static IValidator<ChangeUserAuthDataRequest> validator = new ChangeUserAuthDataRequestValidator();
|
||||
private readonly static ChangeUserAuthDataRequestFaker faker = new();
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="ChangeUserAuthDataRequest"/> validator that should throw exception for empty auth data
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForEmptyAuthDataShouldThrow()
|
||||
{
|
||||
var request = faker.Generate();
|
||||
request.NewAuthData = null!;
|
||||
NullReferenceException? result = null;
|
||||
|
||||
try
|
||||
{
|
||||
await validator.ValidateAsync(request);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
result = e as NullReferenceException;
|
||||
}
|
||||
|
||||
result.Should().NotBeNull();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="ChangeUserAuthDataRequest"/> validator that should no errors for valid entity
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForValidShouldReturnNoErrors()
|
||||
{
|
||||
var request = faker.Generate();
|
||||
var result = await validator.ValidateAsync(request);
|
||||
|
||||
result.IsValid.Should().BeTrue();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
using System.Text;
|
||||
using ApplicationLayer.InfrastructureServicesInterfaces;
|
||||
using ApplicationLayer.Services.VisaApplications.Models;
|
||||
using ApplicationLayer.Services.VisaApplications.Models.Validation;
|
||||
using Domains;
|
||||
using FluentAssertions;
|
||||
using FluentValidation;
|
||||
using VisaApi.Fakers.VisaApplications.Requests;
|
||||
using VisaApi.Services;
|
||||
using Xunit;
|
||||
|
||||
namespace VisaApi.Tests.Application.Validation.VisaApplications
|
||||
{
|
||||
public class PastVisaModelValidatorTests
|
||||
{
|
||||
private readonly static IDateTimeProvider dateTimeProvider = new TestDateTimeProvider();
|
||||
private readonly static IValidator<PastVisaModel> validator = new PastVisaModelValidator(dateTimeProvider);
|
||||
private readonly static PastVisaModelFaker faker = new(dateTimeProvider);
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="PastVisaModel"/> validator that should return error for empty expiration date
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForEmptyExpirationDateShouldReturnError()
|
||||
{
|
||||
var model = faker.GenerateValid();
|
||||
model.ExpirationDate = new();
|
||||
|
||||
var result = await validator.ValidateAsync(model);
|
||||
|
||||
result.Errors.Where(error => error.PropertyName == nameof(model.ExpirationDate))
|
||||
.Should().HaveCount(2); //expected error + error because of expiration date less than issue date
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="PastVisaModel"/> validator that should return error for expiration date less than issue date
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForExpirationDateLessThanIssueDateShouldReturnError()
|
||||
{
|
||||
var model = faker.GenerateValid();
|
||||
model.ExpirationDate = model.IssueDate.AddDays(-4);
|
||||
|
||||
var result = await validator.ValidateAsync(model);
|
||||
|
||||
result.Errors.Where(error => error.PropertyName == nameof(model.ExpirationDate))
|
||||
.Should().HaveCount(1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="PastVisaModel"/> validator that should return error for empty issue date
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForEmptyIssueDateShouldReturnError()
|
||||
{
|
||||
var model = faker.GenerateValid();
|
||||
model.IssueDate = new();
|
||||
|
||||
var result = await validator.ValidateAsync(model);
|
||||
|
||||
result.Errors.Where(error => error.PropertyName == nameof(model.IssueDate))
|
||||
.Should().HaveCount(1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="PastVisaModel"/> validator that should return error for issue date greater than current date
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForIssueDateGreaterThanCurrentDateShouldReturnError()
|
||||
{
|
||||
var model = faker.GenerateValid();
|
||||
model.IssueDate = dateTimeProvider.Now().AddDays(4);
|
||||
|
||||
var result = await validator.ValidateAsync(model);
|
||||
|
||||
result.Errors.Where(error => error.PropertyName == nameof(model.IssueDate))
|
||||
.Should().HaveCount(1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="PastVisaModel"/> validator that should return error for empty name
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForEmptyNameShouldReturnError()
|
||||
{
|
||||
var model = faker.GenerateValid();
|
||||
model.Name = string.Empty;
|
||||
|
||||
var result = await validator.ValidateAsync(model);
|
||||
|
||||
result.Errors.Where(error => error.PropertyName == nameof(model.Name))
|
||||
.Should().HaveCount(1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="PastVisaModel"/> validator that should return error for too long name
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForTooLongNameShouldReturnError()
|
||||
{
|
||||
var model = faker.GenerateValid();
|
||||
var stringBuilder = new StringBuilder();
|
||||
stringBuilder.Append('d', ConfigurationConstraints.VisaNameLength + 1);
|
||||
model.Name = stringBuilder.ToString();
|
||||
|
||||
var result = await validator.ValidateAsync(model);
|
||||
|
||||
result.Errors.Where(error => error.PropertyName == nameof(model.Name))
|
||||
.Should().HaveCount(1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="PastVisaModel"/> validator that should return error for not valid name
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForNotValidNameShouldReturnError()
|
||||
{
|
||||
var model = faker.GenerateValid();
|
||||
model.Name = "|}{%^&";
|
||||
|
||||
var result = await validator.ValidateAsync(model);
|
||||
|
||||
result.Errors.Where(error => error.PropertyName == nameof(model.Name))
|
||||
.Should().HaveCount(1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="PastVisaModel"/> validator that should return no errors for valid model
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForValidShouldReturnNoErrors()
|
||||
{
|
||||
var model = faker.GenerateValid();
|
||||
|
||||
var result = await validator.ValidateAsync(model);
|
||||
|
||||
result.Errors.Should().BeEmpty();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
using System.Text;
|
||||
using ApplicationLayer.InfrastructureServicesInterfaces;
|
||||
using ApplicationLayer.Services.VisaApplications.Models;
|
||||
using ApplicationLayer.Services.VisaApplications.Models.Validation;
|
||||
using Domains;
|
||||
using FluentAssertions;
|
||||
using FluentValidation;
|
||||
using VisaApi.Fakers.VisaApplications.Requests;
|
||||
using VisaApi.Services;
|
||||
using Xunit;
|
||||
|
||||
namespace VisaApi.Tests.Application.Validation.VisaApplications
|
||||
{
|
||||
public class PastVisitModelValidatorTests
|
||||
{
|
||||
private readonly static IDateTimeProvider dateTimeProvider = new TestDateTimeProvider();
|
||||
private readonly static IValidator<PastVisitModel> validator = new PastVisitModelValidator(dateTimeProvider);
|
||||
private readonly static PastVisitModelFaker faker = new(dateTimeProvider);
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="PastVisitModel"/> validator that should return error for empty start date
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForEmptyStartDateShouldReturnError()
|
||||
{
|
||||
var model = faker.GenerateValid();
|
||||
model.StartDate = new();
|
||||
|
||||
var result = await validator.ValidateAsync(model);
|
||||
|
||||
result.Errors.Where(error => error.PropertyName == nameof(model.StartDate))
|
||||
.Should().HaveCount(1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="PastVisitModel"/> validator that should return error for start date greater than end date
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForStartDateGreaterThanEndDateShouldReturnError()
|
||||
{
|
||||
var model = faker.GenerateValid();
|
||||
model.EndDate = dateTimeProvider.Now().AddDays(-10);
|
||||
model.StartDate = model.EndDate.AddDays(4);
|
||||
|
||||
var result = await validator.ValidateAsync(model);
|
||||
|
||||
result.Errors.Where(error => error.PropertyName == nameof(model.StartDate))
|
||||
.Should().HaveCount(1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="PastVisitModel"/> validator that should return error for empty end date
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForEmptyEndDateShouldReturnError()
|
||||
{
|
||||
var model = faker.GenerateValid();
|
||||
model.EndDate = new();
|
||||
|
||||
var result = await validator.ValidateAsync(model);
|
||||
|
||||
result.Errors.Where(error => error.PropertyName == nameof(model.EndDate))
|
||||
.Should().HaveCount(1); //expected error + error because of end date less than start date
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="PastVisitModel"/> validator that should return error for start date greater than current date
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForStartDateGreaterThanCurrentDateShouldReturnError()
|
||||
{
|
||||
var model = faker.GenerateValid();
|
||||
model.StartDate = dateTimeProvider.Now().AddDays(4);
|
||||
model.EndDate = model.StartDate.AddDays(4);
|
||||
|
||||
var result = await validator.ValidateAsync(model);
|
||||
|
||||
result.Errors.Where(error => error.PropertyName == nameof(model.StartDate))
|
||||
.Should().HaveCount(1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="PastVisitModel"/> validator that should return error for empty destination Country
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForEmptyDestinationCountryShouldReturnError()
|
||||
{
|
||||
var model = faker.GenerateValid();
|
||||
model.DestinationCountry = string.Empty;
|
||||
|
||||
var result = await validator.ValidateAsync(model);
|
||||
|
||||
result.Errors.Where(error => error.PropertyName == nameof(model.DestinationCountry))
|
||||
.Should().HaveCount(1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="PastVisitModel"/> validator that should return error for too long destination Country
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForTooLongDestinationCountryShouldReturnError()
|
||||
{
|
||||
var model = faker.GenerateValid();
|
||||
var stringBuilder = new StringBuilder();
|
||||
stringBuilder.Append('d', ConfigurationConstraints.CountryNameLength + 1);
|
||||
model.DestinationCountry = stringBuilder.ToString();
|
||||
|
||||
var result = await validator.ValidateAsync(model);
|
||||
|
||||
result.Errors.Where(error => error.PropertyName == nameof(model.DestinationCountry))
|
||||
.Should().HaveCount(1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="PastVisitModel"/> validator that should return error for not valid destination Country
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForNotValidDestinationCountryShouldReturnError()
|
||||
{
|
||||
var model = faker.GenerateValid();
|
||||
model.DestinationCountry = "|}{%^&";
|
||||
|
||||
var result = await validator.ValidateAsync(model);
|
||||
|
||||
result.Errors.Where(error => error.PropertyName == nameof(model.DestinationCountry))
|
||||
.Should().HaveCount(1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="PastVisitModel"/> validator that should return no errors for valid model
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForValidShouldReturnNoErrors()
|
||||
{
|
||||
var model = faker.GenerateValid();
|
||||
|
||||
var result = await validator.ValidateAsync(model);
|
||||
|
||||
result.Errors.Should().BeEmpty();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
using System.Text;
|
||||
using ApplicationLayer.InfrastructureServicesInterfaces;
|
||||
using ApplicationLayer.Services.VisaApplications.Models;
|
||||
using ApplicationLayer.Services.VisaApplications.Models.Validation;
|
||||
using Domains;
|
||||
using FluentAssertions;
|
||||
using FluentValidation;
|
||||
using VisaApi.Fakers.VisaApplications.Requests;
|
||||
using VisaApi.Services;
|
||||
using Xunit;
|
||||
|
||||
namespace VisaApi.Tests.Application.Validation.VisaApplications
|
||||
{
|
||||
public class PermissionToDestCountryModelValidatorTests
|
||||
{
|
||||
private readonly static IDateTimeProvider dateTimeProvider = new TestDateTimeProvider();
|
||||
private readonly static IValidator<PermissionToDestCountryModel> validator = new PermissionToDestCountryModelValidator(dateTimeProvider);
|
||||
private readonly static PermissionToDestCountryModelFaker faker = new(dateTimeProvider);
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="PermissionToDestCountryModel"/> validator that should return error for empty expiration date
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForEmptyExpirationDateShouldReturnError()
|
||||
{
|
||||
var model = faker.Generate();
|
||||
model.ExpirationDate = new();
|
||||
|
||||
var result = await validator.ValidateAsync(model);
|
||||
|
||||
result.Errors.Where(error => error.PropertyName == nameof(model.ExpirationDate))
|
||||
.Should().HaveCount(2); //expected error + error because of expired
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="PermissionToDestCountryModel"/> validator that should return error for expiration date less than current date
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForExpirationDateLessThanCurrentDateShouldReturnError()
|
||||
{
|
||||
var model = faker.Generate();
|
||||
model.ExpirationDate = dateTimeProvider.Now().AddDays(-1);
|
||||
|
||||
var result = await validator.ValidateAsync(model);
|
||||
|
||||
result.Errors.Where(error => error.PropertyName == nameof(model.ExpirationDate))
|
||||
.Should().HaveCount(1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="PermissionToDestCountryModel"/> validator that should return error for empty issuer
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForEmptyIssuerShouldReturnError()
|
||||
{
|
||||
var model = faker.Generate();
|
||||
model.Issuer = string.Empty;
|
||||
|
||||
var result = await validator.ValidateAsync(model);
|
||||
|
||||
result.Errors.Where(error => error.PropertyName == nameof(model.Issuer))
|
||||
.Should().HaveCount(1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="PermissionToDestCountryModel"/> validator that should return error for not valid issuer
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForNotValidIssuerShouldReturnError()
|
||||
{
|
||||
var model = faker.Generate();
|
||||
model.Issuer = "}{)(*&*^%#!#!:";
|
||||
|
||||
var result = await validator.ValidateAsync(model);
|
||||
|
||||
result.Errors.Where(error => error.PropertyName == nameof(model.Issuer))
|
||||
.Should().HaveCount(1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="PermissionToDestCountryModel"/> validator that should return error for too long issuer
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForTooLongIssuerShouldReturnError()
|
||||
{
|
||||
var model = faker.Generate();
|
||||
var stringBuilder = new StringBuilder();
|
||||
stringBuilder.Append('g', ConfigurationConstraints.IssuerNameLength + 1);
|
||||
model.Issuer = stringBuilder.ToString();
|
||||
|
||||
var result = await validator.ValidateAsync(model);
|
||||
|
||||
result.Errors.Where(error => error.PropertyName == nameof(model.Issuer))
|
||||
.Should().HaveCount(1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="PermissionToDestCountryModel"/> validator that should return no errors for valid model
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForValidShouldReturnNoErrors()
|
||||
{
|
||||
var model = faker.Generate();
|
||||
|
||||
var result = await validator.ValidateAsync(model);
|
||||
|
||||
result.Errors.Should().BeEmpty();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
using System.Text;
|
||||
using ApplicationLayer.InfrastructureServicesInterfaces;
|
||||
using ApplicationLayer.Services.VisaApplications.Models;
|
||||
using ApplicationLayer.Services.VisaApplications.Models.Validation;
|
||||
using Domains;
|
||||
using FluentAssertions;
|
||||
using FluentValidation;
|
||||
using VisaApi.Fakers.VisaApplications.Requests;
|
||||
using VisaApi.Services;
|
||||
using Xunit;
|
||||
|
||||
namespace VisaApi.Tests.Application.Validation.VisaApplications
|
||||
{
|
||||
public class ReentryPermitModelValidatorTests
|
||||
{
|
||||
private readonly static IDateTimeProvider dateTimeProvider = new TestDateTimeProvider();
|
||||
private readonly static IValidator<ReentryPermitModel> validator = new ReentryPermitModelValidator(dateTimeProvider);
|
||||
private readonly static ReentryPermitModelFaker faker = new(dateTimeProvider);
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="ReentryPermitModel"/> validator that should return error for empty expiration date
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForEmptyExpirationDateShouldReturnError()
|
||||
{
|
||||
var model = faker.Generate();
|
||||
model.ExpirationDate = new();
|
||||
|
||||
var result = await validator.ValidateAsync(model);
|
||||
|
||||
result.Errors.Where(error => error.PropertyName == nameof(model.ExpirationDate))
|
||||
.Should().HaveCount(2); //expected error + error because of expired
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="ReentryPermitModel"/> validator that should return error for expiration date less than current date
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForExpirationDateLessThanCurrentDateShouldReturnError()
|
||||
{
|
||||
var model = faker.Generate();
|
||||
model.ExpirationDate = dateTimeProvider.Now().AddDays(-1);
|
||||
|
||||
var result = await validator.ValidateAsync(model);
|
||||
|
||||
result.Errors.Where(error => error.PropertyName == nameof(model.ExpirationDate))
|
||||
.Should().HaveCount(1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="ReentryPermitModel"/> validator that should return error for empty number
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForEmptyNumberShouldReturnError()
|
||||
{
|
||||
var model = faker.Generate();
|
||||
model.Number = string.Empty;
|
||||
|
||||
var result = await validator.ValidateAsync(model);
|
||||
|
||||
result.Errors.Where(error => error.PropertyName == nameof(model.Number))
|
||||
.Should().HaveCount(1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="ReentryPermitModel"/> validator that should return error for not valid number
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForNotValidNumberShouldReturnError()
|
||||
{
|
||||
var model = faker.Generate();
|
||||
model.Number = "}{)(*&*^%#!#!:";
|
||||
|
||||
var result = await validator.ValidateAsync(model);
|
||||
|
||||
result.Errors.Where(error => error.PropertyName == nameof(model.Number))
|
||||
.Should().HaveCount(1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="ReentryPermitModel"/> validator that should return error for too long number
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForTooLongNumberShouldReturnError()
|
||||
{
|
||||
var model = faker.Generate();
|
||||
var stringBuilder = new StringBuilder();
|
||||
stringBuilder.Append('g', ConfigurationConstraints.ReentryPermitNumberLength + 1);
|
||||
model.Number = stringBuilder.ToString();
|
||||
|
||||
var result = await validator.ValidateAsync(model);
|
||||
|
||||
result.Errors.Where(error => error.PropertyName == nameof(model.Number))
|
||||
.Should().HaveCount(1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="ReentryPermitModel"/> validator that should return no errors for valid model
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task ValidateForValidShouldReturnNoErrors()
|
||||
{
|
||||
var model = faker.Generate();
|
||||
|
||||
var result = await validator.ValidateAsync(model);
|
||||
|
||||
result.Errors.Should().BeEmpty();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
using Infrastructure.Database;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Diagnostics;
|
||||
|
||||
namespace VisaApi.Tests.Infrastructure.Database;
|
||||
|
||||
public static class InMemoryContextProvider
|
||||
{
|
||||
private static DbContextOptions<DatabaseContext> opts = new DbContextOptionsBuilder<DatabaseContext>()
|
||||
.UseInMemoryDatabase("VisaApiDB")
|
||||
.ConfigureWarnings(b => b.Ignore(InMemoryEventId.TransactionIgnoredWarning))
|
||||
.Options;
|
||||
|
||||
public static DatabaseContext GetDbContext()
|
||||
{
|
||||
var result = new DatabaseContext(opts);
|
||||
|
||||
result.Database.EnsureDeleted();
|
||||
result.Database.EnsureCreated();
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
using ApplicationLayer.InfrastructureServicesInterfaces;
|
||||
using ApplicationLayer.Services.Applicants.NeededServices;
|
||||
using FluentAssertions;
|
||||
using Infrastructure.Database;
|
||||
using Infrastructure.Database.Applicants.Repositories;
|
||||
using Infrastructure.Database.Applicants.Repositories.Exceptions;
|
||||
using VisaApi.Fakers.Applicants;
|
||||
using VisaApi.Fakers.Users;
|
||||
using VisaApi.Services;
|
||||
using Xunit;
|
||||
|
||||
namespace VisaApi.Tests.Infrastructure.Database.Repositories;
|
||||
|
||||
[Collection(Collections.ContextUsingTestCollection)]
|
||||
public class ApplicantsRepositoryTests
|
||||
{
|
||||
private readonly static UserFaker userFaker = new();
|
||||
private readonly static ApplicantFaker applicantFaker = new(GetDateTimeProvider());
|
||||
|
||||
/// <summary> Returns <see cref="IApplicantsRepository"/> </summary>
|
||||
/// <param name="context"> Database context </param>
|
||||
/// <returns>Repository</returns>
|
||||
private static IApplicantsRepository GetRepository(DatabaseContext context)
|
||||
=> new ApplicantsRepository(context, context);
|
||||
|
||||
/// <summary> Returns <see cref="IDateTimeProvider"/> </summary>
|
||||
private static IDateTimeProvider GetDateTimeProvider() => new TestDateTimeProvider();
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="IApplicantsRepository.FindByUserIdAsync"/> method that should throw exception for not existing entity
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task FindByUserIdForNotExistingShouldThrow()
|
||||
{
|
||||
await using var context = InMemoryContextProvider.GetDbContext();
|
||||
var repository = GetRepository(context);
|
||||
ApplicantNotFoundByUserIdException? result = null;
|
||||
|
||||
try
|
||||
{
|
||||
await repository.FindByUserIdAsync(Guid.NewGuid(), CancellationToken.None);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
result = e as ApplicantNotFoundByUserIdException;
|
||||
}
|
||||
|
||||
result.Should().NotBeNull();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="IApplicantsRepository.FindByUserIdAsync"/> method that should return existing entity
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task FindByUserIdForExistingShouldReturnApplicant()
|
||||
{
|
||||
await using var context = InMemoryContextProvider.GetDbContext();
|
||||
var repository = GetRepository(context);
|
||||
var user = userFaker.Generate();
|
||||
var applicant = applicantFaker.Generate();
|
||||
applicant.UserId = user.Id;
|
||||
await context.AddAsync(user);
|
||||
await repository.AddAsync(applicant, CancellationToken.None);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
var result = await repository.FindByUserIdAsync(user.Id, CancellationToken.None);
|
||||
|
||||
result.Should().BeEquivalentTo(applicant);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="IApplicantsRepository.GetApplicantIdByUserId"/> method that should throw exception for not existing entity
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task GetApplicantIdByUserIdForNotExistingShouldThrow()
|
||||
{
|
||||
await using var context = InMemoryContextProvider.GetDbContext();
|
||||
var repository = GetRepository(context);
|
||||
ApplicantNotFoundByUserIdException? result = null;
|
||||
|
||||
try
|
||||
{
|
||||
await repository.GetApplicantIdByUserId(Guid.NewGuid(), CancellationToken.None);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
result = e as ApplicantNotFoundByUserIdException;
|
||||
}
|
||||
|
||||
result.Should().NotBeNull();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="IApplicantsRepository.GetApplicantIdByUserId"/> method that should return existing entity's identifier
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task GetApplicantIdByUserIdForExistingShouldReturnApplicant()
|
||||
{
|
||||
await using var context = InMemoryContextProvider.GetDbContext();
|
||||
var repository = GetRepository(context);
|
||||
var user = userFaker.Generate();
|
||||
var applicant = applicantFaker.Generate();
|
||||
applicant.UserId = user.Id;
|
||||
await context.AddAsync(user);
|
||||
await repository.AddAsync(applicant, CancellationToken.None);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
var result = await repository.GetApplicantIdByUserId(user.Id, CancellationToken.None);
|
||||
|
||||
result.Should().Be(applicant.Id);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="IApplicantsRepository.IsApplicantNonResidentByUserId"/> method that should throw exception for not existing entity
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task IsApplicantNonResidentByUserIdForNotExistingShouldThrow()
|
||||
{
|
||||
await using var context = InMemoryContextProvider.GetDbContext();
|
||||
var repository = GetRepository(context);
|
||||
ApplicantNotFoundByUserIdException? result = null;
|
||||
|
||||
try
|
||||
{
|
||||
await repository.IsApplicantNonResidentByUserId(Guid.NewGuid(), CancellationToken.None);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
result = e as ApplicantNotFoundByUserIdException;
|
||||
}
|
||||
|
||||
result.Should().NotBeNull();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="IApplicantsRepository.IsApplicantNonResidentByUserId"/> method that should return existing entity's IsNonResident property
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task IsApplicantNonResidentByUserIdForExistingShouldReturnApplicant()
|
||||
{
|
||||
await using var context = InMemoryContextProvider.GetDbContext();
|
||||
var repository = GetRepository(context);
|
||||
var user = userFaker.Generate();
|
||||
var applicant = new ApplicantFaker(GetDateTimeProvider()).Generate();
|
||||
applicant.UserId = user.Id;
|
||||
await context.AddAsync(user);
|
||||
await repository.AddAsync(applicant, CancellationToken.None);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
var result = await repository.IsApplicantNonResidentByUserId(user.Id, CancellationToken.None);
|
||||
|
||||
result.Should().Be(applicant.IsNonResident);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
using ApplicationLayer.GeneralExceptions;
|
||||
using Domains.Users;
|
||||
using FluentAssertions;
|
||||
using Infrastructure.Database;
|
||||
using Infrastructure.Database.Generic;
|
||||
using Xunit;
|
||||
|
||||
namespace VisaApi.Tests.Infrastructure.Database.Repositories.Generic;
|
||||
|
||||
[Collection(Collections.ContextUsingTestCollection)]
|
||||
public class GenericRepositoryTests
|
||||
{
|
||||
/// <summary> Returns <see cref="GenericRepository{T}"/> </summary>
|
||||
/// <param name="context"> Database context </param>
|
||||
/// <returns>Repository</returns>
|
||||
private static GenericRepository<User> GetRepository(DatabaseContext context) => new TestGenericRepository(context, context);
|
||||
|
||||
/// <summary> Test for <see cref="GenericRepository{T}.GetAllAsync"/> method that should return empty collection if nothing added </summary>
|
||||
[Fact]
|
||||
public void GetAllForEmptyShouldReturnEmpty()
|
||||
{
|
||||
using var context = InMemoryContextProvider.GetDbContext();
|
||||
var repository = GetRepository(context);
|
||||
|
||||
var result = repository.GetAllAsync(CancellationToken.None).Result;
|
||||
|
||||
result.Should().BeEmpty();
|
||||
}
|
||||
|
||||
/// <summary> Test for <see cref="GenericRepository{T}.GetAllAsync"/> method that should return collection with added entities </summary>
|
||||
[Fact]
|
||||
public void GetAllForNotEmptyShouldReturnEntities()
|
||||
{
|
||||
using var context = InMemoryContextProvider.GetDbContext();
|
||||
var repository = GetRepository(context);
|
||||
User[] users =
|
||||
[
|
||||
new() { Email = "nasrudin@mail.ru", Password = "12345", Role = Role.Admin },
|
||||
new() { Email = "bruh@mail.ru", Password = "123", Role = Role.Applicant }
|
||||
];
|
||||
foreach (var user in users)
|
||||
{
|
||||
repository.AddAsync(user, CancellationToken.None).Wait();
|
||||
}
|
||||
|
||||
context.SaveChanges();
|
||||
|
||||
var result = repository.GetAllAsync(CancellationToken.None).Result;
|
||||
|
||||
result.Should().Contain(users).And.HaveSameCount(users);
|
||||
}
|
||||
|
||||
/// <summary> Test for <see cref="GenericRepository{T}.GetByIdAsync"/> method that should return existing entity </summary>
|
||||
[Fact]
|
||||
public void GetByIdForExistingShouldReturnEntity()
|
||||
{
|
||||
using var context = InMemoryContextProvider.GetDbContext();
|
||||
var repository = GetRepository(context);
|
||||
var user = new User { Email = "nasrudin@mail.ru", Password = "12345", Role = Role.Admin };
|
||||
repository.AddAsync(user, CancellationToken.None).Wait();
|
||||
|
||||
context.SaveChanges();
|
||||
|
||||
var result = repository.GetByIdAsync(user.Id, CancellationToken.None).Result;
|
||||
|
||||
result.Should().Be(user);
|
||||
}
|
||||
|
||||
/// <summary> Test for <see cref="GenericRepository{T}.GetByIdAsync"/> method that should throw exception for not found entity </summary>
|
||||
[Fact]
|
||||
public void GetByIdForNotExistingShouldThrow()
|
||||
{
|
||||
using var context = InMemoryContextProvider.GetDbContext();
|
||||
var repository = GetRepository(context);
|
||||
|
||||
context.SaveChanges();
|
||||
|
||||
EntityNotFoundByIdException<User>? result = null;
|
||||
try
|
||||
{
|
||||
repository.GetByIdAsync(Guid.NewGuid(), CancellationToken.None).Wait();
|
||||
}
|
||||
catch (AggregateException e)
|
||||
{
|
||||
result = e.InnerException as EntityNotFoundByIdException<User>;
|
||||
}
|
||||
|
||||
result.Should().NotBeNull();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
using Domains.Users;
|
||||
using Infrastructure.Database.Generic;
|
||||
|
||||
namespace VisaApi.Tests.Infrastructure.Database.Repositories.Generic;
|
||||
|
||||
public class TestGenericRepository(IGenericReader reader, IGenericWriter writer) : GenericRepository<User>(reader, writer);
|
||||
@@ -0,0 +1,91 @@
|
||||
using ApplicationLayer.Services.AuthServices.NeededServices;
|
||||
using ApplicationLayer.Services.VisaApplications.NeededServices;
|
||||
using Domains.Users;
|
||||
using FluentAssertions;
|
||||
using Infrastructure.Database;
|
||||
using Infrastructure.Database.Users.Repositories;
|
||||
using VisaApi.Fakers.Users;
|
||||
using Xunit;
|
||||
|
||||
namespace VisaApi.Tests.Infrastructure.Database.Repositories;
|
||||
|
||||
[Collection(Collections.ContextUsingTestCollection)]
|
||||
public class UsersRepositoryTests
|
||||
{
|
||||
private readonly static UserFaker userFaker = new();
|
||||
|
||||
/// <summary> Returns <see cref="IVisaApplicationsRepository"/> </summary>
|
||||
/// <param name="context"> Database context </param>
|
||||
/// <returns>Repository</returns>
|
||||
private static IUsersRepository GetRepository(DatabaseContext context)
|
||||
=> new UsersRepository(context, context);
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="IUsersRepository.FindByEmailAsync"/> method that should return null for not existing email
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task FindByEmailForNotExistingShouldReturnNull()
|
||||
{
|
||||
await using var context = InMemoryContextProvider.GetDbContext();
|
||||
var repository = GetRepository(context);
|
||||
|
||||
var result = await repository.FindByEmailAsync("email@email.ru", CancellationToken.None);
|
||||
|
||||
result.Should().BeNull();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="IUsersRepository.FindByEmailAsync"/> method that should return entity for existing email
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task FindByEmailForExistingShouldReturnEntity()
|
||||
{
|
||||
await using var context = InMemoryContextProvider.GetDbContext();
|
||||
var repository = GetRepository(context);
|
||||
var user = userFaker.Generate();
|
||||
await repository.AddAsync(user, CancellationToken.None);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
var result = await repository.FindByEmailAsync(user.Email, CancellationToken.None);
|
||||
|
||||
result.Should().Be(user);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="IUsersRepository.GetAllOfRoleAsync"/> method that should return empty from empty db
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task GetAllOfRoleForEmptyShouldReturnEmpty()
|
||||
{
|
||||
await using var context = InMemoryContextProvider.GetDbContext();
|
||||
var repository = GetRepository(context);
|
||||
|
||||
var result = await repository.GetAllOfRoleAsync(Role.ApprovingAuthority, CancellationToken.None);
|
||||
|
||||
result.Should().BeEmpty();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="IUsersRepository.GetAllOfRoleAsync"/> method that should return entities from not empty db
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task GetAllOfRoleForNotEmptyShouldReturnEntities()
|
||||
{
|
||||
await using var context = InMemoryContextProvider.GetDbContext();
|
||||
var repository = GetRepository(context);
|
||||
var users = new List<User>();
|
||||
for (var i = 0; i < 3; i++)
|
||||
{
|
||||
var user = userFaker.Generate();
|
||||
user.Role = Role.ApprovingAuthority;
|
||||
users.Add(user);
|
||||
await repository.AddAsync(user, CancellationToken.None);
|
||||
}
|
||||
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
var result = await repository.GetAllOfRoleAsync(Role.ApprovingAuthority, CancellationToken.None);
|
||||
|
||||
result.Should().Contain(users).And.HaveSameCount(users);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,265 @@
|
||||
using ApplicationLayer.InfrastructureServicesInterfaces;
|
||||
using ApplicationLayer.Services.VisaApplications.NeededServices;
|
||||
using Domains.VisaApplicationDomain;
|
||||
using FluentAssertions;
|
||||
using Infrastructure.Database;
|
||||
using Infrastructure.Database.VisaApplications.Repositories;
|
||||
using Infrastructure.Database.VisaApplications.Repositories.Exceptions;
|
||||
using VisaApi.Fakers.Applicants;
|
||||
using VisaApi.Fakers.Users;
|
||||
using VisaApi.Fakers.VisaApplications;
|
||||
using VisaApi.Services;
|
||||
using Xunit;
|
||||
|
||||
namespace VisaApi.Tests.Infrastructure.Database.Repositories;
|
||||
|
||||
[Collection(Collections.ContextUsingTestCollection)]
|
||||
public class VisaApplicationsRepositoryTests
|
||||
{
|
||||
private readonly static UserFaker userFaker = new();
|
||||
private readonly static ApplicantFaker applicantFaker = new(GetDateTimeProvider());
|
||||
private readonly static VisaApplicationFaker applicationFaker = new(GetDateTimeProvider());
|
||||
|
||||
/// <summary> Returns <see cref="IVisaApplicationsRepository"/> </summary>
|
||||
/// <param name="context"> Database context </param>
|
||||
/// <returns>Repository</returns>
|
||||
private static IVisaApplicationsRepository GetRepository(DatabaseContext context)
|
||||
=> new VisaApplicationsRepository(context, context);
|
||||
|
||||
/// <summary> Returns <see cref="IDateTimeProvider"/> </summary>
|
||||
private static IDateTimeProvider GetDateTimeProvider() => new TestDateTimeProvider();
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="IVisaApplicationsRepository.GetOfApplicantAsync"/> method that should return empty if no applications added
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task GetOfApplicantForEmptyShouldReturnEmpty()
|
||||
{
|
||||
await using var context = InMemoryContextProvider.GetDbContext();
|
||||
var repository = GetRepository(context);
|
||||
var user = userFaker.Generate();
|
||||
var applicant = applicantFaker.Generate();
|
||||
applicant.UserId = user.Id;
|
||||
await context.AddAsync(user);
|
||||
await context.AddAsync(applicant);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
var result = await repository.GetOfApplicantAsync(applicant.Id, CancellationToken.None);
|
||||
|
||||
result.Should().BeEmpty();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="IVisaApplicationsRepository.GetOfApplicantAsync"/> method that should return added entities
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task GetOfApplicantForExistingShouldReturnEntities()
|
||||
{
|
||||
await using var context = InMemoryContextProvider.GetDbContext();
|
||||
var repository = GetRepository(context);
|
||||
var user = userFaker.Generate();
|
||||
var applicant = applicantFaker.Generate();
|
||||
applicant.UserId = user.Id;
|
||||
await context.AddAsync(user);
|
||||
await context.AddAsync(applicant);
|
||||
var applications = new List<VisaApplication>();
|
||||
for (var i = 0; i < 5; i++)
|
||||
{
|
||||
var application = applicationFaker.GenerateValid(applicant);
|
||||
applications.Add(application);
|
||||
await context.AddAsync(application);
|
||||
}
|
||||
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
var result = await repository.GetOfApplicantAsync(applicant.Id, CancellationToken.None);
|
||||
|
||||
result.Should().Contain(applications).And.HaveSameCount(applications);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="IVisaApplicationsRepository.GetByApplicantAndApplicationIdAsync"/> method that should throw exception for not existing entities
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task GetApplicantIdByUserIdForNotExistingShouldThrow()
|
||||
{
|
||||
await using var context = InMemoryContextProvider.GetDbContext();
|
||||
var repository = GetRepository(context);
|
||||
ApplicationNotFoundByApplicantAndApplicationIdException? result = null;
|
||||
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
try
|
||||
{
|
||||
await repository.GetByApplicantAndApplicationIdAsync(Guid.NewGuid(), Guid.NewGuid(), CancellationToken.None);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
result = e as ApplicationNotFoundByApplicantAndApplicationIdException;
|
||||
}
|
||||
|
||||
result.Should().NotBeNull();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="IVisaApplicationsRepository.GetByApplicantAndApplicationIdAsync"/> method that should throw exception for not existing applicant
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task GetApplicantIdByUserIdForNotExistingApplicantShouldThrow()
|
||||
{
|
||||
await using var context = InMemoryContextProvider.GetDbContext();
|
||||
var repository = GetRepository(context);
|
||||
var user = userFaker.Generate();
|
||||
var applicant = applicantFaker.Generate();
|
||||
applicant.UserId = user.Id;
|
||||
var application = applicationFaker.GenerateValid(applicant);
|
||||
await context.AddAsync(user);
|
||||
await context.AddAsync(applicant);
|
||||
await context.AddAsync(application);
|
||||
ApplicationNotFoundByApplicantAndApplicationIdException? result = null;
|
||||
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
try
|
||||
{
|
||||
await repository.GetByApplicantAndApplicationIdAsync(Guid.NewGuid(), application.Id, CancellationToken.None);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
result = e as ApplicationNotFoundByApplicantAndApplicationIdException;
|
||||
}
|
||||
|
||||
result.Should().NotBeNull();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="IVisaApplicationsRepository.GetByApplicantAndApplicationIdAsync"/> method that should throw exception for not existing application
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task GetApplicantIdByUserIdForNotExistingApplicationShouldThrow()
|
||||
{
|
||||
await using var context = InMemoryContextProvider.GetDbContext();
|
||||
var repository = GetRepository(context);
|
||||
var user = userFaker.Generate();
|
||||
var applicant = applicantFaker.Generate();
|
||||
applicant.UserId = user.Id;
|
||||
await context.AddAsync(user);
|
||||
await context.AddAsync(applicant);
|
||||
ApplicationNotFoundByApplicantAndApplicationIdException? result = null;
|
||||
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
try
|
||||
{
|
||||
await repository.GetByApplicantAndApplicationIdAsync(applicant.Id, Guid.NewGuid(), CancellationToken.None);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
result = e as ApplicationNotFoundByApplicantAndApplicationIdException;
|
||||
}
|
||||
|
||||
result.Should().NotBeNull();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="IVisaApplicationsRepository.GetByApplicantAndApplicationIdAsync"/> method
|
||||
/// that should throw exception for not accessible application
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task GetApplicantIdByUserIdForNotAccessibleApplicationShouldThrow()
|
||||
{
|
||||
await using var context = InMemoryContextProvider.GetDbContext();
|
||||
var repository = GetRepository(context);
|
||||
var user = userFaker.Generate();
|
||||
var applicant = applicantFaker.Generate();
|
||||
applicant.UserId = user.Id;
|
||||
var otherUser = userFaker.Generate();
|
||||
var otherApplicant = applicantFaker.Generate();
|
||||
otherApplicant.UserId = user.Id;
|
||||
var notAccessibleApplication = applicationFaker.GenerateValid(otherApplicant);
|
||||
await context.AddAsync(user);
|
||||
await context.AddAsync(applicant);
|
||||
await context.AddAsync(otherUser);
|
||||
await context.AddAsync(otherApplicant);
|
||||
await context.AddAsync(notAccessibleApplication);
|
||||
ApplicationNotFoundByApplicantAndApplicationIdException? result = null;
|
||||
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
try
|
||||
{
|
||||
await repository.GetByApplicantAndApplicationIdAsync(applicant.Id, notAccessibleApplication.Id, CancellationToken.None);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
result = e as ApplicationNotFoundByApplicantAndApplicationIdException;
|
||||
}
|
||||
|
||||
result.Should().NotBeNull();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="IVisaApplicationsRepository.GetByApplicantAndApplicationIdAsync"/> method
|
||||
/// that should return application for valid identifiers
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task GetApplicantIdByUserIdForValidIdsShouldReturnApplication()
|
||||
{
|
||||
await using var context = InMemoryContextProvider.GetDbContext();
|
||||
var repository = GetRepository(context);
|
||||
var user = userFaker.Generate();
|
||||
var applicant = applicantFaker.Generate();
|
||||
applicant.UserId = user.Id;
|
||||
var application = applicationFaker.GenerateValid(applicant);
|
||||
await context.AddAsync(user);
|
||||
await context.AddAsync(applicant);
|
||||
await context.AddAsync(application);
|
||||
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
var result = await repository.GetByApplicantAndApplicationIdAsync(applicant.Id, application.Id, CancellationToken.None);
|
||||
|
||||
result.Should().Be(application);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="IVisaApplicationsRepository.GetPendingApplicationsAsync"/> method that should return empty from empty db
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task GetPendingApplicationsForEmptyShouldReturnEmpty()
|
||||
{
|
||||
await using var context = InMemoryContextProvider.GetDbContext();
|
||||
var repository = GetRepository(context);
|
||||
|
||||
var result = await repository.GetPendingApplicationsAsync(CancellationToken.None);
|
||||
|
||||
result.Should().BeEmpty();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for <see cref="IVisaApplicationsRepository.GetPendingApplicationsAsync"/> method that should return pending applications from not empty db
|
||||
/// </summary>
|
||||
[Fact]
|
||||
private async Task GetPendingApplicationsForExistingShouldReturnExistingPending()
|
||||
{
|
||||
await using var context = InMemoryContextProvider.GetDbContext();
|
||||
var repository = GetRepository(context);
|
||||
var user = userFaker.Generate();
|
||||
var applicant = applicantFaker.Generate();
|
||||
applicant.UserId = user.Id;
|
||||
var applicationPending = applicationFaker.GenerateValid(applicant);
|
||||
applicationPending.Status = ApplicationStatus.Pending;
|
||||
var applicationNotPending = applicationFaker.GenerateValid(applicant);
|
||||
applicationNotPending.Status = ApplicationStatus.Approved;
|
||||
await context.AddAsync(user);
|
||||
await context.AddAsync(applicant);
|
||||
await context.AddAsync(applicationPending);
|
||||
await context.AddAsync(applicationNotPending);
|
||||
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
var result = await repository.GetPendingApplicationsAsync(CancellationToken.None);
|
||||
|
||||
result.Should().Contain(applicationPending).And.HaveCount(1);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user