/* * 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; using static OpenIddict.EntityFramework.OpenIddictTokenStoreResolver; namespace OpenIddict.EntityFramework.Tests { public class OpenIddictTokenStoreResolverTests { [Fact] public void Get_ReturnsCustomStoreCorrespondingToTheSpecifiedTypeWhenAvailable() { // Arrange var services = new ServiceCollection(); services.AddSingleton(Mock.Of>()); var options = Mock.Of>(); var provider = services.BuildServiceProvider(); var resolver = new OpenIddictTokenStoreResolver(new TypeResolutionCache(), options, provider); // Act and assert Assert.NotNull(resolver.Get()); } [Fact] public void Get_ThrowsAnExceptionForInvalidEntityType() { // Arrange var services = new ServiceCollection(); var options = Mock.Of>(); var provider = services.BuildServiceProvider(); var resolver = new OpenIddictTokenStoreResolver(new TypeResolutionCache(), options, provider); // Act and assert var exception = Assert.Throws(() => resolver.Get()); 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 options = Mock.Of>( mock => mock.CurrentValue == new OpenIddictEntityFrameworkOptions { DbContextType = null }); var provider = services.BuildServiceProvider(); var resolver = new OpenIddictTokenStoreResolver(new TypeResolutionCache(), options, provider); // Act and assert var exception = Assert.Throws(() => resolver.Get()); 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()'.") .ToString(), exception.Message); } [Fact] public void Get_ReturnsDefaultStoreCorrespondingToTheSpecifiedTypeWhenAvailable() { // Arrange var services = new ServiceCollection(); services.AddSingleton(Mock.Of>()); services.AddSingleton(CreateStore()); var options = Mock.Of>( mock => mock.CurrentValue == new OpenIddictEntityFrameworkOptions { DbContextType = typeof(DbContext) }); var provider = services.BuildServiceProvider(); var resolver = new OpenIddictTokenStoreResolver(new TypeResolutionCache(), options, provider); // Act and assert Assert.NotNull(resolver.Get()); } private static OpenIddictTokenStore CreateStore() => new Mock>( Mock.Of(), Mock.Of(), Mock.Of>()).Object; public class CustomToken { } public class MyApplication : OpenIddictApplication { } public class MyAuthorization : OpenIddictAuthorization { } public class MyScope : OpenIddictScope { } public class MyToken : OpenIddictToken { } } }