public void LoginCreatesNewUserIfUserDoesNotExist() { // Setup Mock<IUserRepository> mockRepository = new Mock<IUserRepository>(); Mock<IFormsAuthentication> mockAuth = new Mock<IFormsAuthentication>(); User newUser = new User() { Email = email, Password = password }; mockRepository.Setup(m => m.GetUserByEmail(It.Is<string>(s => s == email))) .Returns<string>(user => new GetUserResult() { UserCode = UserErrorCode.EmailNotFoundInRepository, User = newUser }); mockRepository.Setup(m => m.SaveUser(It.Is<User>(u => u.Email == email && u.Password == password))) .Returns<User>(userResult => new GetUserResult() { UserCode = UserErrorCode.Success, User = newUser }); HomeController controller = new HomeController(mockRepository.Object, mockAuth.Object); // Act var result = controller.Login(new Login() { Email = email, Password = password }); // Assert mockRepository.VerifyAll(); result.AssertRedirectToRouteResult("Index", "Story"); }
public GetUserResult() { User = new User(); }
public void SaveUserReturnsUserId() { // Setup IDocumentStore store = Global.GetInMemoryStore(); UserRepository repository = new UserRepository(store); User user = new User() { Email = "email", Password = "******" }; // Act repository.SaveUser(user); // Assert Assert.IsNotNullOrEmpty(user.Id); }
private static List<Story> CreateFakeStories(out IDocumentStore store, out User user, int numberOfStories) { store = Global.GetInMemoryStore(); user = FakeEntityFactory.GetGenericUser(); using (var session = store.OpenSession()) { session.Store(user); for (int i = 1; i <= numberOfStories; i++) { var story = FakeEntityFactory.GetGenericStory(user.Id); session.Store(story); } session.SaveChanges(); Global.UpdateIndex<Story>(session); return session.Query<Story>().ToList<Story>(); } }
public void GetUserByIdReturnsUser() { var store = Global.GetInMemoryStore(); // Setup var repository = new UserRepository(store); User user = GetGenericUser(); repository.SaveUser(user); User queryUser = new User() { Email = "*****@*****.**", Password = "******" }; repository.SaveUser(queryUser); string userId = queryUser.Id; user = GetGenericUser(); user.Email = "*****@*****.**"; repository.SaveUser(user); // Act var result = repository.GetUserById(userId); //Assert Assert.IsNotNull(result.User); Assert.AreEqual(result.User.Email, queryUser.Email); Assert.AreEqual(result.User.Password, queryUser.Password); }
public void GetUserByEmailReturnsUser() { // Setup IDocumentStore store = Global.GetInMemoryStore(); using (var session = store.OpenSession()) { var user = new User() { Email = "email", Password = "******" }; session.Store(user); session.SaveChanges(); RavenQueryStatistics stats; var results = session.Query<User>() .Statistics(out stats) .Customize(x => x.WaitForNonStaleResults()) .Where(u => u.Email == "email") .ToArray(); } UserRepository repository = new UserRepository(store); // Act var result = repository.GetUserByEmail("email"); // Assert Assert.AreEqual(result.UserCode, UserErrorCode.Success); Assert.IsNotNull(result.User); }
public GetUserResult SaveUser(User user) { using (var session = _store.OpenSession()) { RunBusinessOperations(user); session.Advanced.UseOptimisticConcurrency = true; new RavenUniqueInserter() .StoreUnique(session, user, p => p.Email); try { session.SaveChanges(); return new GetUserResult() { UserCode = UserErrorCode.Success, User = user }; } catch (ConcurrencyException) { return new GetUserResult() { UserCode = UserErrorCode.EmailAlreadyExists, User = new User() }; } } }
private void RunBusinessOperations(User user) { user.Password = user.Password.Hash(); }
private static GetUserResult ReturnGetUserResult(User user, UserErrorCode failureCode) { var result = new GetUserResult() { User = user }; if (user == null) result.UserCode = failureCode; else result.UserCode = UserErrorCode.Success; return result; }