Skip to content

Commit

Permalink
Fix flaky API tests (#924)
Browse files Browse the repository at this point in the history
  • Loading branch information
gunndabad authored Nov 20, 2023
1 parent 8d0b59a commit 0a8a8d1
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -58,12 +59,20 @@ protected override void ConfigureWebHost(IWebHostBuilder builder)
services.AddTestScoped<IDataverseAdapter>(tss => tss.DataverseAdapterMock.Object);
services.AddTestScoped<IGetAnIdentityApiClient>(tss => tss.GetAnIdentityApiClientMock.Object);
services.AddTestScoped<IOptions<AccessYourQualificationsOptions>>(tss => tss.AccessYourQualificationsOptions);
services.AddTestScoped<IOptions<GetAnIdentityOptions>>(tss => tss.GetAnIdentityOptions);
services.AddTestScoped<ICertificateGenerator>(tss => tss.CertificateGeneratorMock.Object);
services.AddSingleton<TestData>();
services.AddFakeXrm();
services.AddSingleton<FakeTrnGenerator>();

services.Configure<GetAnIdentityOptions>(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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -41,8 +40,6 @@ protected ApiTestBase(ApiFixture apiFixture)

public Mock<IGetAnIdentityApiClient> GetAnIdentityApiClientMock => _testServices.GetAnIdentityApiClientMock;

public IOptions<GetAnIdentityOptions> GetAnIdentityOptions => _testServices.GetAnIdentityOptions;

public HttpClient HttpClientWithApiKey { get; }

public TestData TestData => ApiFixture.Services.GetRequiredService<TestData>();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -13,6 +15,8 @@ public GetAnIdentityEndpointsTests(ApiFixture apiFixture)
{
}

private IOptions<GetAnIdentityOptions> GetAnIdentityOptions => ApiFixture.Services.GetRequiredService<IOptions<GetAnIdentityOptions>>();

[Fact]
public async Task Post_WithNoSignatureInHeader_ReturnsUnauthorised()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -53,8 +44,6 @@ public static TestScopedServices Reset()

public Mock<IGetAnIdentityApiClient> GetAnIdentityApiClientMock { get; }

public IOptions<GetAnIdentityOptions> GetAnIdentityOptions { get; }

public IOptions<AccessYourQualificationsOptions> AccessYourQualificationsOptions { get; }

}

0 comments on commit 0a8a8d1

Please sign in to comment.