public async Task AnUnAuthenticatedUserPostsTokenAndAccountIdThatAreValid_MarksAuthenticationTokenAsUsed() { TestableAccountController controller = TestableAccountController.Create(); var systemTime = new DateTime(2017, 5, 1, 7, 31, 50); controller.MockContextService.Setup(x => x.CurrentUserIsAuthenticated).Returns(false); controller.MockSystemClock.Setup(x => x.UtcNow).Returns(systemTime); const int accountId = 1; const string token = "totally-fake-token"; controller.AuthenticationTokenRepository.AuthenticationTokens.Add(new AuthenticationToken { Id = Guid.NewGuid(), AccountId = accountId, Token = token, IsUsed = false }); EmptyResult result = await controller.Authenticate(new AuthenticationViewModel { AuthenticationToken = token, AccountId = accountId }) as EmptyResult; AuthenticationToken authenticationToken = controller.AuthenticationTokenRepository.AuthenticationTokens.FirstOrDefault(); Assert.IsNotNull(result); Assert.IsNotNull(authenticationToken); Assert.IsTrue(authenticationToken.IsUsed); Assert.AreEqual(systemTime, authenticationToken.UsedOn); Assert.AreEqual(1, controller.AuthenticationTokenRepository.NumberOfTimesUpdateWasInvoked); }
public async Task GetAccountId_ReturnsCorrectAccountDetailsWithPrivacyForEmail() { TestableAccountController controller = TestableAccountController.Create(); const int requestedAccountId = 2; const int currentUserAccountId = 1; controller.MockContextService.Setup(x => x.CurrentUserAccountId).Returns(currentUserAccountId); const int token = 123; var account = new Account { Id = requestedAccountId, Email = "*****@*****.**", DisplayName = "tester" }; var accountCurrentUser = new Account { Id = currentUserAccountId, Email = "*****@*****.**", DisplayName = "tester - 2" }; controller.AccountRepository.Accounts.Add(accountCurrentUser); controller.AccountRepository.Accounts.Add(account); JsonResult result = await controller.Index(token, requestedAccountId) as JsonResult; dynamic accountJson = result?.Data; dynamic accountResult = accountJson?.Account; Assert.IsNotNull(accountJson); Assert.IsNotNull(accountJson.Account); Assert.AreEqual(account.Id, accountResult.Id); Assert.IsNull(accountResult.Email); Assert.AreEqual(token, accountJson.Token); }
public async Task PostFromAnAuthenticatedUserWithAnExistingUnusedToken_ResendsAnEmailToUser() { TestableAccountController controller = TestableAccountController.Create(); controller.MockContextService.Setup(x => x.CurrentUserIsAuthenticated).Returns(false); const string email = "*****@*****.**"; const int accountId = 1; var existingAuthenticationToken = new AuthenticationToken { AccountId = accountId, Token = "SOME_TOKEN" }; var account = new Account { Email = email, Id = accountId, }; controller.AccountRepository.Accounts.Add(account); controller.AuthenticationTokenRepository.AuthenticationTokens.Add(existingAuthenticationToken); await controller.Login(new LoginViewModel { Email = email }); MailMessage emailNotification = controller.NotificationService.Emails.FirstOrDefault(x => x.To.Any(a => a.Address.ToString() == email)); Assert.IsNotNull(emailNotification); Assert.IsNotEmpty(emailNotification.Body); Assert.IsTrue(emailNotification.Body.Contains(existingAuthenticationToken.Token)); Assert.IsTrue(emailNotification.Body.Contains(accountId.ToString())); }
public async Task PostFromAnUnAuthenticatedUserThatHasSentTokens5Times_ReturnsHttpStatusCode403WithCustomMessage() { TestableAccountController controller = TestableAccountController.Create(); controller.MockContextService.Setup(x => x.CurrentUserIsAuthenticated).Returns(false); const string email = "*****@*****.**"; const int accountId = 1; var existingAuthenticationToken = new AuthenticationToken { AccountId = accountId, Token = "SOME_TOKEN", NumberOfTimesSent = 5 }; var account = new Account { Email = email, Id = accountId, }; controller.AccountRepository.Accounts.Add(account); controller.AuthenticationTokenRepository.AuthenticationTokens.Add(existingAuthenticationToken); HttpStatusCodeResult result = await controller.Login(new LoginViewModel { Email = "*****@*****.**" }) as HttpStatusCodeResult; Assert.IsNotNull(result); Assert.AreEqual(403, result.StatusCode); Assert.AreEqual("Forbidden", result.StatusDescription); }
public async Task Get_ReturnsCurrentUserAccountDetails() { TestableAccountController controller = TestableAccountController.Create(); const int currentUserAccountId = 1; controller.MockContextService.Setup(x => x.CurrentUserAccountId).Returns(currentUserAccountId); controller.MockContextService.Setup(x => x.CurrentUserIsAuthenticated).Returns(true); const int token = 123; var account = new Account { Id = currentUserAccountId, Email = "*****@*****.**", DisplayName = "tester" }; controller.AccountRepository.Accounts.Add(account); JsonResult result = await controller.Index(token) as JsonResult; dynamic accountJson = result?.Data; Account accountResult = accountJson?.Account as Account; Assert.IsNotNull(accountJson); Assert.IsNotNull(accountJson.Account); Assert.IsNotNull(accountResult); Assert.AreEqual(account.Id, accountResult.Id); Assert.AreEqual(token, accountJson.Token); }
public async Task PostFromAnAuthenticatedUserWithValidModelState_UpdatesAccountAndReturnsEmptyResult() { TestableAccountController controller = TestableAccountController.Create(); const int currentUserAccountId = 1; const string newDisplayName = "Bobo"; controller.MockContextService.Setup(x => x.CurrentUserAccountId).Returns(currentUserAccountId); var account = new Account { Id = currentUserAccountId, CreatedOn = DateTime.Today, DisplayName = "dude", IsAutoSetup = true, Email = "*****@*****.**", Reputation = 123 }; controller.AccountRepository.Accounts.Add(account); EmptyResult result = await controller.Update(new AccountUpdateViewModel { DisplayName = newDisplayName }) as EmptyResult; Assert.IsNotNull(result); Assert.AreEqual(newDisplayName, account.DisplayName); Assert.IsFalse(account.IsAutoSetup); Assert.AreEqual(1, controller.AccountRepository.NumberOfTimesUpdateWasInvoked); }
public void AnAuthenticatedUser_ReturnEmptyResult() { TestableAccountController controller = TestableAccountController.Create(); controller.MockContextService.Setup(x => x.CurrentUserIsAuthenticated).Returns(true); EmptyResult result = controller.Logout() as EmptyResult; Assert.IsNotNull(result); }
public void AnUnAuthenticatedUser_ReturnsHttpStatusCodeResult400BadRequest() { TestableAccountController controller = TestableAccountController.Create(); HttpStatusCodeResult result = controller.Logout() as HttpStatusCodeResult; Assert.IsNotNull(result); Assert.AreEqual(400, result.StatusCode); Assert.AreEqual("Bad Request", result.StatusDescription); }
public async Task GetAnAccountIdThatIsNotExisting_ReturnsHttpStatusCode404NotFound() { TestableAccountController controller = TestableAccountController.Create(); HttpStatusCodeResult result = await controller.Index(123, 1) as HttpStatusCodeResult; Assert.IsNotNull(result); Assert.AreEqual(404, result.StatusCode); Assert.AreEqual("Not Found", result.StatusDescription); }
public async Task AnAuthenticatedUserPostsToken_ReturnsHttpStatusCodeResult400BadRequest() { TestableAccountController controller = TestableAccountController.Create(); controller.MockContextService.Setup(x => x.CurrentUserIsAuthenticated).Returns(true); HttpStatusCodeResult result = await controller.Authenticate(new AuthenticationViewModel { AuthenticationToken = "test", AccountId = 1 }) as HttpStatusCodeResult; Assert.IsNotNull(result); Assert.AreEqual(400, result.StatusCode); Assert.AreEqual("Bad Request", result.StatusDescription); }
public async Task PostFromAuthenticatedUser_ReturnsHttpStatusCode400BadRequest() { TestableAccountController controller = TestableAccountController.Create(); controller.MockContextService.Setup(x => x.CurrentUserIsAuthenticated).Returns(true); HttpStatusCodeResult result = await controller.Login(new LoginViewModel { Email = "*****@*****.**" }) as HttpStatusCodeResult; Assert.IsNotNull(result); Assert.AreEqual(400, result.StatusCode); Assert.AreEqual("Bad Request", result.StatusDescription); }
public async Task AnUnAuthenticatedUserPostsTokenAndAccountIdThatAreNotValid_ReturnsHttpStatusCodeResult403Forbidden() { TestableAccountController controller = TestableAccountController.Create(); controller.MockContextService.Setup(x => x.CurrentUserIsAuthenticated).Returns(false); HttpStatusCodeResult result = await controller.Authenticate(new AuthenticationViewModel { AuthenticationToken = "test", AccountId = 1 }) as HttpStatusCodeResult; Assert.IsNotNull(result); Assert.AreEqual(403, result.StatusCode); Assert.AreEqual("Forbidden", result.StatusDescription); }
public async Task PostFromAuthenticatedUser_DoesntRegisterUser() { TestableAccountController controller = TestableAccountController.Create(); controller.MockContextService.Setup(x => x.CurrentUserIsAuthenticated).Returns(true); const string email = "*****@*****.**"; await controller.Login(new LoginViewModel { Email = email }); Account account = await controller.AccountRepository.GetByEmailAsync(email); Assert.IsNull(account); }
public async Task PostFromAnAuthenticatedUserWithInvalidModelState_ReturnsHttpStatusCode400BadRequest() { TestableAccountController controller = TestableAccountController.Create(); controller.MockContextService.Setup(x => x.CurrentUserAccountId).Returns(1); controller.ModelState.AddModelError("NOPE", "Erroredededed"); HttpStatusCodeResult result = await controller.Update(new AccountUpdateViewModel()) as HttpStatusCodeResult; Assert.IsNotNull(result); Assert.AreEqual(400, result.StatusCode); Assert.AreEqual("Bad Request", result.StatusDescription); }
public async Task GetFromAnUnauthenticatedUserWithNoAccountIdRequested_ReturnsHttpStatusCodeResult403Forbidden() { TestableAccountController controller = TestableAccountController.Create(); controller.MockContextService.Setup(x => x.CurrentUserIsAuthenticated).Returns(false); const int token = 123; HttpStatusCodeResult result = await controller.Index(token) as HttpStatusCodeResult; Assert.IsNotNull(result); Assert.AreEqual(403, result.StatusCode); Assert.AreEqual("Forbidden", result.StatusDescription); }
public async Task PostFromAnUnAuthenticatedUser_CreatesAnAuthenticationTokenWithCorrectValues() { TestableAccountController controller = TestableAccountController.Create(); var systemTime = new DateTime(2017, 5, 1, 9, 31, 57); controller.MockContextService.Setup(x => x.CurrentUserIsAuthenticated).Returns(false); controller.MockSystemClock.Setup(x => x.UtcNow).Returns(systemTime); await controller.Login(new LoginViewModel { Email = "*****@*****.**" }); AuthenticationToken authenticationToken = controller.AuthenticationTokenRepository.AuthenticationTokens.FirstOrDefault(); Assert.IsNotNull(authenticationToken); Assert.IsNotEmpty(authenticationToken.Token); Assert.AreEqual(systemTime, authenticationToken.CreatedOn); Assert.AreEqual(systemTime.AddMinutes(15), authenticationToken.ExpiresOn); }
public async Task PostFromUnAuthenticatedUser_RegistersUserWithSystemGeneratedUsername() { TestableAccountController controller = TestableAccountController.Create(); var systemTime = new DateTime(2017, 5, 1, 9, 31, 57); // Ticks - 636292279170000000 controller.MockContextService.Setup(x => x.CurrentUserIsAuthenticated).Returns(false); controller.MockSystemClock.Setup(x => x.UtcNow).Returns(systemTime); const string email = "*****@*****.**"; await controller.Login(new LoginViewModel { Email = email }); Account account = await controller.AccountRepository.GetByEmailAsync(email); Assert.IsNotNull(account); Assert.AreEqual(email, account.Email); Assert.AreEqual("user_7000000", account.DisplayName); Assert.AreEqual(systemTime, account.CreatedOn); }
public async Task PostFromAnUnauthenticatedUser_UpdatesNumberOfTokensSentAndLastSendOn() { TestableAccountController controller = TestableAccountController.Create(); var systemTime = new DateTime(2017, 5, 1, 7, 31, 50); controller.MockContextService.Setup(x => x.CurrentUserIsAuthenticated).Returns(false); controller.MockSystemClock.Setup(x => x.UtcNow).Returns(systemTime); const string email = "*****@*****.**"; await controller.Login(new LoginViewModel { Email = email }); AuthenticationToken authenticationToken = controller.AuthenticationTokenRepository.AuthenticationTokens.FirstOrDefault(); Assert.IsNotNull(authenticationToken); Assert.AreEqual(1, authenticationToken.NumberOfTimesSent); Assert.AreEqual(systemTime, authenticationToken.LastSentOn); Assert.AreEqual(1, controller.AuthenticationTokenRepository.NumberOfTimesUpdateWasInvoked); }
public async Task PostFromAnUnAuthenticatedUser_SendsAnEmailToTheUser() { TestableAccountController controller = TestableAccountController.Create(); var systemTime = new DateTime(2017, 5, 1, 9, 31, 57); controller.MockContextService.Setup(x => x.CurrentUserIsAuthenticated).Returns(false); controller.MockSystemClock.Setup(x => x.UtcNow).Returns(systemTime); const string email = "*****@*****.**"; await controller.Login(new LoginViewModel { Email = email }); AuthenticationToken authenticationToken = controller.AuthenticationTokenRepository.AuthenticationTokens.FirstOrDefault(); MailMessage emailNotification = controller.NotificationService.Emails.FirstOrDefault(x => x.To.Any(a => a.Address.ToString() == email)); Assert.IsNotNull(emailNotification); Assert.IsNotEmpty(emailNotification.Body); Assert.IsNotNull(authenticationToken); Assert.IsTrue(emailNotification.Body.Contains(authenticationToken.Token)); }
public async Task AnUnAuthenticatedUserPostsTokenAndAccountIdThatAreValid_ReturnEmptyResult() { TestableAccountController controller = TestableAccountController.Create(); controller.MockContextService.Setup(x => x.CurrentUserIsAuthenticated).Returns(false); const int accountId = 1; const string token = "totally-fake-token"; controller.AuthenticationTokenRepository.AuthenticationTokens.Add(new AuthenticationToken { Id = Guid.NewGuid(), AccountId = accountId, Token = token }); EmptyResult result = await controller.Authenticate(new AuthenticationViewModel { AuthenticationToken = token, AccountId = accountId }) as EmptyResult; Assert.IsNotNull(result); }