Example #1
0
        public void ShouldAuthenticateWithValidCredentials()
        {
            var password       = new Password(SECRET_PHRASE);
            var userClaims     = "ADMIN";
            var userRepository = new Mock <IUserRepository>(MockBehavior.Strict);
            var user           = new User("Name", "Surname", EMAIL, password);

            user.Claims.Add(userClaims);
            user.DefineId(ID);
            var expectedUserAuthenticated = new AuthenticatedUser
            {
                Id    = ID,
                Name  = user.Name,
                EMail = user.EMail
            };

            expectedUserAuthenticated.Claims.Add("ADMIN");
            userRepository
            .Setup(ur => ur.GetByEmail(EMAIL))
            .Returns(user)
            .Verifiable();
            IAuthenticationService authentication = new AuthenticationService(userRepository.Object);

            var userAuthenticated = authentication.AuthenticateBy(EMAIL, SECRET_PHRASE);

            userAuthenticated.Should().NotBeNull();
            userAuthenticated.Should().BeEquivalentTo(expectedUserAuthenticated);
            userRepository.Verify();
        }
Example #2
0
        public void ShoulNoAuthenticateWhenEmailIsInvalid()
        {
            var password       = new Password(SECRET_PHRASE);
            var userRepository = new Mock <IUserRepository>(MockBehavior.Strict);

            userRepository
            .Setup(ur => ur.GetByEmail(EMAIL_INVALID))
            .Returns((User)null)
            .Verifiable();
            IAuthenticationService authentication = new AuthenticationService(userRepository.Object);

            var userAuthenticated = authentication.AuthenticateBy(EMAIL_INVALID, SECRET_PHRASE);

            userAuthenticated.Should().BeNull();
            authentication.IsValid.Should().BeFalse();
            authentication.GetErrorMessages().Should().Be(CREDENTIALS_ERROR);
            userRepository.Verify();
        }
Example #3
0
        public void ShoulAuthenticationBeInvalidWhenPasswordIsWrong()
        {
            const string PASSWORD_INVALID = "wROng";
            var          password         = new Password(SECRET_PHRASE);
            var          user             = new User("Name", "Surname", EMAIL, password);

            user.Claims.Add("ADMIN");
            user.DefineId(ID);
            var userRepository = new Mock <IUserRepository>(MockBehavior.Strict);

            userRepository
            .Setup(ur => ur.GetByEmail(EMAIL))
            .Returns(user)
            .Verifiable();
            IAuthenticationService authentication = new AuthenticationService(userRepository.Object);

            var userAuthenticated = authentication.AuthenticateBy(EMAIL, PASSWORD_INVALID);

            userAuthenticated.Should().BeNull();
            authentication.IsValid.Should().BeFalse();
            authentication.GetErrorMessages().Should().Be(CREDENTIALS_ERROR);
            userRepository.Verify();
        }