Example #1
0
        public async Task AuthorizeUser_ReturnsTokens()
        {
            // Arrange
            var       tokenFactory = new TestTokenFactory();
            string    id_token     = tokenFactory.GetIdToken(true);
            string    access_token = tokenFactory.GetAccessToken(false);
            JwtTokens tokens       = new JwtTokens {
                AccessToken = access_token, IdToken = id_token
            };

            Mock <ITokenRepository> repositoryMock = new Mock <ITokenRepository>();
            Mock <ILogger <OAuthAuthorizationService> > loggerMock = new Mock <ILogger <OAuthAuthorizationService> >();
            Mock <ILoginService> loginMock = new Mock <ILoginService>();

            loginMock
            .Setup(mock => mock.Login())
            .Returns(Task.FromResult(new ServiceResponse <JwtTokens>(tokens)));

            IAccountService accountService = new OAuthAuthorizationService(repositoryMock.Object, loginMock.Object, loggerMock.Object);

            // Act
            ServiceResponse <LoginResponse> authResponse = await accountService.AuthorizeUser();

            // Assert
            Assert.IsNotNull(authResponse.Data.AccessToken.UserId);
            Assert.AreNotEqual(0, authResponse.Data.AccessToken.Scopes.Length);
            Assert.AreNotEqual(0, authResponse.Data.AccessToken.ExpiresAt);

            // Ensure we cleaned up previous auth sessions, attempted to perform a login and then saved the new auth tokens.
            repositoryMock.Verify(repo => repo.DeleteToken(), Times.Once);
            repositoryMock.Verify(repo => repo.SaveToken(It.IsAny <JwtTokens>()), Times.Once);
            loginMock.Verify(repo => repo.Login(), Times.Once);
        }
Example #2
0
        public void JsonDeserialization_AssignsValues()
        {
            // Arrange
            var    tokenFactory    = new TestTokenFactory();
            string idTokenJson     = tokenFactory.GetIdToken(false);
            string accessTokenJson = tokenFactory.GetAccessToken(false);
            string jwtTokenJson    = JsonSerializer.Serialize(new { id_token = idTokenJson, access_token = accessTokenJson });

            // Act
            JwtTokens jwtTokens = Newtonsoft.Json.JsonConvert.DeserializeObject <JwtTokens>(jwtTokenJson);

            // Assert
            Assert.AreEqual(idTokenJson, jwtTokens.IdToken, "Expected the id_token value to have been assigned");
            Assert.AreEqual(accessTokenJson, jwtTokens.AccessToken, "Expected the access_token value to have been assigned");
        }
Example #3
0
        public void AccessToken_HasExpiredToken()
        {
            // Arrange
            var    tokenFactory    = new TestTokenFactory();
            string idTokenJson     = tokenFactory.GetIdToken(false);
            string accessTokenJson = tokenFactory.GetAccessToken(true);
            string jwtTokenJson    = JsonSerializer.Serialize(new { id_token = idTokenJson, access_token = accessTokenJson });

            JwtTokens jwtTokens = Newtonsoft.Json.JsonConvert.DeserializeObject <JwtTokens>(jwtTokenJson);

            // Act
            bool isExpired = jwtTokens.IsAccessTokenExpired();

            // Assert
            Assert.IsTrue(isExpired);
        }
Example #4
0
        public void IdToken_GetTokenReturnsValue()
        {
            // Arrange
            var    tokenFactory    = new TestTokenFactory();
            string idTokenJson     = tokenFactory.GetIdToken(true);
            string accessTokenJson = tokenFactory.GetAccessToken(false);
            string jwtTokenJson    = JsonSerializer.Serialize(new { id_token = idTokenJson, access_token = accessTokenJson });

            JwtTokens jwtTokens = Newtonsoft.Json.JsonConvert.DeserializeObject <JwtTokens>(jwtTokenJson);

            // Act
            IdToken idToken = jwtTokens.GetIdToken();

            // Assert
            Assert.IsNotNull(idToken.UserId);
            Assert.IsNotNull(idToken.Username);
            Assert.AreNotEqual(0, idToken.ExpiresAt);
        }
Example #5
0
        public async Task AuthorizeUser_ReturnsErrorWhenSavingFails()
        {
            // Arrange
            var       tokenFactory = new TestTokenFactory();
            string    id_token     = tokenFactory.GetIdToken(true);
            string    access_token = tokenFactory.GetAccessToken(false);
            JwtTokens tokens       = new JwtTokens {
                AccessToken = access_token, IdToken = id_token
            };

            Mock <ILogger <OAuthAuthorizationService> > loggerMock = new Mock <ILogger <OAuthAuthorizationService> >();

            Mock <ITokenRepository> repositoryMock = new Mock <ITokenRepository>();

            repositoryMock
            .Setup(repo => repo.SaveToken(It.IsAny <JwtTokens>()))
            .Throws <Exception>();

            Mock <ILoginService> loginMock = new Mock <ILoginService>();

            loginMock
            .Setup(mock => mock.Login())
            .Returns(Task.FromResult(new ServiceResponse <JwtTokens>(tokens)));

            IAccountService accountService = new OAuthAuthorizationService(repositoryMock.Object, loginMock.Object, loggerMock.Object);

            // Act
            ServiceResponse <LoginResponse> authResponse = await accountService.AuthorizeUser();

            // Assert
            Assert.IsNull(authResponse.Data);
            Assert.AreEqual(1, authResponse.Errors.Length);
            Assert.AreEqual(SDKErrors.SaveTokenFailed.Code, authResponse.Errors[0].Code);
            Assert.AreEqual(SDKErrors.SaveTokenFailed.Message, authResponse.Errors[0].Message);
            repositoryMock.Verify(repo => repo.DeleteToken(), Times.Once);
            repositoryMock.Verify(repo => repo.SaveToken(It.IsAny <JwtTokens>()), Times.Once);
            loginMock.Verify(repo => repo.Login(), Times.Once);
        }