public void AddValidation_CanBeSafelyInvokedMultipleTimes()
        {
            // Arrange
            var services = new ServiceCollection();
            var builder  = new OpenIddictBuilder(services);

            // Act and assert
            builder.AddValidation();
            builder.AddValidation();
            builder.AddValidation();
        }
        public void AddValidation_ThrowsAnExceptionWhenSchemeIsAlreadyRegisteredWithDifferentHandlerType()
        {
            // Arrange
            var services = new ServiceCollection();

            services.AddAuthentication()
            .AddOAuthValidation();

            var builder = new OpenIddictBuilder(services);

            // Act
            builder.AddValidation();

            // Assert
            var provider  = services.BuildServiceProvider();
            var exception = Assert.Throws <InvalidOperationException>(delegate
            {
                return(provider.GetRequiredService <IOptions <AuthenticationOptions> >().Value);
            });

            Assert.Equal(new StringBuilder()
                         .AppendLine("The OpenIddict validation handler cannot be registered as an authentication scheme.")
                         .AppendLine("This may indicate that an instance of the OAuth validation or JWT bearer handler was registered.")
                         .Append("Make sure that neither 'services.AddAuthentication().AddOAuthValidation()' nor ")
                         .Append("'services.AddAuthentication().AddJwtBearer()' are called from 'ConfigureServices'.")
                         .ToString(), exception.Message);
        }
        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);
        }
        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 <>));
        }
        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);
        }
        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));
        }
        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));
        }