Skip to content

Commit 67ae1d9

Browse files
committed
Beta.2 version: OcelotBuilderExtensions tests
1 parent 312fde9 commit 67ae1d9

9 files changed

+121
-77
lines changed

.github/workflows/publish.yml

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,6 @@ jobs:
126126
contents: read
127127
packages: write
128128
steps:
129-
- name: Checkout repository
130-
uses: actions/checkout@v4
131-
132129
- name: .NET Version
133130
run: dotnet --version
134131
- name: Setup .NET
@@ -140,6 +137,21 @@ jobs:
140137
- name: .NET Info
141138
run: dotnet --info
142139

140+
- name: Checkout repository
141+
uses: actions/checkout@v4
142+
143+
- name: README.md
144+
run: |
145+
echo Original README.md
146+
echo ------------------------
147+
cat README.md
148+
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
151+
echo New content of README.md
152+
echo ------------------------
153+
cat README.md
154+
143155
- name: Restore dependencies
144156
run: dotnet restore ./Ocelot.Administration.IdentityServer4.sln
145157

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
![Ocelot Icon](/ocelot_icon.png "Ocelot Icon")
2-
![IdSer4 Logo](/identity-server-4_logo.png "Identity Server 4 Logo")
2+
![IdSer4 Logo](/idser4_logo.png "Identity Server 4 Logo")
33
# Ocelot.Administration.IdentityServer4
4-
[Ocelot](https://www.nuget.org/packages/Ocelot) [Administration](https://ocelot.readthedocs.io/en/latest/features/administration.html) feature with [IdentityServer4](https://www.nuget.org/packages/IdentityServer4) integration
4+
[Ocelot](https://www.nuget.org/packages/Ocelot) [Administration](https://ocelot.readthedocs.io/en/latest/features/administration.html) feature, powered by [IdentityServer4](https://www.nuget.org/packages/IdentityServer4) integration.
5+
- **Head repository:** [Ocelot | GitHub](https://github.com/ThreeMammals/Ocelot)
6+
- **Head package:** [Ocelot | NuGet](https://www.nuget.org/packages/Ocelot)
7+
- **Extension packages:** [ThreeMammals | NuGet](https://www.nuget.org/profiles/ThreeMammals)

src/Ocelot.Administration.IdentityServer4.csproj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@
99
<IncludeSymbols>True</IncludeSymbols>
1010
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
1111
<!--Package properties-->
12-
<Version>24.0.0-beta.1</Version>
12+
<Version>24.0.0-beta.2</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>
15-
<PackageReleaseNotes>Release notes: [Releases](https://github.com/ThreeMammals/Ocelot.Administration.IdentityServer4/releases)</PackageReleaseNotes>
16-
<PackageTags>API Gateway;.NET;IdentityServer4</PackageTags>
15+
<PackageReleaseNotes>https://github.com/ThreeMammals/Ocelot.Administration.IdentityServer4/releases</PackageReleaseNotes>
16+
<PackageTags>Gateway;.NET;Ocelot;IdentityServer4</PackageTags>
1717
<PackageIcon>ocelot_icon.png</PackageIcon>
1818
<PackageReadmeFile>README.md</PackageReadmeFile>
1919
<PackageLicenseFile>LICENSE</PackageLicenseFile>
20-
<PackageProjectUrl>https://github.com/ThreeMammals/Ocelot</PackageProjectUrl>
21-
<RepositoryUrl>https://github.com/ThreeMammals/Ocelot.Administration.IdentityServer4</RepositoryUrl>
20+
<PackageProjectUrl>https://ocelot.readthedocs.io/en/24.0/features/administration.html</PackageProjectUrl>
21+
<RepositoryUrl>https://github.com/ThreeMammals/Ocelot.Administration.IdentityServer4.git</RepositoryUrl>
2222
<Authors>Tom Pallister, Raman Maksimchuk</Authors>
2323
<Company>Three Mammals</Company>
2424
<Product>Ocelot Gateway</Product>

src/OcelotBuilderExtensions.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,6 @@ void ConfigureOptions(JwtBearerOptions options)
8989
#pragma warning disable IDE0079 // Remove unnecessary suppression
9090
#pragma warning disable SYSLIB0057 // X509Certificate2 and X509Certificate constructors for binary and file content are obsolete
9191
var cert = new X509Certificate2(identityServerConfiguration.CredentialsSigningCertificateLocation, identityServerConfiguration.CredentialsSigningCertificatePassword, X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable);
92-
#pragma warning restore SYSLIB0057
93-
#pragma warning restore IDE0079
9492
identityServerBuilder.AddSigningCredential(cert);
9593
}
9694
}

unit/Ocelot.Administration.IdentityServer4.UnitTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
<Using Include="Moq" />
3434
<Using Include="Xunit" />
3535
<Using Include="Ocelot.Testing" />
36+
<None Update="mycert.pfx" CopyToOutputDirectory="Always" />
3637
</ItemGroup>
3738

3839
</Project>

unit/OcelotAdministrationBuilderTests.cs

Lines changed: 0 additions & 65 deletions
This file was deleted.
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
using IdentityServer4.Models;
2+
using IdentityServer4.Stores;
3+
using Microsoft.AspNetCore.Authentication.JwtBearer;
4+
using Microsoft.AspNetCore.Hosting;
5+
using Microsoft.Extensions.Configuration;
6+
using Microsoft.Extensions.DependencyInjection;
7+
using Ocelot.DependencyInjection;
8+
using System.Reflection;
9+
10+
namespace Ocelot.Administration.IdentityServer4.UnitTests;
11+
12+
public class OcelotBuilderExtensionsTests : UnitTest
13+
{
14+
private readonly IServiceCollection _services;
15+
private readonly IConfiguration _configRoot;
16+
17+
public OcelotBuilderExtensionsTests()
18+
{
19+
_configRoot = new ConfigurationRoot([]);
20+
_services = new ServiceCollection();
21+
_services.AddSingleton(GetHostingEnvironment());
22+
_services.AddSingleton(_configRoot);
23+
}
24+
25+
private static IWebHostEnvironment GetHostingEnvironment()
26+
{
27+
var environment = new Mock<IWebHostEnvironment>();
28+
environment.Setup(e => e.ApplicationName)
29+
.Returns(typeof(OcelotBuilderExtensionsTests).GetTypeInfo().Assembly.GetName().Name!);
30+
return environment.Object;
31+
}
32+
33+
[Fact]
34+
public void AddAdministration_WithSecret_AdminPathIsRegistered()
35+
{
36+
// Arrange, Act
37+
var ocelotBuilder = _services.AddOcelot(_configRoot);
38+
ocelotBuilder.AddAdministration("/administration", "secret");
39+
var provider = _services.BuildServiceProvider(true);
40+
41+
// Assert
42+
AssertCorrectAdminPathIsRegistered(provider, "/administration");
43+
AssertKeysStoreIsRegistered(provider);
44+
}
45+
46+
[Fact]
47+
public void AddAdministration_WithSecretAndWithSigningCertificateEnvVars_AdminPathIsRegistered()
48+
{
49+
// Arrange
50+
Environment.SetEnvironmentVariable(IdentityServerConfigurationCreator.OCELOT_CERTIFICATE, "mycert.pfx");
51+
Environment.SetEnvironmentVariable(IdentityServerConfigurationCreator.OCELOT_CERTIFICATE_PASSWORD, "password");
52+
53+
// Act
54+
var ocelotBuilder = _services.AddOcelot(_configRoot);
55+
ocelotBuilder.AddAdministration("/administration", "password");
56+
var provider = _services.BuildServiceProvider(true);
57+
58+
// Assert
59+
AssertCorrectAdminPathIsRegistered(provider, "/administration");
60+
AssertKeysStoreIsRegistered(provider);
61+
}
62+
63+
[Fact]
64+
public void AddAdministration_WithIdentityServerOptions_AdminPathIsRegistered()
65+
{
66+
// Arrange
67+
static void options(JwtBearerOptions o)
68+
{
69+
}
70+
71+
// Act
72+
var ocelotBuilder = _services.AddOcelot(_configRoot);
73+
ocelotBuilder.AddAdministration("/administration", options);
74+
var provider = _services.BuildServiceProvider(true);
75+
76+
// Assert
77+
AssertCorrectAdminPathIsRegistered(provider, "/administration");
78+
}
79+
80+
private static void AssertCorrectAdminPathIsRegistered(ServiceProvider provider, string expected)
81+
{
82+
var path = provider.GetService<IAdministrationPath>();
83+
Assert.Equal(expected, path?.Path);
84+
}
85+
86+
private static void AssertKeysStoreIsRegistered(ServiceProvider provider)
87+
{
88+
// builder.Services.AddSingleton<IValidationKeysStore>(new InMemoryValidationKeysStore(new[] { keyInfo }));
89+
var store = provider.GetService<IValidationKeysStore>();
90+
Assert.NotNull(store);
91+
IEnumerable<SecurityKeyInfo> sKeys = store.GetValidationKeysAsync().Result;
92+
SecurityKeyInfo sk = sKeys.First();
93+
Assert.NotNull(sk);
94+
}
95+
}

unit/mycert.pfx

2.59 KB
Binary file not shown.

0 commit comments

Comments
 (0)