public async Task KV_Quota_Limit_Scopes_Data_Size()
        {
            await EnforceRateLimits();

            var scopeName = "rateLimitDataSize2";
            var limits    = new ScopeRateLimits();

            limits.KeyValueScopeRateLimit = new KeyValueScopeRateLimit(1024 * 1024);
            await CreateRateLimitedScope(scopeName, _fixture.GetDefaultBucket().Result.Name, limits);

            var collectionManager = _fixture.GetDefaultBucket().Result.Collections;
            await collectionManager.CreateCollectionAsync(new CollectionSpec(scopeName, scopeName));

            await Task.Delay(1000);//wait time

            var bucket = await _fixture.Cluster.BucketAsync(_fixture.GetDefaultBucket().Result.Name);

            var scope = await bucket.ScopeAsync(scopeName);

            var collection = await scope.CollectionAsync(scopeName);

            await _fixture.GetDefaultCollectionAsync().Result.UpsertAsync("test", "test");

            try
            {
                await collection.UpsertAsync("rateLimitKvScope", RandomDoc(512));

                await Assert.ThrowsAsync <QuotaLimitedException>(async() =>
                                                                 await collection.UpsertAsync("rateLimitKvScope", RandomDoc(2048)));
            }
            finally
            {
                await collectionManager.DropScopeAsync(scopeName);
            }
        }
        public async Task Scope_Quota_Limit_Max_Collections()
        {
            await EnforceRateLimits();

            var scopeName = "clusterManager";
            var limits    = new ScopeRateLimits();

            limits.ClusterManagerScopeRateLimit = new ClusterManagerScopeRateLimit(1);
            await CreateRateLimitedScope(scopeName, _fixture.GetDefaultBucket().Result.Name, limits);

            var collectionManager = _fixture.GetDefaultBucket().Result.Collections;
            await collectionManager.CreateCollectionAsync(new CollectionSpec(scopeName, "collectionName"));

            try
            {
                var ex = await Assert.ThrowsAsync <QuotaLimitedException>(async() =>
                {
                    await collectionManager.CreateCollectionAsync(new CollectionSpec(scopeName, "collectionName2"));
                });

                Assert.True(ex.Context?.Message.Contains("Maximum number of collections has been reached for scope"));
            }
            finally
            {
                await collectionManager.DropScopeAsync(scopeName);
            }
        }
        public async Task CreateRateLimitedScope(string name, string bucket, ScopeRateLimits limits)
        {
            var jsonLimits = new JObject();

            if (limits.KeyValueScopeRateLimit != null)
            {
                jsonLimits.Add("kv", new JObject
                {
                    { "data_size", limits.KeyValueScopeRateLimit.DataSize },
                });
            }

            if (limits.SearchScopeRateLimit != null)
            {
                var fts = limits.SearchScopeRateLimit;
                jsonLimits.Add("fts", new JObject
                {
                    { "num_fts_indexes", limits.SearchScopeRateLimit.NumFtsIndexes },
                });
            }

            if (limits.IndexScopeRateLimit != null)
            {
                jsonLimits.Add("index", new JObject
                {
                    { "num_indexes", limits.IndexScopeRateLimit.NumIndexes },
                });
            }

            if (limits.ClusterManagerScopeRateLimit != null)
            {
                jsonLimits.Add("clusterManager", new JObject
                {
                    { "num_collections", limits.ClusterManagerScopeRateLimit.NumCollections },
                });
            }

            var content = new FormUrlEncodedContent(new[]
            {
                new KeyValuePair <string, string>("name", name),
                new KeyValuePair <string, string>("limits",
                                                  jsonLimits
                                                  .ToString()) // var content = new StringContent(jsonLimits.ToString(), Encoding.UTF8, "application/json");
            });

            var response = await _httpClient.PostAsync($"/pools/default/buckets/{bucket}/scopes", content);

            Assert.True(response.IsSuccessStatusCode);
        }
        public async Task Search_Quota_Limit_Max_Scope_Indexes()
        {
            await EnforceRateLimits();

            var scopeName      = "searchRateLimit";
            var collectionName = "searchCollection";
            var limits         = new ScopeRateLimits();

            limits.SearchScopeRateLimit = new SearchScopeRateLimit(1);
            await CreateRateLimitedScope(scopeName, _fixture.GetDefaultBucket().Result.Name, limits);

            var collectionManager = _fixture.GetDefaultBucket().Result.Collections;
            await collectionManager.CreateCollectionAsync(new CollectionSpec(scopeName, collectionName));

            await Task.Delay(1000);//wait time

            var indexParams = new Dictionary <string, dynamic>()
            {
                {
                    "mapping", new Dictionary <string, dynamic>()
                    {
                        {
                            "types", new Dictionary <string, dynamic>()
                            {
                                {
                                    scopeName + "." + collectionName, new Dictionary <string, dynamic>()
                                    {
                                        { "enabled", true },
                                        { "dynamic", true }
                                    }
                                }
                            }
                        },
                        {
                            "default_mapping", new Dictionary <string, dynamic>()
                            {
                                { "enabled", false }
                            }
                        },
                        { "default_type", "_default" },
                        { "default_analyzer", "standard" },
                        { "default_field", "_all" }
                    }
                },
                {
                    "doc_config", new Dictionary <string, dynamic>()
                    {
                        { "mode", "scope.collection.type_field" },
                        { "type_field", "type" }
                    }
                }
            };

            try
            {
                await _fixture.Cluster.SearchIndexes.UpsertIndexAsync(new SearchIndex()
                {
                    Name       = "ratelimits1",
                    Type       = "fulltext-index",
                    SourceName = _fixture.GetDefaultBucket().Result.Name,
                    SourceType = "couchbase",
                    Params     = indexParams
                });


                var ex = await Assert.ThrowsAsync <QuotaLimitedException>(async() =>
                {
                    await _fixture.Cluster.SearchIndexes.UpsertIndexAsync(new SearchIndex()
                    {
                        Name       = "ratelimit2",
                        Type       = "fulltext-index",
                        SourceName = _fixture.GetDefaultBucket().Result.Name,
                        SourceType = "couchbase",
                        Params     = indexParams
                    });
                });

                Assert.True(ex.Context?.Message.Contains("num_fts_indexes"));
            }
            finally
            {
                await collectionManager.DropScopeAsync(scopeName);
            }
        }