public async Task UdpateAsync_should_notify_provider_updated()
        {
            using var documentStore = new RavenDbTestDriverWrapper().GetDocumentStore();
            var services = new ServiceCollection()
                           .AddLogging();

            services.AddIdentity <IdentityUser, IdentityRole>()
            .AddRavenDbStores(p => documentStore);

            services.AddSingleton(p => documentStore)
            .AddAuthentication()
            .AddDynamic <SchemeDefinition>()
            .AddRavenDbStore()
            .AddGoogle();

            IServiceProvider provider = services.AddIdentityServer4AdminRavenDbkStores <IdentityUser, IdentityRole>(p => documentStore).BuildServiceProvider();

            using var scope = provider.CreateScope();
            provider        = scope.ServiceProvider;

            var manager            = provider.GetRequiredService <PersistentDynamicManager <SchemeDefinition> >();
            var serializer         = provider.GetRequiredService <IAuthenticationSchemeOptionsSerializer>();
            var mockProviderClient = new Mock <IProviderClient>();

            mockProviderClient.Setup(m => m.ProviderUpdatedAsync(It.IsAny <string>(), It.IsAny <CancellationToken>())).Verifiable();

            var id = Guid.NewGuid().ToString();
            await manager.AddAsync(new SchemeDefinition
            {
                DisplayName = Guid.NewGuid().ToString(),
                Options     = new GoogleOptions(),
                HandlerType = typeof(GoogleHandler),
                Scheme      = id
            });

            var sut = new ExternalProviderStore(manager,
                                                serializer,
                                                new ScopedAsynDocumentcSession(documentStore.OpenAsyncSession()),
                                                mockProviderClient.Object);

            var expected = Guid.NewGuid().ToString();
            await sut.UpdateAsync(new Entity.ExternalProvider
            {
                DisplayName           = expected,
                Id                    = id,
                SerializedHandlerType = serializer.SerializeType(typeof(GoogleHandler)),
                SerializedOptions     = serializer.SerializeOptions(new GoogleOptions(), typeof(GoogleOptions))
            } as object);

            mockProviderClient.Verify();
        }
        public async Task UdpateAsync_should_update_scheme()
        {
            using var documentStore = new RavenDbTestDriverWrapper().GetDocumentStore();
            var services = new ServiceCollection()
                           .AddLogging();

            services.AddIdentity <IdentityUser, IdentityRole>()
            .AddRavenDbStores(p => documentStore);

            services.AddSingleton(p => documentStore)
            .AddAuthentication()
            .AddDynamic <SchemeDefinition>()
            .AddRavenDbStore()
            .AddGoogle();

            IServiceProvider provider = services.AddIdentityServer4AdminRavenDbkStores <IdentityUser, IdentityRole>(p => documentStore).BuildServiceProvider();

            using var scope = provider.CreateScope();
            provider        = scope.ServiceProvider;

            var manager    = provider.GetRequiredService <PersistentDynamicManager <SchemeDefinition> >();
            var serializer = provider.GetRequiredService <IAuthenticationSchemeOptionsSerializer>();


            var id = Guid.NewGuid().ToString();
            await manager.AddAsync(new SchemeDefinition
            {
                DisplayName = Guid.NewGuid().ToString(),
                Options     = new GoogleOptions(),
                HandlerType = typeof(GoogleHandler),
                Scheme      = id
            });

            var sut = new ExternalProviderStore(manager,
                                                serializer,
                                                new ScopedAsynDocumentcSession(documentStore.OpenAsyncSession()),
                                                null);

            var expected = Guid.NewGuid().ToString();
            await sut.UpdateAsync(new Entity.ExternalProvider
            {
                DisplayName           = expected,
                Id                    = id,
                SerializedHandlerType = serializer.SerializeType(typeof(GoogleHandler)),
                SerializedOptions     = serializer.SerializeOptions(new GoogleOptions(), typeof(GoogleOptions))
            } as object);

            var scheme = await manager.FindBySchemeAsync(id);

            Assert.Equal(expected, scheme.DisplayName);
        }
        public async Task GetAsync_by_id_should_find_scheme()
        {
            using var documentStore = new RavenDbTestDriverWrapper().GetDocumentStore();
            var services = new ServiceCollection()
                           .AddLogging();

            services.AddIdentity <IdentityUser, IdentityRole>()
            .AddRavenDbStores(p => documentStore);

            services.AddSingleton(p => documentStore)
            .AddAuthentication()
            .AddDynamic <SchemeDefinition>()
            .AddRavenDbStore()
            .AddGoogle();

            IServiceProvider provider = services.AddIdentityServer4AdminRavenDbkStores <IdentityUser, IdentityRole>(p => documentStore).BuildServiceProvider();

            using var scope = provider.CreateScope();
            provider        = scope.ServiceProvider;

            var manager = provider.GetRequiredService <PersistentDynamicManager <SchemeDefinition> >();

            var id = Guid.NewGuid().ToString();
            await manager.AddAsync(new SchemeDefinition
            {
                DisplayName = Guid.NewGuid().ToString(),
                Options     = new GoogleOptions(),
                HandlerType = typeof(GoogleHandler),
                Scheme      = id
            });

            var sut = new ExternalProviderStore(manager,
                                                provider.GetRequiredService <IAuthenticationSchemeOptionsSerializer>(),
                                                new ScopedAsynDocumentcSession(documentStore.OpenAsyncSession()),
                                                provider.GetService <IProviderClient>());

            var result = await sut.GetAsync(id, null);

            Assert.NotNull(result);
        }
        public async Task GetAsync_by_id_should_expand_transforms()
        {
            using var documentStore = new RavenDbTestDriverWrapper().GetDocumentStore();
            var services = new ServiceCollection()
                           .AddLogging();

            services.AddIdentity <IdentityUser, IdentityRole>()
            .AddRavenDbStores(p => documentStore);

            services.AddSingleton(p => documentStore)
            .AddAuthentication()
            .AddDynamic <SchemeDefinition>()
            .AddRavenDbStore()
            .AddGoogle();

            IServiceProvider provider = services.AddIdentityServer4AdminRavenDbkStores <IdentityUser, IdentityRole>(p => documentStore).BuildServiceProvider();

            using var scope = provider.CreateScope();
            provider        = scope.ServiceProvider;

            var manager = provider.GetRequiredService <PersistentDynamicManager <SchemeDefinition> >();

            var id = Guid.NewGuid().ToString();
            await manager.AddAsync(new SchemeDefinition
            {
                DisplayName = Guid.NewGuid().ToString(),
                Options     = new GoogleOptions(),
                HandlerType = typeof(GoogleHandler),
                Scheme      = id
            });

            using var session = documentStore.OpenAsyncSession();
            var transformId = Guid.NewGuid().ToString();
            await session.StoreAsync(new Entity.ExternalClaimTransformation
            {
                Scheme        = id,
                Id            = transformId,
                FromClaimType = "test",
                ToClaimType   = "test"
            }, $"{nameof(Entity.ExternalClaimTransformation).ToLowerInvariant()}/{transformId}").ConfigureAwait(false);

            var saved = await session.LoadAsync <SchemeDefinition>($"{nameof(SchemeDefinition).ToLowerInvariant()}/{id}").ConfigureAwait(false);

            saved.ClaimTransformations = new List <Entity.ExternalClaimTransformation>
            {
                new Entity.ExternalClaimTransformation
                {
                    Id = $"{nameof(Entity.ExternalClaimTransformation).ToLowerInvariant()}/{transformId}"
                }
            };
            await session.SaveChangesAsync().ConfigureAwait(false);

            var sut = new ExternalProviderStore(manager,
                                                provider.GetRequiredService <IAuthenticationSchemeOptionsSerializer>(),
                                                new ScopedAsynDocumentcSession(documentStore.OpenAsyncSession()),
                                                provider.GetService <IProviderClient>());

            var result = await sut.GetAsync(id, new GetRequest
            {
                Expand = nameof(SchemeDefinition.ClaimTransformations)
            });

            Assert.NotNull(result);
            Assert.Single(result.ClaimTransformations);
            Assert.Equal("test", result.ClaimTransformations.First().FromClaimType);
        }