From 07f5663c7568c1f3e283bb9bf24fa7e0f31c0f48 Mon Sep 17 00:00:00 2001 From: prtsie Date: Sun, 11 May 2025 18:28:33 +0300 Subject: [PATCH] tests and fixes --- SchengenVisaApi/ApplicationLayer/Constants.cs | 2 +- .../Domains/ConfigurationConstraints.cs | 4 +- .../Fakers/Applicants/ApplicantFaker.cs | 4 +- .../Applicants/Requests/PassportModelFaker.cs | 9 +-- .../Auth/RegisterApplicantRequestFaker.cs | 35 +++++++++ .../Fakers/Auth/RegisterRequestFaker.cs | 4 +- .../Applicants/PassportModelValidatorTests.cs | 2 +- .../RegisterApplicantRequestValidatorTests.cs | 73 +++++++++++++++++++ 8 files changed, 120 insertions(+), 13 deletions(-) create mode 100644 SchengenVisaApi/VisaApiTests/Fakers/Auth/RegisterApplicantRequestFaker.cs create mode 100644 SchengenVisaApi/VisaApiTests/Tests/Application/Validation/Auth/RegisterApplicantRequestValidatorTests.cs diff --git a/SchengenVisaApi/ApplicationLayer/Constants.cs b/SchengenVisaApi/ApplicationLayer/Constants.cs index 0ac909b..0299c24 100644 --- a/SchengenVisaApi/ApplicationLayer/Constants.cs +++ b/SchengenVisaApi/ApplicationLayer/Constants.cs @@ -6,7 +6,7 @@ public static class Constants { 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}$"); } diff --git a/SchengenVisaApi/Domains/ConfigurationConstraints.cs b/SchengenVisaApi/Domains/ConfigurationConstraints.cs index 2ffe908..76b5326 100644 --- a/SchengenVisaApi/Domains/ConfigurationConstraints.cs +++ b/SchengenVisaApi/Domains/ConfigurationConstraints.cs @@ -4,7 +4,7 @@ public static class ConfigurationConstraints { public const int CityNameLength = 70; public const int CountryNameLength = 70; - public const int CitizenshipLength = 30; + public const int CitizenshipLength = 50; public const int ReentryPermitNumberLength = 25; public const int IssuerNameLength = 200; public const int VisaNameLength = 70; @@ -20,4 +20,4 @@ public static class ConfigurationConstraints public const int ApplicantMinAge = 14; public const int JobTitleLength = 50; public const int MaxValidDays = 90; -} \ No newline at end of file +} diff --git a/SchengenVisaApi/VisaApiTests/Fakers/Applicants/ApplicantFaker.cs b/SchengenVisaApi/VisaApiTests/Fakers/Applicants/ApplicantFaker.cs index 55c45da..f96c152 100644 --- a/SchengenVisaApi/VisaApiTests/Fakers/Applicants/ApplicantFaker.cs +++ b/SchengenVisaApi/VisaApiTests/Fakers/Applicants/ApplicantFaker.cs @@ -31,7 +31,7 @@ public sealed class ApplicantFaker : Faker => new Passport { 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()), IssueDate = f.Date.Past(4, dateTimeProvider.Now()) }); @@ -62,4 +62,4 @@ public sealed class ApplicantFaker : Faker PhoneNum = f.Phone.PhoneNumber() }); } -} \ No newline at end of file +} diff --git a/SchengenVisaApi/VisaApiTests/Fakers/Applicants/Requests/PassportModelFaker.cs b/SchengenVisaApi/VisaApiTests/Fakers/Applicants/Requests/PassportModelFaker.cs index 0917c60..79d5100 100644 --- a/SchengenVisaApi/VisaApiTests/Fakers/Applicants/Requests/PassportModelFaker.cs +++ b/SchengenVisaApi/VisaApiTests/Fakers/Applicants/Requests/PassportModelFaker.cs @@ -1,4 +1,3 @@ -using ApplicationLayer.InfrastructureServicesInterfaces; using ApplicationLayer.Services.Applicants.Models; using Bogus; using Domains; @@ -7,7 +6,7 @@ namespace VisaApi.Fakers.Applicants.Requests; public sealed class PassportModelFaker : Faker { - public PassportModelFaker(IDateTimeProvider dateTimeProvider) + public PassportModelFaker() { RuleFor(m => m.Issuer, f => f.Company.CompanyName()); @@ -15,9 +14,9 @@ public sealed class PassportModelFaker : Faker f => f.Random.String(ConfigurationConstraints.PassportNumberLength, 'a', 'z')); RuleFor(m => m.ExpirationDate, - f => f.Date.Future(4, dateTimeProvider.Now())); + f => f.Date.Future(4)); RuleFor(m => m.IssueDate, - f => f.Date.Past(4, dateTimeProvider.Now())); + f => f.Date.Past(4)); } -} \ No newline at end of file +} diff --git a/SchengenVisaApi/VisaApiTests/Fakers/Auth/RegisterApplicantRequestFaker.cs b/SchengenVisaApi/VisaApiTests/Fakers/Auth/RegisterApplicantRequestFaker.cs new file mode 100644 index 0000000..12ed70e --- /dev/null +++ b/SchengenVisaApi/VisaApiTests/Fakers/Auth/RegisterApplicantRequestFaker.cs @@ -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 + { + 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()); + RuleFor(x => x.IsNonResident, faker => faker.Random.Bool()); + RuleFor(x => x.JobTitle, faker => faker.Lorem.Word()); + RuleFor(x => x.MaritalStatus, faker => faker.PickRandom()); + RuleFor(x => x.MotherName, () => nameModelFaker.Generate()); + RuleFor(x => x.Passport, () => passportModelFaker.Generate()); + RuleFor(x => x.PlaceOfWork, () => placeOfWorkModelFaker.Generate()); + RuleFor(x => x.RegisterRequest, () => registerRequestFaker.Generate()); + } + } +} diff --git a/SchengenVisaApi/VisaApiTests/Fakers/Auth/RegisterRequestFaker.cs b/SchengenVisaApi/VisaApiTests/Fakers/Auth/RegisterRequestFaker.cs index eb57366..71f5095 100644 --- a/SchengenVisaApi/VisaApiTests/Fakers/Auth/RegisterRequestFaker.cs +++ b/SchengenVisaApi/VisaApiTests/Fakers/Auth/RegisterRequestFaker.cs @@ -10,5 +10,5 @@ public sealed class RegisterRequestFaker : Faker public RegisterRequestFaker() { RuleFor(r => r.AuthData, () => authDataFaker.Generate()); - } -} \ No newline at end of file + } +} diff --git a/SchengenVisaApi/VisaApiTests/Tests/Application/Validation/Applicants/PassportModelValidatorTests.cs b/SchengenVisaApi/VisaApiTests/Tests/Application/Validation/Applicants/PassportModelValidatorTests.cs index f1179d4..fd1f79b 100644 --- a/SchengenVisaApi/VisaApiTests/Tests/Application/Validation/Applicants/PassportModelValidatorTests.cs +++ b/SchengenVisaApi/VisaApiTests/Tests/Application/Validation/Applicants/PassportModelValidatorTests.cs @@ -15,7 +15,7 @@ public class PassportModelValidatorTests { private readonly static IDateTimeProvider dateTimeProvider = new TestDateTimeProvider(); private readonly static IValidator validator = new PassportModelValidator(dateTimeProvider); - private readonly static PassportModelFaker faker = new(dateTimeProvider); + private readonly static PassportModelFaker faker = new(); /// /// Test for validator that should return error for empty number diff --git a/SchengenVisaApi/VisaApiTests/Tests/Application/Validation/Auth/RegisterApplicantRequestValidatorTests.cs b/SchengenVisaApi/VisaApiTests/Tests/Application/Validation/Auth/RegisterApplicantRequestValidatorTests.cs new file mode 100644 index 0000000..aa330b7 --- /dev/null +++ b/SchengenVisaApi/VisaApiTests/Tests/Application/Validation/Auth/RegisterApplicantRequestValidatorTests.cs @@ -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() + ); + } + + /// + /// Validation should return no errors + /// + [Fact] + public async Task ValidateShouldWork() + { + var request = requestFaker.Generate(); + + var result = await validator.TestValidateAsync(request); + + result.ShouldNotHaveAnyValidationErrors(); + } + + /// + /// Validation should return errors + /// + [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); + } + } + } +}