From 028ca7727d6a345599153a183698f1580798b647 Mon Sep 17 00:00:00 2001 From: Leonid Bugaev Date: Mon, 18 Mar 2019 14:05:30 +0100 Subject: [PATCH] Improve policy limit tests Add tests for quota reset --- policy_test.go | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/policy_test.go b/policy_test.go index b7b64425afb4..31ab0f5a47fe 100644 --- a/policy_test.go +++ b/policy_test.go @@ -449,7 +449,7 @@ func TestApplyPoliciesQuotaAPILimit(t *testing.T) { ID: "two_of_three_with_api_limit", Per: 1, Rate: 1000, - QuotaMax: 50, + QuotaMax: 5, QuotaRenewalRate: 3600, OrgID: "default", Partitions: user.PolicyPartitions{ @@ -614,10 +614,10 @@ func TestApplyPoliciesQuotaAPILimit(t *testing.T) { api3LimitExpected := user.APILimit{ Rate: 1000, Per: 1, - QuotaMax: 50, + QuotaMax: 5, QuotaRenewalRate: 3600, QuotaRenews: api3Limit.QuotaRenews, - QuotaRemaining: 45, + QuotaRemaining: 0, SetByPolicy: true, } if !reflect.DeepEqual(*api3Limit, api3LimitExpected) { @@ -633,7 +633,7 @@ func TestApplyPoliciesQuotaAPILimit(t *testing.T) { ts.Run(t, []test.TestCase{ { Method: http.MethodPut, - Path: "/tyk/keys/" + key, + Path: "/tyk/keys/" + key + "?suppress_reset=1", AdminAuth: true, Code: http.StatusOK, Data: session, @@ -649,13 +649,37 @@ func TestApplyPoliciesQuotaAPILimit(t *testing.T) { t.Log(err.Error()) return false } - api1Limit := sessionData.AccessRights["api1"].Limit - if api1Limit == nil { - t.Error("api1 limit is not set") + + api1Limit := sessionData.AccessRights["api3"].Limit + if api1Limit.QuotaRemaining != 0 { + t.Error("Should not reset quota") + return false + } + + return true + }, + }, + { + Method: http.MethodPut, + Path: "/tyk/keys/" + key, + AdminAuth: true, + Code: http.StatusOK, + Data: session, + }, + { + Method: http.MethodGet, + Path: "/tyk/keys/" + key, + AdminAuth: true, + Code: http.StatusOK, + BodyMatchFunc: func(data []byte) bool { + sessionData := user.SessionState{} + if err := json.Unmarshal(data, &sessionData); err != nil { + t.Log(err.Error()) return false } - if api1Limit.QuotaRemaining != 100 { + api1Limit := sessionData.AccessRights["api3"].Limit + if api1Limit.QuotaRemaining != 5 { t.Error("Should reset quota:", api1Limit.QuotaRemaining) return false }