Example #1
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);
        }
Example #2
0
        public async Task CanTokenBeRefreshedAsync_TokenDoesNotExistScenario_Test()
        {
            //arrange
            var claimsPrincipal = new ClaimsPrincipal();

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

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

            //assert
            repository.Setup(repository => repository.GetRefreshToken(It.IsAny <string>()))
            .ReturnsAsync((RefreshToken)null);
            _unitOfWork.Setup(uOfWork => uOfWork.RefreshTokenRepository).Returns(repository.Object);

            Assert.Null(await _tokenService.CanTokenBeRefreshedAsync(claimsPrincipal, DataFixtures.RefreshToken));
        }
Example #3
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);
        }