/* * Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) * See https://github.com/openiddict/openiddict-core for more information concerning * the license and the contributors participating to this project. */ using System; using System.Text; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Moq; using OpenIddict.Abstractions; using Xunit; namespace OpenIddict.Core.Tests { public class OpenIddictCoreExtensionsTests { [Fact] public void AddCore_ThrowsAnExceptionForNullBuilder() { // Arrange var builder = (OpenIddictBuilder) null; // Act and assert var exception = Assert.Throws(() => builder.AddCore()); Assert.Equal("builder", exception.ParamName); } [Fact] public void AddCore_ThrowsAnExceptionForNullConfiguration() { // Arrange var services = new ServiceCollection(); var builder = new OpenIddictBuilder(services); // Act and assert var exception = Assert.Throws(() => builder.AddCore(configuration: null)); Assert.Equal("configuration", exception.ParamName); } [Fact] public void AddCore_RegistersLoggingServices() { // Arrange var services = new ServiceCollection(); var builder = new OpenIddictBuilder(services); // Act builder.AddCore(); // Assert Assert.Contains(services, service => service.ServiceType == typeof(ILogger<>)); } [Fact] public void AddCore_RegistersOptionsServices() { // Arrange var services = new ServiceCollection(); var builder = new OpenIddictBuilder(services); // Act builder.AddCore(); // Assert Assert.Contains(services, service => service.ServiceType == typeof(IOptions<>)); } [Theory] [InlineData(typeof(OpenIddictApplicationManager<>))] [InlineData(typeof(OpenIddictAuthorizationManager<>))] [InlineData(typeof(OpenIddictScopeManager<>))] [InlineData(typeof(OpenIddictTokenManager<>))] public void AddCore_RegistersDefaultManagers(Type type) { // Arrange var services = new ServiceCollection(); var builder = new OpenIddictBuilder(services); // Act builder.AddCore(); // Assert Assert.Contains(services, service => service.ServiceType == type && service.ImplementationType == type); } [Theory] [InlineData(typeof(IOpenIddictApplicationStoreResolver), typeof(OpenIddictApplicationStoreResolver))] [InlineData(typeof(IOpenIddictAuthorizationStoreResolver), typeof(OpenIddictAuthorizationStoreResolver))] [InlineData(typeof(IOpenIddictScopeStoreResolver), typeof(OpenIddictScopeStoreResolver))] [InlineData(typeof(IOpenIddictTokenStoreResolver), typeof(OpenIddictTokenStoreResolver))] public void AddCore_RegistersDefaultResolvers(Type serviceType, Type implementationType) { // Arrange var services = new ServiceCollection(); var builder = new OpenIddictBuilder(services); // Act builder.AddCore(); // Assert Assert.Contains(services, service => service.ServiceType == serviceType && service.ImplementationType == implementationType); } [Theory] [InlineData(typeof(IOpenIddictApplicationManager))] [InlineData(typeof(IOpenIddictAuthorizationManager))] [InlineData(typeof(IOpenIddictScopeManager))] [InlineData(typeof(IOpenIddictTokenManager))] public void AddCore_RegistersUntypedProxies(Type type) { // Arrange var services = new ServiceCollection(); var builder = new OpenIddictBuilder(services); // Act builder.AddCore(); // Assert Assert.Contains(services, service => service.ServiceType == type && service.ImplementationFactory != null); } [Fact] public void AddCore_ResolvingUntypedApplicationManagerThrowsAnExceptionWhenDefaultEntityIsNotSet() { // Arrange var services = new ServiceCollection(); var builder = new OpenIddictBuilder(services); // Act builder.AddCore(); // Assert var provider = services.BuildServiceProvider(); var exception = Assert.Throws(delegate { return provider.GetRequiredService(); }); Assert.Equal(new StringBuilder() .Append("No default application entity type was configured in the OpenIddict core options, ") .AppendLine("which generally indicates that no application store was registered in the DI container.") .Append("To register the Entity Framework Core stores, reference the 'OpenIddict.EntityFrameworkCore' ") .Append("package and call 'services.AddOpenIddict().AddCore().UseEntityFrameworkCore()'.") .ToString(), exception.Message); } [Fact] public void AddCore_ResolvingUntypedAuthorizationManagerThrowsAnExceptionWhenDefaultEntityIsNotSet() { // Arrange var services = new ServiceCollection(); var builder = new OpenIddictBuilder(services); // Act builder.AddCore(); // Assert var provider = services.BuildServiceProvider(); var exception = Assert.Throws(delegate { return provider.GetRequiredService(); }); Assert.Equal(new StringBuilder() .Append("No default authorization entity type was configured in the OpenIddict core options, ") .AppendLine("which generally indicates that no authorization store was registered in the DI container.") .Append("To register the Entity Framework Core stores, reference the 'OpenIddict.EntityFrameworkCore' ") .Append("package and call 'services.AddOpenIddict().AddCore().UseEntityFrameworkCore()'.") .ToString(), exception.Message); } [Fact] public void AddCore_ResolvingUntypedScopeManagerThrowsAnExceptionWhenDefaultEntityIsNotSet() { // Arrange var services = new ServiceCollection(); var builder = new OpenIddictBuilder(services); // Act builder.AddCore(); // Assert var provider = services.BuildServiceProvider(); var exception = Assert.Throws(delegate { return provider.GetRequiredService(); }); Assert.Equal(new StringBuilder() .Append("No default scope entity type was configured in the OpenIddict core options, ") .AppendLine("which generally indicates that no scope store was registered in the DI container.") .Append("To register the Entity Framework Core stores, reference the 'OpenIddict.EntityFrameworkCore' ") .Append("package and call 'services.AddOpenIddict().AddCore().UseEntityFrameworkCore()'.") .ToString(), exception.Message); } [Fact] public void AddCore_ResolvingUntypedTokenManagerThrowsAnExceptionWhenDefaultEntityIsNotSet() { // Arrange var services = new ServiceCollection(); var builder = new OpenIddictBuilder(services); // Act builder.AddCore(); // Assert var provider = services.BuildServiceProvider(); var exception = Assert.Throws(delegate { return provider.GetRequiredService(); }); Assert.Equal(new StringBuilder() .Append("No default token entity type was configured in the OpenIddict core options, ") .AppendLine("which generally indicates that no token store was registered in the DI container.") .Append("To register the Entity Framework Core stores, reference the 'OpenIddict.EntityFrameworkCore' ") .Append("package and call 'services.AddOpenIddict().AddCore().UseEntityFrameworkCore()'.") .ToString(), exception.Message); } [Fact] public void AddCore_ResolvingUntypedApplicationManagerReturnsGenericManager() { // Arrange var services = new ServiceCollection(); var builder = new OpenIddictBuilder(services); // Act builder.AddCore(options => { options.SetDefaultApplicationEntity(); options.Services.AddSingleton(Mock.Of>()); }); var provider = services.BuildServiceProvider(); var manager = provider.GetRequiredService(); // Assert Assert.IsType>(manager); } [Fact] public void AddCore_ResolvingUntypedAuthorizationManagerReturnsGenericManager() { // Arrange var services = new ServiceCollection(); var builder = new OpenIddictBuilder(services); // Act builder.AddCore(options => { options.SetDefaultAuthorizationEntity(); options.Services.AddSingleton(Mock.Of>()); }); var provider = services.BuildServiceProvider(); var manager = provider.GetRequiredService(); // Assert Assert.IsType>(manager); } [Fact] public void AddCore_ResolvingUntypedScopeManagerReturnsGenericManager() { // Arrange var services = new ServiceCollection(); var builder = new OpenIddictBuilder(services); // Act builder.AddCore(options => { options.SetDefaultScopeEntity(); options.Services.AddSingleton(Mock.Of>()); }); var provider = services.BuildServiceProvider(); var manager = provider.GetRequiredService(); // Assert Assert.IsType>(manager); } [Fact] public void AddCore_ResolvingUntypedTokenManagerReturnsGenericManager() { // Arrange var services = new ServiceCollection(); var builder = new OpenIddictBuilder(services); // Act builder.AddCore(options => { options.SetDefaultTokenEntity(); options.Services.AddSingleton(Mock.Of>()); }); var provider = services.BuildServiceProvider(); var manager = provider.GetRequiredService(); // Assert Assert.IsType>(manager); } public class OpenIddictApplication { } public class OpenIddictAuthorization { } public class OpenIddictScope { } public class OpenIddictToken { } } }