44 changed files with 2970 additions and 101 deletions
@ -0,0 +1,26 @@ |
|||
<Project Sdk="Microsoft.NET.Sdk"> |
|||
|
|||
<Import Project="..\..\build\common.props" /> |
|||
|
|||
<PropertyGroup> |
|||
<TargetFrameworks>netcoreapp2.0;net461</TargetFrameworks> |
|||
<TargetFrameworks Condition=" '$(OS)' != 'Windows_NT' ">netcoreapp2.0</TargetFrameworks> |
|||
</PropertyGroup> |
|||
|
|||
<ItemGroup> |
|||
<ProjectReference Include="..\..\src\OpenIddict.Abstractions\OpenIddict.Abstractions.csproj" /> |
|||
</ItemGroup> |
|||
|
|||
<ItemGroup> |
|||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="$(AspNetCoreVersion)" /> |
|||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(TestSdkVersion)" /> |
|||
<PackageReference Include="Moq" Version="$(MoqVersion)" /> |
|||
<PackageReference Include="xunit" Version="$(XunitVersion)" /> |
|||
<PackageReference Include="xunit.runner.visualstudio" Version="$(XunitVersion)" /> |
|||
</ItemGroup> |
|||
|
|||
<ItemGroup> |
|||
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" /> |
|||
</ItemGroup> |
|||
|
|||
</Project> |
|||
@ -0,0 +1,27 @@ |
|||
/* |
|||
* 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 Microsoft.Extensions.DependencyInjection; |
|||
using Xunit; |
|||
|
|||
namespace OpenIddict.Abstractions.Tests |
|||
{ |
|||
public class OpenIddictBuilderTests |
|||
{ |
|||
[Fact] |
|||
public void Constructor_ThrowsAnExceptionForNullServices() |
|||
{ |
|||
// Arrange
|
|||
var services = (IServiceCollection) null; |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<ArgumentNullException>(() => new OpenIddictBuilder(services)); |
|||
|
|||
Assert.Equal("services", exception.ParamName); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,39 @@ |
|||
/* |
|||
* 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 Microsoft.Extensions.DependencyInjection; |
|||
using Xunit; |
|||
|
|||
namespace OpenIddict.Abstractions.Tests |
|||
{ |
|||
public class OpenIddictExtensionsTests |
|||
{ |
|||
[Fact] |
|||
public void AddOpenIddict_ThrowsAnExceptionForNullServices() |
|||
{ |
|||
// Arrange
|
|||
var services = (IServiceCollection) null; |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<ArgumentNullException>(() => services.AddOpenIddict()); |
|||
|
|||
Assert.Equal("services", exception.ParamName); |
|||
} |
|||
|
|||
[Fact] |
|||
public void AddOpenIddict_ThrowsAnExceptionForNullConfigurationDelegate() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<ArgumentNullException>(() => services.AddOpenIddict(configuration: null)); |
|||
|
|||
Assert.Equal("configuration", exception.ParamName); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,321 @@ |
|||
/* |
|||
* 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<ArgumentNullException>(() => 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<ArgumentNullException>(() => 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<InvalidOperationException>(delegate |
|||
{ |
|||
return provider.GetRequiredService<IOpenIddictApplicationManager>(); |
|||
}); |
|||
|
|||
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<InvalidOperationException>(delegate |
|||
{ |
|||
return provider.GetRequiredService<IOpenIddictAuthorizationManager>(); |
|||
}); |
|||
|
|||
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<InvalidOperationException>(delegate |
|||
{ |
|||
return provider.GetRequiredService<IOpenIddictScopeManager>(); |
|||
}); |
|||
|
|||
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<InvalidOperationException>(delegate |
|||
{ |
|||
return provider.GetRequiredService<IOpenIddictTokenManager>(); |
|||
}); |
|||
|
|||
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<OpenIddictApplication>(); |
|||
options.Services.AddSingleton(Mock.Of<IOpenIddictApplicationStore<OpenIddictApplication>>()); |
|||
}); |
|||
|
|||
var provider = services.BuildServiceProvider(); |
|||
var manager = provider.GetRequiredService<IOpenIddictApplicationManager>(); |
|||
|
|||
// Assert
|
|||
Assert.IsType<OpenIddictApplicationManager<OpenIddictApplication>>(manager); |
|||
} |
|||
|
|||
[Fact] |
|||
public void AddCore_ResolvingUntypedAuthorizationManagerReturnsGenericManager() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
var builder = new OpenIddictBuilder(services); |
|||
|
|||
// Act
|
|||
builder.AddCore(options => |
|||
{ |
|||
options.SetDefaultAuthorizationEntity<OpenIddictAuthorization>(); |
|||
options.Services.AddSingleton(Mock.Of<IOpenIddictAuthorizationStore<OpenIddictAuthorization>>()); |
|||
}); |
|||
|
|||
var provider = services.BuildServiceProvider(); |
|||
var manager = provider.GetRequiredService<IOpenIddictAuthorizationManager>(); |
|||
|
|||
// Assert
|
|||
Assert.IsType<OpenIddictAuthorizationManager<OpenIddictAuthorization>>(manager); |
|||
} |
|||
|
|||
[Fact] |
|||
public void AddCore_ResolvingUntypedScopeManagerReturnsGenericManager() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
var builder = new OpenIddictBuilder(services); |
|||
|
|||
// Act
|
|||
builder.AddCore(options => |
|||
{ |
|||
options.SetDefaultScopeEntity<OpenIddictScope>(); |
|||
options.Services.AddSingleton(Mock.Of<IOpenIddictScopeStore<OpenIddictScope>>()); |
|||
}); |
|||
|
|||
var provider = services.BuildServiceProvider(); |
|||
var manager = provider.GetRequiredService<IOpenIddictScopeManager>(); |
|||
|
|||
// Assert
|
|||
Assert.IsType<OpenIddictScopeManager<OpenIddictScope>>(manager); |
|||
} |
|||
|
|||
[Fact] |
|||
public void AddCore_ResolvingUntypedTokenManagerReturnsGenericManager() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
var builder = new OpenIddictBuilder(services); |
|||
|
|||
// Act
|
|||
builder.AddCore(options => |
|||
{ |
|||
options.SetDefaultTokenEntity<OpenIddictToken>(); |
|||
options.Services.AddSingleton(Mock.Of<IOpenIddictTokenStore<OpenIddictToken>>()); |
|||
}); |
|||
|
|||
var provider = services.BuildServiceProvider(); |
|||
var manager = provider.GetRequiredService<IOpenIddictTokenManager>(); |
|||
|
|||
// Assert
|
|||
Assert.IsType<OpenIddictTokenManager<OpenIddictToken>>(manager); |
|||
} |
|||
|
|||
public class OpenIddictApplication { } |
|||
public class OpenIddictAuthorization { } |
|||
public class OpenIddictScope { } |
|||
public class OpenIddictToken { } |
|||
} |
|||
} |
|||
@ -0,0 +1,54 @@ |
|||
/* |
|||
* 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 Moq; |
|||
using OpenIddict.Abstractions; |
|||
using Xunit; |
|||
|
|||
namespace OpenIddict.Core.Tests |
|||
{ |
|||
public class OpenIddictApplicationStoreResolverTests |
|||
{ |
|||
[Fact] |
|||
public void Get_ThrowsAnExceptionWhenStoreCannotBeFound() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictApplicationStoreResolver(provider); |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<InvalidOperationException>(() => resolver.Get<OpenIddictApplication>()); |
|||
|
|||
Assert.Equal(new StringBuilder() |
|||
.AppendLine("No application store has been registered in the dependency injection container.") |
|||
.Append("To register the Entity Framework Core stores, reference the 'OpenIddict.EntityFrameworkCore' ") |
|||
.AppendLine("package and call 'services.AddOpenIddict().AddCore().UseEntityFrameworkCore()'.") |
|||
.Append("To register a custom store, create an implementation of 'IOpenIddictApplicationStore' and ") |
|||
.Append("use 'services.AddOpenIddict().AddCore().AddApplicationStore()' to add it to the DI container.") |
|||
.ToString(), exception.Message); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Get_ReturnsCustomStoreCorrespondingToTheSpecifiedType() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
services.AddSingleton(Mock.Of<IOpenIddictApplicationStore<OpenIddictApplication>>()); |
|||
|
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictApplicationStoreResolver(provider); |
|||
|
|||
// Act and assert
|
|||
Assert.NotNull(resolver.Get<OpenIddictApplication>()); |
|||
} |
|||
|
|||
public class OpenIddictApplication { } |
|||
} |
|||
} |
|||
@ -0,0 +1,54 @@ |
|||
/* |
|||
* 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 Moq; |
|||
using OpenIddict.Abstractions; |
|||
using Xunit; |
|||
|
|||
namespace OpenIddict.Core.Tests |
|||
{ |
|||
public class OpenIddictAuthorizationStoreResolverTests |
|||
{ |
|||
[Fact] |
|||
public void Get_ThrowsAnExceptionWhenStoreCannotBeFound() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictAuthorizationStoreResolver(provider); |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<InvalidOperationException>(() => resolver.Get<OpenIddictAuthorization>()); |
|||
|
|||
Assert.Equal(new StringBuilder() |
|||
.AppendLine("No authorization store has been registered in the dependency injection container.") |
|||
.Append("To register the Entity Framework Core stores, reference the 'OpenIddict.EntityFrameworkCore' ") |
|||
.AppendLine("package and call 'services.AddOpenIddict().AddCore().UseEntityFrameworkCore()'.") |
|||
.Append("To register a custom store, create an implementation of 'IOpenIddictAuthorizationStore' and ") |
|||
.Append("use 'services.AddOpenIddict().AddCore().AddAuthorizationStore()' to add it to the DI container.") |
|||
.ToString(), exception.Message); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Get_ReturnsCustomStoreCorrespondingToTheSpecifiedType() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
services.AddSingleton(Mock.Of<IOpenIddictAuthorizationStore<OpenIddictAuthorization>>()); |
|||
|
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictAuthorizationStoreResolver(provider); |
|||
|
|||
// Act and assert
|
|||
Assert.NotNull(resolver.Get<OpenIddictAuthorization>()); |
|||
} |
|||
|
|||
public class OpenIddictAuthorization { } |
|||
} |
|||
} |
|||
@ -0,0 +1,54 @@ |
|||
/* |
|||
* 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 Moq; |
|||
using OpenIddict.Abstractions; |
|||
using Xunit; |
|||
|
|||
namespace OpenIddict.Core.Tests |
|||
{ |
|||
public class OpenIddictScopeStoreResolverTests |
|||
{ |
|||
[Fact] |
|||
public void Get_ThrowsAnExceptionWhenStoreCannotBeFound() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictScopeStoreResolver(provider); |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<InvalidOperationException>(() => resolver.Get<OpenIddictScope>()); |
|||
|
|||
Assert.Equal(new StringBuilder() |
|||
.AppendLine("No scope store has been registered in the dependency injection container.") |
|||
.Append("To register the Entity Framework Core stores, reference the 'OpenIddict.EntityFrameworkCore' ") |
|||
.AppendLine("package and call 'services.AddOpenIddict().AddCore().UseEntityFrameworkCore()'.") |
|||
.Append("To register a custom store, create an implementation of 'IOpenIddictScopeStore' and ") |
|||
.Append("use 'services.AddOpenIddict().AddCore().AddScopeStore()' to add it to the DI container.") |
|||
.ToString(), exception.Message); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Get_ReturnsCustomStoreCorrespondingToTheSpecifiedType() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
services.AddSingleton(Mock.Of<IOpenIddictScopeStore<OpenIddictScope>>()); |
|||
|
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictScopeStoreResolver(provider); |
|||
|
|||
// Act and assert
|
|||
Assert.NotNull(resolver.Get<OpenIddictScope>()); |
|||
} |
|||
|
|||
public class OpenIddictScope { } |
|||
} |
|||
} |
|||
@ -0,0 +1,54 @@ |
|||
/* |
|||
* 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 Moq; |
|||
using OpenIddict.Abstractions; |
|||
using Xunit; |
|||
|
|||
namespace OpenIddict.Core.Tests |
|||
{ |
|||
public class OpenIddictTokenStoreResolverTests |
|||
{ |
|||
[Fact] |
|||
public void Get_ThrowsAnExceptionWhenStoreCannotBeFound() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictTokenStoreResolver(provider); |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<InvalidOperationException>(() => resolver.Get<OpenIddictToken>()); |
|||
|
|||
Assert.Equal(new StringBuilder() |
|||
.AppendLine("No token store has been registered in the dependency injection container.") |
|||
.Append("To register the Entity Framework Core stores, reference the 'OpenIddict.EntityFrameworkCore' ") |
|||
.AppendLine("package and call 'services.AddOpenIddict().AddCore().UseEntityFrameworkCore()'.") |
|||
.Append("To register a custom store, create an implementation of 'IOpenIddictTokenStore' and ") |
|||
.Append("use 'services.AddOpenIddict().AddCore().AddTokenStore()' to add it to the DI container.") |
|||
.ToString(), exception.Message); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Get_ReturnsCustomStoreCorrespondingToTheSpecifiedType() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
services.AddSingleton(Mock.Of<IOpenIddictTokenStore<OpenIddictToken>>()); |
|||
|
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictTokenStoreResolver(provider); |
|||
|
|||
// Act and assert
|
|||
Assert.NotNull(resolver.Get<OpenIddictToken>()); |
|||
} |
|||
|
|||
public class OpenIddictToken { } |
|||
} |
|||
} |
|||
@ -0,0 +1,141 @@ |
|||
/* |
|||
* 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.Data.Entity; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Options; |
|||
using OpenIddict.Core; |
|||
using OpenIddict.EntityFramework.Models; |
|||
using Xunit; |
|||
|
|||
namespace OpenIddict.EntityFramework.Tests |
|||
{ |
|||
public class OpenIddictEntityFrameworkBuilderTests |
|||
{ |
|||
[Fact] |
|||
public void Constructor_ThrowsAnExceptionForNullServices() |
|||
{ |
|||
// Arrange
|
|||
var services = (IServiceCollection) null; |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<ArgumentNullException>(() => new OpenIddictEntityFrameworkBuilder(services)); |
|||
|
|||
Assert.Equal("services", exception.ParamName); |
|||
} |
|||
|
|||
[Fact] |
|||
public void ReplaceDefaultEntities_EntitiesAreCorrectlyReplaced() |
|||
{ |
|||
// Arrange
|
|||
var services = CreateServices(); |
|||
var builder = CreateBuilder(services); |
|||
|
|||
// Act
|
|||
builder.ReplaceDefaultEntities<CustomApplication, CustomAuthorization, CustomScope, CustomToken, long>(); |
|||
|
|||
// Assert
|
|||
var provider = services.BuildServiceProvider(); |
|||
var options = provider.GetRequiredService<IOptionsMonitor<OpenIddictCoreOptions>>().CurrentValue; |
|||
|
|||
Assert.Equal(typeof(CustomApplication), options.DefaultApplicationType); |
|||
Assert.Equal(typeof(CustomAuthorization), options.DefaultAuthorizationType); |
|||
Assert.Equal(typeof(CustomScope), options.DefaultScopeType); |
|||
Assert.Equal(typeof(CustomToken), options.DefaultTokenType); |
|||
} |
|||
|
|||
[Fact] |
|||
public void UseDbContext_ThrowsAnExceptionForNullType() |
|||
{ |
|||
// Arrange
|
|||
var services = CreateServices(); |
|||
var builder = CreateBuilder(services); |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<ArgumentNullException>(delegate |
|||
{ |
|||
return builder.UseDbContext(type: null); |
|||
}); |
|||
|
|||
Assert.Equal("type", exception.ParamName); |
|||
} |
|||
|
|||
[Fact] |
|||
public void UseDbContext_ThrowsAnExceptionForInvalidType() |
|||
{ |
|||
// Arrange
|
|||
var services = CreateServices(); |
|||
var builder = CreateBuilder(services); |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<ArgumentException>(delegate |
|||
{ |
|||
return builder.UseDbContext(typeof(object)); |
|||
}); |
|||
|
|||
Assert.Equal("type", exception.ParamName); |
|||
Assert.StartsWith("The specified type is invalid.", exception.Message); |
|||
} |
|||
|
|||
[Fact] |
|||
public void UseDbContext_RegistersDbContextAsScopedService() |
|||
{ |
|||
// Arrange
|
|||
var services = CreateServices(); |
|||
var builder = CreateBuilder(services); |
|||
|
|||
// Act
|
|||
builder.UseDbContext<CustomDbContext>(); |
|||
|
|||
// Assert
|
|||
Assert.Contains(services, service => service.Lifetime == ServiceLifetime.Scoped && |
|||
service.ServiceType == typeof(CustomDbContext) && |
|||
service.ImplementationType == typeof(CustomDbContext)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void UseDbContext_SetsDbContextTypeInOptions() |
|||
{ |
|||
// Arrange
|
|||
var services = CreateServices(); |
|||
var builder = CreateBuilder(services); |
|||
|
|||
// Act
|
|||
builder.UseDbContext<CustomDbContext>(); |
|||
|
|||
// Assert
|
|||
var provider = services.BuildServiceProvider(); |
|||
var options = provider.GetRequiredService<IOptionsMonitor<OpenIddictEntityFrameworkOptions>>().CurrentValue; |
|||
|
|||
Assert.Equal(typeof(CustomDbContext), options.DbContextType); |
|||
} |
|||
|
|||
private static OpenIddictEntityFrameworkBuilder CreateBuilder(IServiceCollection services) |
|||
=> services.AddOpenIddict().AddCore().UseEntityFramework(); |
|||
|
|||
private static IServiceCollection CreateServices() |
|||
{ |
|||
var services = new ServiceCollection(); |
|||
services.AddOptions(); |
|||
|
|||
return services; |
|||
} |
|||
|
|||
public class CustomApplication : OpenIddictApplication<long, CustomAuthorization, CustomToken> { } |
|||
public class CustomAuthorization : OpenIddictAuthorization<long, CustomApplication, CustomToken> { } |
|||
public class CustomScope : OpenIddictScope<long> { } |
|||
public class CustomToken : OpenIddictToken<long, CustomApplication, CustomAuthorization> { } |
|||
|
|||
public class CustomDbContext : DbContext |
|||
{ |
|||
public CustomDbContext(string nameOrConnectionString) |
|||
: base(nameOrConnectionString) |
|||
{ |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,117 @@ |
|||
/* |
|||
* 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.Data.Entity; |
|||
using System.Text; |
|||
using Microsoft.Extensions.Caching.Memory; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Options; |
|||
using Moq; |
|||
using OpenIddict.Abstractions; |
|||
using OpenIddict.EntityFramework.Models; |
|||
using Xunit; |
|||
|
|||
namespace OpenIddict.EntityFramework.Tests |
|||
{ |
|||
public class OpenIddictApplicationStoreResolverTests |
|||
{ |
|||
[Fact] |
|||
public void Get_ReturnsCustomStoreCorrespondingToTheSpecifiedTypeWhenAvailable() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
services.AddSingleton(Mock.Of<IOpenIddictApplicationStore<CustomApplication>>()); |
|||
|
|||
var monitor = Mock.Of<IOptionsMonitor<OpenIddictEntityFrameworkOptions>>(); |
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictApplicationStoreResolver(monitor, provider); |
|||
|
|||
// Act and assert
|
|||
Assert.NotNull(resolver.Get<CustomApplication>()); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Get_ThrowsAnExceptionForInvalidEntityType() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
|
|||
var monitor = Mock.Of<IOptionsMonitor<OpenIddictEntityFrameworkOptions>>(); |
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictApplicationStoreResolver(monitor, provider); |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<InvalidOperationException>(() => resolver.Get<CustomApplication>()); |
|||
|
|||
Assert.Equal(new StringBuilder() |
|||
.AppendLine("The specified application type is not compatible with the Entity Framework 6.x stores.") |
|||
.Append("When enabling the Entity Framework 6.x stores, make sure you use the built-in ") |
|||
.Append("'OpenIddictApplication' entity (from the 'OpenIddict.EntityFramework.Models' package) ") |
|||
.Append("or a custom entity that inherits from the generic 'OpenIddictApplication' entity.") |
|||
.ToString(), exception.Message); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Get_ThrowsAnExceptionWhenDbContextTypeIsNotAvailable() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
|
|||
var monitor = Mock.Of<IOptionsMonitor<OpenIddictEntityFrameworkOptions>>( |
|||
mock => mock.CurrentValue == new OpenIddictEntityFrameworkOptions |
|||
{ |
|||
DbContextType = null |
|||
}); |
|||
|
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictApplicationStoreResolver(monitor, provider); |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<InvalidOperationException>(() => resolver.Get<OpenIddictApplication>()); |
|||
|
|||
Assert.Equal(new StringBuilder() |
|||
.AppendLine("No Entity Framework 6.x context was specified in the OpenIddict options.") |
|||
.Append("To configure the OpenIddict Entity Framework 6.x stores to use a specific 'DbContext', ") |
|||
.Append("use 'options.UseEntityFramework().UseDbContext<TContext>()'.") |
|||
.ToString(), exception.Message); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Get_ReturnsDefaultStoreCorrespondingToTheSpecifiedTypeWhenAvailable() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
services.AddSingleton(Mock.Of<IOpenIddictApplicationStore<CustomApplication>>()); |
|||
services.AddSingleton(CreateStore()); |
|||
|
|||
var monitor = Mock.Of<IOptionsMonitor<OpenIddictEntityFrameworkOptions>>( |
|||
mock => mock.CurrentValue == new OpenIddictEntityFrameworkOptions |
|||
{ |
|||
DbContextType = typeof(DbContext) |
|||
}); |
|||
|
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictApplicationStoreResolver(monitor, provider); |
|||
|
|||
// Act and assert
|
|||
Assert.NotNull(resolver.Get<MyApplication>()); |
|||
} |
|||
|
|||
private static OpenIddictApplicationStore<MyApplication, MyAuthorization, MyToken, DbContext, long> CreateStore() |
|||
=> new Mock<OpenIddictApplicationStore<MyApplication, MyAuthorization, MyToken, DbContext, long>>( |
|||
Mock.Of<IMemoryCache>(), |
|||
Mock.Of<DbContext>(), |
|||
Mock.Of<IOptionsMonitor<OpenIddictEntityFrameworkOptions>>()).Object; |
|||
|
|||
public class CustomApplication { } |
|||
|
|||
public class MyApplication : OpenIddictApplication<long, MyAuthorization, MyToken> { } |
|||
public class MyAuthorization : OpenIddictAuthorization<long, MyApplication, MyToken> { } |
|||
public class MyScope : OpenIddictScope<long> { } |
|||
public class MyToken : OpenIddictToken<long, MyApplication, MyAuthorization> { } |
|||
} |
|||
} |
|||
@ -0,0 +1,117 @@ |
|||
/* |
|||
* 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.Data.Entity; |
|||
using System.Text; |
|||
using Microsoft.Extensions.Caching.Memory; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Options; |
|||
using Moq; |
|||
using OpenIddict.Abstractions; |
|||
using OpenIddict.EntityFramework.Models; |
|||
using Xunit; |
|||
|
|||
namespace OpenIddict.EntityFramework.Tests |
|||
{ |
|||
public class OpenIddictAuthorizationStoreResolverTests |
|||
{ |
|||
[Fact] |
|||
public void Get_ReturnsCustomStoreCorrespondingToTheSpecifiedTypeWhenAvailable() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
services.AddSingleton(Mock.Of<IOpenIddictAuthorizationStore<CustomAuthorization>>()); |
|||
|
|||
var monitor = Mock.Of<IOptionsMonitor<OpenIddictEntityFrameworkOptions>>(); |
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictAuthorizationStoreResolver(monitor, provider); |
|||
|
|||
// Act and assert
|
|||
Assert.NotNull(resolver.Get<CustomAuthorization>()); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Get_ThrowsAnExceptionForInvalidEntityType() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
|
|||
var monitor = Mock.Of<IOptionsMonitor<OpenIddictEntityFrameworkOptions>>(); |
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictAuthorizationStoreResolver(monitor, provider); |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<InvalidOperationException>(() => resolver.Get<CustomAuthorization>()); |
|||
|
|||
Assert.Equal(new StringBuilder() |
|||
.AppendLine("The specified authorization type is not compatible with the Entity Framework 6.x stores.") |
|||
.Append("When enabling the Entity Framework 6.x stores, make sure you use the built-in ") |
|||
.Append("'OpenIddictAuthorization' entity (from the 'OpenIddict.EntityFramework.Models' package) ") |
|||
.Append("or a custom entity that inherits from the generic 'OpenIddictAuthorization' entity.") |
|||
.ToString(), exception.Message); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Get_ThrowsAnExceptionWhenDbContextTypeIsNotAvailable() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
|
|||
var monitor = Mock.Of<IOptionsMonitor<OpenIddictEntityFrameworkOptions>>( |
|||
mock => mock.CurrentValue == new OpenIddictEntityFrameworkOptions |
|||
{ |
|||
DbContextType = null |
|||
}); |
|||
|
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictAuthorizationStoreResolver(monitor, provider); |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<InvalidOperationException>(() => resolver.Get<OpenIddictAuthorization>()); |
|||
|
|||
Assert.Equal(new StringBuilder() |
|||
.AppendLine("No Entity Framework 6.x context was specified in the OpenIddict options.") |
|||
.Append("To configure the OpenIddict Entity Framework 6.x stores to use a specific 'DbContext', ") |
|||
.Append("use 'options.UseEntityFramework().UseDbContext<TContext>()'.") |
|||
.ToString(), exception.Message); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Get_ReturnsDefaultStoreCorrespondingToTheSpecifiedTypeWhenAvailable() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
services.AddSingleton(Mock.Of<IOpenIddictAuthorizationStore<CustomAuthorization>>()); |
|||
services.AddSingleton(CreateStore()); |
|||
|
|||
var monitor = Mock.Of<IOptionsMonitor<OpenIddictEntityFrameworkOptions>>( |
|||
mock => mock.CurrentValue == new OpenIddictEntityFrameworkOptions |
|||
{ |
|||
DbContextType = typeof(DbContext) |
|||
}); |
|||
|
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictAuthorizationStoreResolver(monitor, provider); |
|||
|
|||
// Act and assert
|
|||
Assert.NotNull(resolver.Get<MyAuthorization>()); |
|||
} |
|||
|
|||
private static OpenIddictAuthorizationStore<MyAuthorization, MyApplication, MyToken, DbContext, long> CreateStore() |
|||
=> new Mock<OpenIddictAuthorizationStore<MyAuthorization, MyApplication, MyToken, DbContext, long>>( |
|||
Mock.Of<IMemoryCache>(), |
|||
Mock.Of<DbContext>(), |
|||
Mock.Of<IOptionsMonitor<OpenIddictEntityFrameworkOptions>>()).Object; |
|||
|
|||
public class CustomAuthorization { } |
|||
|
|||
public class MyApplication : OpenIddictApplication<long, MyAuthorization, MyToken> { } |
|||
public class MyAuthorization : OpenIddictAuthorization<long, MyApplication, MyToken> { } |
|||
public class MyScope : OpenIddictScope<long> { } |
|||
public class MyToken : OpenIddictToken<long, MyApplication, MyAuthorization> { } |
|||
} |
|||
} |
|||
@ -0,0 +1,117 @@ |
|||
/* |
|||
* 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.Data.Entity; |
|||
using System.Text; |
|||
using Microsoft.Extensions.Caching.Memory; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Options; |
|||
using Moq; |
|||
using OpenIddict.Abstractions; |
|||
using OpenIddict.EntityFramework.Models; |
|||
using Xunit; |
|||
|
|||
namespace OpenIddict.EntityFramework.Tests |
|||
{ |
|||
public class OpenIddictScopeStoreResolverTests |
|||
{ |
|||
[Fact] |
|||
public void Get_ReturnsCustomStoreCorrespondingToTheSpecifiedTypeWhenAvailable() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
services.AddSingleton(Mock.Of<IOpenIddictScopeStore<CustomScope>>()); |
|||
|
|||
var monitor = Mock.Of<IOptionsMonitor<OpenIddictEntityFrameworkOptions>>(); |
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictScopeStoreResolver(monitor, provider); |
|||
|
|||
// Act and assert
|
|||
Assert.NotNull(resolver.Get<CustomScope>()); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Get_ThrowsAnExceptionForInvalidEntityType() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
|
|||
var monitor = Mock.Of<IOptionsMonitor<OpenIddictEntityFrameworkOptions>>(); |
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictScopeStoreResolver(monitor, provider); |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<InvalidOperationException>(() => resolver.Get<CustomScope>()); |
|||
|
|||
Assert.Equal(new StringBuilder() |
|||
.AppendLine("The specified scope type is not compatible with the Entity Framework 6.x stores.") |
|||
.Append("When enabling the Entity Framework 6.x stores, make sure you use the built-in ") |
|||
.Append("'OpenIddictScope' entity (from the 'OpenIddict.EntityFramework.Models' package) ") |
|||
.Append("or a custom entity that inherits from the generic 'OpenIddictScope' entity.") |
|||
.ToString(), exception.Message); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Get_ThrowsAnExceptionWhenDbContextTypeIsNotAvailable() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
|
|||
var monitor = Mock.Of<IOptionsMonitor<OpenIddictEntityFrameworkOptions>>( |
|||
mock => mock.CurrentValue == new OpenIddictEntityFrameworkOptions |
|||
{ |
|||
DbContextType = null |
|||
}); |
|||
|
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictScopeStoreResolver(monitor, provider); |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<InvalidOperationException>(() => resolver.Get<OpenIddictScope>()); |
|||
|
|||
Assert.Equal(new StringBuilder() |
|||
.AppendLine("No Entity Framework 6.x context was specified in the OpenIddict options.") |
|||
.Append("To configure the OpenIddict Entity Framework 6.x stores to use a specific 'DbContext', ") |
|||
.Append("use 'options.UseEntityFramework().UseDbContext<TContext>()'.") |
|||
.ToString(), exception.Message); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Get_ReturnsDefaultStoreCorrespondingToTheSpecifiedTypeWhenAvailable() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
services.AddSingleton(Mock.Of<IOpenIddictScopeStore<CustomScope>>()); |
|||
services.AddSingleton(CreateStore()); |
|||
|
|||
var monitor = Mock.Of<IOptionsMonitor<OpenIddictEntityFrameworkOptions>>( |
|||
mock => mock.CurrentValue == new OpenIddictEntityFrameworkOptions |
|||
{ |
|||
DbContextType = typeof(DbContext) |
|||
}); |
|||
|
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictScopeStoreResolver(monitor, provider); |
|||
|
|||
// Act and assert
|
|||
Assert.NotNull(resolver.Get<MyScope>()); |
|||
} |
|||
|
|||
private static OpenIddictScopeStore<MyScope, DbContext, long> CreateStore() |
|||
=> new Mock<OpenIddictScopeStore<MyScope, DbContext, long>>( |
|||
Mock.Of<IMemoryCache>(), |
|||
Mock.Of<DbContext>(), |
|||
Mock.Of<IOptionsMonitor<OpenIddictEntityFrameworkOptions>>()).Object; |
|||
|
|||
public class CustomScope { } |
|||
|
|||
public class MyApplication : OpenIddictApplication<long, MyAuthorization, MyToken> { } |
|||
public class MyAuthorization : OpenIddictAuthorization<long, MyApplication, MyToken> { } |
|||
public class MyScope : OpenIddictScope<long> { } |
|||
public class MyToken : OpenIddictToken<long, MyApplication, MyAuthorization> { } |
|||
} |
|||
} |
|||
@ -0,0 +1,117 @@ |
|||
/* |
|||
* 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.Data.Entity; |
|||
using System.Text; |
|||
using Microsoft.Extensions.Caching.Memory; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Options; |
|||
using Moq; |
|||
using OpenIddict.Abstractions; |
|||
using OpenIddict.EntityFramework.Models; |
|||
using Xunit; |
|||
|
|||
namespace OpenIddict.EntityFramework.Tests |
|||
{ |
|||
public class OpenIddictTokenStoreResolverTests |
|||
{ |
|||
[Fact] |
|||
public void Get_ReturnsCustomStoreCorrespondingToTheSpecifiedTypeWhenAvailable() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
services.AddSingleton(Mock.Of<IOpenIddictTokenStore<CustomToken>>()); |
|||
|
|||
var monitor = Mock.Of<IOptionsMonitor<OpenIddictEntityFrameworkOptions>>(); |
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictTokenStoreResolver(monitor, provider); |
|||
|
|||
// Act and assert
|
|||
Assert.NotNull(resolver.Get<CustomToken>()); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Get_ThrowsAnExceptionForInvalidEntityType() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
|
|||
var monitor = Mock.Of<IOptionsMonitor<OpenIddictEntityFrameworkOptions>>(); |
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictTokenStoreResolver(monitor, provider); |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<InvalidOperationException>(() => resolver.Get<CustomToken>()); |
|||
|
|||
Assert.Equal(new StringBuilder() |
|||
.AppendLine("The specified token type is not compatible with the Entity Framework 6.x stores.") |
|||
.Append("When enabling the Entity Framework 6.x stores, make sure you use the built-in ") |
|||
.Append("'OpenIddictToken' entity (from the 'OpenIddict.EntityFramework.Models' package) ") |
|||
.Append("or a custom entity that inherits from the generic 'OpenIddictToken' entity.") |
|||
.ToString(), exception.Message); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Get_ThrowsAnExceptionWhenDbContextTypeIsNotAvailable() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
|
|||
var monitor = Mock.Of<IOptionsMonitor<OpenIddictEntityFrameworkOptions>>( |
|||
mock => mock.CurrentValue == new OpenIddictEntityFrameworkOptions |
|||
{ |
|||
DbContextType = null |
|||
}); |
|||
|
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictTokenStoreResolver(monitor, provider); |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<InvalidOperationException>(() => resolver.Get<OpenIddictToken>()); |
|||
|
|||
Assert.Equal(new StringBuilder() |
|||
.AppendLine("No Entity Framework 6.x context was specified in the OpenIddict options.") |
|||
.Append("To configure the OpenIddict Entity Framework 6.x stores to use a specific 'DbContext', ") |
|||
.Append("use 'options.UseEntityFramework().UseDbContext<TContext>()'.") |
|||
.ToString(), exception.Message); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Get_ReturnsDefaultStoreCorrespondingToTheSpecifiedTypeWhenAvailable() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
services.AddSingleton(Mock.Of<IOpenIddictTokenStore<CustomToken>>()); |
|||
services.AddSingleton(CreateStore()); |
|||
|
|||
var monitor = Mock.Of<IOptionsMonitor<OpenIddictEntityFrameworkOptions>>( |
|||
mock => mock.CurrentValue == new OpenIddictEntityFrameworkOptions |
|||
{ |
|||
DbContextType = typeof(DbContext) |
|||
}); |
|||
|
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictTokenStoreResolver(monitor, provider); |
|||
|
|||
// Act and assert
|
|||
Assert.NotNull(resolver.Get<MyToken>()); |
|||
} |
|||
|
|||
private static OpenIddictTokenStore<MyToken, MyApplication, MyAuthorization, DbContext, long> CreateStore() |
|||
=> new Mock<OpenIddictTokenStore<MyToken, MyApplication, MyAuthorization, DbContext, long>>( |
|||
Mock.Of<IMemoryCache>(), |
|||
Mock.Of<DbContext>(), |
|||
Mock.Of<IOptionsMonitor<OpenIddictEntityFrameworkOptions>>()).Object; |
|||
|
|||
public class CustomToken { } |
|||
|
|||
public class MyApplication : OpenIddictApplication<long, MyAuthorization, MyToken> { } |
|||
public class MyAuthorization : OpenIddictAuthorization<long, MyApplication, MyToken> { } |
|||
public class MyScope : OpenIddictScope<long> { } |
|||
public class MyToken : OpenIddictToken<long, MyApplication, MyAuthorization> { } |
|||
} |
|||
} |
|||
@ -0,0 +1,145 @@ |
|||
/* |
|||
* 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 Microsoft.EntityFrameworkCore; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Options; |
|||
using OpenIddict.Core; |
|||
using OpenIddict.EntityFrameworkCore.Models; |
|||
using Xunit; |
|||
|
|||
namespace OpenIddict.EntityFrameworkCore.Tests |
|||
{ |
|||
public class OpenIddictEntityFrameworkCoreBuilderTests |
|||
{ |
|||
[Fact] |
|||
public void Constructor_ThrowsAnExceptionForNullServices() |
|||
{ |
|||
// Arrange
|
|||
var services = (IServiceCollection) null; |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<ArgumentNullException>(() => new OpenIddictEntityFrameworkCoreBuilder(services)); |
|||
|
|||
Assert.Equal("services", exception.ParamName); |
|||
} |
|||
|
|||
[Fact] |
|||
public void ReplaceDefaultEntities_EntitiesAreCorrectlyReplaced() |
|||
{ |
|||
// Arrange
|
|||
var services = CreateServices(); |
|||
var builder = CreateBuilder(services); |
|||
|
|||
// Act
|
|||
builder.ReplaceDefaultEntities<CustomApplication, CustomAuthorization, CustomScope, CustomToken, long>(); |
|||
|
|||
// Assert
|
|||
var provider = services.BuildServiceProvider(); |
|||
var options = provider.GetRequiredService<IOptionsMonitor<OpenIddictCoreOptions>>().CurrentValue; |
|||
|
|||
Assert.Equal(typeof(CustomApplication), options.DefaultApplicationType); |
|||
Assert.Equal(typeof(CustomAuthorization), options.DefaultAuthorizationType); |
|||
Assert.Equal(typeof(CustomScope), options.DefaultScopeType); |
|||
Assert.Equal(typeof(CustomToken), options.DefaultTokenType); |
|||
} |
|||
|
|||
[Fact] |
|||
public void ReplaceDefaultEntities_AllowsSpecifyingCustomKeyType() |
|||
{ |
|||
// Arrange
|
|||
var services = CreateServices(); |
|||
var builder = CreateBuilder(services); |
|||
|
|||
// Act
|
|||
builder.ReplaceDefaultEntities<long>(); |
|||
|
|||
// Assert
|
|||
var provider = services.BuildServiceProvider(); |
|||
var options = provider.GetRequiredService<IOptionsMonitor<OpenIddictCoreOptions>>().CurrentValue; |
|||
|
|||
Assert.Equal(typeof(OpenIddictApplication<long>), options.DefaultApplicationType); |
|||
Assert.Equal(typeof(OpenIddictAuthorization<long>), options.DefaultAuthorizationType); |
|||
Assert.Equal(typeof(OpenIddictScope<long>), options.DefaultScopeType); |
|||
Assert.Equal(typeof(OpenIddictToken<long>), options.DefaultTokenType); |
|||
} |
|||
|
|||
[Fact] |
|||
public void UseDbContext_ThrowsAnExceptionForNullType() |
|||
{ |
|||
// Arrange
|
|||
var services = CreateServices(); |
|||
var builder = CreateBuilder(services); |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<ArgumentNullException>(delegate |
|||
{ |
|||
return builder.UseDbContext(type: null); |
|||
}); |
|||
|
|||
Assert.Equal("type", exception.ParamName); |
|||
} |
|||
|
|||
[Fact] |
|||
public void UseDbContext_ThrowsAnExceptionForInvalidType() |
|||
{ |
|||
// Arrange
|
|||
var services = CreateServices(); |
|||
var builder = CreateBuilder(services); |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<ArgumentException>(delegate |
|||
{ |
|||
return builder.UseDbContext(typeof(object)); |
|||
}); |
|||
|
|||
Assert.Equal("type", exception.ParamName); |
|||
Assert.StartsWith("The specified type is invalid.", exception.Message); |
|||
} |
|||
|
|||
[Fact] |
|||
public void UseDbContext_SetsDbContextTypeInOptions() |
|||
{ |
|||
// Arrange
|
|||
var services = CreateServices(); |
|||
var builder = CreateBuilder(services); |
|||
|
|||
// Act
|
|||
builder.UseDbContext<CustomDbContext>(); |
|||
|
|||
// Assert
|
|||
var provider = services.BuildServiceProvider(); |
|||
var options = provider.GetRequiredService<IOptionsMonitor<OpenIddictEntityFrameworkCoreOptions>>().CurrentValue; |
|||
|
|||
Assert.Equal(typeof(CustomDbContext), options.DbContextType); |
|||
} |
|||
|
|||
private static OpenIddictEntityFrameworkCoreBuilder CreateBuilder(IServiceCollection services) |
|||
=> services.AddOpenIddict().AddCore().UseEntityFrameworkCore(); |
|||
|
|||
private static IServiceCollection CreateServices() |
|||
{ |
|||
var services = new ServiceCollection(); |
|||
services.AddOptions(); |
|||
|
|||
return services; |
|||
} |
|||
|
|||
public class CustomApplication : OpenIddictApplication<long, CustomAuthorization, CustomToken> { } |
|||
public class CustomAuthorization : OpenIddictAuthorization<long, CustomApplication, CustomToken> { } |
|||
public class CustomScope : OpenIddictScope<long> { } |
|||
public class CustomToken : OpenIddictToken<long, CustomApplication, CustomAuthorization> { } |
|||
|
|||
public class CustomDbContext : DbContext |
|||
{ |
|||
public CustomDbContext(DbContextOptions options) |
|||
: base(options) |
|||
{ |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,117 @@ |
|||
/* |
|||
* 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.EntityFrameworkCore; |
|||
using Microsoft.Extensions.Caching.Memory; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Options; |
|||
using Moq; |
|||
using OpenIddict.Abstractions; |
|||
using OpenIddict.EntityFrameworkCore.Models; |
|||
using Xunit; |
|||
|
|||
namespace OpenIddict.EntityFrameworkCore.Tests |
|||
{ |
|||
public class OpenIddictApplicationStoreResolverTests |
|||
{ |
|||
[Fact] |
|||
public void Get_ReturnsCustomStoreCorrespondingToTheSpecifiedTypeWhenAvailable() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
services.AddSingleton(Mock.Of<IOpenIddictApplicationStore<CustomApplication>>()); |
|||
|
|||
var monitor = Mock.Of<IOptionsMonitor<OpenIddictEntityFrameworkCoreOptions>>(); |
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictApplicationStoreResolver(monitor, provider); |
|||
|
|||
// Act and assert
|
|||
Assert.NotNull(resolver.Get<CustomApplication>()); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Get_ThrowsAnExceptionForInvalidEntityType() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
|
|||
var monitor = Mock.Of<IOptionsMonitor<OpenIddictEntityFrameworkCoreOptions>>(); |
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictApplicationStoreResolver(monitor, provider); |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<InvalidOperationException>(() => resolver.Get<CustomApplication>()); |
|||
|
|||
Assert.Equal(new StringBuilder() |
|||
.AppendLine("The specified application type is not compatible with the Entity Framework Core stores.") |
|||
.Append("When enabling the Entity Framework Core stores, make sure you use the built-in ") |
|||
.Append("'OpenIddictApplication' entity (from the 'OpenIddict.EntityFrameworkCore.Models' package) ") |
|||
.Append("or a custom entity that inherits from the generic 'OpenIddictApplication' entity.") |
|||
.ToString(), exception.Message); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Get_ThrowsAnExceptionWhenDbContextTypeIsNotAvailable() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
|
|||
var monitor = Mock.Of<IOptionsMonitor<OpenIddictEntityFrameworkCoreOptions>>( |
|||
mock => mock.CurrentValue == new OpenIddictEntityFrameworkCoreOptions |
|||
{ |
|||
DbContextType = null |
|||
}); |
|||
|
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictApplicationStoreResolver(monitor, provider); |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<InvalidOperationException>(() => resolver.Get<OpenIddictApplication>()); |
|||
|
|||
Assert.Equal(new StringBuilder() |
|||
.AppendLine("No Entity Framework Core context was specified in the OpenIddict options.") |
|||
.Append("To configure the OpenIddict Entity Framework Core stores to use a specific 'DbContext', ") |
|||
.Append("use 'options.UseEntityFrameworkCore().UseDbContext<TContext>()'.") |
|||
.ToString(), exception.Message); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Get_ReturnsDefaultStoreCorrespondingToTheSpecifiedTypeWhenAvailable() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
services.AddSingleton(Mock.Of<IOpenIddictApplicationStore<CustomApplication>>()); |
|||
services.AddSingleton(CreateStore()); |
|||
|
|||
var monitor = Mock.Of<IOptionsMonitor<OpenIddictEntityFrameworkCoreOptions>>( |
|||
mock => mock.CurrentValue == new OpenIddictEntityFrameworkCoreOptions |
|||
{ |
|||
DbContextType = typeof(DbContext) |
|||
}); |
|||
|
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictApplicationStoreResolver(monitor, provider); |
|||
|
|||
// Act and assert
|
|||
Assert.NotNull(resolver.Get<MyApplication>()); |
|||
} |
|||
|
|||
private static OpenIddictApplicationStore<MyApplication, MyAuthorization, MyToken, DbContext, long> CreateStore() |
|||
=> new Mock<OpenIddictApplicationStore<MyApplication, MyAuthorization, MyToken, DbContext, long>>( |
|||
Mock.Of<IMemoryCache>(), |
|||
Mock.Of<DbContext>(), |
|||
Mock.Of<IOptionsMonitor<OpenIddictEntityFrameworkCoreOptions>>()).Object; |
|||
|
|||
public class CustomApplication { } |
|||
|
|||
public class MyApplication : OpenIddictApplication<long, MyAuthorization, MyToken> { } |
|||
public class MyAuthorization : OpenIddictAuthorization<long, MyApplication, MyToken> { } |
|||
public class MyScope : OpenIddictScope<long> { } |
|||
public class MyToken : OpenIddictToken<long, MyApplication, MyAuthorization> { } |
|||
} |
|||
} |
|||
@ -0,0 +1,117 @@ |
|||
/* |
|||
* 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.EntityFrameworkCore; |
|||
using Microsoft.Extensions.Caching.Memory; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Options; |
|||
using Moq; |
|||
using OpenIddict.Abstractions; |
|||
using OpenIddict.EntityFrameworkCore.Models; |
|||
using Xunit; |
|||
|
|||
namespace OpenIddict.EntityFrameworkCore.Tests |
|||
{ |
|||
public class OpenIddictAuthorizationStoreResolverTests |
|||
{ |
|||
[Fact] |
|||
public void Get_ReturnsCustomStoreCorrespondingToTheSpecifiedTypeWhenAvailable() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
services.AddSingleton(Mock.Of<IOpenIddictAuthorizationStore<CustomAuthorization>>()); |
|||
|
|||
var monitor = Mock.Of<IOptionsMonitor<OpenIddictEntityFrameworkCoreOptions>>(); |
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictAuthorizationStoreResolver(monitor, provider); |
|||
|
|||
// Act and assert
|
|||
Assert.NotNull(resolver.Get<CustomAuthorization>()); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Get_ThrowsAnExceptionForInvalidEntityType() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
|
|||
var monitor = Mock.Of<IOptionsMonitor<OpenIddictEntityFrameworkCoreOptions>>(); |
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictAuthorizationStoreResolver(monitor, provider); |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<InvalidOperationException>(() => resolver.Get<CustomAuthorization>()); |
|||
|
|||
Assert.Equal(new StringBuilder() |
|||
.AppendLine("The specified authorization type is not compatible with the Entity Framework Core stores.") |
|||
.Append("When enabling the Entity Framework Core stores, make sure you use the built-in ") |
|||
.Append("'OpenIddictAuthorization' entity (from the 'OpenIddict.EntityFrameworkCore.Models' package) ") |
|||
.Append("or a custom entity that inherits from the generic 'OpenIddictAuthorization' entity.") |
|||
.ToString(), exception.Message); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Get_ThrowsAnExceptionWhenDbContextTypeIsNotAvailable() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
|
|||
var monitor = Mock.Of<IOptionsMonitor<OpenIddictEntityFrameworkCoreOptions>>( |
|||
mock => mock.CurrentValue == new OpenIddictEntityFrameworkCoreOptions |
|||
{ |
|||
DbContextType = null |
|||
}); |
|||
|
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictAuthorizationStoreResolver(monitor, provider); |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<InvalidOperationException>(() => resolver.Get<OpenIddictAuthorization>()); |
|||
|
|||
Assert.Equal(new StringBuilder() |
|||
.AppendLine("No Entity Framework Core context was specified in the OpenIddict options.") |
|||
.Append("To configure the OpenIddict Entity Framework Core stores to use a specific 'DbContext', ") |
|||
.Append("use 'options.UseEntityFrameworkCore().UseDbContext<TContext>()'.") |
|||
.ToString(), exception.Message); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Get_ReturnsDefaultStoreCorrespondingToTheSpecifiedTypeWhenAvailable() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
services.AddSingleton(Mock.Of<IOpenIddictAuthorizationStore<CustomAuthorization>>()); |
|||
services.AddSingleton(CreateStore()); |
|||
|
|||
var monitor = Mock.Of<IOptionsMonitor<OpenIddictEntityFrameworkCoreOptions>>( |
|||
mock => mock.CurrentValue == new OpenIddictEntityFrameworkCoreOptions |
|||
{ |
|||
DbContextType = typeof(DbContext) |
|||
}); |
|||
|
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictAuthorizationStoreResolver(monitor, provider); |
|||
|
|||
// Act and assert
|
|||
Assert.NotNull(resolver.Get<MyAuthorization>()); |
|||
} |
|||
|
|||
private static OpenIddictAuthorizationStore<MyAuthorization, MyApplication, MyToken, DbContext, long> CreateStore() |
|||
=> new Mock<OpenIddictAuthorizationStore<MyAuthorization, MyApplication, MyToken, DbContext, long>>( |
|||
Mock.Of<IMemoryCache>(), |
|||
Mock.Of<DbContext>(), |
|||
Mock.Of<IOptionsMonitor<OpenIddictEntityFrameworkCoreOptions>>()).Object; |
|||
|
|||
public class CustomAuthorization { } |
|||
|
|||
public class MyApplication : OpenIddictApplication<long, MyAuthorization, MyToken> { } |
|||
public class MyAuthorization : OpenIddictAuthorization<long, MyApplication, MyToken> { } |
|||
public class MyScope : OpenIddictScope<long> { } |
|||
public class MyToken : OpenIddictToken<long, MyApplication, MyAuthorization> { } |
|||
} |
|||
} |
|||
@ -0,0 +1,117 @@ |
|||
/* |
|||
* 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.EntityFrameworkCore; |
|||
using Microsoft.Extensions.Caching.Memory; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Options; |
|||
using Moq; |
|||
using OpenIddict.Abstractions; |
|||
using OpenIddict.EntityFrameworkCore.Models; |
|||
using Xunit; |
|||
|
|||
namespace OpenIddict.EntityFrameworkCore.Tests |
|||
{ |
|||
public class OpenIddictScopeStoreResolverTests |
|||
{ |
|||
[Fact] |
|||
public void Get_ReturnsCustomStoreCorrespondingToTheSpecifiedTypeWhenAvailable() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
services.AddSingleton(Mock.Of<IOpenIddictScopeStore<CustomScope>>()); |
|||
|
|||
var monitor = Mock.Of<IOptionsMonitor<OpenIddictEntityFrameworkCoreOptions>>(); |
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictScopeStoreResolver(monitor, provider); |
|||
|
|||
// Act and assert
|
|||
Assert.NotNull(resolver.Get<CustomScope>()); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Get_ThrowsAnExceptionForInvalidEntityType() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
|
|||
var monitor = Mock.Of<IOptionsMonitor<OpenIddictEntityFrameworkCoreOptions>>(); |
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictScopeStoreResolver(monitor, provider); |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<InvalidOperationException>(() => resolver.Get<CustomScope>()); |
|||
|
|||
Assert.Equal(new StringBuilder() |
|||
.AppendLine("The specified scope type is not compatible with the Entity Framework Core stores.") |
|||
.Append("When enabling the Entity Framework Core stores, make sure you use the built-in ") |
|||
.Append("'OpenIddictScope' entity (from the 'OpenIddict.EntityFrameworkCore.Models' package) ") |
|||
.Append("or a custom entity that inherits from the generic 'OpenIddictScope' entity.") |
|||
.ToString(), exception.Message); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Get_ThrowsAnExceptionWhenDbContextTypeIsNotAvailable() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
|
|||
var monitor = Mock.Of<IOptionsMonitor<OpenIddictEntityFrameworkCoreOptions>>( |
|||
mock => mock.CurrentValue == new OpenIddictEntityFrameworkCoreOptions |
|||
{ |
|||
DbContextType = null |
|||
}); |
|||
|
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictScopeStoreResolver(monitor, provider); |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<InvalidOperationException>(() => resolver.Get<OpenIddictScope>()); |
|||
|
|||
Assert.Equal(new StringBuilder() |
|||
.AppendLine("No Entity Framework Core context was specified in the OpenIddict options.") |
|||
.Append("To configure the OpenIddict Entity Framework Core stores to use a specific 'DbContext', ") |
|||
.Append("use 'options.UseEntityFrameworkCore().UseDbContext<TContext>()'.") |
|||
.ToString(), exception.Message); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Get_ReturnsDefaultStoreCorrespondingToTheSpecifiedTypeWhenAvailable() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
services.AddSingleton(Mock.Of<IOpenIddictScopeStore<CustomScope>>()); |
|||
services.AddSingleton(CreateStore()); |
|||
|
|||
var monitor = Mock.Of<IOptionsMonitor<OpenIddictEntityFrameworkCoreOptions>>( |
|||
mock => mock.CurrentValue == new OpenIddictEntityFrameworkCoreOptions |
|||
{ |
|||
DbContextType = typeof(DbContext) |
|||
}); |
|||
|
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictScopeStoreResolver(monitor, provider); |
|||
|
|||
// Act and assert
|
|||
Assert.NotNull(resolver.Get<MyScope>()); |
|||
} |
|||
|
|||
private static OpenIddictScopeStore<MyScope, DbContext, long> CreateStore() |
|||
=> new Mock<OpenIddictScopeStore<MyScope, DbContext, long>>( |
|||
Mock.Of<IMemoryCache>(), |
|||
Mock.Of<DbContext>(), |
|||
Mock.Of<IOptionsMonitor<OpenIddictEntityFrameworkCoreOptions>>()).Object; |
|||
|
|||
public class CustomScope { } |
|||
|
|||
public class MyApplication : OpenIddictApplication<long, MyAuthorization, MyToken> { } |
|||
public class MyAuthorization : OpenIddictAuthorization<long, MyApplication, MyToken> { } |
|||
public class MyScope : OpenIddictScope<long> { } |
|||
public class MyToken : OpenIddictToken<long, MyApplication, MyAuthorization> { } |
|||
} |
|||
} |
|||
@ -0,0 +1,117 @@ |
|||
/* |
|||
* 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.EntityFrameworkCore; |
|||
using Microsoft.Extensions.Caching.Memory; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Options; |
|||
using Moq; |
|||
using OpenIddict.Abstractions; |
|||
using OpenIddict.EntityFrameworkCore.Models; |
|||
using Xunit; |
|||
|
|||
namespace OpenIddict.EntityFrameworkCore.Tests |
|||
{ |
|||
public class OpenIddictTokenStoreResolverTests |
|||
{ |
|||
[Fact] |
|||
public void Get_ReturnsCustomStoreCorrespondingToTheSpecifiedTypeWhenAvailable() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
services.AddSingleton(Mock.Of<IOpenIddictTokenStore<CustomToken>>()); |
|||
|
|||
var monitor = Mock.Of<IOptionsMonitor<OpenIddictEntityFrameworkCoreOptions>>(); |
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictTokenStoreResolver(monitor, provider); |
|||
|
|||
// Act and assert
|
|||
Assert.NotNull(resolver.Get<CustomToken>()); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Get_ThrowsAnExceptionForInvalidEntityType() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
|
|||
var monitor = Mock.Of<IOptionsMonitor<OpenIddictEntityFrameworkCoreOptions>>(); |
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictTokenStoreResolver(monitor, provider); |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<InvalidOperationException>(() => resolver.Get<CustomToken>()); |
|||
|
|||
Assert.Equal(new StringBuilder() |
|||
.AppendLine("The specified token type is not compatible with the Entity Framework Core stores.") |
|||
.Append("When enabling the Entity Framework Core stores, make sure you use the built-in ") |
|||
.Append("'OpenIddictToken' entity (from the 'OpenIddict.EntityFrameworkCore.Models' package) ") |
|||
.Append("or a custom entity that inherits from the generic 'OpenIddictToken' entity.") |
|||
.ToString(), exception.Message); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Get_ThrowsAnExceptionWhenDbContextTypeIsNotAvailable() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
|
|||
var monitor = Mock.Of<IOptionsMonitor<OpenIddictEntityFrameworkCoreOptions>>( |
|||
mock => mock.CurrentValue == new OpenIddictEntityFrameworkCoreOptions |
|||
{ |
|||
DbContextType = null |
|||
}); |
|||
|
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictTokenStoreResolver(monitor, provider); |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<InvalidOperationException>(() => resolver.Get<OpenIddictToken>()); |
|||
|
|||
Assert.Equal(new StringBuilder() |
|||
.AppendLine("No Entity Framework Core context was specified in the OpenIddict options.") |
|||
.Append("To configure the OpenIddict Entity Framework Core stores to use a specific 'DbContext', ") |
|||
.Append("use 'options.UseEntityFrameworkCore().UseDbContext<TContext>()'.") |
|||
.ToString(), exception.Message); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Get_ReturnsDefaultStoreCorrespondingToTheSpecifiedTypeWhenAvailable() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
services.AddSingleton(Mock.Of<IOpenIddictTokenStore<CustomToken>>()); |
|||
services.AddSingleton(CreateStore()); |
|||
|
|||
var monitor = Mock.Of<IOptionsMonitor<OpenIddictEntityFrameworkCoreOptions>>( |
|||
mock => mock.CurrentValue == new OpenIddictEntityFrameworkCoreOptions |
|||
{ |
|||
DbContextType = typeof(DbContext) |
|||
}); |
|||
|
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictTokenStoreResolver(monitor, provider); |
|||
|
|||
// Act and assert
|
|||
Assert.NotNull(resolver.Get<MyToken>()); |
|||
} |
|||
|
|||
private static OpenIddictTokenStore<MyToken, MyApplication, MyAuthorization, DbContext, long> CreateStore() |
|||
=> new Mock<OpenIddictTokenStore<MyToken, MyApplication, MyAuthorization, DbContext, long>>( |
|||
Mock.Of<IMemoryCache>(), |
|||
Mock.Of<DbContext>(), |
|||
Mock.Of<IOptionsMonitor<OpenIddictEntityFrameworkCoreOptions>>()).Object; |
|||
|
|||
public class CustomToken { } |
|||
|
|||
public class MyApplication : OpenIddictApplication<long, MyAuthorization, MyToken> { } |
|||
public class MyAuthorization : OpenIddictAuthorization<long, MyApplication, MyToken> { } |
|||
public class MyScope : OpenIddictScope<long> { } |
|||
public class MyToken : OpenIddictToken<long, MyApplication, MyAuthorization> { } |
|||
} |
|||
} |
|||
@ -0,0 +1,26 @@ |
|||
<Project Sdk="Microsoft.NET.Sdk"> |
|||
|
|||
<Import Project="..\..\build\common.props" /> |
|||
|
|||
<PropertyGroup> |
|||
<TargetFrameworks>netcoreapp2.0;net461</TargetFrameworks> |
|||
<TargetFrameworks Condition=" '$(OS)' != 'Windows_NT' ">netcoreapp2.0</TargetFrameworks> |
|||
</PropertyGroup> |
|||
|
|||
<ItemGroup> |
|||
<ProjectReference Include="..\..\src\OpenIddict.MongoDb\OpenIddict.MongoDb.csproj" /> |
|||
</ItemGroup> |
|||
|
|||
<ItemGroup> |
|||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="$(AspNetCoreVersion)" /> |
|||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(TestSdkVersion)" /> |
|||
<PackageReference Include="Moq" Version="$(MoqVersion)" /> |
|||
<PackageReference Include="xunit" Version="$(XunitVersion)" /> |
|||
<PackageReference Include="xunit.runner.visualstudio" Version="$(XunitVersion)" /> |
|||
</ItemGroup> |
|||
|
|||
<ItemGroup> |
|||
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" /> |
|||
</ItemGroup> |
|||
|
|||
</Project> |
|||
@ -0,0 +1,282 @@ |
|||
/* |
|||
* 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 Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Options; |
|||
using MongoDB.Driver; |
|||
using Moq; |
|||
using OpenIddict.Core; |
|||
using OpenIddict.MongoDb.Models; |
|||
using Xunit; |
|||
|
|||
namespace OpenIddict.MongoDb.Tests |
|||
{ |
|||
public class OpenIddictMongoDbBuilderTests |
|||
{ |
|||
[Fact] |
|||
public void Constructor_ThrowsAnExceptionForNullServices() |
|||
{ |
|||
// Arrange
|
|||
var services = (IServiceCollection) null; |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<ArgumentNullException>(() => new OpenIddictMongoDbBuilder(services)); |
|||
|
|||
Assert.Equal("services", exception.ParamName); |
|||
} |
|||
|
|||
[Fact] |
|||
public void ReplaceDefaultApplicationEntity_EntityIsCorrectlySet() |
|||
{ |
|||
// Arrange
|
|||
var services = CreateServices(); |
|||
var builder = CreateBuilder(services); |
|||
|
|||
// Act
|
|||
builder.ReplaceDefaultApplicationEntity<CustomApplication>(); |
|||
|
|||
// Assert
|
|||
var provider = services.BuildServiceProvider(); |
|||
var options = provider.GetRequiredService<IOptionsMonitor<OpenIddictCoreOptions>>().CurrentValue; |
|||
|
|||
Assert.Equal(typeof(CustomApplication), options.DefaultApplicationType); |
|||
} |
|||
|
|||
[Fact] |
|||
public void ReplaceDefaultAuthorizationEntity_EntityIsCorrectlySet() |
|||
{ |
|||
// Arrange
|
|||
var services = CreateServices(); |
|||
var builder = CreateBuilder(services); |
|||
|
|||
// Act
|
|||
builder.ReplaceDefaultAuthorizationEntity<CustomAuthorization>(); |
|||
|
|||
// Assert
|
|||
var provider = services.BuildServiceProvider(); |
|||
var options = provider.GetRequiredService<IOptionsMonitor<OpenIddictCoreOptions>>().CurrentValue; |
|||
|
|||
Assert.Equal(typeof(CustomAuthorization), options.DefaultAuthorizationType); |
|||
} |
|||
|
|||
[Fact] |
|||
public void ReplaceDefaultScopeEntity_EntityIsCorrectlySet() |
|||
{ |
|||
// Arrange
|
|||
var services = CreateServices(); |
|||
var builder = CreateBuilder(services); |
|||
|
|||
// Act
|
|||
builder.ReplaceDefaultScopeEntity<CustomScope>(); |
|||
|
|||
// Assert
|
|||
var provider = services.BuildServiceProvider(); |
|||
var options = provider.GetRequiredService<IOptionsMonitor<OpenIddictCoreOptions>>().CurrentValue; |
|||
|
|||
Assert.Equal(typeof(CustomScope), options.DefaultScopeType); |
|||
} |
|||
|
|||
[Fact] |
|||
public void ReplaceDefaultTokenEntity_EntityIsCorrectlySet() |
|||
{ |
|||
// Arrange
|
|||
var services = CreateServices(); |
|||
var builder = CreateBuilder(services); |
|||
|
|||
// Act
|
|||
builder.ReplaceDefaultTokenEntity<CustomToken>(); |
|||
|
|||
// Assert
|
|||
var provider = services.BuildServiceProvider(); |
|||
var options = provider.GetRequiredService<IOptionsMonitor<OpenIddictCoreOptions>>().CurrentValue; |
|||
|
|||
Assert.Equal(typeof(CustomToken), options.DefaultTokenType); |
|||
} |
|||
|
|||
[Theory] |
|||
[InlineData(null)] |
|||
[InlineData("")] |
|||
public void SetApplicationsCollectionName_ThrowsAnExceptionForNullOrEmptyCollectionName(string name) |
|||
{ |
|||
// Arrange
|
|||
var services = CreateServices(); |
|||
var builder = CreateBuilder(services); |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<ArgumentException>(() => builder.SetApplicationsCollectionName(name)); |
|||
|
|||
Assert.Equal("name", exception.ParamName); |
|||
Assert.StartsWith("The collection name cannot be null or empty.", exception.Message); |
|||
} |
|||
|
|||
[Fact] |
|||
public void SetApplicationsCollectionName_CollectionNameIsCorrectlySet() |
|||
{ |
|||
// Arrange
|
|||
var services = CreateServices(); |
|||
var builder = CreateBuilder(services); |
|||
|
|||
// Act
|
|||
builder.SetApplicationsCollectionName("custom_collection"); |
|||
|
|||
// Assert
|
|||
var provider = services.BuildServiceProvider(); |
|||
var options = provider.GetRequiredService<IOptionsMonitor<OpenIddictMongoDbOptions>>().CurrentValue; |
|||
|
|||
Assert.Equal("custom_collection", options.ApplicationsCollectionName); |
|||
} |
|||
|
|||
[Theory] |
|||
[InlineData(null)] |
|||
[InlineData("")] |
|||
public void SetAuthorizationsCollectionName_ThrowsAnExceptionForNullOrEmptyCollectionName(string name) |
|||
{ |
|||
// Arrange
|
|||
var services = CreateServices(); |
|||
var builder = CreateBuilder(services); |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<ArgumentException>(() => builder.SetAuthorizationsCollectionName(name)); |
|||
|
|||
Assert.Equal("name", exception.ParamName); |
|||
Assert.StartsWith("The collection name cannot be null or empty.", exception.Message); |
|||
} |
|||
|
|||
[Fact] |
|||
public void SetAuthorizationsCollectionName_CollectionNameIsCorrectlySet() |
|||
{ |
|||
// Arrange
|
|||
var services = CreateServices(); |
|||
var builder = CreateBuilder(services); |
|||
|
|||
// Act
|
|||
builder.SetAuthorizationsCollectionName("custom_collection"); |
|||
|
|||
// Assert
|
|||
var provider = services.BuildServiceProvider(); |
|||
var options = provider.GetRequiredService<IOptionsMonitor<OpenIddictMongoDbOptions>>().CurrentValue; |
|||
|
|||
Assert.Equal("custom_collection", options.AuthorizationsCollectionName); |
|||
} |
|||
|
|||
[Theory] |
|||
[InlineData(null)] |
|||
[InlineData("")] |
|||
public void SetScopesCollectionName_ThrowsAnExceptionForNullOrEmptyCollectionName(string name) |
|||
{ |
|||
// Arrange
|
|||
var services = CreateServices(); |
|||
var builder = CreateBuilder(services); |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<ArgumentException>(() => builder.SetScopesCollectionName(name)); |
|||
|
|||
Assert.Equal("name", exception.ParamName); |
|||
Assert.StartsWith("The collection name cannot be null or empty.", exception.Message); |
|||
} |
|||
|
|||
[Fact] |
|||
public void SetScopesCollectionName_CollectionNameIsCorrectlySet() |
|||
{ |
|||
// Arrange
|
|||
var services = CreateServices(); |
|||
var builder = CreateBuilder(services); |
|||
|
|||
// Act
|
|||
builder.SetScopesCollectionName("custom_collection"); |
|||
|
|||
// Assert
|
|||
var provider = services.BuildServiceProvider(); |
|||
var options = provider.GetRequiredService<IOptionsMonitor<OpenIddictMongoDbOptions>>().CurrentValue; |
|||
|
|||
Assert.Equal("custom_collection", options.ScopesCollectionName); |
|||
} |
|||
|
|||
[Theory] |
|||
[InlineData(null)] |
|||
[InlineData("")] |
|||
public void SetTokensCollectionName_ThrowsAnExceptionForNullOrEmptyCollectionName(string name) |
|||
{ |
|||
// Arrange
|
|||
var services = CreateServices(); |
|||
var builder = CreateBuilder(services); |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<ArgumentException>(() => builder.SetTokensCollectionName(name)); |
|||
|
|||
Assert.Equal("name", exception.ParamName); |
|||
Assert.StartsWith("The collection name cannot be null or empty.", exception.Message); |
|||
} |
|||
|
|||
[Fact] |
|||
public void SetTokensCollectionName_CollectionNameIsCorrectlySet() |
|||
{ |
|||
// Arrange
|
|||
var services = CreateServices(); |
|||
var builder = CreateBuilder(services); |
|||
|
|||
// Act
|
|||
builder.SetTokensCollectionName("custom_collection"); |
|||
|
|||
// Assert
|
|||
var provider = services.BuildServiceProvider(); |
|||
var options = provider.GetRequiredService<IOptionsMonitor<OpenIddictMongoDbOptions>>().CurrentValue; |
|||
|
|||
Assert.Equal("custom_collection", options.TokensCollectionName); |
|||
} |
|||
|
|||
[Fact] |
|||
public void UseDatabase_ThrowsAnExceptionForNullDatabase() |
|||
{ |
|||
// Arrange
|
|||
var services = CreateServices(); |
|||
var builder = CreateBuilder(services); |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<ArgumentNullException>(delegate |
|||
{ |
|||
return builder.UseDatabase(database: null); |
|||
}); |
|||
|
|||
Assert.Equal("database", exception.ParamName); |
|||
} |
|||
|
|||
[Fact] |
|||
public void UseDatabase_SetsDatabaseInOptions() |
|||
{ |
|||
// Arrange
|
|||
var services = CreateServices(); |
|||
var builder = CreateBuilder(services); |
|||
var database = Mock.Of<IMongoDatabase>(); |
|||
|
|||
// Act
|
|||
builder.UseDatabase(database); |
|||
|
|||
// Assert
|
|||
var provider = services.BuildServiceProvider(); |
|||
var options = provider.GetRequiredService<IOptionsMonitor<OpenIddictMongoDbOptions>>().CurrentValue; |
|||
|
|||
Assert.Equal(database, options.Database); |
|||
} |
|||
|
|||
private static OpenIddictMongoDbBuilder CreateBuilder(IServiceCollection services) |
|||
=> services.AddOpenIddict().AddCore().UseMongoDb(); |
|||
|
|||
private static IServiceCollection CreateServices() |
|||
{ |
|||
var services = new ServiceCollection(); |
|||
services.AddOptions(); |
|||
|
|||
return services; |
|||
} |
|||
|
|||
public class CustomApplication : OpenIddictApplication { } |
|||
public class CustomAuthorization : OpenIddictAuthorization { } |
|||
public class CustomScope : OpenIddictScope { } |
|||
public class CustomToken : OpenIddictToken { } |
|||
} |
|||
} |
|||
@ -0,0 +1,132 @@ |
|||
/* |
|||
* 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 Microsoft.Extensions.Caching.Memory; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Options; |
|||
using OpenIddict.Abstractions; |
|||
using OpenIddict.Core; |
|||
using OpenIddict.MongoDb.Models; |
|||
using Xunit; |
|||
|
|||
namespace OpenIddict.MongoDb.Tests |
|||
{ |
|||
public class OpenIddictMongoDbExtensionsTests |
|||
{ |
|||
[Fact] |
|||
public void UseMongoDb_ThrowsAnExceptionForNullBuilder() |
|||
{ |
|||
// Arrange
|
|||
var builder = (OpenIddictCoreBuilder) null; |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<ArgumentNullException>(() => builder.UseMongoDb()); |
|||
|
|||
Assert.Equal("builder", exception.ParamName); |
|||
} |
|||
|
|||
[Fact] |
|||
public void UseMongoDb_ThrowsAnExceptionForNullConfiguration() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
var builder = new OpenIddictCoreBuilder(services); |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<ArgumentNullException>(() => builder.UseMongoDb(configuration: null)); |
|||
|
|||
Assert.Equal("configuration", exception.ParamName); |
|||
} |
|||
|
|||
[Fact] |
|||
public void UseMongoDb_RegistersCachingServices() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
var builder = new OpenIddictCoreBuilder(services); |
|||
|
|||
// Act
|
|||
builder.UseMongoDb(); |
|||
|
|||
// Assert
|
|||
Assert.Contains(services, service => service.ServiceType == typeof(IMemoryCache)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void UseMongoDb_RegistersDefaultEntities() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection().AddOptions(); |
|||
var builder = new OpenIddictCoreBuilder(services); |
|||
|
|||
// Act
|
|||
builder.UseMongoDb(); |
|||
|
|||
// Assert
|
|||
var provider = services.BuildServiceProvider(); |
|||
var options = provider.GetRequiredService<IOptionsMonitor<OpenIddictCoreOptions>>().CurrentValue; |
|||
|
|||
Assert.Equal(typeof(OpenIddictApplication), options.DefaultApplicationType); |
|||
Assert.Equal(typeof(OpenIddictAuthorization), options.DefaultAuthorizationType); |
|||
Assert.Equal(typeof(OpenIddictScope), options.DefaultScopeType); |
|||
Assert.Equal(typeof(OpenIddictToken), options.DefaultTokenType); |
|||
} |
|||
|
|||
[Theory] |
|||
[InlineData(typeof(IOpenIddictApplicationStoreResolver), typeof(OpenIddictApplicationStoreResolver))] |
|||
[InlineData(typeof(IOpenIddictAuthorizationStoreResolver), typeof(OpenIddictAuthorizationStoreResolver))] |
|||
[InlineData(typeof(IOpenIddictScopeStoreResolver), typeof(OpenIddictScopeStoreResolver))] |
|||
[InlineData(typeof(IOpenIddictTokenStoreResolver), typeof(OpenIddictTokenStoreResolver))] |
|||
public void UseMongoDb_RegistersMongoDbStoreResolvers(Type serviceType, Type implementationType) |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
var builder = new OpenIddictCoreBuilder(services); |
|||
|
|||
// Act
|
|||
builder.UseMongoDb(); |
|||
|
|||
// Assert
|
|||
Assert.Contains(services, service => service.ServiceType == serviceType && |
|||
service.ImplementationType == implementationType); |
|||
} |
|||
|
|||
[Theory] |
|||
[InlineData(typeof(OpenIddictApplicationStore<>))] |
|||
[InlineData(typeof(OpenIddictAuthorizationStore<>))] |
|||
[InlineData(typeof(OpenIddictScopeStore<>))] |
|||
[InlineData(typeof(OpenIddictTokenStore<>))] |
|||
public void UseMongoDb_RegistersMongoDbStore(Type type) |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
var builder = new OpenIddictCoreBuilder(services); |
|||
|
|||
// Act
|
|||
builder.UseMongoDb(); |
|||
|
|||
// Assert
|
|||
Assert.Contains(services, service => service.ServiceType == type && service.ImplementationType == type); |
|||
} |
|||
|
|||
[Fact] |
|||
public void UseMongoDb_RegistersMongoDbContext() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
var builder = new OpenIddictCoreBuilder(services); |
|||
|
|||
// Act
|
|||
builder.UseMongoDb(); |
|||
|
|||
// Assert
|
|||
Assert.Contains(services, service => service.Lifetime == ServiceLifetime.Singleton && |
|||
service.ServiceType == typeof(IOpenIddictMongoDbContext) && |
|||
service.ImplementationType == typeof(OpenIddictMongoDbContext)); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,208 @@ |
|||
/* |
|||
* 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 AspNet.Security.OpenIdConnect.Server; |
|||
using Microsoft.AspNetCore.Authentication; |
|||
using Microsoft.Extensions.Caching.Distributed; |
|||
using Microsoft.Extensions.Caching.Memory; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Logging; |
|||
using Microsoft.Extensions.Options; |
|||
using Xunit; |
|||
|
|||
namespace OpenIddict.Server.Tests |
|||
{ |
|||
public class OpenIddictServerExtensionsTests |
|||
{ |
|||
[Fact] |
|||
public void AddServer_ThrowsAnExceptionForNullBuilder() |
|||
{ |
|||
// Arrange
|
|||
var builder = (OpenIddictBuilder) null; |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<ArgumentNullException>(() => builder.AddServer()); |
|||
|
|||
Assert.Equal("builder", exception.ParamName); |
|||
} |
|||
|
|||
[Fact] |
|||
public void AddServer_ThrowsAnExceptionForNullConfiguration() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
var builder = new OpenIddictBuilder(services); |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<ArgumentNullException>(() => builder.AddServer(configuration: null)); |
|||
|
|||
Assert.Equal("configuration", exception.ParamName); |
|||
} |
|||
|
|||
[Fact] |
|||
public void AddServer_RegistersAuthenticationServices() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
var builder = new OpenIddictBuilder(services); |
|||
|
|||
// Act
|
|||
builder.AddServer(); |
|||
|
|||
// Assert
|
|||
Assert.Contains(services, service => service.ServiceType == typeof(IAuthenticationService)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void AddServer_RegistersCachingServices() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
var builder = new OpenIddictBuilder(services); |
|||
|
|||
// Act
|
|||
builder.AddServer(); |
|||
|
|||
// Assert
|
|||
Assert.Contains(services, service => service.ServiceType == typeof(IDistributedCache)); |
|||
Assert.Contains(services, service => service.ServiceType == typeof(IMemoryCache)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void AddServer_RegistersLoggingServices() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
var builder = new OpenIddictBuilder(services); |
|||
|
|||
// Act
|
|||
builder.AddServer(); |
|||
|
|||
// Assert
|
|||
Assert.Contains(services, service => service.ServiceType == typeof(ILogger<>)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void AddServer_RegistersOptionsServices() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
var builder = new OpenIddictBuilder(services); |
|||
|
|||
// Act
|
|||
builder.AddServer(); |
|||
|
|||
// Assert
|
|||
Assert.Contains(services, service => service.ServiceType == typeof(IOptions<>)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void AddServer_RegistersEventService() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
var builder = new OpenIddictBuilder(services); |
|||
|
|||
// Act
|
|||
builder.AddServer(); |
|||
|
|||
// Assert
|
|||
Assert.Contains(services, service => service.Lifetime == ServiceLifetime.Scoped && |
|||
service.ServiceType == typeof(IOpenIddictServerEventService) && |
|||
service.ImplementationType == typeof(OpenIddictServerEventService)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void AddServer_RegistersHandler() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
var builder = new OpenIddictBuilder(services); |
|||
|
|||
// Act
|
|||
builder.AddServer(); |
|||
|
|||
// Assert
|
|||
Assert.Contains(services, service => service.Lifetime == ServiceLifetime.Scoped && |
|||
service.ServiceType == typeof(OpenIddictServerHandler) && |
|||
service.ImplementationType == typeof(OpenIddictServerHandler)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void AddServer_RegistersProvider() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
var builder = new OpenIddictBuilder(services); |
|||
|
|||
// Act
|
|||
builder.AddServer(); |
|||
|
|||
// Assert
|
|||
Assert.Contains(services, service => service.Lifetime == ServiceLifetime.Scoped && |
|||
service.ServiceType == typeof(OpenIddictServerProvider) && |
|||
service.ImplementationFactory != null); |
|||
} |
|||
|
|||
[Fact] |
|||
public void AddServer_ResolvingProviderThrowsAnExceptionWhenCoreServicesAreNotRegistered() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
var builder = new OpenIddictBuilder(services); |
|||
|
|||
// Act
|
|||
builder.AddServer(); |
|||
|
|||
// Assert
|
|||
var provider = services.BuildServiceProvider(); |
|||
|
|||
var exception = Assert.Throws<InvalidOperationException>(() => provider.GetRequiredService<OpenIddictServerProvider>()); |
|||
|
|||
Assert.Equal(new StringBuilder() |
|||
.AppendLine("The core services must be registered when enabling the server handler.") |
|||
.Append("To register the OpenIddict core services, use 'services.AddOpenIddict().AddCore()'.") |
|||
.ToString(), exception.Message); |
|||
} |
|||
|
|||
[Theory] |
|||
[InlineData(typeof(IPostConfigureOptions<OpenIddictServerOptions>), typeof(OpenIddictServerInitializer))] |
|||
[InlineData(typeof(IPostConfigureOptions<OpenIddictServerOptions>), typeof(OpenIdConnectServerInitializer))] |
|||
public void AddServer_RegistersInitializers(Type serviceType, Type implementationType) |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
var builder = new OpenIddictBuilder(services); |
|||
|
|||
// Act
|
|||
builder.AddServer(); |
|||
|
|||
// Assert
|
|||
Assert.Contains(services, service => service.ServiceType == serviceType && |
|||
service.ImplementationType == implementationType); |
|||
} |
|||
|
|||
[Fact] |
|||
public void AddServer_RegistersAuthenticationScheme() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
var builder = new OpenIddictBuilder(services); |
|||
|
|||
// Act
|
|||
builder.AddServer(); |
|||
|
|||
// Assert
|
|||
var provider = services.BuildServiceProvider(); |
|||
var options = provider.GetRequiredService<IOptions<AuthenticationOptions>>().Value; |
|||
|
|||
Assert.Contains(options.Schemes, scheme => scheme.Name == OpenIddictServerDefaults.AuthenticationScheme && |
|||
scheme.HandlerType == typeof(OpenIddictServerHandler)); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,169 @@ |
|||
/* |
|||
* 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 AspNet.Security.OAuth.Validation; |
|||
using Microsoft.AspNetCore.Authentication; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Logging; |
|||
using Microsoft.Extensions.Options; |
|||
using Xunit; |
|||
|
|||
namespace OpenIddict.Validation.Tests |
|||
{ |
|||
public class OpenIddictValidationExtensionsTests |
|||
{ |
|||
[Fact] |
|||
public void AddValidation_ThrowsAnExceptionForNullBuilder() |
|||
{ |
|||
// Arrange
|
|||
var builder = (OpenIddictBuilder) null; |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<ArgumentNullException>(() => builder.AddValidation()); |
|||
|
|||
Assert.Equal("builder", exception.ParamName); |
|||
} |
|||
|
|||
[Fact] |
|||
public void AddValidation_ThrowsAnExceptionForNullConfiguration() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
var builder = new OpenIddictBuilder(services); |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<ArgumentNullException>(() => builder.AddValidation(configuration: null)); |
|||
|
|||
Assert.Equal("configuration", exception.ParamName); |
|||
} |
|||
|
|||
[Fact] |
|||
public void AddValidation_RegistersAuthenticationServices() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
var builder = new OpenIddictBuilder(services); |
|||
|
|||
// Act
|
|||
builder.AddValidation(); |
|||
|
|||
// Assert
|
|||
Assert.Contains(services, service => service.ServiceType == typeof(IAuthenticationService)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void AddValidation_RegistersLoggingServices() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
var builder = new OpenIddictBuilder(services); |
|||
|
|||
// Act
|
|||
builder.AddValidation(); |
|||
|
|||
// Assert
|
|||
Assert.Contains(services, service => service.ServiceType == typeof(ILogger<>)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void AddValidation_RegistersOptionsServices() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
var builder = new OpenIddictBuilder(services); |
|||
|
|||
// Act
|
|||
builder.AddValidation(); |
|||
|
|||
// Assert
|
|||
Assert.Contains(services, service => service.ServiceType == typeof(IOptions<>)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void AddValidation_RegistersEventService() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
var builder = new OpenIddictBuilder(services); |
|||
|
|||
// Act
|
|||
builder.AddValidation(); |
|||
|
|||
// Assert
|
|||
Assert.Contains(services, service => service.Lifetime == ServiceLifetime.Scoped && |
|||
service.ServiceType == typeof(IOpenIddictValidationEventService) && |
|||
service.ImplementationType == typeof(OpenIddictValidationEventService)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void AddValidation_RegistersHandler() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
var builder = new OpenIddictBuilder(services); |
|||
|
|||
// Act
|
|||
builder.AddValidation(); |
|||
|
|||
// Assert
|
|||
Assert.Contains(services, service => service.Lifetime == ServiceLifetime.Scoped && |
|||
service.ServiceType == typeof(OpenIddictValidationHandler) && |
|||
service.ImplementationType == typeof(OpenIddictValidationHandler)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void AddValidation_RegistersProvider() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
var builder = new OpenIddictBuilder(services); |
|||
|
|||
// Act
|
|||
builder.AddValidation(); |
|||
|
|||
// Assert
|
|||
Assert.Contains(services, service => service.Lifetime == ServiceLifetime.Scoped && |
|||
service.ServiceType == typeof(OpenIddictValidationProvider) && |
|||
service.ImplementationType == typeof(OpenIddictValidationProvider)); |
|||
} |
|||
|
|||
[Theory] |
|||
[InlineData(typeof(IPostConfigureOptions<OpenIddictValidationOptions>), typeof(OpenIddictValidationInitializer))] |
|||
[InlineData(typeof(IPostConfigureOptions<OpenIddictValidationOptions>), typeof(OAuthValidationInitializer))] |
|||
public void AddValidation_RegistersInitializers(Type serviceType, Type implementationType) |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
var builder = new OpenIddictBuilder(services); |
|||
|
|||
// Act
|
|||
builder.AddValidation(); |
|||
|
|||
// Assert
|
|||
Assert.Contains(services, service => service.ServiceType == serviceType && |
|||
service.ImplementationType == implementationType); |
|||
} |
|||
|
|||
[Fact] |
|||
public void AddValidation_RegistersAuthenticationScheme() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
var builder = new OpenIddictBuilder(services); |
|||
|
|||
// Act
|
|||
builder.AddValidation(); |
|||
|
|||
// Assert
|
|||
var provider = services.BuildServiceProvider(); |
|||
var options = provider.GetRequiredService<IOptions<AuthenticationOptions>>().Value; |
|||
|
|||
Assert.Contains(options.Schemes, scheme => scheme.Name == OpenIddictValidationDefaults.AuthenticationScheme && |
|||
scheme.HandlerType == typeof(OpenIddictValidationHandler)); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue