Exemplo n.º 1
0
        public async Task RetrieveToken_Success()
        {
            // ARRANGE
            var appCache    = (IAppCache)_context.Properties["appCache"];
            var bearerToken = new BearerTokenResponse
            {
                access_token = "token"
            };
            var json = System.Text.Json.JsonSerializer.Serialize(bearerToken);
            var clientHandlerStub = new DelegatingHandlerStub((request, cancellationToken) =>
            {
                var response = new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new StringContent(json, Encoding.UTF8)
                };
                return(Task.FromResult(response));
            }
                                                              );
            var client = new HttpClient(clientHandlerStub);
            var httpClientFactoryMock = new Mock <IHttpClientFactory>();

            httpClientFactoryMock.Setup(mock => mock.CreateClient(It.IsAny <string>()))
            .Returns(client)
            .Verifiable();

            var configurationMock = new Mock <IConfiguration>();

            configurationMock.Setup(mock => mock[It.IsAny <string>()])
            .Returns("http://test.url")
            .Verifiable();

            var keyVaultMock = new Mock <IAzureKeyVaultService>();

            var unitUnderTest =
                new BearerTokenProvider(appCache,
                                        httpClientFactoryMock.Object,
                                        configurationMock.Object,
                                        keyVaultMock.Object);

            // ACT
            var result = await unitUnderTest.RetrieveToken();

            // ASSERT
            configurationMock.Verify();
            httpClientFactoryMock.Verify();
            Assert.IsNotNull(result);
            Assert.AreEqual(bearerToken.access_token, result);
        }
Exemplo n.º 2
0
        public void TestInitialize()
        {
            mockLogger = new Mock <ILogger>();

            mockAdalTokenProvider = new Mock <IAdalTokenProvider>();

            mockAdalTokenProviderFactory = new Mock <IAdalTokenProviderFactory>();
            mockAdalTokenProviderFactory
            .Setup(x => x.Get(It.IsAny <string>()))
            .Returns(mockAdalTokenProvider.Object);

            mockAuthUtil = new Mock <IAuthUtil>();
            mockAuthUtil
            .Setup(x => x.GetAadAuthorityUriAsync(It.IsAny <Uri>(), It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(testAuthority));

            bearerTokenProvider = new BearerTokenProvider(mockLogger.Object, mockAdalTokenProviderFactory.Object, mockAuthUtil.Object);
        }
Exemplo n.º 3
0
 public AuthenticatedMessageHandler(BearerTokenProvider getBearerToken) : this(new HttpClientHandler(), getBearerToken)
 {
 }
Exemplo n.º 4
0
 public AuthenticatedMessageHandler(HttpMessageHandler innerHandler, BearerTokenProvider getBearerToken)
 {
     InnerHandler    = innerHandler;
     mGetBearerToken = getBearerToken;
 }