public async Task RegisterUserAsyncTest()
        {
            var user   = MockConfigurator.MockUser();
            var policy = MockConfigurator.MockPolicy();

            _userManager.Setup(uManager => uManager.GetClaimsAsync(user))
            .ReturnsAsync(new List <Claim> {
                new Claim(policy.Claim, "true")
            });

            var organisation = new Organisation {
                Users = new List <User> {
                    user
                },
                Policies = new List <Policy> {
                    MockConfigurator.MockPolicy()
                }
            };

            var response = await _authService.RegisterUserAsync(_registrationRequest, organisation, user);

            Assert.Null(response.RefreshToken);
            Assert.Null(response.Token);
            Assert.Same(response.Email, user.Email);
        }
Beispiel #2
0
        public async Task GetTokensTest()
        {
            User user   = MockConfigurator.MockUser();
            var  claims = new List <Claim> {
                new Claim(DataFixtures.PolicyClaim1, "true")
            };

            _userManager.Setup(uManager => uManager.GetClaimsAsync(user)).ReturnsAsync(claims);

            var repository = new Mock <IRefreshTokenRepository>();

            repository.Setup(repository => repository.CreateRefreshToken(It.IsAny <string>(), It.IsAny <string>()))
            .Returns(MockConfigurator.MockRefreshToken());

            _unitOfWork.Setup(uOfWork => uOfWork.RefreshTokenRepository).Returns(repository.Object);

            var tokens = await _tokenService.GetTokensAsync(user);

            tokens.TryGetValue("SecurityToken", out string securityToken);
            tokens.TryGetValue("RefreshToken", out string refreshToken);

            Assert.NotNull(tokens);
            Assert.NotNull(securityToken);
            Assert.NotNull(refreshToken);
        }
        public async Task LoginUserAsync_FailureScenario_Test()
        {
            var user = MockConfigurator.MockUser();

            _userManager.Setup(uManager => uManager.CheckPasswordAsync(user, _loginRequest.Password))
            .ReturnsAsync(false);

            Assert.Null(await _authService.LoginUserAsync(_loginRequest, user));
        }
        public async Task ValidateRegistrationAsync_EmailExists_OrganisationExists_Test()
        {
            //arrange
            var organisation = new Mock <Organisation>();
            var user         = MockConfigurator.MockUser();

            _userManager.Setup(uManager => uManager.FindByEmailAsync(_registrationRequest.Email))
            .ReturnsAsync(user);

            //assert
            Assert.Collection(await _authService.ValidateRegistrationAsync(_registrationRequest, organisation.Object, user),
                              item => Assert.DoesNotContain("No Organisation found with the given Organisation Name", item.Message));
        }
        public async Task ValidateRegistrationAsync_SuccessScenario_Test()
        {
            //arrange
            var organisation = new Mock <Organisation>();
            var user         = MockConfigurator.MockUser();

            _userManager.Setup(uManager => uManager.FindByEmailAsync(_registrationRequest.Email))
            .ReturnsAsync((User)null);

            _userManager.Setup(uManager => uManager.CreateAsync(user, DataFixtures.Password))
            .ReturnsAsync(new IdentityResult());

            //assert
            Assert.Empty(await _authService.ValidateRegistrationAsync(_registrationRequest, organisation.Object, user));
        }
        public async Task LoginUserAsync_SuccessScenario_Test()
        {
            var user = MockConfigurator.MockUser();

            _userManager.Setup(uManager => uManager.CheckPasswordAsync(user, _loginRequest.Password))
            .ReturnsAsync(true);

            var response = await _authService.LoginUserAsync(_loginRequest, user);

            Assert.IsType <LoginResponse>(response);
            Assert.NotNull(response);
            Assert.Same(response.Email, _loginRequest.Email);
            Assert.Null(response.RefreshToken);
            Assert.Null(response.Token);
        }
        public async Task ValidateRegistrationAsync_RegistrationConstraintNotMetScenario_Test()
        {
            //arrange
            var organisation = new Mock <Organisation>();
            var user         = MockConfigurator.MockUser();

            _userManager.Setup(uManager => uManager.FindByEmailAsync(_registrationRequest.Email))
            .ReturnsAsync((User)null);

            var errors = new IdentityError[1] {
                new IdentityError {
                    Description = "Passwords must have at least one non alphanumeric character."
                }
            };

            _userManager.Setup(uManager => uManager.CreateAsync(user, DataFixtures.Password))
            .ReturnsAsync(IdentityResult.Failed(errors));

            Assert.Collection(await _authService.ValidateRegistrationAsync(_registrationRequest, organisation.Object, user),
                              item => Assert.Contains("Passwords must have at least one non alphanumeric character.", item.Message));
        }
Beispiel #8
0
        public async Task RefreshTokenAsyncTest()
        {
            //arrange
            var claimsPrincipal = new ClaimsPrincipal();
            var organisationID  = Guid.NewGuid().ToString();
            var user            = MockConfigurator.MockUser();

            var claims = new List <Claim> {
                new Claim(DataFixtures.PolicyClaim1, "true")
            };

            _userManager.Setup(uManager => uManager.GetClaimsAsync(user)).ReturnsAsync(claims);

            var refreshToken           = MockConfigurator.MockRefreshToken();
            var refreshTokenRepository = new Mock <IRefreshTokenRepository>();

            refreshTokenRepository.Setup(repository => repository.CreateRefreshToken(It.IsAny <string>(), It.IsAny <string>()))
            .Returns(refreshToken);
            _unitOfWork.Setup(uOfWork => uOfWork.RefreshTokenRepository).Returns(refreshTokenRepository.Object);

            string claimName = "ID";

            ClaimHelper.GetNamedClaim     = (claimsPrincipal, claimName) => DataFixtures.GUID;
            ClaimHelper.GetUnixExpiryDate = (claimsPrincipal) => DateTimeOffset.Now.ToUnixTimeSeconds();
            ClaimHelper.GetJTI            = (claimsPrincipal) => DataFixtures.GUID;

            var UserRepository = new Mock <IUserRepository>();

            UserRepository.Setup(repository => repository.GetWithDetailsAsync(It.IsAny <string>(), It.IsAny <string>()))
            .ReturnsAsync(user);
            _unitOfWork.Setup(uOfWork => uOfWork.UserRepository).Returns(UserRepository.Object);

            var response = await _tokenService.RefreshTokenAsync(claimsPrincipal, refreshToken, organisationID);

            //assert
            Assert.NotNull(response);
            Assert.Same(user.Email, response.Email);
        }