Exemplo n.º 1
0
        public async Task EditProfileAsync_ThrowsNullException_IfTheUserIsNull()
        {
            var context               = ApplicationDbContextInMemoryFactory.InitializeContext();
            var userRepository        = new EfDeletableEntityRepository <ApplicationUser>(context);
            var userService           = this.GetUserService(userRepository, context);
            var profileEditInputModel = new ProfileEdintInputModel();

            await Assert.ThrowsAsync <ArgumentNullException>(async() =>
            {
                await userService.EditProfileAsync(profileEditInputModel);
            });
        }
Exemplo n.º 2
0
        public async Task <IActionResult> ProfileEdit(ProfileEdintInputModel profileEdintInputModel)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(profileEdintInputModel));
            }

            if (!await this.userService.EditProfileAsync(profileEdintInputModel))
            {
                this.TempData["Error"] = ValidationMessages.UsernameNotUniqueErrorMessage;
                return(this.RedirectToAction("ProfileEdit", "Users", new { userId = profileEdintInputModel.Id }));
            }

            return(this.RedirectToAction(nameof(this.Profile), new { userId = profileEdintInputModel.Id }));
        }
Exemplo n.º 3
0
        public async Task <bool> EditProfileAsync(ProfileEdintInputModel profileEditInputModel)
        {
            var user = await this.userRepository.All().Where(x => x.Id == profileEditInputModel.Id).SingleOrDefaultAsync();

            if (user == null)
            {
                throw new ArgumentNullException("User was null !");
            }

            if (user.UserName != profileEditInputModel.Username &&
                await this.UserNameIsNotUnique(profileEditInputModel.Username))
            {
                return(false);
            }

            profileEditInputModel.To(user);

            await this.userRepository.SaveChangesAsync();

            return(true);
        }
Exemplo n.º 4
0
        public async Task EditProfileAsync_ReturnsFalse_IfUsernameAlreadyExists()
        {
            var errorMessage = "EditProfileAsync did not return false when a user tried to change his username to an already existing one.";

            var context        = ApplicationDbContextInMemoryFactory.InitializeContext();
            var userRepository = new EfDeletableEntityRepository <ApplicationUser>(context);
            var userService    = this.GetUserService(userRepository, context);
            var seeder         = new UserTestSeeder();
            await seeder.SeedUsersWithAddressesAsync(context);

            var userWithoutAddresses = context.Users.First(x => x.UserName == "UserWithoutAddresses");

            var profileEditInputModel = new ProfileEdintInputModel()
            {
                Id       = userWithoutAddresses.Id,
                Username = "******",
            };

            var result = await userService.EditProfileAsync(profileEditInputModel);

            Assert.False(result, errorMessage);
        }
Exemplo n.º 5
0
        public async Task EditProfileAsync_ReturnsTrueAndMakesChanges_IfTheModelIsCorrect()
        {
            var errorMessageBool = "EditProfileAsync did not return true";

            MapperInitializer.InitializeMapper();
            var context        = ApplicationDbContextInMemoryFactory.InitializeContext();
            var userRepository = new EfDeletableEntityRepository <ApplicationUser>(context);
            var userService    = this.GetUserService(userRepository, context);
            var seeder         = new UserTestSeeder();
            await seeder.SeedUsersWithAddressesAsync(context);

            var userWithoutAddresses = context.Users.First(x => x.UserName == "UserWithoutAddresses");

            var profileEditInputModel = new ProfileEdintInputModel()
            {
                Id       = userWithoutAddresses.Id,
                Username = "******",
            };

            var result = await userService.EditProfileAsync(profileEditInputModel);

            Assert.True(result, errorMessageBool);
            Assert.Equal("Changed", userWithoutAddresses.UserName);
        }