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); }); }
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 })); }
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); }
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); }
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); }