From 0a8a8d19b588e71c04f5dc28e3517bf7961d165a Mon Sep 17 00:00:00 2001 From: James Gunn Date: Mon, 20 Nov 2023 13:37:04 +0000 Subject: [PATCH] Fix flaky API tests (#924) --- .../Services/GetAnIdentity/GetAnIdentityOptions.cs | 8 ++++---- .../TeachingRecordSystem.Api.Tests/ApiFixture.cs | 11 ++++++++++- .../TeachingRecordSystem.Api.Tests/ApiTestBase.cs | 3 --- .../IdentityWebHooks/GetAnIdentityEndpointsTests.cs | 4 ++++ .../TestScopedServices.cs | 11 ----------- 5 files changed, 18 insertions(+), 19 deletions(-) diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.Core/Services/GetAnIdentity/GetAnIdentityOptions.cs b/TeachingRecordSystem/src/TeachingRecordSystem.Core/Services/GetAnIdentity/GetAnIdentityOptions.cs index be3362691..2cdf1aa86 100644 --- a/TeachingRecordSystem/src/TeachingRecordSystem.Core/Services/GetAnIdentity/GetAnIdentityOptions.cs +++ b/TeachingRecordSystem/src/TeachingRecordSystem.Core/Services/GetAnIdentity/GetAnIdentityOptions.cs @@ -8,14 +8,14 @@ public class GetAnIdentityOptions public required string TokenEndpoint { get; set; } [Required] - public required string ClientId { get; init; } + public required string ClientId { get; set; } [Required] - public required string ClientSecret { get; init; } + public required string ClientSecret { get; set; } [Required] - public required string BaseAddress { get; init; } + public required string BaseAddress { get; set; } [Required] - public required string WebHookClientSecret { get; init; } + public required string WebHookClientSecret { get; set; } } diff --git a/TeachingRecordSystem/tests/TeachingRecordSystem.Api.Tests/ApiFixture.cs b/TeachingRecordSystem/tests/TeachingRecordSystem.Api.Tests/ApiFixture.cs index 36ec2a3e2..ee0acfd38 100644 --- a/TeachingRecordSystem/tests/TeachingRecordSystem.Api.Tests/ApiFixture.cs +++ b/TeachingRecordSystem/tests/TeachingRecordSystem.Api.Tests/ApiFixture.cs @@ -23,6 +23,7 @@ public ApiFixture(IConfiguration configuration) { _configuration = configuration; JwtSigningCredentials = new SigningCredentials(new RsaSecurityKey(RSA.Create()), SecurityAlgorithms.RsaSha256); + _ = Services; // Start the host } public HttpClientInterceptorOptions EvidenceFilesHttpClientInterceptorOptions { get; } = new(); @@ -58,12 +59,20 @@ protected override void ConfigureWebHost(IWebHostBuilder builder) services.AddTestScoped(tss => tss.DataverseAdapterMock.Object); services.AddTestScoped(tss => tss.GetAnIdentityApiClientMock.Object); services.AddTestScoped>(tss => tss.AccessYourQualificationsOptions); - services.AddTestScoped>(tss => tss.GetAnIdentityOptions); services.AddTestScoped(tss => tss.CertificateGeneratorMock.Object); services.AddSingleton(); services.AddFakeXrm(); services.AddSingleton(); + services.Configure(options => + { + options.TokenEndpoint = "dummy"; + options.ClientId = "dummy"; + options.ClientSecret = "dummy"; + options.BaseAddress = "dummy"; + options.WebHookClientSecret = "dummy"; + }); + services.AddHttpClient("EvidenceFiles") .AddHttpMessageHandler(_ => EvidenceFilesHttpClientInterceptorOptions.CreateHttpMessageHandler()) .ConfigurePrimaryHttpMessageHandler(_ => new NotFoundHandler()); diff --git a/TeachingRecordSystem/tests/TeachingRecordSystem.Api.Tests/ApiTestBase.cs b/TeachingRecordSystem/tests/TeachingRecordSystem.Api.Tests/ApiTestBase.cs index 92c348f63..1837963db 100644 --- a/TeachingRecordSystem/tests/TeachingRecordSystem.Api.Tests/ApiTestBase.cs +++ b/TeachingRecordSystem/tests/TeachingRecordSystem.Api.Tests/ApiTestBase.cs @@ -2,7 +2,6 @@ using System.Security.Claims; using FakeXrmEasy.Abstractions; using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.Options; using Microsoft.IdentityModel.Tokens; using TeachingRecordSystem.Api.Infrastructure.Json; using TeachingRecordSystem.Api.Tests.Infrastructure.Security; @@ -41,8 +40,6 @@ protected ApiTestBase(ApiFixture apiFixture) public Mock GetAnIdentityApiClientMock => _testServices.GetAnIdentityApiClientMock; - public IOptions GetAnIdentityOptions => _testServices.GetAnIdentityOptions; - public HttpClient HttpClientWithApiKey { get; } public TestData TestData => ApiFixture.Services.GetRequiredService(); diff --git a/TeachingRecordSystem/tests/TeachingRecordSystem.Api.Tests/Endpoints/IdentityWebHooks/GetAnIdentityEndpointsTests.cs b/TeachingRecordSystem/tests/TeachingRecordSystem.Api.Tests/Endpoints/IdentityWebHooks/GetAnIdentityEndpointsTests.cs index 51d226cd9..8b2e7bf47 100644 --- a/TeachingRecordSystem/tests/TeachingRecordSystem.Api.Tests/Endpoints/IdentityWebHooks/GetAnIdentityEndpointsTests.cs +++ b/TeachingRecordSystem/tests/TeachingRecordSystem.Api.Tests/Endpoints/IdentityWebHooks/GetAnIdentityEndpointsTests.cs @@ -1,8 +1,10 @@ using System.Security.Cryptography; using System.Text; using System.Text.Json; +using Microsoft.Extensions.Options; using TeachingRecordSystem.Api.Endpoints.IdentityWebHooks; using TeachingRecordSystem.Api.Endpoints.IdentityWebHooks.Messages; +using TeachingRecordSystem.Core.Services.GetAnIdentityApi; namespace TeachingRecordSystem.Api.Tests.Endpoints.IdentityWebHooks; @@ -13,6 +15,8 @@ public GetAnIdentityEndpointsTests(ApiFixture apiFixture) { } + private IOptions GetAnIdentityOptions => ApiFixture.Services.GetRequiredService>(); + [Fact] public async Task Post_WithNoSignatureInHeader_ReturnsUnauthorised() { diff --git a/TeachingRecordSystem/tests/TeachingRecordSystem.Api.Tests/TestScopedServices.cs b/TeachingRecordSystem/tests/TeachingRecordSystem.Api.Tests/TestScopedServices.cs index 69caa51d1..1683aaf4b 100644 --- a/TeachingRecordSystem/tests/TeachingRecordSystem.Api.Tests/TestScopedServices.cs +++ b/TeachingRecordSystem/tests/TeachingRecordSystem.Api.Tests/TestScopedServices.cs @@ -17,15 +17,6 @@ public TestScopedServices() DataverseAdapterMock = new(); GetAnIdentityApiClientMock = new(); - GetAnIdentityOptions = Options.Create(new GetAnIdentityOptions() - { - TokenEndpoint = "dummy", - ClientId = "dummy", - ClientSecret = "dummy", - BaseAddress = "dummy", - WebHookClientSecret = "dummy" - }); - AccessYourQualificationsOptions = Options.Create(new AccessYourQualificationsOptions() { BaseAddress = "https://aytq.com", @@ -53,8 +44,6 @@ public static TestScopedServices Reset() public Mock GetAnIdentityApiClientMock { get; } - public IOptions GetAnIdentityOptions { get; } - public IOptions AccessYourQualificationsOptions { get; } }