public async Task AddAdditionalInfoAsync_WithCorrectData_ShouldReturnCorrectResult()
        {
            var errorMessagePrefix = "UserService AddAdditionalInfoAsync() method does not work properly.";

            // Arrange
            MapperInitializer.InitializeMapper();
            var context        = ApplicationDbContextInMemoryFactory.InitializeContext();
            var userRepository = new EfDeletableEntityRepository <ApplicationUser>(context);
            var userService    = this.GetUserService(userRepository, context);

            await this.SeedDataAsync(context);

            var userId = context.Users.First().Id;
            var userAdditionalInfoServiceModel = new UserAdditionalInfoServiceModel()
            {
                Biography    = "Biography",
                ProfilePhoto = "ProfilePhoto",
                Lifestyle    = context.Lifestyles.First().To <LifestyleServiceModel>(),
            };

            userAdditionalInfoServiceModel.Allergies.Add(new UserAllergenServiceModel()
            {
                Allergen = context.Allergens.First().To <AllergenServiceModel>(),
            });

            // Act
            var result = await userService.AddAdditionalInfoAsync(userId, userAdditionalInfoServiceModel);

            // Assert
            Assert.True(result, errorMessagePrefix + " " + "Returns false.");
        }
Exemple #2
0
        public async Task <bool> AddAdditionalInfoAsync(string userId, UserAdditionalInfoServiceModel userAdditionalInfoServiceModel)
        {
            var user = await this.userRepository.GetByIdWithDeletedAsync(userId);

            if (user == null)
            {
                throw new ArgumentNullException(
                          string.Format(InvalidUserIdErrorMessage, userId));
            }

            user.HasAdditionalInfo = true;
            user.Biography         = userAdditionalInfoServiceModel.Biography;
            user.ProfilePhoto      = userAdditionalInfoServiceModel.ProfilePhoto;

            if (userAdditionalInfoServiceModel.Lifestyle != null)
            {
                await this.lifestyleService
                .SetLifestyleToUserAsync(userAdditionalInfoServiceModel.Lifestyle.Type, user);
            }

            if (userAdditionalInfoServiceModel.Allergies != null)
            {
                foreach (var userAllergen in userAdditionalInfoServiceModel.Allergies)
                {
                    await this.allergenService
                    .SetAllergenToUserAsync(userAllergen.Allergen.Name, user);
                }
            }

            var result = await this.userManager.UpdateAsync(user);

            return(result.Succeeded);
        }
        public async Task EditAdditionalInfoAsync_WithNonExistentUserId_ShouldThrowArgumentNullException()
        {
            // Arrange
            MapperInitializer.InitializeMapper();
            var context           = ApplicationDbContextInMemoryFactory.InitializeContext();
            var userRepository    = new EfDeletableEntityRepository <ApplicationUser>(context);
            var userService       = this.GetUserService(userRepository, context);
            var nonExistentUserId = Guid.NewGuid().ToString();
            var userAdditionalInfoServiceModel = new UserAdditionalInfoServiceModel();

            // Act

            // Assert
            await Assert.ThrowsAsync <ArgumentNullException>(async() =>
            {
                await userService.EditAdditionalInfoAsync(nonExistentUserId, userAdditionalInfoServiceModel);
            });
        }
        public async Task EditAdditionalInfoAsync_WithCorrectData_ShouldSuccessfullyEdit()
        {
            var errorMessagePrefix = "UserService EditAdditionalInfoAsync() method does not work properly.";

            // Arrange
            MapperInitializer.InitializeMapper();
            var context        = ApplicationDbContextInMemoryFactory.InitializeContext();
            var userRepository = new EfDeletableEntityRepository <ApplicationUser>(context);
            var userService    = this.GetUserService(userRepository, context);

            await this.SeedDataAsync(context);

            var userId = context.Users.First().Id;
            var userAdditionalInfoServiceModel = new UserAdditionalInfoServiceModel()
            {
                FullName     = "FullName",
                Biography    = "Biography",
                ProfilePhoto = "ProfilePhoto",
                Lifestyle    = context.Lifestyles.First().To <LifestyleServiceModel>(),
            };

            userAdditionalInfoServiceModel.Allergies.Add(new UserAllergenServiceModel()
            {
                Allergen = context.Allergens.First().To <AllergenServiceModel>(),
            });

            // Act
            await userService.EditAdditionalInfoAsync(userId, userAdditionalInfoServiceModel);

            var actualResult   = userRepository.All().First(x => x.Id == userId);
            var expectedResult = userAdditionalInfoServiceModel;

            // Assert
            Assert.True(expectedResult.FullName == actualResult.FullName, errorMessagePrefix + " " + "FullName is not returned properly.");
            Assert.True(expectedResult.Biography == actualResult.Biography, errorMessagePrefix + " " + "Biography is not returned properly.");
            Assert.True(expectedResult.ProfilePhoto == actualResult.ProfilePhoto, errorMessagePrefix + " " + "ProfilePhoto is not returned properly.");
            Assert.True(expectedResult.Lifestyle.Id == actualResult.Lifestyle.Id, errorMessagePrefix + " " + "Lifestyle is not returned properly.");
            Assert.True(expectedResult.Allergies.First().Allergen.Id == actualResult.Allergies.First().Allergen.Id, errorMessagePrefix + " " + "Allergen is not returned properly.");
        }