tests and fixes
This commit is contained in:
@@ -6,7 +6,7 @@ public static class Constants
|
|||||||
{
|
{
|
||||||
public readonly static Regex EnglishWordRegex = new("^[a-zA-Z']*$");
|
public readonly static Regex EnglishWordRegex = new("^[a-zA-Z']*$");
|
||||||
|
|
||||||
public readonly static Regex EnglishPhraseRegex = new(@"^[a-zA-Z №0-9;,\-_+=#*']*$");
|
public readonly static Regex EnglishPhraseRegex = new("""^[a-zA-Z №0-9;,\-_+=#*'\"]*$""");
|
||||||
|
|
||||||
public readonly static Regex PhoneNumRegex = new(@"^[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$");
|
public readonly static Regex PhoneNumRegex = new(@"^[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ public static class ConfigurationConstraints
|
|||||||
{
|
{
|
||||||
public const int CityNameLength = 70;
|
public const int CityNameLength = 70;
|
||||||
public const int CountryNameLength = 70;
|
public const int CountryNameLength = 70;
|
||||||
public const int CitizenshipLength = 30;
|
public const int CitizenshipLength = 50;
|
||||||
public const int ReentryPermitNumberLength = 25;
|
public const int ReentryPermitNumberLength = 25;
|
||||||
public const int IssuerNameLength = 200;
|
public const int IssuerNameLength = 200;
|
||||||
public const int VisaNameLength = 70;
|
public const int VisaNameLength = 70;
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ public sealed class ApplicantFaker : Faker<Applicant>
|
|||||||
=> new Passport
|
=> new Passport
|
||||||
{
|
{
|
||||||
Issuer = f.Company.CompanyName(),
|
Issuer = f.Company.CompanyName(),
|
||||||
Number = f.Random.String(ConfigurationConstraints.PasswordLength, 'a', 'z'),
|
Number = f.Random.String(ConfigurationConstraints.PassportNumberLength, 'a', 'z'),
|
||||||
ExpirationDate = f.Date.Future(4, dateTimeProvider.Now()),
|
ExpirationDate = f.Date.Future(4, dateTimeProvider.Now()),
|
||||||
IssueDate = f.Date.Past(4, dateTimeProvider.Now())
|
IssueDate = f.Date.Past(4, dateTimeProvider.Now())
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
using ApplicationLayer.InfrastructureServicesInterfaces;
|
|
||||||
using ApplicationLayer.Services.Applicants.Models;
|
using ApplicationLayer.Services.Applicants.Models;
|
||||||
using Bogus;
|
using Bogus;
|
||||||
using Domains;
|
using Domains;
|
||||||
@@ -7,7 +6,7 @@ namespace VisaApi.Fakers.Applicants.Requests;
|
|||||||
|
|
||||||
public sealed class PassportModelFaker : Faker<PassportModel>
|
public sealed class PassportModelFaker : Faker<PassportModel>
|
||||||
{
|
{
|
||||||
public PassportModelFaker(IDateTimeProvider dateTimeProvider)
|
public PassportModelFaker()
|
||||||
{
|
{
|
||||||
RuleFor(m => m.Issuer, f => f.Company.CompanyName());
|
RuleFor(m => m.Issuer, f => f.Company.CompanyName());
|
||||||
|
|
||||||
@@ -15,9 +14,9 @@ public sealed class PassportModelFaker : Faker<PassportModel>
|
|||||||
f => f.Random.String(ConfigurationConstraints.PassportNumberLength, 'a', 'z'));
|
f => f.Random.String(ConfigurationConstraints.PassportNumberLength, 'a', 'z'));
|
||||||
|
|
||||||
RuleFor(m => m.ExpirationDate,
|
RuleFor(m => m.ExpirationDate,
|
||||||
f => f.Date.Future(4, dateTimeProvider.Now()));
|
f => f.Date.Future(4));
|
||||||
|
|
||||||
RuleFor(m => m.IssueDate,
|
RuleFor(m => m.IssueDate,
|
||||||
f => f.Date.Past(4, dateTimeProvider.Now()));
|
f => f.Date.Past(4));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
using ApplicationLayer.InfrastructureServicesInterfaces;
|
||||||
|
using ApplicationLayer.Services.AuthServices.Requests;
|
||||||
|
using Bogus;
|
||||||
|
using Domains.ApplicantDomain;
|
||||||
|
using VisaApi.Fakers.Applicants.Requests;
|
||||||
|
|
||||||
|
namespace VisaApi.Fakers.Auth
|
||||||
|
{
|
||||||
|
public sealed class RegisterApplicantRequestFaker : Faker<RegisterApplicantRequest>
|
||||||
|
{
|
||||||
|
private readonly NameModelFaker nameModelFaker = new();
|
||||||
|
private readonly PassportModelFaker passportModelFaker = new();
|
||||||
|
private readonly PlaceOfWorkModelFaker placeOfWorkModelFaker = new();
|
||||||
|
private readonly RegisterRequestFaker registerRequestFaker = new();
|
||||||
|
|
||||||
|
public RegisterApplicantRequestFaker(IDateTimeProvider dateTimeProvider)
|
||||||
|
{
|
||||||
|
RuleFor(x => x.ApplicantName, () => nameModelFaker.Generate());
|
||||||
|
RuleFor(x => x.BirthDate, faker => dateTimeProvider.Now().AddYears(-faker.Random.Int(20, 100)));
|
||||||
|
RuleFor(x => x.Citizenship, faker => faker.Address.Country());
|
||||||
|
RuleFor(x => x.CitizenshipByBirth, faker => faker.Address.Country());
|
||||||
|
RuleFor(x => x.CityOfBirth, faker => faker.Address.City());
|
||||||
|
RuleFor(x => x.CountryOfBirth, faker => faker.Address.Country());
|
||||||
|
RuleFor(x => x.FatherName, () => nameModelFaker.Generate());
|
||||||
|
RuleFor(x => x.Gender, faker => faker.PickRandom<Gender>());
|
||||||
|
RuleFor(x => x.IsNonResident, faker => faker.Random.Bool());
|
||||||
|
RuleFor(x => x.JobTitle, faker => faker.Lorem.Word());
|
||||||
|
RuleFor(x => x.MaritalStatus, faker => faker.PickRandom<MaritalStatus>());
|
||||||
|
RuleFor(x => x.MotherName, () => nameModelFaker.Generate());
|
||||||
|
RuleFor(x => x.Passport, () => passportModelFaker.Generate());
|
||||||
|
RuleFor(x => x.PlaceOfWork, () => placeOfWorkModelFaker.Generate());
|
||||||
|
RuleFor(x => x.RegisterRequest, () => registerRequestFaker.Generate());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -15,7 +15,7 @@ public class PassportModelValidatorTests
|
|||||||
{
|
{
|
||||||
private readonly static IDateTimeProvider dateTimeProvider = new TestDateTimeProvider();
|
private readonly static IDateTimeProvider dateTimeProvider = new TestDateTimeProvider();
|
||||||
private readonly static IValidator<PassportModel> validator = new PassportModelValidator(dateTimeProvider);
|
private readonly static IValidator<PassportModel> validator = new PassportModelValidator(dateTimeProvider);
|
||||||
private readonly static PassportModelFaker faker = new(dateTimeProvider);
|
private readonly static PassportModelFaker faker = new();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Test for <see cref="PassportModel"/> validator that should return error for empty number
|
/// Test for <see cref="PassportModel"/> validator that should return error for empty number
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user