public void WhenGettingUser_ThenReturnsCorrectUser() { var userRepositoryMock = new Mock<IUserRepository>(); var dataUser = new User { UserId = 1, DisplayName = "test", Password = "******" }; userRepositoryMock.Setup(repo => repo.GetById(It.IsAny<int>())).Returns(dataUser); var services = new UserService(userRepositoryMock.Object, new Mock<IUnitOfWork>().Object); User returnedUser = services.GetById(0); Assert.NotNull(returnedUser); Assert.Equal("test", returnedUser.DisplayName); }
public void WhenCreatingUser_ThenReturnsUpdatedUserWithUserId() { var userRepositoryMock = new Mock<IUserRepository>(); userRepositoryMock.Setup(u => u.Add(It.IsAny<User>())).Callback<User>(user => user.UserId = 12); var services = new UserService(userRepositoryMock.Object, new Mock<IUnitOfWork>().Object); var servicesUser = new User { DisplayName = "test", Password = "******" }; Assert.Equal(0, servicesUser.UserId); User createdUser = services.Create(servicesUser); Assert.Equal(12, createdUser.UserId); }
public CmsUserManagerAddViewModel AddViewModelInsert(CmsUserManagerAddViewModel viewModel) { var isSuccess = false; var user = this.userService.GetByUsername(viewModel.UserName); if (user == null) { try { user = new User(); Mapper.CreateMap<CmsUserManagerAddViewModel, User>(); Mapper.Map(viewModel, user); user.Password = "******"; this.userService.Add(user); AddNewUserRoles(user, viewModel); PopulateUserRolesAvailable(ref viewModel); isSuccess = true; } catch (System.Exception) { isSuccess = false; } } else { viewModel.AddErrorMessage = "Username already exists. Please choose another username."; } viewModel.ActionIsAddSuccessful = isSuccess; return viewModel; }
private void AddNewUserRoles(User user, CmsUserManagerAddViewModel viewModel) { foreach(var roleId in viewModel.UserRolesSelectedItems) { this.AddNewUserRole(user.UserId, roleId); } }
/// <summary> /// The add. /// </summary> /// <param name="context"> /// The context. /// </param> public static void Add(FirebrickContext context) { var admin = new User { Country = "England", DisplayName = "Mr Admin", HasRegistered = true, Password = "******", PortalId = 1, PostalCode = "GL50", UserId = 1, UserName = "******", FirstName = "John", LastName = "Barnes", FullName = "John Barnes", DateCreated = DateTime.Now, JobTitle = "Winger", LastModified = DateTime.Now, IsContactable = true, IsActive = true, EMail = "*****@*****.**" }; var registered = new User { Country = "England", DisplayName = "Rushy", HasRegistered = true, Password = "******", PortalId = 1, PostalCode = "GL50", UserId = 2, UserName = "******", FirstName = "Ian", LastName = "Rush", FullName = "Ian Rush", DateCreated = DateTime.Now, JobTitle = "Striker", LastModified = DateTime.Now, IsContactable = true, IsActive = true, EMail = "*****@*****.**" }; // Password "86F7E437FAA5A7FCE15D1DDCB9EAEAEA377667B8" equals "a" // 5BAA61E4C9B93F3F0682250B6CF8331B7EE68FD8 = password context.Users.AddOrUpdate(admin); context.Users.AddOrUpdate(registered); try { context.SaveChanges(); } catch (DbEntityValidationException dbEx) { foreach (DbEntityValidationResult validationErrors in dbEx.EntityValidationErrors) { foreach (DbValidationError validationError in validationErrors.ValidationErrors) { Debug.WriteLine( "Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage); } } } }
public void WhenUserAdded_ThenUpdatesRepository() { DatabaseTestUtility.DropCreateFirebrickDatabase(); var repository = new UserRepository(new DatabaseFactory()); var newUser = new User { DisplayName = "name", Country = "United States", PostalCode = "a1b2c3", Password = "******", UserName = "******" }; repository.Add(newUser); repository.Commit(); List<User> actualList = repository.GetAll().ToList(); Assert.Equal(1, actualList.Count); Assert.Equal(newUser.DisplayName, actualList[0].DisplayName); }
/// <summary> /// The initialize fixture. /// </summary> private void InitializeFixture() { DatabaseTestUtility.DropCreateFirebrickDatabase(); this.defaultTestUser = new User { DisplayName = "name", Country = "United States", PostalCode = "a1b2c3", Password = "******", UserName = "******" }; var repository = new UserRepository(new DatabaseFactory()); repository.Add(this.defaultTestUser); }
/// <summary> /// The save user. /// </summary> /// <param name="user"> /// The user. /// </param> public void SaveUser(User user) { this.unitOfWork.Commit(); }