Skip to content

Commit

Permalink
Improve policy limit tests
Browse files Browse the repository at this point in the history
Add tests for quota reset
  • Loading branch information
buger committed Mar 18, 2019
1 parent 241fd01 commit 028ca77
Showing 1 changed file with 32 additions and 8 deletions.
40 changes: 32 additions & 8 deletions policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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) {
Expand All @@ -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,
Expand All @@ -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
}
Expand Down

0 comments on commit 028ca77

Please sign in to comment.