/* * 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.Data.Entity; using Microsoft.Extensions.DependencyInjection; 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(() => new OpenIddictEntityFrameworkBuilder(services)); Assert.Equal("services", exception.ParamName); } [Fact] public void ReplaceDefaultEntities_StoresAreCorrectlyReplaced() { // Arrange var services = CreateServices(); var builder = CreateBuilder(services); // Act builder.ReplaceDefaultEntities(); // Assert Assert.Contains(services, service => service.Lifetime == ServiceLifetime.Scoped && service.ServiceType == typeof(IOpenIddictApplicationStore) && service.ImplementationType == typeof(OpenIddictEntityFrameworkApplicationStore)); Assert.Contains(services, service => service.Lifetime == ServiceLifetime.Scoped && service.ServiceType == typeof(IOpenIddictAuthorizationStore) && service.ImplementationType == typeof(OpenIddictEntityFrameworkAuthorizationStore)); Assert.Contains(services, service => service.Lifetime == ServiceLifetime.Scoped && service.ServiceType == typeof(IOpenIddictScopeStore) && service.ImplementationType == typeof(OpenIddictEntityFrameworkScopeStore)); Assert.Contains(services, service => service.Lifetime == ServiceLifetime.Scoped && service.ServiceType == typeof(IOpenIddictTokenStore) && service.ImplementationType == typeof(OpenIddictEntityFrameworkTokenStore)); } [Fact] public void UseDbContext_OverridesContextType() { // Arrange var services = CreateServices(); var builder = CreateBuilder(services); // Act builder.UseDbContext(); // Assert Assert.Contains(services, service => service.Lifetime == ServiceLifetime.Scoped && service.ServiceType == typeof(IOpenIddictEntityFrameworkContext) && service.ImplementationType == typeof(OpenIddictEntityFrameworkContext)); } 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 : OpenIddictEntityFrameworkApplication { } public class CustomAuthorization : OpenIddictEntityFrameworkAuthorization { } public class CustomScope : OpenIddictEntityFrameworkScope { } public class CustomToken : OpenIddictEntityFrameworkToken { } public class CustomDbContext : DbContext { public CustomDbContext(string nameOrConnectionString) : base(nameOrConnectionString) { } } }