Skip to content

Commit 59af7df

Browse files
committed
Beta.3 version: Coverage by unit tests.
Line coverage is 93%, and branch coverage is 100%
1 parent 67ae1d9 commit 59af7df

File tree

5 files changed

+107
-36
lines changed

5 files changed

+107
-36
lines changed

.github/workflows/publish.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,8 @@ jobs:
146146
echo ------------------------
147147
cat README.md
148148
echo ------------------------
149-
# Replace "AAA" with "BBB" and "CCC" with "DDD" in a UTF-8 encoded file
150-
sed -i 's|/ocelot_icon.png|https://raw.githubusercontent.com/ThreeMammals/Ocelot.Administration.IdentityServer4/refs/heads/main/ocelot_icon.png|g; s|/idser4_logo.png|https://raw.githubusercontent.com/ThreeMammals/Ocelot.Administration.IdentityServer4/refs/heads/main/identity-server-4_logo.png|g' README.md
149+
# Replaces relative GitHub paths with NuGet-friendly absolute URLs
150+
sed -i 's|/ocelot_icon.png|https://raw.githubusercontent.com/ThreeMammals/Ocelot.Administration.IdentityServer4/refs/heads/main/ocelot_icon.png|g; s|/idser4_logo.png|https://raw.githubusercontent.com/ThreeMammals/Ocelot.Administration.IdentityServer4/refs/heads/main/idser4_logo.png|g' README.md
151151
echo New content of README.md
152152
echo ------------------------
153153
cat README.md
Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,41 @@
11
using Microsoft.AspNetCore.Builder;
2+
using Microsoft.AspNetCore.Routing;
23
using Microsoft.Extensions.DependencyInjection;
3-
using Ocelot.Configuration.Repository;
44

55
namespace Ocelot.Administration.IdentityServer4;
66

77
public static class IdentityServerMiddlewareConfigurationProvider
88
{
9-
public static OcelotMiddlewareConfigurationDelegate Get { get; } = Getter;
9+
public static OcelotMiddlewareConfigurationDelegate Get { get; } = ConfigureMiddleware;
1010

11-
private static Task Getter(IApplicationBuilder builder)
11+
private static Task ConfigureMiddleware(IApplicationBuilder builder)
1212
{
13-
var repo = builder.ApplicationServices.GetService<IInternalConfigurationRepository>();
14-
var config = repo?.Get();
15-
16-
if (!string.IsNullOrEmpty(config?.Data.AdministrationPath))
13+
var adminPath = builder.ApplicationServices.GetService<IAdministrationPath>();
14+
if (!string.IsNullOrEmpty(adminPath?.Path))
1715
{
18-
builder.Map(config.Data.AdministrationPath, app =>
19-
{
20-
//todo - hack so we know that we are using internal identity server
21-
var identityServerConfiguration = builder.ApplicationServices.GetService<IIdentityServerConfiguration>();
22-
23-
if (identityServerConfiguration != null)
24-
{
25-
app.UseIdentityServer();
26-
}
16+
builder.Map(adminPath.Path, UseIdentityServerMiddleware);
17+
}
18+
return Task.CompletedTask;
19+
}
2720

28-
app.UseAuthentication();
29-
app.UseRouting();
30-
app.UseAuthorization();
31-
app.UseEndpoints(endpoints =>
32-
{
33-
endpoints.MapDefaultControllerRoute();
34-
endpoints.MapControllers();
35-
});
36-
});
21+
private static void UseIdentityServerMiddleware(IApplicationBuilder app)
22+
{
23+
// TODO hack so we know that we are using internal identity server
24+
var identityServerConfiguration = app.ApplicationServices.GetService<IIdentityServerConfiguration>();
25+
if (identityServerConfiguration != null)
26+
{
27+
app.UseIdentityServer();
3728
}
3829

39-
return Task.CompletedTask;
30+
app.UseAuthentication();
31+
app.UseRouting();
32+
app.UseAuthorization();
33+
app.UseEndpoints(ConfigureEndpoints);
34+
}
35+
36+
private static void ConfigureEndpoints(IEndpointRouteBuilder endpoints)
37+
{
38+
endpoints.MapDefaultControllerRoute();
39+
endpoints.MapControllers();
4040
}
4141
}

src/Ocelot.Administration.IdentityServer4.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<IncludeSymbols>True</IncludeSymbols>
1010
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
1111
<!--Package properties-->
12-
<Version>24.0.0-beta.2</Version>
12+
<Version>24.0.0-beta.3</Version>
1313
<PackageId>Ocelot.Administration.IdentityServer4</PackageId>
1414
<PackageDescription>Provides Ocelot extensions to use the Administration API and IdentityServer4 dependencies that come with it</PackageDescription>
1515
<PackageReleaseNotes>https://github.com/ThreeMammals/Ocelot.Administration.IdentityServer4/releases</PackageReleaseNotes>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using Microsoft.AspNetCore.Builder;
2+
using Microsoft.Extensions.Configuration;
3+
using Microsoft.Extensions.DependencyInjection;
4+
using Ocelot.Configuration.File;
5+
using Ocelot.DependencyInjection;
6+
using Ocelot.Middleware;
7+
using System.Runtime.CompilerServices;
8+
9+
namespace Ocelot.Administration.IdentityServer4.UnitTests;
10+
11+
public class IdentityServerMiddlewareConfigurationProviderTests : UnitTest
12+
{
13+
[Fact]
14+
public async Task ConfigureMiddleware_HappyPath()
15+
{
16+
// Arrange
17+
var config = new FileConfiguration();
18+
var builder = WebApplication.CreateBuilder();
19+
builder.Configuration
20+
.SetBasePath(builder.Environment.ContentRootPath)
21+
.AddOcelot(config, builder.Environment, MergeOcelotJson.ToMemory);
22+
builder.Services
23+
.AddOcelot(builder.Configuration)
24+
.AddAdministration(AdminPath(), TestID);
25+
using var app = builder.Build();
26+
27+
// Act 1
28+
// Ocelot core invokes OcelotMiddlewareConfigurationDelegate within UseOcelot() method of the OcelotMiddlewareExtensions class.
29+
// We must call UseOcelot to cover all lines of the attached helper-delegates.
30+
// More: https://github.com/ThreeMammals/Ocelot/blob/24.0.0/src/Ocelot/Middleware/OcelotMiddlewareExtensions.cs#L101
31+
await app.UseOcelot();
32+
33+
// Act 2
34+
// Directly call the delegate
35+
var task = IdentityServerMiddlewareConfigurationProvider.Get.Invoke(app);
36+
37+
// Assert
38+
Assert.NotNull(task);
39+
Assert.True(task.IsCompleted);
40+
}
41+
42+
[Fact]
43+
public void ConfigureMiddleware_NoAdminPath()
44+
{
45+
// Arrange
46+
var mocking = new Mock<IApplicationBuilder>();
47+
var services = new ServiceCollection();
48+
var provider = services.BuildServiceProvider();
49+
mocking.SetupGet(x => x.ApplicationServices).Returns(provider);
50+
var builder = mocking.Object;
51+
52+
// Act
53+
var task = IdentityServerMiddlewareConfigurationProvider.Get.Invoke(builder);
54+
55+
// Assert
56+
Assert.NotNull(task);
57+
Assert.True(task.IsCompleted);
58+
}
59+
60+
private string AdminPath([CallerMemberName] string? testName = null)
61+
=> $"/{nameof(IdentityServerMiddlewareConfigurationProviderTests)}/{testName ?? TestID}";
62+
}

unit/OcelotBuilderExtensionsTests.cs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Microsoft.Extensions.DependencyInjection;
77
using Ocelot.DependencyInjection;
88
using System.Reflection;
9+
using System.Runtime.CompilerServices;
910

1011
namespace Ocelot.Administration.IdentityServer4.UnitTests;
1112

@@ -33,50 +34,58 @@ private static IWebHostEnvironment GetHostingEnvironment()
3334
[Fact]
3435
public void AddAdministration_WithSecret_AdminPathIsRegistered()
3536
{
36-
// Arrange, Act
37+
// Arrange
38+
var adminPath = AdminPath();
39+
40+
// Act
3741
var ocelotBuilder = _services.AddOcelot(_configRoot);
38-
ocelotBuilder.AddAdministration("/administration", "secret");
42+
ocelotBuilder.AddAdministration(adminPath, TestID);
3943
var provider = _services.BuildServiceProvider(true);
4044

4145
// Assert
42-
AssertCorrectAdminPathIsRegistered(provider, "/administration");
46+
AssertCorrectAdminPathIsRegistered(provider, adminPath);
4347
AssertKeysStoreIsRegistered(provider);
4448
}
4549

4650
[Fact]
4751
public void AddAdministration_WithSecretAndWithSigningCertificateEnvVars_AdminPathIsRegistered()
4852
{
4953
// Arrange
54+
var adminPath = AdminPath();
5055
Environment.SetEnvironmentVariable(IdentityServerConfigurationCreator.OCELOT_CERTIFICATE, "mycert.pfx");
5156
Environment.SetEnvironmentVariable(IdentityServerConfigurationCreator.OCELOT_CERTIFICATE_PASSWORD, "password");
5257

5358
// Act
5459
var ocelotBuilder = _services.AddOcelot(_configRoot);
55-
ocelotBuilder.AddAdministration("/administration", "password");
60+
ocelotBuilder.AddAdministration(adminPath, "password");
5661
var provider = _services.BuildServiceProvider(true);
5762

5863
// Assert
59-
AssertCorrectAdminPathIsRegistered(provider, "/administration");
64+
AssertCorrectAdminPathIsRegistered(provider, adminPath);
6065
AssertKeysStoreIsRegistered(provider);
6166
}
6267

6368
[Fact]
6469
public void AddAdministration_WithIdentityServerOptions_AdminPathIsRegistered()
6570
{
6671
// Arrange
72+
var adminPath = AdminPath();
6773
static void options(JwtBearerOptions o)
6874
{
6975
}
7076

7177
// Act
7278
var ocelotBuilder = _services.AddOcelot(_configRoot);
73-
ocelotBuilder.AddAdministration("/administration", options);
79+
ocelotBuilder.AddAdministration(adminPath, options);
7480
var provider = _services.BuildServiceProvider(true);
7581

7682
// Assert
77-
AssertCorrectAdminPathIsRegistered(provider, "/administration");
83+
AssertCorrectAdminPathIsRegistered(provider, adminPath);
7884
}
7985

86+
private string AdminPath([CallerMemberName] string? testName = null)
87+
=> $"/{nameof(OcelotBuilderExtensionsTests)}/{testName ?? TestID}";
88+
8089
private static void AssertCorrectAdminPathIsRegistered(ServiceProvider provider, string expected)
8190
{
8291
var path = provider.GetService<IAdministrationPath>();

0 commit comments

Comments
 (0)