Пример #1
0
        public async Task EditUserAsyncShouldThrowExceptionIfUserIsNull()
        {
            var options = new DbContextOptionsBuilder <MyCalisthenicAppDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                          .Options;

            var dbContext = new MyCalisthenicAppDbContext(options);

            IHttpContextAccessor httpContextAccessor = new HttpContextAccessor();

            var mockMapper = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile(new MyCalisthenicAppProfile());
            });

            var mapper = mockMapper.CreateMapper();

            var usersService = new UsersService(httpContextAccessor, dbContext, mapper);

            var userModel = new UserAdminEditViewModel
            {
                Id = null,
            };

            var exception = await Assert.ThrowsAsync <NullReferenceException>(async() => await usersService.EditUserAsync(userModel));

            Assert.IsType <NullReferenceException>(exception);
        }
Пример #2
0
        public async Task <IActionResult> Edit(UserAdminEditViewModel inputModel)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(inputModel));
            }

            await this.usersService.EditUserAsync(inputModel);

            return(this.RedirectToAction("All"));
        }
	    public async Task<ActionResult> Edit(UserAdminEditViewModel viewmodel)
	    {
		    string id = viewmodel.Id;
		    string email = viewmodel.Email;
		    string password = viewmodel.Password;
		    string username = viewmodel.Email;
			
			AppUser user = await UserManager.FindByIdAsync(id);
		    if (user != null)
		    {
			    user.Email = email;
			    user.UserName = username;
			    IdentityResult validEmail = await UserManager.UserValidator.ValidateAsync(user);
			    if (!validEmail.Succeeded)
			    {
				    AddErrorsFromResult(validEmail);
			    }
			    IdentityResult validPass = null;
			    if (password != string.Empty)
			    {
				    validPass = await UserManager.PasswordValidator.ValidateAsync(password);
				    if (validPass.Succeeded)
				    {
					    user.PasswordHash = UserManager.PasswordHasher.HashPassword(password);
				    }
				    else
				    {
					    AddErrorsFromResult(validPass);
				    }
			    }
			    if ((validEmail.Succeeded && validPass == null) ||
			        (validEmail.Succeeded && password != string.Empty && validPass.Succeeded))
			    {
				    IdentityResult result = await UserManager.UpdateAsync(user);
				    if (result.Succeeded)
				    {
					    return RedirectToAction("Index");
				    }
				    else
				    {
					    AddErrorsFromResult(result);
				    }
			    }
		    }
		    else
		    {
			    ModelState.AddModelError("", "User Not Found");
		    }
		    return View(user);
	    }
Пример #4
0
        public async Task <IActionResult> Edit(string id, UserAdminEditViewModel model)
        {
            if (!ModelState.IsValid)
            {
                model.AllRoles = await this.roleManager.Roles.Select(r => r.Name).ToListAsync();

                return(View(model));
            }

            var exists = await this.users.ExistsAsync(id);

            if (!exists)
            {
                return(BadRequest());
            }

            foreach (var role in model.Roles)
            {
                if (!await roleManager.RoleExistsAsync(role))
                {
                    return(BadRequest());
                }
            }

            var usernameAlreadyTaken = await this.users.AlreadyExistsAsync(id, model.Email);

            if (usernameAlreadyTaken)
            {
                TempData.AddErrorMessage(UserErrorConstants.UserAlreadyExists);
                model.AllRoles = await this.roleManager.Roles.Select(r => r.Name).ToListAsync();

                return(View(model));
            }

            string profilePictureUrl = model.NewProfilePicture != null ? await this.fileService.UploadImageAndGetUrlAsync(
                model.NewProfilePicture, UserDataConstants.ProfilePictureWidth, UserDataConstants.ProfilePictureHeight) : null;

            string profilePictureNavUrl = model.NewProfilePicture != null ? await this.fileService.UploadImageAndGetUrlAsync(
                model.NewProfilePicture, UserDataConstants.ProfilePictureNavWidth, UserDataConstants.ProfilePictureNavHeight) : null;

            await this.users.EditAsync(id, model.FirstName, model.LastName, model.Email, model.Email, model.Roles, profilePictureUrl, profilePictureNavUrl);

            TempData.AddSuccessMessage(UserSuccessMessages.EditMessage);
            return(RedirectToAction(nameof(All)));
        }
Пример #5
0
        public async Task EditUserAsyncShouldReturnUserSuccessfully()
        {
            var options = new DbContextOptionsBuilder <MyCalisthenicAppDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                          .Options;

            var dbContext = new MyCalisthenicAppDbContext(options);

            IHttpContextAccessor httpContextAccessor = new HttpContextAccessor();

            var mockMapper = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile(new MyCalisthenicAppProfile());
            });

            var mapper = mockMapper.CreateMapper();

            var usersService = new UsersService(httpContextAccessor, dbContext, mapper);

            var user = new ApplicationUser
            {
                Id        = UserId,
                FirstName = UserFirstName,
                LastName  = UserLastName,
            };

            await dbContext.Users.AddAsync(user);

            await dbContext.SaveChangesAsync();

            var userModel = new UserAdminEditViewModel
            {
                Id            = UserId,
                HasMembership = true,
            };

            await usersService.EditUserAsync(userModel);

            var actual = await dbContext.Users.FirstOrDefaultAsync(u => u.Id == UserId);

            Assert.True(actual.HasMembership);
        }
Пример #6
0
        public async Task EditUserAsync(UserAdminEditViewModel inputModel)
        {
            var userFromDb = await this.dbContext.Users
                             .FirstOrDefaultAsync(u => u.Id == inputModel.Id);

            if (userFromDb == null)
            {
                throw new NullReferenceException(string.Format(ServicesConstants.InvalidUserId, inputModel.Id));
            }

            userFromDb.HasCoupon = inputModel.HasCoupon;

            userFromDb.HasMembership = inputModel.HasMembership;

            userFromDb.MembershipExpirationDate = inputModel.MembershipExpirationDate;

            userFromDb.HasSubscribe = inputModel.HasSubscribe;

            this.dbContext.Update(userFromDb);

            await this.dbContext.SaveChangesAsync();
        }