public void Login_Set_Token()
        {
            var password = "******";

            var userSecurity = UserSecurity.CreateBasic(
                Guid.Empty,
                "*****@*****.**",
                password
                );

            userSecurity.Id = Guid.NewGuid();

            var localAuthService = LocalAuthServiceMock.Create();

            localAuthService.UserSecurityRepositoryMock.Setup(us => us.GetByEmail(userSecurity.Email)).Returns(userSecurity);
            var token = Guid.NewGuid().ToString();

            localAuthService.AbiokaTokenMock.Setup(t => t.Encode(It.Is <UserClaim>(uc => uc.Email == userSecurity.Email && uc.Id == userSecurity.Id))).Returns(token);
            var result = localAuthService.LoginAsync(new AuthRequest {
                Email    = userSecurity.Email,
                Password = password
            }).Result;

            Assert.AreEqual(userSecurity.Token, token);
            localAuthService.UserSecurityRepositoryMock.Verify(us => us.Update(userSecurity), Times.Once());
        }
        public void ArePasswordEqual()
        {
            var userSecurity = UserSecurity.CreateBasic(Guid.Empty, "*****@*****.**", "1234");

            Assert.IsTrue(userSecurity.ArePasswordEqual(userSecurity.Email, "1234"));
            Assert.IsFalse(userSecurity.ArePasswordEqual(userSecurity.Email, "1235"));
        }
        public void ChangePassword_Throws_AnException_If_The_OldPassword_Is_Wrong()
        {
            var userSecurity = UserSecurity.CreateBasic(Guid.NewGuid(), "*****@*****.**", "1234");

            var exception = Assert.Throws <DenialException>(() => userSecurity.ChangePassword("123", "1234"));

            Assert.IsNotNull(exception);
            Assert.AreEqual("WrongPassword", exception.Message);
        }
        public void ChangePassword_Throws_AnException_If_The_OldPassword_And_NewPassword_Are_Same()
        {
            var password     = "******";
            var userSecurity = UserSecurity.CreateBasic(Guid.Empty, "*****@*****.**", password);

            var exception = Assert.Throws <DenialException>(() => userSecurity.ChangePassword(password, password));

            Assert.IsNotNull(exception);
            Assert.AreEqual("NewPasswordCannotBeSameAsTheOldPassword", exception.Message);
        }
        public void ChangePassword_Throws_AnException_If_The_NewPassword_Is_Null()
        {
            var password     = "******";
            var userSecurity = UserSecurity.CreateBasic(Guid.NewGuid(), "*****@*****.**", password);

            var exception = Assert.Throws <DenialException>(() => userSecurity.ChangePassword(password, string.Empty));

            Assert.IsNotNull(exception);
            Assert.AreEqual("PasswordCannotBeEmpty", exception.Message);
        }
Beispiel #6
0
        public void Login_Adds_Successful_LoginAttempt()
        {
            var password     = "******";
            var userSecurity = UserSecurity.CreateBasic(Guid.Empty, "*****@*****.**", password, true, false);

            authRequestValidator.UserSecurityRepositoryMock.Setup(us => us.GetByEmail(userSecurity.Email)).Returns(userSecurity);

            authRequestValidator.DataValidate(new AuthRequest {
                Email    = userSecurity.Email,
                Password = password,
                provider = AuthProvider.Local
            }, ActionType.Add);

            authRequestValidator.LoginAttemptRepositoryMock.Verify(l => l.Add(It.Is <LoginAttempt>(la => la.LoginResult == LoginResult.Successful)), Times.Once());
        }
Beispiel #7
0
        public void Login_Throws_Wrong_Password()
        {
            var userSecurity = UserSecurity.CreateBasic(Guid.Empty, "*****@*****.**", "1234");

            authRequestValidator.UserSecurityRepositoryMock.Setup(us => us.GetByEmail(userSecurity.Email)).Returns(userSecurity);

            var exception = Assert.Throws <DenialException>(() => authRequestValidator.DataValidate(new AuthRequest {
                Email    = userSecurity.Email,
                Password = "******",
                provider = AuthProvider.Local
            }, ActionType.Add));

            Assert.AreEqual(exception.Message, "WrongPassword");
            Assert.AreEqual(exception.StatusCode, HttpStatusCode.BadRequest);
            authRequestValidator.LoginAttemptRepositoryMock.Verify(l => l.Add(It.Is <LoginAttempt>(la => la.LoginResult == LoginResult.WrongPassword)), Times.Once());
        }
Beispiel #8
0
        public void Add_Calls_Repository_Add_Method_And_Returns_User()
        {
            var addUserRequest = new AddUserRequest {
                Email    = "*****@*****.**",
                Password = "******"
            };

            var userService = UserServiceMock.Create();

            userService.UserSecurityRepositoryMock.Setup(us => us.GetByEmail(addUserRequest.Email)).Returns((UserSecurity)null);
            userService.CurrentContextMock.Setup(ct => ct.Principal).Returns(new CustomPrincipal("")
            {
                Language = "en"
            });
            userService.CurrentContextMock.Setup(ct => ct.Current).Returns(userService.CurrentContextMock.Object);
            var user = userService.Add(addUserRequest);

            var userSecurity = UserSecurity.CreateBasic(Guid.Empty, addUserRequest.Email, addUserRequest.Password);

            userService.UserSecurityRepositoryMock.Verify(us => us.Add(It.Is <UserSecurity>(e => e.Email == addUserRequest.Email && e.AuthProvider == AuthProvider.Local && e.Password == userSecurity.Password && e.Language == "en")), Times.Once());
            Assert.AreEqual(user.Email, addUserRequest.Email);
        }
        public void Add_Throws_User_Is_Already_Registered()
        {
            var registerUserRequest = new RegisterUserRequest {
                Email    = "*****@*****.**",
                Password = "******"
            };

            var userService = AddUserRequestValidatorMock.Create();

            userService.UserSecurityRepositoryMock.Setup(us => us.GetByEmail(registerUserRequest.Email)).Returns(UserSecurity.CreateBasic(Guid.Empty, string.Empty, string.Empty));
            var exception = Assert.Throws <DenialException>(() => userService.DataValidate(registerUserRequest, ActionType.Add));

            Assert.AreEqual(exception.Message, "UserIsAlreadyRegistered");
            Assert.AreEqual(exception.StatusCode, HttpStatusCode.BadRequest);
        }
Beispiel #10
0
        public void NotComputeHashPassword()
        {
            var userSecurity = UserSecurity.CreateBasic(Guid.NewGuid(), "*****@*****.**", "1234");

            Assert.AreEqual("1234", userSecurity.Password);
        }