public void PostConfigure_ThrowsAnExceptionWhenDefaultSchemesPointToServerHandler(string[] schemes)
        {
            // Arrange
            var options = new AuthenticationOptions
            {
                DefaultAuthenticateScheme = schemes[0],
                DefaultChallengeScheme    = schemes[1],
                DefaultForbidScheme       = schemes[2],
                DefaultScheme             = schemes[3],
                DefaultSignInScheme       = schemes[4],
                DefaultSignOutScheme      = schemes[5]
            };

            options.AddScheme <OpenIddictServerHandler>(OpenIddictServerDefaults.AuthenticationScheme, displayName: null);

            var configuration = new OpenIddictServerConfiguration(
                Mock.Of <IDistributedCache>(),
                Mock.Of <IDataProtectionProvider>());

            // Act and assert
            var exception = Assert.Throws <InvalidOperationException>(() => configuration.PostConfigure(Options.DefaultName, options));

            // Assert
            Assert.Equal(new StringBuilder()
                         .AppendLine("The OpenIddict server handler cannot be used as the default scheme handler.")
                         .Append("Make sure that neither DefaultAuthenticateScheme, DefaultChallengeScheme, ")
                         .Append("DefaultForbidScheme, DefaultSignInScheme, DefaultSignOutScheme nor DefaultScheme ")
                         .Append("point to an instance of the OpenIddict server handler.")
                         .ToString(), exception.Message);
        }
        public void Configure_ThrowsAnExceptionForNullOptions()
        {
            // Arrange
            var configuration = new OpenIddictServerConfiguration(
                Mock.Of <IDistributedCache>(),
                Mock.Of <IDataProtectionProvider>());

            // Act and assert
            var exception = Assert.Throws <ArgumentNullException>(() => configuration.Configure(null));

            Assert.Equal("options", exception.ParamName);
        }
        public void Configure_ThrowsAnExceptionWhenSchemeIsAlreadyRegisteredWithDifferentHandlerType()
        {
            // Arrange
            var options = new AuthenticationOptions();

            options.AddScheme(OpenIddictServerDefaults.AuthenticationScheme, builder =>
            {
                builder.HandlerType = typeof(OpenIdConnectServerHandler);
            });

            var configuration = new OpenIddictServerConfiguration(
                Mock.Of <IDistributedCache>(),
                Mock.Of <IDataProtectionProvider>());

            // Act and assert
            var exception = Assert.Throws <InvalidOperationException>(() => configuration.Configure(options));

            Assert.Equal(new StringBuilder()
                         .AppendLine("The OpenIddict server handler cannot be registered as an authentication scheme.")
                         .AppendLine("This may indicate that an instance of the OpenID Connect server was registered.")
                         .Append("Make sure that 'services.AddAuthentication().AddOpenIdConnectServer()' is not used.")
                         .ToString(), exception.Message);
        }