Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion API.IntegrationTests/AccountTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public async Task CreateAccount_ShouldAdd_NewUserToDatabase()
username = "Bob",
password = "SecurePassword123#",
email = "bob@example.com",
turnstileresponse = "lmao"
turnstileresponse = "valid-token"
});


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using System.Net;
using System.Text.Json;
using System.Web;

namespace OpenShock.API.IntegrationTests.HttpMessageHandlers;

sealed class InterceptedHttpMessageHandler : DelegatingHandler
{
private async Task<HttpResponseMessage> HandleCloudflareTurnstileRequest(HttpRequestMessage request, CancellationToken cancellationToken)
{
var formData = request.Content != null ? await request.Content.ReadAsStringAsync(cancellationToken) : string.Empty;
var parsedForm = HttpUtility.ParseQueryString(formData);
var responseToken = parsedForm["response"];

var responseDto = responseToken switch
{
"valid-token" => new CloudflareTurnstileVerifyResponseDto
{
Success = true,
ErrorCodes = [],
ChallengeTs = DateTime.UtcNow,
Hostname = "validhost",
Action = "validaction",
Cdata = ""
},
"invalid-token" => new CloudflareTurnstileVerifyResponseDto
{
Success = false,
ErrorCodes = ["invalid-input-response"],
ChallengeTs = DateTime.UtcNow,
Hostname = "invalidhost",
Action = "invalidaction",
Cdata = ""
},
_ => new CloudflareTurnstileVerifyResponseDto
{
Success = false,
ErrorCodes = ["bad-request"],
ChallengeTs = DateTime.UtcNow,
Hostname = "unknownhost",
Action = "unknownaction",
Cdata = ""
}
};

var responseJson = JsonSerializer.Serialize(responseDto);

var responseMessage = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(responseJson, System.Text.Encoding.UTF8, "application/json")
};

return responseMessage;
}

private async Task<HttpResponseMessage> HandleMailJetApiHost(HttpRequestMessage request, CancellationToken cancellationToken)

Check warning on line 56 in API.IntegrationTests/HttpMessageHandlers/InterceptedHttpMessageHandler.cs

View workflow job for this annotation

GitHub Actions / build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 56 in API.IntegrationTests/HttpMessageHandlers/InterceptedHttpMessageHandler.cs

View workflow job for this annotation

GitHub Actions / build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
return new HttpResponseMessage(HttpStatusCode.NotFound);
}

protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
return request.RequestUri switch
{
{ Host: "challenges.cloudflare.com", AbsolutePath: "/turnstile/v0/siteverify" } => await HandleCloudflareTurnstileRequest(request, cancellationToken),
{ Host: "api.mailjet.com" } => await HandleMailJetApiHost(request, cancellationToken),
_ => new HttpResponseMessage(HttpStatusCode.NotFound)
};
}

private class CloudflareTurnstileVerifyResponseDto
{
public bool Success { get; set; }
public string[] ErrorCodes { get; set; }

Check warning on line 74 in API.IntegrationTests/HttpMessageHandlers/InterceptedHttpMessageHandler.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'ErrorCodes' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 74 in API.IntegrationTests/HttpMessageHandlers/InterceptedHttpMessageHandler.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'ErrorCodes' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
public DateTime ChallengeTs { get; set; }
public string Hostname { get; set; }

Check warning on line 76 in API.IntegrationTests/HttpMessageHandlers/InterceptedHttpMessageHandler.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Hostname' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
public string Action { get; set; }

Check warning on line 77 in API.IntegrationTests/HttpMessageHandlers/InterceptedHttpMessageHandler.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Action' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
public string Cdata { get; set; }

Check warning on line 78 in API.IntegrationTests/HttpMessageHandlers/InterceptedHttpMessageHandler.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Cdata' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Microsoft.Extensions.Http;

namespace OpenShock.API.IntegrationTests.HttpMessageHandlers;

sealed class InterceptedHttpMessageHandlerBuilder : HttpMessageHandlerBuilder
{
public override string? Name { get; set; }
public override HttpMessageHandler PrimaryHandler { get; set; }

Check warning on line 8 in API.IntegrationTests/HttpMessageHandlers/InterceptedHttpMessageHandlerBuilder.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'PrimaryHandler' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 8 in API.IntegrationTests/HttpMessageHandlers/InterceptedHttpMessageHandlerBuilder.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'PrimaryHandler' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
public override IList<DelegatingHandler> AdditionalHandlers => [];


public override HttpMessageHandler Build()
{
return new InterceptedHttpMessageHandler();
}
}
7 changes: 6 additions & 1 deletion API.IntegrationTests/IntegrationTestWebAppFactory.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Http;
using OpenShock.API.IntegrationTests.HttpMessageHandlers;
using OpenShock.Common.Services.Turnstile;
using Testcontainers.PostgreSql;
using Testcontainers.Redis;
using TUnit.Core.Interfaces;
Expand Down Expand Up @@ -78,7 +83,7 @@ protected override void ConfigureWebHost(IWebHostBuilder builder)

builder.ConfigureTestServices(services =>
{
// We can replace services here
services.AddTransient<HttpMessageHandlerBuilder, InterceptedHttpMessageHandlerBuilder>();
});
}

Expand Down
Loading