/* * 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 Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using MongoDB.Driver; using Moq; 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(() => new OpenIddictMongoDbBuilder(services)); Assert.Equal("services", exception.ParamName); } [Fact] public void ReplaceDefaultApplicationEntity_StoreIsCorrectlyReplaced() { // Arrange var services = CreateServices(); var builder = CreateBuilder(services); // Act builder.ReplaceDefaultApplicationEntity(); // Assert Assert.Contains(services, service => service.Lifetime == ServiceLifetime.Scoped && service.ServiceType == typeof(IOpenIddictApplicationStore) && service.ImplementationType == typeof(OpenIddictMongoDbApplicationStore)); } [Fact] public void ReplaceDefaultAuthorizationEntity_StoreIsCorrectlyReplaced() { // Arrange var services = CreateServices(); var builder = CreateBuilder(services); // Act builder.ReplaceDefaultAuthorizationEntity(); // Assert Assert.Contains(services, service => service.Lifetime == ServiceLifetime.Scoped && service.ServiceType == typeof(IOpenIddictAuthorizationStore) && service.ImplementationType == typeof(OpenIddictMongoDbAuthorizationStore)); } [Fact] public void ReplaceDefaultScopeEntity_StoreIsCorrectlyReplaced() { // Arrange var services = CreateServices(); var builder = CreateBuilder(services); // Act builder.ReplaceDefaultScopeEntity(); // Assert Assert.Contains(services, service => service.Lifetime == ServiceLifetime.Scoped && service.ServiceType == typeof(IOpenIddictScopeStore) && service.ImplementationType == typeof(OpenIddictMongoDbScopeStore)); } [Fact] public void ReplaceDefaultTokenEntity_StoreIsCorrectlyReplaced() { // Arrange var services = CreateServices(); var builder = CreateBuilder(services); // Act builder.ReplaceDefaultTokenEntity(); // Assert Assert.Contains(services, service => service.Lifetime == ServiceLifetime.Scoped && service.ServiceType == typeof(IOpenIddictTokenStore) && service.ImplementationType == typeof(OpenIddictMongoDbTokenStore)); } [Theory] [InlineData(null)] [InlineData("")] public void SetApplicationsCollectionName_ThrowsAnExceptionForNullOrEmptyCollectionName(string? name) { // Arrange var services = CreateServices(); var builder = CreateBuilder(services); // Act and assert var exception = Assert.ThrowsAny(() => builder.SetApplicationsCollectionName(name!)); Assert.Equal("name", exception.ParamName); } [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>().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.ThrowsAny(() => builder.SetAuthorizationsCollectionName(name!)); Assert.Equal("name", exception.ParamName); } [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>().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.ThrowsAny(() => builder.SetScopesCollectionName(name!)); Assert.Equal("name", exception.ParamName); } [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>().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.ThrowsAny(() => builder.SetTokensCollectionName(name!)); Assert.Equal("name", exception.ParamName); } [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>().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(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(); // Act builder.UseDatabase(database); // Assert var provider = services.BuildServiceProvider(); var options = provider.GetRequiredService>().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 : OpenIddictMongoDbApplication { } public class CustomAuthorization : OpenIddictMongoDbAuthorization { } public class CustomScope : OpenIddictMongoDbScope { } public class CustomToken : OpenIddictMongoDbToken { } }