示例#1
0
        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);
        }
示例#2
0
文件: Index.cs 项目: n1ghtmare/Kauntr
        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);
        }
示例#3
0
        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()));
        }
示例#4
0
        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);
        }
示例#5
0
文件: Index.cs 项目: n1ghtmare/Kauntr
        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);
        }
示例#6
0
        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);
        }
示例#7
0
        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);
        }
示例#8
0
        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);
        }
示例#9
0
文件: Index.cs 项目: n1ghtmare/Kauntr
        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);
        }
示例#10
0
        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);
        }
示例#11
0
        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);
        }
示例#12
0
        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);
        }
示例#13
0
        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);
        }
示例#14
0
        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);
        }
示例#15
0
文件: Index.cs 项目: n1ghtmare/Kauntr
        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);
        }
示例#16
0
        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);
        }
示例#17
0
        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);
        }
示例#18
0
        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);
        }
示例#19
0
        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));
        }
示例#20
0
        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);
        }