Skip to content

Commit

Permalink
Code cleanup. Disabled nullable suppression warning
Browse files Browse the repository at this point in the history
  • Loading branch information
jezzsantos committed Feb 2, 2025
1 parent 4ff28d1 commit 7687b79
Show file tree
Hide file tree
Showing 13 changed files with 230 additions and 215 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ public async Task WhenDeliverAuditAsync_ThenDelivers()
[Fact]
public async Task WhenSearchAllDeliveredEmails_ThenReturnsEmails()
{
var datum = DateTime.UtcNow;
var audit = new Audit
{
Id = "anid",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ public EndUsersApplicationDomainEventHandlersSpec()
Task.FromResult<Result<EndUserRoot, Error>>(root));
var invitationRepository = new Mock<IInvitationRepository>();
_userProfilesService = new Mock<IUserProfilesService>();
var notificationsService = new Mock<IUserNotificationsService>();
_subscriptionsService = new Mock<ISubscriptionsService>();

_application =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
using System.Text;
using System.Text.Json;
using Application.Interfaces;
using Common;
using FluentAssertions;
using Infrastructure.Web.Api.Common.Clients;
using Infrastructure.Web.Interfaces;
using Moq;
using Xunit;

namespace Infrastructure.Web.Api.Common.UnitTests.Clients;

[Trait("Category", "Unit")]
public class InterHostServiceClientSpec
{
private readonly InterHostServiceClient _client;

public InterHostServiceClientSpec()
{
var httpClientFactory = new Mock<IHttpClientFactory>();
_client = new InterHostServiceClient(httpClientFactory.Object, JsonSerializerOptions.Default, "abaseurl",
"asecret");
}

[Fact]
public void WhenSetAuthorizationAndNoAuthorizationValue_ThenDoesNothing()
{
var message = new HttpRequestMessage();
var caller =
Mock.Of<ICallerContext>(cc => cc.Authorization == Optional<ICallerContext.CallerAuthorization>.None);

_client.SetAuthorization(message, caller, "asecret");

message.Headers.Should().BeEmpty();
}

[Fact]
public void WhenSetAuthorizationAndHMACAuthorization_ThenThrows()
{
var message = new HttpRequestMessage();
var caller = Mock.Of<ICallerContext>(cc =>
cc.Authorization
== new ICallerContext.CallerAuthorization(ICallerContext.AuthorizationMethod.HMAC, "avalue").ToOptional());

_client.Invoking(x => x.SetAuthorization(message, caller, "asecret"))
.Should().Throw<NotSupportedException>()
.WithMessage(Resources.RequestExtensions_HMACAuthorization_NotSupported);

message.Headers.Should().BeEmpty();
}

[Fact]
public void WhenSetAuthorizationAndPrivateInterHostAuthorizationAndCallerHasAuthorization_ThenAuthorizes()
{
var message = new HttpRequestMessage();
var caller = Mock.Of<ICallerContext>(cc =>
cc.Authorization
== new ICallerContext.CallerAuthorization(ICallerContext.AuthorizationMethod.PrivateInterHost, "avalue")
.ToOptional());

_client.SetAuthorization(message, caller, "asecret");

message.Headers.GetValues(HttpConstants.Headers.PrivateInterHostSignature).Should()
.OnlyContain(hdr => hdr == "sha256=f8dbae1fc1114a368a46f762db4a5ad5417e0e1ea4bc34d7924d166621c45653");
message.Headers.GetValues(HttpConstants.Headers.Authorization).Should()
.OnlyContain(hdr => hdr == "Bearer avalue");
}

[Fact]
public void WhenSetAuthorizationAndPrivateInterHostAuthorizationAndCallerHasNoAuthorization_ThenAuthorizes()
{
var message = new HttpRequestMessage();
var caller = Mock.Of<ICallerContext>(cc =>
cc.Authorization
== new ICallerContext.CallerAuthorization(ICallerContext.AuthorizationMethod.PrivateInterHost,
Optional<string>.None)
.ToOptional());

_client.SetAuthorization(message, caller, "asecret");

message.Headers.GetValues(HttpConstants.Headers.PrivateInterHostSignature).Should()
.OnlyContain(hdr => hdr == "sha256=f8dbae1fc1114a368a46f762db4a5ad5417e0e1ea4bc34d7924d166621c45653");
message.Headers.Contains(HttpConstants.Headers.Authorization).Should().BeFalse();
}

[Fact]
public void WhenSetAuthorizationAndTokenAuthorization_ThenAuthorizes()
{
var message = new HttpRequestMessage();
var caller = Mock.Of<ICallerContext>(cc =>
cc.Authorization
== new ICallerContext.CallerAuthorization(ICallerContext.AuthorizationMethod.PrivateInterHost, "avalue")
.ToOptional());

_client.SetAuthorization(message, caller, "asecret");

message.Headers.GetValues(HttpConstants.Headers.Authorization).Should()
.OnlyContain(hdr => hdr == "Bearer avalue");
}

[Fact]
public void WhenSetAuthorizationAndApiKeyAuthorization_ThenAuthorizes()
{
var message = new HttpRequestMessage();
var caller = Mock.Of<ICallerContext>(cc =>
cc.Authorization
== new ICallerContext.CallerAuthorization(ICallerContext.AuthorizationMethod.APIKey, "avalue")
.ToOptional());

_client.SetAuthorization(message, caller, "asecret");

var base64Credential = Convert.ToBase64String(Encoding.UTF8.GetBytes("avalue:"));
message.Headers.GetValues(HttpConstants.Headers.Authorization).Should()
.OnlyContain(hdr => hdr == $"Basic {base64Credential}");
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
using System.Text;
using Application.Interfaces;
using Common;
using Common.Extensions;
using FluentAssertions;
using Infrastructure.Web.Api.Common.Extensions;
using Infrastructure.Web.Api.Interfaces;
using Infrastructure.Web.Interfaces;
using Moq;
using Xunit;

// ReSharper disable UnusedMember.Local
Expand Down Expand Up @@ -385,98 +381,6 @@ public void WhenGetRouteTemplatePlaceholdersAndRouteTemplateHasPlaceholdersForPo
result[nameof(HasPlaceholdersPostRequest.AStringProperty2)].Should().Be(typeof(string));
}

[Fact]
public void WhenSetAuthorizationAndNoAuthorizationValue_ThenDoesNothing()
{
var message = new HttpRequestMessage();
var caller =
Mock.Of<ICallerContext>(cc => cc.Authorization == Optional<ICallerContext.CallerAuthorization>.None);

message.SetAuthorization(caller, "asecret");

message.Headers.Should().BeEmpty();
}

[Fact]
public void WhenSetAuthorizationAndHMACAuthorization_ThenThrows()
{
var message = new HttpRequestMessage();
var caller = Mock.Of<ICallerContext>(cc =>
cc.Authorization
== new ICallerContext.CallerAuthorization(ICallerContext.AuthorizationMethod.HMAC, "avalue").ToOptional());

message.Invoking(x => x.SetAuthorization(caller, "asecret"))
.Should().Throw<NotSupportedException>()
.WithMessage(Resources.RequestExtensions_HMACAuthorizationNotSupported);

message.Headers.Should().BeEmpty();
}

[Fact]
public void WhenSetAuthorizationAndPrivateInterHostAuthorizationAndCallerHasAuthorization_ThenAuthorizes()
{
var message = new HttpRequestMessage();
var caller = Mock.Of<ICallerContext>(cc =>
cc.Authorization
== new ICallerContext.CallerAuthorization(ICallerContext.AuthorizationMethod.PrivateInterHost, "avalue")
.ToOptional());

message.SetAuthorization(caller, "asecret");

message.Headers.GetValues(HttpConstants.Headers.PrivateInterHostSignature).Should()
.OnlyContain(hdr => hdr == "sha256=f8dbae1fc1114a368a46f762db4a5ad5417e0e1ea4bc34d7924d166621c45653");
message.Headers.GetValues(HttpConstants.Headers.Authorization).Should()
.OnlyContain(hdr => hdr == "Bearer avalue");
}

[Fact]
public void WhenSetAuthorizationAndPrivateInterHostAuthorizationAndCallerHasNoAuthorization_ThenAuthorizes()
{
var message = new HttpRequestMessage();
var caller = Mock.Of<ICallerContext>(cc =>
cc.Authorization
== new ICallerContext.CallerAuthorization(ICallerContext.AuthorizationMethod.PrivateInterHost,
Optional<string>.None)
.ToOptional());

message.SetAuthorization(caller, "asecret");

message.Headers.GetValues(HttpConstants.Headers.PrivateInterHostSignature).Should()
.OnlyContain(hdr => hdr == "sha256=f8dbae1fc1114a368a46f762db4a5ad5417e0e1ea4bc34d7924d166621c45653");
message.Headers.Contains(HttpConstants.Headers.Authorization).Should().BeFalse();
}

[Fact]
public void WhenSetAuthorizationAndTokenAuthorization_ThenAuthorizes()
{
var message = new HttpRequestMessage();
var caller = Mock.Of<ICallerContext>(cc =>
cc.Authorization
== new ICallerContext.CallerAuthorization(ICallerContext.AuthorizationMethod.PrivateInterHost, "avalue")
.ToOptional());

message.SetAuthorization(caller, "asecret");

message.Headers.GetValues(HttpConstants.Headers.Authorization).Should()
.OnlyContain(hdr => hdr == "Bearer avalue");
}

[Fact]
public void WhenSetAuthorizationAndApiKeyAuthorization_ThenAuthorizes()
{
var message = new HttpRequestMessage();
var caller = Mock.Of<ICallerContext>(cc =>
cc.Authorization
== new ICallerContext.CallerAuthorization(ICallerContext.AuthorizationMethod.APIKey, "avalue")
.ToOptional());

message.SetAuthorization(caller, "asecret");

var base64Credential = Convert.ToBase64String(Encoding.UTF8.GetBytes("avalue:"));
message.Headers.GetValues(HttpConstants.Headers.Authorization).Should()
.OnlyContain(hdr => hdr == $"Basic {base64Credential}");
}

private class NoRouteRequest : IWebRequest<TestResponse>;

[Route("/aroute/{unknown}", OperationMethod.Get)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,75 @@ private static void AddCorrelationId(HttpRequestMessage message, ICallerContext?
}
}

private static void AddCallerAuthorization(HttpRequestMessage message, ICallerContext? context,
private void AddCallerAuthorization(HttpRequestMessage message, ICallerContext? context,
string privateInterHostSecret)
{
if (context.Exists())
{
message.SetAuthorization(context, privateInterHostSecret);
SetAuthorization(message, context, privateInterHostSecret);
}
}

internal void SetAuthorization(HttpRequestMessage message, ICallerContext caller,
string privateInterHostSecret)
{
var authorization = caller.Authorization;
if (!authorization.HasValue)
{
return;
}

var authorizationValue = authorization is { HasValue: true, Value.Value.HasValue: true }
? authorization.Value.Value.Value
: null;

switch (authorization.Value.Method)
{
case ICallerContext.AuthorizationMethod.Token:
{
if (authorizationValue.HasValue())
{
var token = authorization.Value.Value.Value;
message.SetJWTBearerToken(token);
}

break;
}

case ICallerContext.AuthorizationMethod.APIKey:
{
if (authorizationValue.HasValue())
{
var apiKey = authorization.Value.Value.Value;
message.SetAPIKey(apiKey);
}

break;
}

case ICallerContext.AuthorizationMethod.PrivateInterHost:
{
if (authorizationValue.HasValue())
{
var token = authorization.Value.Value.Value;
message.SetPrivateInterHostAuth(privateInterHostSecret, token);
}
else
{
message.SetPrivateInterHostAuth(privateInterHostSecret);
}

break;
}

case ICallerContext.AuthorizationMethod.HMAC:
{
//We don't expect this client to be used to forward maintenance service workloads
throw new NotSupportedException(Resources.RequestExtensions_HMACAuthorization_NotSupported);
}

default:
throw new ArgumentOutOfRangeException();
}
}
}
Loading

0 comments on commit 7687b79

Please sign in to comment.