/* * 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 OpenIddict.Models; using Xunit; namespace OpenIddict.EntityFrameworkCore.Tests { public class OpenIddictExtensionsTests { [Fact] public void AddEntityFrameworkCoreStores_ThrowsAnExceptionForInvalidApplicationEntity() { // Arrange var builder = new OpenIddictBuilder(new ServiceCollection()) { ApplicationType = typeof(object) }; // Act and assert var exception = Assert.Throws(delegate { builder.AddEntityFrameworkCoreStores(); }); Assert.Equal("The Entity Framework Core stores can only be used " + "with the built-in OpenIddictApplication entity.", exception.Message); } [Fact] public void AddEntityFrameworkCoreStores_ThrowsAnExceptionForInvalidAuthorizationEntity() { // Arrange var builder = new OpenIddictBuilder(new ServiceCollection()) { AuthorizationType = typeof(object) }; // Act and assert var exception = Assert.Throws(delegate { builder.AddEntityFrameworkCoreStores(); }); Assert.Equal("The Entity Framework Core stores can only be used " + "with the built-in OpenIddictAuthorization entity.", exception.Message); } [Fact] public void AddEntityFrameworkCoreStores_ThrowsAnExceptionForInvalidScopeEntity() { // Arrange var builder = new OpenIddictBuilder(new ServiceCollection()) { ScopeType = typeof(object) }; // Act and assert var exception = Assert.Throws(delegate { builder.AddEntityFrameworkCoreStores(); }); Assert.Equal("The Entity Framework Core stores can only be used " + "with the built-in OpenIddictScope entity.", exception.Message); } [Fact] public void AddEntityFrameworkCoreStores_ThrowsAnExceptionForInvalidTokenEntity() { // Arrange var builder = new OpenIddictBuilder(new ServiceCollection()) { TokenType = typeof(object) }; // Act and assert var exception = Assert.Throws(delegate { builder.AddEntityFrameworkCoreStores(); }); Assert.Equal("The Entity Framework Core stores can only be used " + "with the built-in OpenIddictToken entity.", exception.Message); } [Theory] [InlineData(typeof(OpenIddictApplicationStore))] [InlineData(typeof(OpenIddictAuthorizationStore))] [InlineData(typeof(OpenIddictScopeStore))] [InlineData(typeof(OpenIddictTokenStore))] public void AddEntityFrameworkCoreStores_RegistersEntityFrameworkStores(Type type) { // Arrange var services = new ServiceCollection(); var builder = new OpenIddictBuilder(services); // Act builder.AddEntityFrameworkCoreStores(); // Assert Assert.Contains(services, service => service.ImplementationType == type); } [Theory] [InlineData(typeof(OpenIddictApplicationStore, OpenIddictAuthorization, OpenIddictToken, DbContext, Guid>))] [InlineData(typeof(OpenIddictAuthorizationStore, OpenIddictApplication, OpenIddictToken, DbContext, Guid>))] [InlineData(typeof(OpenIddictScopeStore, DbContext, Guid>))] [InlineData(typeof(OpenIddictTokenStore, OpenIddictApplication, OpenIddictAuthorization, DbContext, Guid>))] public void AddEntityFrameworkCoreStores_KeyTypeIsInferredFromEntities(Type type) { // Arrange var services = new ServiceCollection(); var builder = new OpenIddictBuilder(services) { ApplicationType = typeof(OpenIddictApplication), AuthorizationType = typeof(OpenIddictAuthorization), ScopeType = typeof(OpenIddictScope), TokenType = typeof(OpenIddictToken) }; // Act builder.AddEntityFrameworkCoreStores(); // Assert Assert.Contains(services, service => service.ImplementationType == type); } [Theory] [InlineData(typeof(OpenIddictApplicationStore))] [InlineData(typeof(OpenIddictAuthorizationStore))] [InlineData(typeof(OpenIddictScopeStore))] [InlineData(typeof(OpenIddictTokenStore))] public void AddEntityFrameworkCoreStores_DefaultEntitiesCanBeReplaced(Type type) { // Arrange var services = new ServiceCollection(); var builder = new OpenIddictBuilder(services) { ApplicationType = typeof(CustomApplication), AuthorizationType = typeof(CustomAuthorization), ScopeType = typeof(CustomScope), TokenType = typeof(CustomToken) }; // Act builder.AddEntityFrameworkCoreStores(); // Assert Assert.Contains(services, service => service.ImplementationType == type); } public class CustomApplication : OpenIddictApplication { } public class CustomAuthorization : OpenIddictAuthorization { } public class CustomScope : OpenIddictScope { } public class CustomToken : OpenIddictToken { } } }