From 922f76ab3b70f95922c1677948c7c48a909154e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Chalet?= Date: Sun, 3 Jun 2018 12:17:09 +0200 Subject: [PATCH] Introduce non-generic SetDefaultApplicationEntity/SetDefaultAuthorizationEntity/SetDefaultScopeEntity/SetDefaultTokenEntity --- src/OpenIddict.Core/OpenIddictCoreBuilder.cs | 100 +++++++-- .../OpenIddictCoreBuilderTests.cs | 189 +++++++++++++++++- 2 files changed, 270 insertions(+), 19 deletions(-) diff --git a/src/OpenIddict.Core/OpenIddictCoreBuilder.cs b/src/OpenIddict.Core/OpenIddictCoreBuilder.cs index 59a75f57..35f582de 100644 --- a/src/OpenIddict.Core/OpenIddictCoreBuilder.cs +++ b/src/OpenIddict.Core/OpenIddictCoreBuilder.cs @@ -651,32 +651,108 @@ namespace Microsoft.Extensions.DependencyInjection /// Configures OpenIddict to use the specified entity as the default application entity. /// /// The . - public OpenIddictCoreBuilder SetDefaultApplicationEntity() - where TApplication : class, new() - => Configure(options => options.DefaultApplicationType = typeof(TApplication)); + public OpenIddictCoreBuilder SetDefaultApplicationEntity() where TApplication : class + => SetDefaultApplicationEntity(typeof(TApplication)); + + /// + /// Configures OpenIddict to use the specified entity as the default application entity. + /// + /// The application entity type. + /// The . + public OpenIddictCoreBuilder SetDefaultApplicationEntity([NotNull] Type type) + { + if (type == null) + { + throw new ArgumentNullException(nameof(type)); + } + + if (type.IsValueType) + { + throw new ArgumentException("The specified type is invalid.", nameof(type)); + } + + return Configure(options => options.DefaultApplicationType = type); + } /// /// Configures OpenIddict to use the specified entity as the default authorization entity. /// /// The . - public OpenIddictCoreBuilder SetDefaultAuthorizationEntity() - where TAuthorization : class, new() - => Configure(options => options.DefaultAuthorizationType = typeof(TAuthorization)); + public OpenIddictCoreBuilder SetDefaultAuthorizationEntity() where TAuthorization : class + => SetDefaultAuthorizationEntity(typeof(TAuthorization)); + + /// + /// Configures OpenIddict to use the specified entity as the default authorization entity. + /// + /// The authorization entity type. + /// The . + public OpenIddictCoreBuilder SetDefaultAuthorizationEntity([NotNull] Type type) + { + if (type == null) + { + throw new ArgumentNullException(nameof(type)); + } + + if (type.IsValueType) + { + throw new ArgumentException("The specified type is invalid.", nameof(type)); + } + + return Configure(options => options.DefaultAuthorizationType = type); + } + + /// + /// Configures OpenIddict to use the specified entity as the default scope entity. + /// + /// The . + public OpenIddictCoreBuilder SetDefaultScopeEntity() where TScope : class + => SetDefaultScopeEntity(typeof(TScope)); /// /// Configures OpenIddict to use the specified entity as the default scope entity. /// + /// The scope entity type. + /// The . + public OpenIddictCoreBuilder SetDefaultScopeEntity([NotNull] Type type) + { + if (type == null) + { + throw new ArgumentNullException(nameof(type)); + } + + if (type.IsValueType) + { + throw new ArgumentException("The specified type is invalid.", nameof(type)); + } + + return Configure(options => options.DefaultScopeType = type); + } + + /// + /// Configures OpenIddict to use the specified entity as the default token entity. + /// /// The . - public OpenIddictCoreBuilder SetDefaultScopeEntity() - where TScope : class, new() - => Configure(options => options.DefaultScopeType = typeof(TScope)); + public OpenIddictCoreBuilder SetDefaultTokenEntity() where TToken : class + => SetDefaultTokenEntity(typeof(TToken)); /// /// Configures OpenIddict to use the specified entity as the default token entity. /// + /// The token entity type. /// The . - public OpenIddictCoreBuilder SetDefaultTokenEntity() - where TToken : class, new() - => Configure(options => options.DefaultTokenType = typeof(TToken)); + public OpenIddictCoreBuilder SetDefaultTokenEntity([NotNull] Type type) + { + if (type == null) + { + throw new ArgumentNullException(nameof(type)); + } + + if (type.IsValueType) + { + throw new ArgumentException("The specified type is invalid.", nameof(type)); + } + + return Configure(options => options.DefaultTokenType = type); + } } } \ No newline at end of file diff --git a/test/OpenIddict.Core.Tests/OpenIddictCoreBuilderTests.cs b/test/OpenIddict.Core.Tests/OpenIddictCoreBuilderTests.cs index 73d7cce6..5efa4b09 100644 --- a/test/OpenIddict.Core.Tests/OpenIddictCoreBuilderTests.cs +++ b/test/OpenIddict.Core.Tests/OpenIddictCoreBuilderTests.cs @@ -356,27 +356,202 @@ namespace OpenIddict.Core.Tests } [Fact] - public void RegisterDefaultModels_CustomEntitiesAreCorrectlySet() + public void SetDefaultApplicationEntity_ThrowsAnExceptionForNullType() + { + // Arrange + var services = CreateServices(); + var builder = CreateBuilder(services); + + // Act and assert + var exception = Assert.Throws(delegate + { + return builder.SetDefaultApplicationEntity(type: null); + }); + + Assert.Equal("type", exception.ParamName); + } + + [Fact] + public void SetDefaultApplicationEntity_ThrowsAnExceptionForInvalidType() + { + // Arrange + var services = CreateServices(); + var builder = CreateBuilder(services); + + // Act and assert + var exception = Assert.Throws(delegate + { + return builder.SetDefaultApplicationEntity(typeof(long)); + }); + + Assert.Equal("type", exception.ParamName); + Assert.StartsWith("The specified type is invalid.", exception.Message); + } + + [Fact] + public void SetDefaultApplicationEntity_EntityIsCorrectlySet() { // Arrange var services = CreateServices(); var builder = CreateBuilder(services); // Act - services.AddOpenIddict() - .AddCore() - .SetDefaultApplicationEntity() - .SetDefaultAuthorizationEntity() - .SetDefaultScopeEntity() - .SetDefaultTokenEntity(); + builder.SetDefaultApplicationEntity(); // Assert var provider = services.BuildServiceProvider(); var options = provider.GetRequiredService>().CurrentValue; Assert.Equal(typeof(CustomApplication), options.DefaultApplicationType); + } + + [Fact] + public void SetDefaultAuthorizationEntity_ThrowsAnExceptionForNullType() + { + // Arrange + var services = CreateServices(); + var builder = CreateBuilder(services); + + // Act and assert + var exception = Assert.Throws(delegate + { + return builder.SetDefaultAuthorizationEntity(type: null); + }); + + Assert.Equal("type", exception.ParamName); + } + + [Fact] + public void SetDefaultAuthorizationEntity_ThrowsAnExceptionForInvalidType() + { + // Arrange + var services = CreateServices(); + var builder = CreateBuilder(services); + + // Act and assert + var exception = Assert.Throws(delegate + { + return builder.SetDefaultAuthorizationEntity(typeof(long)); + }); + + Assert.Equal("type", exception.ParamName); + Assert.StartsWith("The specified type is invalid.", exception.Message); + } + + [Fact] + public void SetDefaultAuthorizationEntity_EntityIsCorrectlySet() + { + // Arrange + var services = CreateServices(); + var builder = CreateBuilder(services); + + // Act + builder.SetDefaultAuthorizationEntity(); + + // Assert + var provider = services.BuildServiceProvider(); + var options = provider.GetRequiredService>().CurrentValue; + Assert.Equal(typeof(CustomAuthorization), options.DefaultAuthorizationType); + } + + [Fact] + public void SetDefaultScopeEntity_ThrowsAnExceptionForNullType() + { + // Arrange + var services = CreateServices(); + var builder = CreateBuilder(services); + + // Act and assert + var exception = Assert.Throws(delegate + { + return builder.SetDefaultScopeEntity(type: null); + }); + + Assert.Equal("type", exception.ParamName); + } + + [Fact] + public void SetDefaultScopeEntity_ThrowsAnExceptionForInvalidType() + { + // Arrange + var services = CreateServices(); + var builder = CreateBuilder(services); + + // Act and assert + var exception = Assert.Throws(delegate + { + return builder.SetDefaultScopeEntity(typeof(long)); + }); + + Assert.Equal("type", exception.ParamName); + Assert.StartsWith("The specified type is invalid.", exception.Message); + } + + [Fact] + public void SetDefaultScopeEntity_EntityIsCorrectlySet() + { + // Arrange + var services = CreateServices(); + var builder = CreateBuilder(services); + + // Act + builder.SetDefaultScopeEntity(); + + // Assert + var provider = services.BuildServiceProvider(); + var options = provider.GetRequiredService>().CurrentValue; + Assert.Equal(typeof(CustomScope), options.DefaultScopeType); + } + + [Fact] + public void SetDefaultTokenEntity_ThrowsAnExceptionForNullType() + { + // Arrange + var services = CreateServices(); + var builder = CreateBuilder(services); + + // Act and assert + var exception = Assert.Throws(delegate + { + return builder.SetDefaultTokenEntity(type: null); + }); + + Assert.Equal("type", exception.ParamName); + } + + [Fact] + public void SetDefaultTokenEntity_ThrowsAnExceptionForInvalidType() + { + // Arrange + var services = CreateServices(); + var builder = CreateBuilder(services); + + // Act and assert + var exception = Assert.Throws(delegate + { + return builder.SetDefaultTokenEntity(typeof(long)); + }); + + Assert.Equal("type", exception.ParamName); + Assert.StartsWith("The specified type is invalid.", exception.Message); + } + + [Fact] + public void SetDefaultTokenEntity_EntityIsCorrectlySet() + { + // Arrange + var services = CreateServices(); + var builder = CreateBuilder(services); + + // Act + builder.SetDefaultTokenEntity(); + + // Assert + var provider = services.BuildServiceProvider(); + var options = provider.GetRequiredService>().CurrentValue; + Assert.Equal(typeof(CustomToken), options.DefaultTokenType); }