Esempio n. 1
0
        public static TestServer CreateTestServer(
            Action <IServiceCollection> configureServices = null,
            IEnumerable <KeyValuePair <string, string> > configurationOverrides = null)
        {
            var webHostBuilder = new WebHostBuilder()
                                 .UseEnvironment("Development")
                                 .UseSerilog((hostingContext, loggerConfiguration) => loggerConfiguration
                                             .ReadFrom.Configuration(hostingContext.Configuration))
                                 .ConfigureServices((context, services) =>
            {
                configureServices?.Invoke(services);

                var configurationManager = new ConfigurationManager();
#if DUENDE
                configurationManager.AddJsonFile(Path.Combine(Environment.CurrentDirectory, @"..\..\..\..\..\..\src\Aguacongas.TheIdServer.Duende\appsettings.json"));
#else
                configurationManager.AddJsonFile(Path.Combine(Environment.CurrentDirectory, @"..\..\..\..\..\..\src\Aguacongas.TheIdServer.IS4\appsettings.json"));
#endif
                configurationManager.AddJsonFile(Path.Combine(Environment.CurrentDirectory, @"appsettings.Test.json"), true);
                if (configurationOverrides != null)
                {
                    configurationManager.AddInMemoryCollection(configurationOverrides);
                }

                var isProxy = configurationManager.GetValue <bool>("Proxy");
                var dbType  = configurationManager.GetValue <DbTypes>("DbType");

                services.AddTheIdServer(configurationManager);
                services.AddSingleton <TestUserService>()
                .AddMvc().AddApplicationPart(typeof(Config).Assembly);
                configureServices?.Invoke(services);
            })
                                 .Configure((context, builder) =>
            {
                builder.Use(async(context, next) =>
                {
                    var testService = context.RequestServices.GetRequiredService <TestUserService>();
                    context.User    = testService.User;
                    await next();
                });

                builder.UseTheIdServer(context.HostingEnvironment, context.Configuration);
            });

            var testServer = new TestServer(webHostBuilder);

            return(testServer);
        }
Esempio n. 2
0
        public void ConfigureService_should_configure_mongodb_services()
        {
            var sessionMock  = new Mock <IAsyncDocumentSession>();
            var advancedMock = new Mock <IAsyncAdvancedSessionOperations>();

            sessionMock.SetupGet(m => m.Advanced).Returns(advancedMock.Object);
            using var sut = new HostBuilder()
                            .ConfigureServices((context, services) =>
            {
                var configurationManager = new ConfigurationManager();
                configurationManager.AddJsonFile(Path.Combine(Environment.CurrentDirectory, @"appsettings.json"));
                configurationManager.AddJsonFile(Path.Combine(Environment.CurrentDirectory, @"appsettings.Test.json"), true);
                configurationManager.AddInMemoryCollection(new Dictionary <string, string>
                {
                    ["DbType"] = DbTypes.MongoDb.ToString(),
                    ["ConnectionStrings:DefaultConnection"] = "mongodb://localhost/test",
                    ["IdentityServer:Key:StorageKind"]      = StorageKind.MongoDb.ToString(),
                    ["DataProtectionOptions:StorageKind"]   = StorageKind.MongoDb.ToString(),
                    ["Seed"] = "false"
                });
                services.AddTheIdServer(configurationManager);
            }).Build();

            var provider = sut.Services;

            Assert.NotNull(provider.GetService <IAdminStore <ApiClaim> >());
            var configureRotationOptions = provider.GetService <IConfigureOptions <KeyRotationOptions> >();
            var rotationOptions          = new KeyRotationOptions();

            configureRotationOptions?.Configure(rotationOptions);
            Assert.IsType <MongoDb.MongoDbXmlRepository <MongoDb.KeyRotationKey> >(rotationOptions.XmlRepository);
            var configureManagementOptions = provider.GetService <IConfigureOptions <KeyManagementOptions> >();
            var managementOptions          = new KeyRotationOptions();

            configureManagementOptions?.Configure(managementOptions);
            Assert.IsType <MongoDb.MongoDbXmlRepository <MongoDb.DataProtectionKey> >(managementOptions.XmlRepository);
        }
Esempio n. 3
0
        public async Task ConfigureService_should_configure_proxy_services(bool disableStrictSll, string path, string otk, string token)
        {
            var sessionMock  = new Mock <IAsyncDocumentSession>();
            var advancedMock = new Mock <IAsyncAdvancedSessionOperations>();

            sessionMock.SetupGet(m => m.Advanced).Returns(advancedMock.Object);
            using var sut = new HostBuilder()
                            .ConfigureServices((context, services) =>
            {
                var configurationMAnager = new ConfigurationManager();
                configurationMAnager.AddJsonFile(Path.Combine(Environment.CurrentDirectory, @"appsettings.json"));
                configurationMAnager.AddJsonFile(Path.Combine(Environment.CurrentDirectory, @"appsettings.Test.json"), true);
                configurationMAnager.AddInMemoryCollection(new Dictionary <string, string>
                {
                    ["Proxy"]            = "true",
                    ["DisableStrictSsl"] = $"{disableStrictSll}",
                    ["Seed"]             = "false"
                });
                services.AddTheIdServer(configurationMAnager);
            }).Build();

            var provider = sut.Services;

            Assert.NotNull(provider.GetService <IProfileService>());
            Assert.NotNull(provider.GetService <IAdminStore <Client> >());
            var jwtBearerHandler = provider.GetService <JwtBearerHandler>();

            Assert.NotNull(jwtBearerHandler);
            var mockHeader      = new Mock <IHeaderDictionary>();
            var queryCollection = new QueryCollection(new Dictionary <string, StringValues>
            {
                ["otk"] = otk
            });
            var mockOneTimeTokenRetriver = new Mock <IRetrieveOneTimeToken>();

            mockOneTimeTokenRetriver.Setup(m => m.GetOneTimeToken(It.IsAny <string>())).Returns(token);
            var requestServices = new ServiceCollection()
                                  .AddTransient(p => mockOneTimeTokenRetriver.Object)
                                  .BuildServiceProvider();

            var mockHttRequest = new Mock <HttpRequest>();

            mockHttRequest.SetupGet(m => m.Headers).Returns(mockHeader.Object);
            mockHttRequest.SetupGet(m => m.Query).Returns(queryCollection);
            mockHttRequest.SetupGet(m => m.Path).Returns(path);
            var mockHttpContext = new Mock <HttpContext>();

            mockHttRequest.SetupGet(m => m.HttpContext).Returns(mockHttpContext.Object);
            mockHttpContext.SetupGet(m => m.Request).Returns(mockHttRequest.Object);
            mockHttpContext.SetupGet(m => m.RequestServices).Returns(requestServices);
            if (jwtBearerHandler != null)
            {
                await jwtBearerHandler.InitializeAsync(new AuthenticationScheme("Bearer", null, typeof(JwtBearerHandler)), mockHttpContext.Object).ConfigureAwait(false);

                await jwtBearerHandler.AuthenticateAsync().ConfigureAwait(false);
            }

            var oauthIntrospectionHandler = provider.GetService <OAuth2IntrospectionHandler>();

            Assert.NotNull(oauthIntrospectionHandler);
            if (oauthIntrospectionHandler != null)
            {
                await oauthIntrospectionHandler.InitializeAsync(new AuthenticationScheme("introspection", null, typeof(OAuth2IntrospectionHandler)), mockHttpContext.Object).ConfigureAwait(false);

                await oauthIntrospectionHandler.AuthenticateAsync().ConfigureAwait(false);
            }
        }