Skip to content

Commit

Permalink
Fixed Multi-Tenanted middleware to accomodate Organizations APIs.
Browse files Browse the repository at this point in the history
  • Loading branch information
jezzsantos committed Apr 2, 2024
1 parent d86fcab commit 747d9f1
Show file tree
Hide file tree
Showing 8 changed files with 501 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public MultiTenancyFilterSpec()
}

[Fact]
public async Task WhenInvokeAndWrongRequestDelegateSignature_ThenContinuesPipeline()
public async Task WhenInvokeAndWrongRequestDelegateSignature_ThenNoRewriteAndContinuesPipeline()
{
var httpContext = new DefaultHttpContext
{
Expand All @@ -41,7 +41,7 @@ public async Task WhenInvokeAndWrongRequestDelegateSignature_ThenContinuesPipeli
}

[Fact]
public async Task WhenInvokeAndUnTenantedRequestInDelegateSignature_ThenContinuesPipeline()
public async Task WhenInvokeAndUnTenantedRequestInDelegateSignature_ThenNoRewriteAndContinuesPipeline()
{
var httpContext = new DefaultHttpContext
{
Expand All @@ -56,7 +56,7 @@ public async Task WhenInvokeAndUnTenantedRequestInDelegateSignature_ThenContinue
}

[Fact]
public async Task WhenInvokeAndNoTenantInTenancyContext_ThenContinuesPipeline()
public async Task WhenInvokeAndNoTenantInTenancyContext_ThenNoRewriteAndContinuesPipeline()
{
var httpContext = new DefaultHttpContext
{
Expand All @@ -72,7 +72,7 @@ public async Task WhenInvokeAndNoTenantInTenancyContext_ThenContinuesPipeline()
}

[Fact]
public async Task WhenInvokeAndEmptyTenantInTenancyContext_ThenContinuesPipeline()
public async Task WhenInvokeAndEmptyTenantInTenancyContext_ThenNoRewriteAndContinuesPipeline()
{
_tenancyContext.Setup(tc => tc.Current)
.Returns(string.Empty);
Expand All @@ -90,7 +90,8 @@ public async Task WhenInvokeAndEmptyTenantInTenancyContext_ThenContinuesPipeline
}

[Fact]
public async Task WhenInvokeAndTenantIdInRequestAlreadyPopulated_ThenContinuesPipeline()
public async Task
WhenInvokeForTenantedRequestAndTenantIdInRequestAlreadyPopulated_ThenNoRewriteAndContinuesPipeline()
{
_tenancyContext.Setup(tc => tc.Current)
.Returns("atenantid");
Expand All @@ -102,19 +103,19 @@ public async Task WhenInvokeAndTenantIdInRequestAlreadyPopulated_ThenContinuesPi
{
new { }, new TestTenantedRequest
{
OrganizationId = "anorganizationid"
OrganizationId = "anoldorganizationid"
}
};
var context = new DefaultEndpointFilterInvocationContext(httpContext, args);

await _filter.InvokeAsync(context, _next.Object);

_next.Verify(n => n.Invoke(context));
context.Arguments[1].As<TestTenantedRequest>().OrganizationId.Should().Be("anorganizationid");
context.Arguments[1].As<TestTenantedRequest>().OrganizationId.Should().Be("anoldorganizationid");
}

[Fact]
public async Task WhenInvokeAndTenantIdInRequestIsEmpty_ThenPopulatesAndContinuesPipeline()
public async Task WhenInvokeForTenantedRequestAndTenantIdInRequestIsEmpty_ThenRewritesAndContinuesPipeline()
{
_tenancyContext.Setup(tc => tc.Current)
.Returns("atenantid");
Expand All @@ -138,10 +139,41 @@ public async Task WhenInvokeAndTenantIdInRequestIsEmpty_ThenPopulatesAndContinue
context.Arguments[1].As<TestTenantedRequest>().OrganizationId.Should().Be("atenantid");
}

[Fact]
public async Task
WhenInvokeForUnTenantedOrganizationRequestAndTenantIdInRequestIsEmpty_ThenRewritesAndContinuesPipeline()
{
_tenancyContext.Setup(tc => tc.Current)
.Returns("atenantid");
var httpContext = new DefaultHttpContext
{
RequestServices = _serviceProvider.Object
};

var args = new object[]
{
new { }, new TestUnTenantedOrganizationRequest
{
Id = null
}
};
var context = new DefaultEndpointFilterInvocationContext(httpContext, args);

await _filter.InvokeAsync(context, _next.Object);

_next.Verify(n => n.Invoke(context));
context.Arguments[1].As<TestUnTenantedOrganizationRequest>().Id.Should().Be("atenantid");
}

private class TestUnTenantedRequest : IWebRequest<TestResponse>;

private class TestTenantedRequest : IWebRequest<TestResponse>, ITenantedRequest
{
public string? OrganizationId { get; set; }
}

private class TestUnTenantedOrganizationRequest : IWebRequest<TestResponse>, IUnTenantedOrganizationRequest
{
public string? Id { get; set; }
}
}
Loading

0 comments on commit 747d9f1

Please sign in to comment.