public LocalConfigServerTests()
        {
            var configurationCollection = new ConfigurationRegistry();

            configurationCollection.AddRegistration(ConfigurationRegistration.Build <SimpleConfig>());
            repository = new InMemoryRepository();
        }
Example #2
0
        public IConfigurationHandle RegisterConfiguration(ConfigurationRegistration config)
        {
            var id = Guid.NewGuid();

            _configurations[id] = config;
            RebuildStores();
            return(new ConfigurationHandle(id, this));
        }
        private async Task <object> GetConfigInternal(ConfigurationRegistration registration, string clientId)
        {
            var result = await GetConfig(registration.ConfigurationName, clientId);

            return(JsonConvert.DeserializeObject(result, registration.ConfigType, new JsonSerializerSettings {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            }));
        }
        public void Given_IdSvr4Specification_When_ConfigureOptions_Then_InformationAdded()
        {
            // Arrange
            ConfigurationRegistration registration = null;
            var store = A.Fake <IStore>();

            A.CallTo(() => store.RegisterConfiguration(A <ConfigurationRegistration> ._)).Invokes((ConfigurationRegistration c) => registration = c);

            var spec         = (IConfigurationSpecification) new IdSvr4Specification(store);
            var configurator = (IIdSvr4Configurator)spec;

            var client1 = new Client {
                ClientId = "clientId1"
            };
            var client2 = new Client {
                ClientId = "clientId2"
            };

            var idResource1 = new IdentityResource {
                Name = "idResource1"
            };
            var idResource2 = new IdentityResource {
                Name = "idResource2"
            };

            var apiResource1 = new ApiResource("apiResource1");
            var apiResource2 = new ApiResource("apiResource2");

            // Act
            configurator.Client(client1);
            configurator.Client(client2);
            configurator.IdentityResource(idResource1);
            configurator.IdentityResource(idResource2);
            configurator.ApiResource(apiResource1);
            configurator.ApiResource(apiResource2);

            var handle = spec.ApplyAsync();

            // Assert
            Assert.That(handle, Is.Not.Null);
            Assert.That(registration, Is.Not.Null);

            // Client
            Assert.That(registration.Clients.Count, Is.EqualTo(2));
            Assert.That(registration.Clients, Does.Contain(client1));
            Assert.That(registration.Clients, Does.Contain(client2));

            // Id Resource
            Assert.That(registration.IdentityResources.Count, Is.EqualTo(2));
            Assert.That(registration.IdentityResources, Does.Contain(idResource1));
            Assert.That(registration.IdentityResources, Does.Contain(idResource2));

            // API Resource 1
            Assert.That(registration.ApiResources.Count, Is.EqualTo(2));
            Assert.That(registration.ApiResources, Does.Contain(apiResource1));
            Assert.That(registration.ApiResources, Does.Contain(apiResource2));
        }
Example #5
0
 public ConfigServerClientTest()
 {
     collection = new ConfigurationRegistry();
     collection.AddRegistration(ConfigurationRegistration.Build <SimpleConfig>());
     options                         = new ConfigServerClientOptions();
     options.ClientId                = "1234-5678-1234";
     options.ConfigServer            = "https://test.com/Config";
     options.CacheOptions.IsDisabled = true;
     clientWrapper                   = new Mock <IHttpClientWrapper>();
     cache  = new Mock <IMemoryCache>();
     target = new ConfigServerClient(clientWrapper.Object, cache.Object, collection, options);
 }
Example #6
0
 public ConfigServerClientTest()
 {
     collection = new ConfigurationRegistry();
     collection.AddRegistration(ConfigurationRegistration.Build <SimpleConfig>());
     collection.AddRegistration(ConfigurationRegistration.Build <SampleConfig>(configRegisration));
     options = new ConfigServerClientOptions
     {
         ConfigServer = "https://test.com/Config"
     };
     options.CacheOptions.IsDisabled = true;
     clientWrapper = new Mock <IHttpClientWrapper>();
     target        = new ConfigServerClient(clientWrapper.Object, new NoCachingStrategy(), new SingleClientIdProvider(clientId), collection, options);
 }
Example #7
0
        public async Task Given_RegisteredConfiguration_When_UnregisterConfiguration_Then_ConfigurationRemoved()
        {
            // Arrange
            const string enabledClientId = "Client1";
            const string apiResource     = "API";
            const string apiScope        = "API_Scope";
            const string idResource      = "ID";

            var config = new ConfigurationRegistration(
                new List <Client>
            {
                new Client {
                    ClientId = enabledClientId, Enabled = true
                }
            },
                new List <IdentityResource>
            {
                new IdentityResource {
                    Name = idResource
                }
            },
                new List <ApiResource>
            {
                new ApiResource(apiResource)
                {
                    Scopes = new List <Scope> {
                        new Scope(apiScope)
                    }
                }
            });

            var store        = new Store();
            var configHandle = store.RegisterConfiguration(config);

            var clientStore   = (IClientStore)store;
            var resourceStore = (IResourceStore)store;

            Assume.That((await clientStore.FindClientByIdAsync(enabledClientId)) != null);
            Assume.That((await resourceStore.FindApiResourceAsync(apiResource)) != null);
            Assume.That((await resourceStore.FindApiResourcesByScopeAsync(new[] { apiScope })).Any());
            Assume.That((await resourceStore.FindIdentityResourcesByScopeAsync(new[] { idResource })).Any());

            // Act
            Assert.DoesNotThrowAsync(() => configHandle.DisposeAsync().AsTask());

            // Assert
            Assert.That(await clientStore.FindClientByIdAsync(enabledClientId), Is.Null);
            Assert.That(await resourceStore.FindApiResourceAsync(apiResource), Is.Null);
            Assert.That(await resourceStore.FindApiResourcesByScopeAsync(new[] { apiScope }), Is.Empty);
            Assert.That(await resourceStore.FindIdentityResourcesByScopeAsync(new[] { idResource }), Is.Empty);
        }
Example #8
0
        public LocalConfigServerTests()
        {
            var configurationCollection = new ConfigurationRegistry();

            configurationCollection.AddRegistration(ConfigurationRegistration.Build <SimpleConfig>());
            var repo = new InMemoryRepository();

            repository    = repo;
            clientservice = new Mock <IConfigurationClientService>();
            clientservice.Setup(service => service.GetClientOrDefault(configIdentity.Client.ClientId))
            .ReturnsAsync(configIdentity.Client);
            registry = new Mock <IConfigurationModelRegistry>();
            registry.Setup(s => s.GetVersion())
            .Returns(() => configIdentity.ServerVersion);
            resourceStore = new Mock <IResourceStore>();
        }
        private static ConfigInstance BuildInstance(ConfigurationIdentity targetConfigurationIdentity, SnapshotTextEntry entry, ConfigurationRegistration configInfo)
        {
            if (configInfo.IsCollection)
            {
                var collectionType        = typeof(IEnumerable <>).MakeGenericType(configInfo.ConfigType);
                var newCollectionInstance = ConfigFactory.CreateGenericCollectionInstance(configInfo.ConfigType, targetConfigurationIdentity);
                newCollectionInstance.SetConfiguration(ConfigStorageObjectHelper.ParseConfigurationStoredObject(entry.ConfigurationJson, collectionType));
                return(newCollectionInstance);
            }
            var newInstance = ConfigFactory.CreateGenericInstance(configInfo.ConfigType, targetConfigurationIdentity);

            newInstance.SetConfiguration(ConfigStorageObjectHelper.ParseConfigurationStoredObject(entry.ConfigurationJson, configInfo.ConfigType));
            return(newInstance);
        }
Example #10
0
        public async Task Given_Configuration_When_QueryClientAndResourceStores_Then_ReceiveExpectedResults()
        {
            // Arrange
            const string enabledClientId  = "Client1";
            const string disabledClientId = "Client2";
            const string fakeClientId     = "FakeClient";
            const string apiResource      = "API";
            const string fakeApiResource  = "Fake_API";
            const string apiScope         = "API_Scope";
            const string fakeApiScope     = "Fake_API_Scope";
            const string idResource       = "ID";
            const string fakeIdResource   = "FakeID";

            var config = new ConfigurationRegistration(
                new List <Client>
            {
                new Client {
                    ClientId = enabledClientId, Enabled = true
                },
                new Client {
                    ClientId = disabledClientId, Enabled = false
                }
            },
                new List <IdentityResource>
            {
                new IdentityResource {
                    Name = idResource
                }
            },
                new List <ApiResource>
            {
                new ApiResource(apiResource)
                {
                    Scopes = new List <Scope>
                    {
                        new Scope(apiScope)
                    }
                }
            });

            var store = new Store();

            store.RegisterConfiguration(config);

            var clientStore   = (IClientStore)store;
            var resourceStore = (IResourceStore)store;

            // Act & Assert
            Assert.That(await clientStore.FindClientByIdAsync(enabledClientId), Is.Not.Null);
            Assert.That(await clientStore.FindClientByIdAsync(disabledClientId), Is.Not.Null);
            Assert.That(await clientStore.FindClientByIdAsync(fakeClientId), Is.Null);
            Assert.That(await clientStore.FindEnabledClientByIdAsync(enabledClientId), Is.Not.Null);
            Assert.That(await clientStore.FindEnabledClientByIdAsync(disabledClientId), Is.Null);
            Assert.That(await clientStore.FindEnabledClientByIdAsync(fakeClientId), Is.Null);

            Assert.That(await resourceStore.FindApiResourceAsync(apiResource), Is.Not.Null);
            Assert.That(await resourceStore.FindApiResourceAsync(fakeApiResource), Is.Null);
            Assert.That(await resourceStore.FindApiResourcesByScopeAsync(new[] { apiScope }), Is.Not.Empty);
            Assert.That(await resourceStore.FindApiResourcesByScopeAsync(new[] { fakeApiScope }), Is.Empty);
            Assert.That(await resourceStore.FindIdentityResourcesByScopeAsync(new[] { idResource }), Is.Not.Empty);
            Assert.That(await resourceStore.FindIdentityResourcesByScopeAsync(new[] { fakeIdResource }), Is.Empty);

            var resources = await resourceStore.GetAllResourcesAsync();

            Assert.That(resources, Is.Not.Null);
            Assert.That(resources.IdentityResources, Is.Not.Empty);
            Assert.That(resources.ApiResources, Is.Not.Empty);
        }
Example #11
0
 /// <summary>
 /// Adds ConfigInstance type to ConfigServer client registry
 /// </summary>
 /// <typeparam name="TConfig">ConfigInstance type to be added to registry</typeparam>
 /// <param name="source">Current ConfigServer client builder</param>
 /// <param name="name">Name of config on config server</param>
 /// <returns>ConfigServer client builder for further configuration</returns>
 public static ConfigServerClientBuilder WithCollectionConfig <TConfig>(this ConfigServerClientBuilder source, string name) where TConfig : class, new()
 {
     source.ConfigurationRegistry.AddRegistration(ConfigurationRegistration.BuildCollection <TConfig>(name));
     return(source);
 }
        private async Task <IEnumerable <TConfig> > GetCollectionConfigInternal <TConfig>(ConfigurationRegistration registration, string clientId)
        {
            var result = await GetConfig(registration.ConfigurationName, clientId);

            return((IEnumerable <TConfig>)JsonConvert.DeserializeObject(result, typeof(List <TConfig>), new JsonSerializerSettings {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            }));
        }