Пример #1
0
        public void UserDependentDto_Validation_FirstLastName_IsNotValid(
            string firstName,
            string lastName)
        {
            if (firstName?.Length == 1)
            {
                firstName = new string('\t', 201);
            }
            else if (lastName?.Length == 1)
            {
                lastName = new string('\t', 201);
            }

            var dto = new UserDependentDto
            {
                FirstName = firstName,
                LastName  = lastName,
                Age       = 12,
                Email     = $"{firstName}.{lastName}@aiof.com",
                AmountOfSupportProvided = 15M,
                UserRelationship        = UserRelationship.Child.ToString()
            };

            Assert.False(_userDependentDtoValidator.Validate(dto).IsValid);
        }
Пример #2
0
        public async Task GetDependentAsync_ByDto_IsSuccessful(int userId)
        {
            var _repo = new ServiceHelper()
            {
                UserId = userId
            }.GetRequiredService <IUserRepository>();

            var dependentDto = new UserDependentDto
            {
                FirstName = $"FirstName{userId}",
                LastName  = $"Lastname{userId}",
                Age       = 10,
                Email     = null,
                AmountOfSupportProvided = 15000M,
                UserRelationship        = UserRelationship.Child.ToString()
            };
            await _repo.AddDependentAsync(dependentDto);

            var dependent = await _repo.GetDependentAsync(dependentDto);

            Assert.NotNull(dependent);
            Assert.NotEqual(0, dependent.Id);
            Assert.NotEqual(Guid.Empty, dependent.PublicKey);
            Assert.NotNull(dependent.FirstName);
            Assert.NotNull(dependent.LastName);
            Assert.NotEqual(0, dependent.Age);
            Assert.NotEqual(0, dependent.AmountOfSupportProvided);
            Assert.NotNull(dependent.UserRelationship);
            Assert.NotEqual(0, dependent.UserId);
            Assert.NotEqual(DateTime.MinValue, dependent.Created);
        }
Пример #3
0
 public async Task <IUserDependent> GetDependentAsync(UserDependentDto dto)
 {
     return(await _context.UserDependents
            .FirstOrDefaultAsync(x => x.FirstName == dto.FirstName &&
                                 x.LastName == dto.LastName &&
                                 x.Age == dto.Age &&
                                 x.Email == dto.Email &&
                                 x.AmountOfSupportProvided == dto.AmountOfSupportProvided &&
                                 x.UserRelationship == dto.UserRelationship));
 }
Пример #4
0
        public void UserDependentDto_Validation_AmountOfSupportProvided_IsNotValid(decimal amountOfSupportProvided)
        {
            var dto = new UserDependentDto
            {
                FirstName = "Firstname",
                LastName  = "Lastname",
                Age       = 12,
                Email     = "*****@*****.**",
                AmountOfSupportProvided = decimal.Negate(amountOfSupportProvided),
                UserRelationship        = UserRelationship.Child.ToString()
            };

            Assert.False(_userDependentDtoValidator.Validate(dto).IsValid);
        }
Пример #5
0
        public void UserDependentDto_Validation_Email_IsNotValid(string email)
        {
            var dto = new UserDependentDto
            {
                FirstName = "Firstname",
                LastName  = "Lastname",
                Age       = 12,
                Email     = email,
                AmountOfSupportProvided = 15M,
                UserRelationship        = UserRelationship.Child.ToString()
            };

            Assert.False(_userDependentDtoValidator.Validate(dto).IsValid);
        }
Пример #6
0
        public void UserDependentDto_Validation_Age_IsNotValid(int age)
        {
            var dto = new UserDependentDto
            {
                FirstName = "Firstname",
                LastName  = "Lastname",
                Age       = age,
                Email     = "*****@*****.**",
                AmountOfSupportProvided = 15M,
                UserRelationship        = UserRelationship.Child.ToString()
            };

            Assert.False(_userDependentDtoValidator.Validate(dto).IsValid);
        }
Пример #7
0
        public void UserDependentDto_Validation_IsSuccessful(
            string firstName,
            string lastName,
            int age)
        {
            var dto = new UserDependentDto
            {
                FirstName = firstName,
                LastName  = lastName,
                Age       = age,
                Email     = $"{firstName}.{lastName}@aiof.com",
                AmountOfSupportProvided = 15M,
                UserRelationship        = UserRelationship.Child.ToString()
            };

            Assert.True(_userDependentDtoValidator.Validate(dto).IsValid);
        }
Пример #8
0
        public async Task <IUserDependent> UpdateDependentAsync(
            int userDependentId,
            UserDependentDto userDependentDto)
        {
            var userDependentInDb = await GetDependentAsync(userDependentId) as UserDependent;

            var userDependent = _mapper.Map(userDependentDto, userDependentInDb);

            _context.UserDependents
            .Update(userDependent);

            await _context.SaveChangesAsync();

            _logger.LogInformation("{Tenant} | Updated UserDependent={UserDependent}",
                                   _tenant.Log,
                                   JsonSerializer.Serialize(userDependent));

            return(userDependentInDb);
        }
Пример #9
0
        public async Task UpdateDependentAsync_IsSuccessful(
            int userId,
            int userDependentId)
        {
            var _repo = new ServiceHelper()
            {
                UserId = userId
            }.GetRequiredService <IUserRepository>();

            var dependentDto = new UserDependentDto
            {
                LastName = "Smith"
            };

            var dependent = await _repo.UpdateDependentAsync(userDependentId, dependentDto);

            Assert.NotNull(dependent);
            Assert.Equal(dependentDto.LastName, dependent.LastName);
        }
Пример #10
0
        public void UserDependentDto_Validation_UserRelationship_IsNotValid(string userRelationship)
        {
            var dto = new UserDependentDto
            {
                FirstName = "Firstname",
                LastName  = "Lastname",
                Age       = 12,
                Email     = "*****@*****.**",
                AmountOfSupportProvided = 15M,
                UserRelationship        = userRelationship
            };

            var validation = _userDependentDtoValidator.Validate(dto);

            Assert.False(validation.IsValid);

            if (!string.IsNullOrWhiteSpace(userRelationship))
            {
                Assert.NotNull(validation.Errors.FirstOrDefault(x => x.ErrorMessage.Contains("User relationship must be")));
            }
        }
Пример #11
0
        public async Task <IUserDependent> AddDependentAsync(UserDependentDto userDependentDto)
        {
            await _dependentDtoValidator.ValidateAndThrowAsync(userDependentDto);

            if (await GetDependentAsync(userDependentDto) != null)
            {
                throw new AiofFriendlyException(HttpStatusCode.BadRequest,
                                                $"User dependent already exists");
            }

            var dependent = _mapper.Map <UserDependent>(userDependentDto);

            dependent.UserId = _tenant.UserId;

            await _context.UserDependents.AddAsync(dependent);

            await _context.SaveChangesAsync();

            _logger.LogInformation("{Tenant} | Added UserDependent={UserDependent}",
                                   _tenant.Log,
                                   JsonSerializer.Serialize(dependent));

            return(dependent);
        }
Пример #12
0
 public async Task <IActionResult> AddDependentAsync([FromBody, Required] UserDependentDto userDependentDto)
 {
     return(Ok(await _repo.AddDependentAsync(userDependentDto)));
 }