Esempio n. 1
0
        public async Task TestTPaaSClientTokenThrowsExceptionBadRevokeResponse()
        {
            var accessToken = "token";
            var handlerMock = new Mock <HttpMessageHandler>(MockBehavior.Loose);

            handlerMock
            .Protected()
            // Setup the protected method to mock
            .Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.Is <HttpRequestMessage>(request =>
                                               request.Headers.Authorization.ToString() == $"Basic {accessToken}"
                                               ),
                ItExpr.IsAny <CancellationToken>()
                )
            // prepare the expected response of the mocked http call
            .ReturnsAsync((HttpRequestMessage req, CancellationToken token) =>
            {
                return(new HttpResponseMessage()
                {
                    StatusCode = HttpStatusCode.Unauthorized,
                    RequestMessage = req
                });
            })
            .Verifiable();

            var authHandler = new TPaaSApplicationCredentialsRequestHandler(handlerMock.Object);

            authHandler.SetTPaasToken(accessToken);

            // use real http client with mocked handler here
            var httpClient = new HttpClient(authHandler)
            {
                BaseAddress = new Uri("http://nowhere.specific/"),
            };

            var tpaasClient = new TPaaSClient(httpClient, new Mock <ILogger <TPaaSClient> >().Object);
            var clientState = new Mock <ITPaaSClientState>().SetupAllProperties();

            clientState.Object.TPaaSTokenExpiry = DateTime.MinValue;
            clientState.Object.TPaaSToken       = accessToken;

            await tpaasClient.SetState(clientState.Object);


            Func <Task> act = async() => { await tpaasClient.GetBearerTokenAsync(); };

            act.Should().Throw <TPaaSAuthenticationException>().WithMessage("Error revoking access token*");

            //Check that the client calling the correct methods
            handlerMock.Protected().Verify(
                "SendAsync",
                Times.Once(), // two to /token one to /revoke
                ItExpr.Is <HttpRequestMessage>(arg =>

                                               arg.Headers.Authorization.ToString() == $"Basic {accessToken}"
                                               ),
                ItExpr.IsAny <CancellationToken>()
                );
        }
Esempio n. 2
0
        public async Task TestApplicationBearerTokenIsCached()
        {
            var bearertokenType = "Bearer";

            var accessToken    = "token";
            var tokenExpiresIn = 100;
            var handlerMock    = new Mock <HttpMessageHandler>(MockBehavior.Loose);

            handlerMock
            .Protected()
            // Setup the protected method to mock
            .Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.Is <HttpRequestMessage>(request =>
                                               request.Headers.Authorization.ToString() == $"Basic {accessToken}"
                                               ),
                ItExpr.IsAny <CancellationToken>()
                )
            // prepare the expected response of the mocked http call
            .ReturnsAsync((HttpRequestMessage req, CancellationToken token) =>
            {
                return(new HttpResponseMessage
                {
                    StatusCode = HttpStatusCode.OK,
                    Content = new StringContent(JsonConvert.SerializeObject(new TPaaSClientCredentialsRawResponse
                    {
                        TokenType = bearertokenType,
                        TokenExpiry = tokenExpiresIn,
                        AccessToken = accessToken
                    })),
                    RequestMessage = req
                });
            })
            .Verifiable();


            var authHandler = new TPaaSApplicationCredentialsRequestHandler(handlerMock.Object);

            authHandler.SetTPaasToken(accessToken);

            // use real http client with mocked handler here
            var httpClient = new HttpClient(authHandler)
            {
                BaseAddress = new Uri("http://nowhere.specific/"),
            };



            var tpaasClient = new TPaaSClient(httpClient, new Mock <ILogger <TPaaSClient> >().Object);
            var clientState = new Mock <ITPaaSClientState>().SetupAllProperties();
            await tpaasClient.SetState(clientState.Object);

            var bearerToken = await tpaasClient.GetBearerTokenAsync();

            var bearerToken2 = await tpaasClient.GetBearerTokenAsync();

            bearerToken.Should().Be(bearerToken2);

            //Check that the client calling the correct methods
            //This should only be called once
            handlerMock.Protected().Verify(
                "SendAsync",
                Times.Once(), // we expected a single request
                ItExpr.Is <HttpRequestMessage>(arg =>
                                               arg.Headers.Authorization.ToString() == $"Basic {accessToken}"
                                               ),
                ItExpr.IsAny <CancellationToken>()
                );
        }
Esempio n. 3
0
        public async Task TestApplicationBearerTokenCacheExpires()
        {
            var bearertokenType = "Bearer";
            var accessToken     = "token";
            var tokenExpiresIn  = 100;
            var handlerMock     = new Mock <HttpMessageHandler>(MockBehavior.Loose);

            handlerMock
            .Protected()
            // Setup the protected method to mock calls to /token
            .Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.Is <HttpRequestMessage>(request =>
                                               request.RequestUri.ToString().Contains("/token") &&
                                               request.Headers.Authorization.ToString() == $"Basic {accessToken}"
                                               ),
                ItExpr.IsAny <CancellationToken>()
                )
            .ReturnsInOrder(
                Task.FromResult(new HttpResponseMessage()
            {
                StatusCode = HttpStatusCode.OK,
                Content    = new StringContent(JsonConvert.SerializeObject(new TPaaSClientCredentialsRawResponse
                {
                    TokenType   = bearertokenType,
                    TokenExpiry = 50,
                    AccessToken = accessToken
                }))
            }),
                Task.FromResult(new HttpResponseMessage()
            {
                StatusCode = HttpStatusCode.OK,
                Content    = new StringContent(JsonConvert.SerializeObject(new TPaaSClientCredentialsRawResponse
                {
                    TokenType   = bearertokenType,
                    TokenExpiry = tokenExpiresIn,
                    AccessToken = accessToken + '2'
                }))
            })
                );

            handlerMock
            .Protected()
            // Setup the protected method to mock calls to /revoke
            .Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.Is <HttpRequestMessage>(request =>
                                               request.RequestUri.ToString().Contains("/revoke") &&
                                               request.Headers.Authorization.ToString() == $"Basic {accessToken}"
                                               ),
                ItExpr.IsAny <CancellationToken>()
                )
            .ReturnsAsync(
                new HttpResponseMessage()
            {
                StatusCode = HttpStatusCode.OK,
            }
                )
            .Verifiable();

            var authHandler = new TPaaSApplicationCredentialsRequestHandler(handlerMock.Object);

            authHandler.SetTPaasToken(accessToken);

            // use real http client with mocked handler here
            var httpClient = new HttpClient(authHandler)
            {
                BaseAddress = new Uri("http://nowhere.specific/"),
            };

            var tpaasClient = new TPaaSClient(httpClient, new Mock <ILogger <TPaaSClient> >().Object);
            var clientState = new Mock <ITPaaSClientState>().SetupAllProperties();
            await tpaasClient.SetState(clientState.Object);


            var bearerToken = await tpaasClient.GetBearerTokenAsync();

            var bearerToken2 = await tpaasClient.GetBearerTokenAsync();


            bearerToken.Should().Be($"{bearertokenType} {accessToken}");
            bearerToken2.Should().Be($"{bearertokenType} {accessToken}2");

            //Check that the client calling the correct methods
            handlerMock.Protected().Verify(
                "SendAsync",
                Times.Exactly(3), // two to /token one to /revoke
                ItExpr.Is <HttpRequestMessage>(arg =>

                                               arg.Headers.Authorization.ToString() == $"Basic {accessToken}"
                                               ),
                ItExpr.IsAny <CancellationToken>()
                );
        }
Esempio n. 4
0
        public async Task TestApplicationAuthHeaderIsAdded()
        {
            var bearertokenType = "Bearer";

            var accessToken    = "token";
            var tokenExpiresIn = 100;
            var handlerMock    = new Mock <HttpMessageHandler>(MockBehavior.Loose);

            handlerMock
            .Protected()
            // Setup the PROTECTED method to mock
            .Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.Is <HttpRequestMessage>(request =>
                                               request.Headers.Authorization.ToString() == $"Basic {accessToken}"
                                               ),
                ItExpr.IsAny <CancellationToken>()
                )
            // prepare the expected response of the mocked http call
            .ReturnsAsync((HttpRequestMessage req, CancellationToken token) =>
            {
                return(new HttpResponseMessage()
                {
                    StatusCode = HttpStatusCode.OK,
                    Content = new StringContent(JsonConvert.SerializeObject(new TPaaSClientCredentialsRawResponse
                    {
                        TokenType = bearertokenType,
                        TokenExpiry = tokenExpiresIn,
                        AccessToken = accessToken
                    })),
                    RequestMessage = req
                });
            })
            .Verifiable();

            var authHandler = new TPaaSApplicationCredentialsRequestHandler(handlerMock.Object);

            authHandler.SetTPaasToken(accessToken);

            // use real http client with mocked handler here
            var httpClient = new HttpClient(authHandler)
            {
                BaseAddress = new Uri("http://nowhere.specific/"),
            };

            var result = await httpClient.PostAsync("", new StringContent(string.Empty));

            result.Should().NotBeNull();

            //Check that the middleware is adding the header and calling the correct methods
            handlerMock.Protected().Verify(
                "SendAsync",
                Times.Once(), // we expected a single request
                ItExpr.Is <HttpRequestMessage>(arg =>
                                               arg.Headers.Authorization.ToString() == $"Basic {accessToken}"
                                               ),
                ItExpr.IsAny <CancellationToken>()
                );
            var resultObject = JsonConvert.DeserializeObject <TPaaSClientCredentialsRawResponse>(result.Content.ReadAsStringAsync().Result);

            resultObject.TokenType.Should().Be(bearertokenType);
            resultObject.AccessToken.Should().Be(accessToken);
            resultObject.TokenExpiry.Should().Be(tokenExpiresIn);
        }