|
| 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 | +} |
0 commit comments