Exemplo n.º 1
0
        public async Task add_response_badrequest_if_apikey_with_the_same_name_already_exist()
        {
            var apiKey1 = Builders.ApiKey()
                          .WithName("apikey#1")
                          .Withkey("key-1")
                          .Build();

            var apiKey2 = Builders.ApiKey()
                          .WithName("apikey#2")
                          .Withkey("key-2")
                          .Build();

            await _fixture.Given
            .AddApiKey(apiKey1, apiKey2);

            var addApiKeyRequest = new AddApiKeyRequest()
            {
                Name        = "apikey#1",
                Description = "description",
            };

            var response = await _fixture.TestServer
                           .CreateRequest(ApiDefinitions.V1.ApiKeys.Add())
                           .WithIdentity(Builders.Identity().WithDefaultClaims().Build())
                           .PostAsJsonAsync(addApiKeyRequest);

            response.StatusCode
            .Should()
            .Be(StatusCodes.Status400BadRequest);
        }
        public async Task get_response_apikey_when_exist()
        {
            var apiKey = Builders.ApiKey()
                         .WithName("apikey#1")
                         .Withkey("key-1")
                         .Build();

            await _fixture.Given
            .AddApiKey(apiKey);

            var response = await _fixture.TestServer
                           .CreateRequest(ApiDefinitions.V1.ApiKeys.Get(apiKey.Id))
                           .WithIdentity(Builders.Identity().WithDefaultClaims().Build())
                           .GetAsync();

            var content = await response.Content
                          .ReadAs <DetailsApiKeyResponse>();

            content.Name
            .Should()
            .BeEquivalentTo("apikey#1");

            response.StatusCode
            .Should()
            .Be(StatusCodes.Status200OK);
        }
Exemplo n.º 3
0
        public async Task get_response_forbidden_when_user_is_not_authorized()
        {
            var permission = Builders.Permission()
                .WithAllPrivilegesForDefaultIdentity()
                .WithManagementPermission(false)
                .Build();

            await _fixture.Given
                .AddPermission(permission);

            var apiKey = Builders.ApiKey()
               .WithName("apikey#1")
               .Withkey("key-1")
               .Build();

            await _fixture.Given
                .AddApiKey(apiKey);

            var response = await _fixture.TestServer
                .CreateRequest(ApiDefinitions.V1.ApiKeys.Get(apiKey.Id))
                .WithIdentity(Builders.Identity().WithDefaultClaims().Build())
                .GetAsync();

            response.StatusCode
                .Should()
                .Be(StatusCodes.Status403Forbidden);
        }
Exemplo n.º 4
0
        public async Task get_response_forbidden_when_user_is_not_authorized()
        {
            var permission = Builders.Permission()
                             .WithReaderPermission()
                             .Build();

            await _fixture.Given
            .AddPermission(permission);

            var apiKey = Builders.ApiKey()
                         .WithName("fooname")
                         .Withkey("barkey")
                         .Build();

            await _fixture.Given
            .AddApiKey(apiKey);

            var response = await _fixture.TestServer
                           .CreateRequest(ApiDefinitions.V3.ApiKeys.Get(apiKey.Name))
                           .WithIdentity(Builders.Identity().WithDefaultClaims().Build())
                           .GetAsync();

            response.StatusCode
            .Should()
            .Be(StatusCodes.Status403Forbidden);
        }
Exemplo n.º 5
0
        public async Task list_response_ok_and_use_specific_skip_take_values()
        {
            var permission = Builders.Permission()
               .WithAllPrivilegesForDefaultIdentity()
               .Build();

            await _fixture.Given
                .AddPermission(permission);

            var apiKey1 = Builders.ApiKey()
               .WithName("apikey#1")
               .Withkey("key-1")
               .WithValidTo(DateTime.UtcNow.AddYears(1))
               .Build();

            var apiKey2 = Builders.ApiKey()
              .WithName("apikey#2")
              .Withkey("key-2")
              .WithValidTo(DateTime.UtcNow.AddYears(1))
              .Build();

            await _fixture.Given
                .AddApiKey(apiKey1, apiKey2);

            var response = await _fixture.TestServer
                  .CreateRequest(ApiDefinitions.V1.ApiKeys.List(pageIndex: 1, pageCount: 1))
                  .WithIdentity(Builders.Identity().WithDefaultClaims().Build())
                  .GetAsync();

            response.StatusCode
                .Should()
                .Be(StatusCodes.Status200OK);

            response.StatusCode
                .Should()
                .Be(StatusCodes.Status200OK);

            var content = await response.Content
                .ReadAs<ListApiKeyResponse>();

            content.Total
                .Should().Be(2);

            content.Count
                .Should().Be(1);

            content.PageIndex
                .Should().Be(1);

            content.Result
                .Single().Name
                .Should().BeEquivalentTo("apikey#2");
        }
Exemplo n.º 6
0
        public async Task list_response_ok_and_use_default_skip_take_values()
        {
            var permission = Builders.Permission()
                             .WithManagementPermission()
                             .Build();

            await _fixture.Given
            .AddPermission(permission);

            var apiKey1 = Builders.ApiKey()
                          .WithName("apikey#1")
                          .Withkey("key-1")
                          .WithValidTo(DateTime.UtcNow.AddYears(1))
                          .Build();

            var apiKey2 = Builders.ApiKey()
                          .WithName("apikey#2")
                          .Withkey("key-2")
                          .WithValidTo(DateTime.UtcNow.AddYears(1))
                          .Build();

            await _fixture.Given
            .AddApiKey(apiKey1, apiKey2);

            var response = await _fixture.TestServer
                           .CreateRequest(ApiDefinitions.V3.ApiKeys.List())
                           .WithIdentity(Builders.Identity().WithDefaultClaims().Build())
                           .GetAsync();

            response.StatusCode
            .Should()
            .Be(StatusCodes.Status200OK);

            var content = await response.Content
                          .ReadAs <PaginatedResult <ListApiKeyResponseDetail> >();

            content.Total
            .Should().Be(2);

            content.Count
            .Should().Be(2);

            content.PageIndex
            .Should().Be(0);

            content.Items
            .First().Name
            .Should().BeEquivalentTo("apikey#1");

            content.Items
            .Last().Name
            .Should().BeEquivalentTo("apikey#2");
        }
Exemplo n.º 7
0
        public async Task list_response_ok_when_no_page_data()
        {
            var permission = Builders.Permission()
                             .WithManagementPermission()
                             .Build();

            await _fixture.Given
            .AddPermission(permission);

            var apiKey1 = Builders.ApiKey()
                          .WithName("apikey#1")
                          .Withkey("key-1")
                          .WithValidTo(DateTime.UtcNow.AddYears(1))
                          .Build();

            var apiKey2 = Builders.ApiKey()
                          .WithName("apikey#2")
                          .Withkey("key-2")
                          .WithValidTo(DateTime.UtcNow.AddYears(1))
                          .Build();

            await _fixture.Given
            .AddApiKey(apiKey1, apiKey2);

            var response = await _fixture.TestServer
                           .CreateRequest(ApiDefinitions.V5.ApiKeys.List(pageIndex: 10, pageCount: 10))
                           .WithIdentity(Builders.Identity().WithDefaultClaims().Build())
                           .GetAsync();

            response.StatusCode
            .Should()
            .Be(HttpStatusCode.OK);

            var content = await response.Content
                          .ReadAs <PaginatedResult <ListApiKeyResponseDetail> >();

            content.Total
            .Should().Be(2);

            content.Count
            .Should().Be(0);

            content.PageIndex
            .Should().Be(10);

            content.Items
            .Count
            .Should().Be(0);
        }
Exemplo n.º 8
0
        public async Task list_response_ok_and_use_default_skip_take_values()
        {
            var apiKey1 = Builders.ApiKey()
                          .WithName("apikey#1")
                          .Withkey("key-1")
                          .Build();

            var apiKey2 = Builders.ApiKey()
                          .WithName("apikey#2")
                          .Withkey("key-2")
                          .Build();

            await _fixture.Given
            .AddApiKey(apiKey1, apiKey2);

            var response = await _fixture.TestServer
                           .CreateRequest(ApiDefinitions.V1.ApiKeys.List())
                           .WithIdentity(Builders.Identity().WithDefaultClaims().Build())
                           .GetAsync();

            response.StatusCode
            .Should()
            .Be(StatusCodes.Status200OK);

            var content = await response.Content
                          .ReadAs <ListApiKeyResponse>();

            content.Total
            .Should().Be(2);

            content.Count
            .Should().Be(2);

            content.PageIndex
            .Should().Be(0);

            content.Result
            .First().Name
            .Should().BeEquivalentTo("apikey#1");

            content.Result
            .Last().Name
            .Should().BeEquivalentTo("apikey#2");
        }
Exemplo n.º 9
0
        public async Task delete_response_no_content_when_apikey_is_removed()
        {
            var apiKey = Builders.ApiKey()
                         .WithName("apikey#1")
                         .Withkey("key-1")
                         .Build();

            await _fixture.Given
            .AddApiKey(apiKey);

            var response = await _fixture.TestServer
                           .CreateRequest(ApiDefinitions.V1.ApiKeys.Delete(apiKey.Id))
                           .WithIdentity(Builders.Identity().WithDefaultClaims().Build())
                           .DeleteAsync();

            response.StatusCode
            .Should()
            .Be(StatusCodes.Status204NoContent);
        }
Exemplo n.º 10
0
        public async Task get_response_apikey_when_exist()
        {
            var apiKeyValue = "barkey";
            var apiKeyName  = "fooname";

            var requesterPermission = Builders.Permission()
                                      .WithManagementPermission()
                                      .Build();

            var apiKeyPermission = Builders.Permission()
                                   .WithManagementPermission()
                                   .WithNameIdentifier(apiKeyValue)
                                   .Build();

            await _fixture.Given
            .AddPermission(requesterPermission, apiKeyPermission);

            var apiKey = Builders.ApiKey()
                         .WithName(apiKeyName)
                         .Withkey(apiKeyValue)
                         .WithValidTo(DateTime.UtcNow.AddYears(1))
                         .Build();

            await _fixture.Given
            .AddApiKey(apiKey);

            var response = await _fixture.TestServer
                           .CreateRequest(ApiDefinitions.V3.ApiKeys.Get(apiKey.Name))
                           .WithIdentity(Builders.Identity().WithDefaultClaims().Build())
                           .GetAsync();

            var content = await response.Content
                          .ReadAs <DetailsApiKeyResponse>();

            content.Name
            .Should()
            .BeEquivalentTo(apiKeyName);

            response.StatusCode
            .Should()
            .Be(StatusCodes.Status200OK);
        }
Exemplo n.º 11
0
        public async Task list_response_ok_when_no_page_data()
        {
            var apiKey1 = Builders.ApiKey()
                          .WithName("apikey#1")
                          .Withkey("key-1")
                          .Build();

            var apiKey2 = Builders.ApiKey()
                          .WithName("apikey#2")
                          .Withkey("key-2")
                          .Build();

            await _fixture.Given
            .AddApiKey(apiKey1, apiKey2);

            var response = await _fixture.TestServer
                           .CreateRequest(ApiDefinitions.V1.ApiKeys.List(pageIndex: 10, pageCount: 10))
                           .WithIdentity(Builders.Identity().WithDefaultClaims().Build())
                           .GetAsync();

            response.StatusCode
            .Should()
            .Be(StatusCodes.Status200OK);

            var content = await response.Content
                          .ReadAs <ListApiKeyResponse>();

            content.Total
            .Should().Be(2);

            content.Count
            .Should().Be(0);

            content.PageIndex
            .Should().Be(10);

            content.Result
            .Count
            .Should().Be(0);
        }
Exemplo n.º 12
0
        public async Task get_feature_from_the_store_dont_use_cache_if_isnot_configured()
        {
            var permission = Builders.Permission()
                             .WithManagementPermission()
                             .Build();

            await _fixture.Given
            .AddPermission(permission);

            var defaultRing = Builders.Deployment()
                              .WithName(EsquioConstants.DEFAULT_DEPLOYMENT_NAME)
                              .WithDefault(true)
                              .Build();

            var product = Builders.Product()
                          .WithName("fooproduct")
                          .Build();

            product.Deployments
            .Add(defaultRing);

            var feature = Builders.Feature()
                          .WithName("barfeature")
                          .Build();

            var toggle = Builders.Toggle()
                         .WithType("Esquio.Toggles.EnvironmentToggle,Esquio")
                         .Build();

            var parameter = Builders.Parameter()
                            .WithName("Environments")
                            .WithValue("Development")
                            .WithRingName(defaultRing.Name)
                            .Build();

            toggle.Parameters
            .Add(parameter);

            feature.Toggles
            .Add(toggle);

            product.Features
            .Add(feature);

            await _fixture.Given
            .AddProduct(product);

            var apiKeyValue = "barkey";

            var apiKey = Builders.ApiKey()
                         .WithName("fooname")
                         .Withkey(apiKeyValue)
                         .WithValidTo(DateTime.UtcNow.AddDays(1))
                         .Build();

            await _fixture.Given
            .AddApiKey(apiKey);

            var cache = CreateDefaultCache();
            var store = CreateCachedStore(cache, apiKey: apiKey.Key, useCache: false);

            var featureModel = await store
                               .FindFeatureAsync(feature.Name, product.Name, EsquioConstants.DEFAULT_DEPLOYMENT_NAME);

            featureModel.Name
            .Should()
            .BeEquivalentTo(feature.Name);

            var cacheKey = CacheKeyCreator.GetCacheKey(product.Name, feature.Name, defaultRing.Name, "3.0");

            var entry = await cache.GetStringAsync(cacheKey);

            entry.Should()
            .BeNull();

            await cache.SetStringAsync(cacheKey, "{\"featureName\":\"barfeature\",\"enabled\":false}");

            featureModel = await store
                           .FindFeatureAsync(feature.Name, product.Name, EsquioConstants.DEFAULT_DEPLOYMENT_NAME);

            featureModel.IsEnabled
            .Should().BeFalse();
        }
Exemplo n.º 13
0
        public async Task get_null_if_features_does_not_exist()
        {
            var permission = Builders.Permission()
                             .WithManagementPermission()
                             .Build();

            await _fixture.Given
            .AddPermission(permission);

            var defaultRing = Builders.Deployment()
                              .WithName(EsquioConstants.DEFAULT_DEPLOYMENT_NAME)
                              .WithDefault(true)
                              .Build();

            var product = Builders.Product()
                          .WithName("fooproduct")
                          .Build();

            product.Deployments
            .Add(defaultRing);

            var feature = Builders.Feature()
                          .WithName("barfeature")
                          .Build();

            var toggle = Builders.Toggle()
                         .WithType("Esquio.Toggles.EnvironmentToggle,Esquio")
                         .Build();

            var parameter = Builders.Parameter()
                            .WithName("Environments")
                            .WithValue("Development")
                            .WithRingName(defaultRing.Name)
                            .Build();

            toggle.Parameters
            .Add(parameter);

            feature.Toggles
            .Add(toggle);

            product.Features
            .Add(feature);

            await _fixture.Given
            .AddProduct(product);

            var apiKeyValue = "barkey";

            var apiKey = Builders.ApiKey()
                         .WithName("fooname")
                         .Withkey(apiKeyValue)
                         .WithValidTo(DateTime.UtcNow.AddDays(1))
                         .Build();

            await _fixture.Given
            .AddApiKey(apiKey);

            var store = CreateStore(apiKey: apiKey.Key);

            var featureModel = await store
                               .FindFeatureAsync("non-existing", product.Name, EsquioConstants.DEFAULT_DEPLOYMENT_NAME);

            featureModel.Should()
            .BeNull();
        }
Exemplo n.º 14
0
        public async Task get_feature_from_the_store_throw_if_cache_is_enabled_but_distributed_cache_service_is_null()
        {
            var permission = Builders.Permission()
                             .WithManagementPermission()
                             .Build();

            await _fixture.Given
            .AddPermission(permission);

            var defaultRing = Builders.Deployment()
                              .WithName(EsquioConstants.DEFAULT_DEPLOYMENT_NAME)
                              .WithDefault(true)
                              .Build();

            var product = Builders.Product()
                          .WithName("fooproduct")
                          .Build();

            product.Deployments
            .Add(defaultRing);

            var feature = Builders.Feature()
                          .WithName("barfeature")
                          .Build();

            var toggle = Builders.Toggle()
                         .WithType("Esquio.Toggles.EnvironmentToggle,Esquio")
                         .Build();

            var parameter = Builders.Parameter()
                            .WithName("Environments")
                            .WithValue("Development")
                            .WithRingName(defaultRing.Name)
                            .Build();

            toggle.Parameters
            .Add(parameter);

            feature.Toggles
            .Add(toggle);

            product.Features
            .Add(feature);

            await _fixture.Given
            .AddProduct(product);

            var apiKeyValue = "barkey";

            var apiKey = Builders.ApiKey()
                         .WithName("fooname")
                         .Withkey(apiKeyValue)
                         .WithValidTo(DateTime.UtcNow.AddDays(1))
                         .Build();

            await _fixture.Given
            .AddApiKey(apiKey);

            var store = CreateCachedStore(null, apiKey: apiKey.Key, useCache: true);

            await Assert.ThrowsAsync <InvalidOperationException>(async() =>
            {
                await store.FindFeatureAsync(feature.Name, product.Name, EsquioConstants.DEFAULT_DEPLOYMENT_NAME);
            });
        }
Exemplo n.º 15
0
        public async Task get_feature_from_the_store()
        {
            var permission = Builders.Permission()
                             .WithManagementPermission()
                             .Build();

            await _fixture.Given
            .AddPermission(permission);

            var defaultRing = Builders.Deployment()
                              .WithName(EsquioConstants.DEFAULT_DEPLOYMENT_NAME)
                              .WithDefault(true)
                              .Build();

            var product = Builders.Product()
                          .WithName("fooproduct")
                          .Build();

            product.Deployments
            .Add(defaultRing);

            var feature = Builders.Feature()
                          .WithName("barfeature")
                          .Build();

            var toggle = Builders.Toggle()
                         .WithType("Esquio.Toggles.EnvironmentToggle,Esquio")
                         .Build();

            var parameter = Builders.Parameter()
                            .WithName("Environments")
                            .WithValue("Development")
                            .WithRingName(defaultRing.Name)
                            .Build();

            toggle.Parameters
            .Add(parameter);

            feature.Toggles
            .Add(toggle);

            product.Features
            .Add(feature);

            await _fixture.Given
            .AddProduct(product);

            var apiKeyValue = "barkey";

            var apiKey = Builders.ApiKey()
                         .WithName("fooname")
                         .Withkey(apiKeyValue)
                         .WithValidTo(DateTime.UtcNow.AddDays(1))
                         .Build();

            await _fixture.Given
            .AddApiKey(apiKey);

            var store = CreateStore(apiKey: apiKey.Key);

            var featureModel = await store
                               .FindFeatureAsync(feature.Name, product.Name, EsquioConstants.DEFAULT_DEPLOYMENT_NAME);

            featureModel.Name
            .Should()
            .BeEquivalentTo(feature.Name);

            featureModel.IsEnabled
            .Should()
            .Be(false);

            featureModel.GetToggles()
            .Count()
            .Should().Be(1);

            featureModel.GetToggles()
            .First()
            .Type
            .Should()
            .BeEquivalentTo("Esquio.Toggles.EnvironmentToggle,Esquio");

            featureModel.GetToggles()
            .First()
            .GetParameters()
            .Count()
            .Should()
            .Be(1);

            featureModel.GetToggles()
            .First()
            .GetParameters()
            .First()
            .Name
            .Should()
            .BeEquivalentTo("Environments");

            featureModel.GetToggles()
            .First()
            .GetParameters()
            .First()
            .Value
            .Should()
            .BeEquivalentTo("Development");
        }