Вытащил солюшен на уровень выше, чтобы прощё было дотнетить
	
		
			
	
		
	
	
		
	
		
			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,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(); | ||||
|         } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user