From 77f946fc2219d87c926f2c7d94ff5acee81dba32 Mon Sep 17 00:00:00 2001 From: Philip Gichuhi Date: Tue, 5 Nov 2024 09:11:28 +0200 Subject: [PATCH] Increase test coverage --- .../httpClient/KiotaClientFactoryTests.cs | 9 ++++ .../Middleware/AuthorizationHandlerTests.cs | 49 +++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/tests/http/httpClient/KiotaClientFactoryTests.cs b/tests/http/httpClient/KiotaClientFactoryTests.cs index 675acf0..9b28cbc 100644 --- a/tests/http/httpClient/KiotaClientFactoryTests.cs +++ b/tests/http/httpClient/KiotaClientFactoryTests.cs @@ -2,9 +2,11 @@ using System.Linq; using System.Net; using System.Net.Http; +using Microsoft.Kiota.Abstractions.Authentication; using Microsoft.Kiota.Http.HttpClientLibrary.Middleware; using Microsoft.Kiota.Http.HttpClientLibrary.Middleware.Options; using Microsoft.Kiota.Http.HttpClientLibrary.Tests.Mocks; +using Moq; using Xunit; namespace Microsoft.Kiota.Http.HttpClientLibrary.Tests @@ -138,5 +140,12 @@ public void CreateWithCustomMiddlewarePipelineReturnsHttpClient() var client = KiotaClientFactory.Create(handlers); Assert.IsType(client); } + + [Fact] + public void CreateWithAuthenticationProvider() + { + var client = KiotaClientFactory.Create(new BaseBearerTokenAuthenticationProvider(new Mock().Object)); + Assert.IsType(client); + } } } diff --git a/tests/http/httpClient/Middleware/AuthorizationHandlerTests.cs b/tests/http/httpClient/Middleware/AuthorizationHandlerTests.cs index 1082e9a..f8947f2 100644 --- a/tests/http/httpClient/Middleware/AuthorizationHandlerTests.cs +++ b/tests/http/httpClient/Middleware/AuthorizationHandlerTests.cs @@ -1,3 +1,4 @@ +using System.ComponentModel; using System.Net; using System.Net.Http; using System.Net.Http.Headers; @@ -54,6 +55,16 @@ public void Dispose() GC.SuppressFinalize(this); } + [Fact] + public void AuthorizationHandlerConstructor() + { + // Arrange + BaseBearerTokenAuthenticationProvider? authenticationProvider = null; + + // Assert + Assert.Throws(() => new AuthorizationHandler(authenticationProvider!)); + } + [Fact] public async Task AuthorizationHandlerShouldAddAuthHeaderIfNotPresent() { @@ -91,6 +102,24 @@ public async Task AuthorizationHandlerShouldNotAddAuthHeaderIfPresent() Assert.Equal($"Bearer existing", response.RequestMessage.Headers.GetValues("Authorization").First()); } + [Fact] + public async Task AuthorizationHandlerShouldNotAddAuthHeaderIfHostIsNotValid() + { + // Arrange + HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "https://example.com"); + + HttpResponseMessage httpResponse = new HttpResponseMessage(HttpStatusCode.OK); + + this._testHttpMessageHandler.SetHttpResponse(httpResponse);// set the mock response + + // Act + HttpResponseMessage response = await this._invoker.SendAsync(httpRequestMessage, new CancellationToken()); + + // Assert + Assert.NotNull(response.RequestMessage); + Assert.False(response.RequestMessage.Headers.Contains("Authorization")); + } + [Fact] public async Task AuthorizationHandlerShouldAttemptCAEClaimsChallenge() { @@ -113,5 +142,25 @@ public async Task AuthorizationHandlerShouldAttemptCAEClaimsChallenge() Assert.Equal($"Bearer {_expectedAccessTokenAfterCAE}", response.RequestMessage.Headers.GetValues("Authorization").First()); Assert.Equal("test", await response.RequestMessage.Content!.ReadAsStringAsync()); } + + [Fact] + public async Task AuthorizationHandlerShouldReturnInitialResponseIfClaimsHeaderIsEmpty() + { + // Arrange + HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "https://graph.microsoft.com"); + httpRequestMessage.Content = new ByteArrayContent(Encoding.UTF8.GetBytes("test")); + + HttpResponseMessage httpResponse = new HttpResponseMessage(HttpStatusCode.Unauthorized); + httpResponse.Headers.WwwAuthenticate.Add(new AuthenticationHeaderValue("Bearer", "authorization_uri=\"https://login.windows.net/common/oauth2/authorize\"")); + + this._testHttpMessageHandler.SetHttpResponse(httpResponse, new HttpResponseMessage(HttpStatusCode.OK));// set the mock response + + // Act + HttpResponseMessage response = await this._invoker.SendAsync(httpRequestMessage, new CancellationToken()); + + // Assert + Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode); + Assert.Equal("test", await response.RequestMessage!.Content!.ReadAsStringAsync()); + } } }