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 f13d3d8
Showing 1 changed file with 47 additions and 10 deletions.
57 changes: 47 additions & 10 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 All @@ -462,7 +462,7 @@ func TestApplyPoliciesQuotaAPILimit(t *testing.T) {
"api1": {
Versions: []string{"v1"},
Limit: &user.APILimit{
QuotaMax: 100,
QuotaMax: 2,
QuotaRenewalRate: 3600,
Rate: 1000,
Per: 1,
Expand Down Expand Up @@ -547,6 +547,7 @@ func TestApplyPoliciesQuotaAPILimit(t *testing.T) {
// 2 requests to api1, API limit quota remaining should be 98
{Method: http.MethodGet, Path: "/api1", Headers: authHeader, Code: http.StatusOK},
{Method: http.MethodGet, Path: "/api1", Headers: authHeader, Code: http.StatusOK},
{Method: http.MethodGet, Path: "/api1", Headers: authHeader, Code: http.StatusForbidden},
// 3 requests to api2, API limit quota remaining should be 197
{Method: http.MethodGet, Path: "/api2", Headers: authHeader, Code: http.StatusOK},
{Method: http.MethodGet, Path: "/api2", Headers: authHeader, Code: http.StatusOK},
Expand Down Expand Up @@ -580,10 +581,10 @@ func TestApplyPoliciesQuotaAPILimit(t *testing.T) {
api1LimitExpected := user.APILimit{
Rate: 1000,
Per: 1,
QuotaMax: 100,
QuotaMax: 2,
QuotaRenewalRate: 3600,
QuotaRenews: api1Limit.QuotaRenews,
QuotaRemaining: 98,
QuotaRemaining: 0,
}
if !reflect.DeepEqual(*api1Limit, api1LimitExpected) {
t.Log("api1 limit received:", *api1Limit, "expected:", api1LimitExpected)
Expand Down Expand Up @@ -614,10 +615,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 @@ -631,6 +632,40 @@ func TestApplyPoliciesQuotaAPILimit(t *testing.T) {

// Reset quota
ts.Run(t, []test.TestCase{
{
Method: http.MethodPut,
Path: "/tyk/keys/" + key + "?suppress_reset=1",
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
}

api1Limit := sessionData.AccessRights["api1"].Limit
if api1Limit.QuotaRemaining != 0 {
t.Error("Should not reset quota", api1Limit.QuotaRemaining)
return false
}

api3Limit := sessionData.AccessRights["api3"].Limit
if api3Limit.QuotaRemaining != 0 {
t.Error("Should not reset quota", api3Limit.QuotaRemaining)
return false
}

return true
},
},
{
Method: http.MethodPut,
Path: "/tyk/keys/" + key,
Expand All @@ -649,14 +684,16 @@ 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")
if api1Limit.QuotaRemaining != 2 {
t.Error("Should reset quota", api1Limit.QuotaRemaining)
return false
}

if api1Limit.QuotaRemaining != 100 {
t.Error("Should reset quota:", api1Limit.QuotaRemaining)
api3Limit := sessionData.AccessRights["api3"].Limit
if api3Limit.QuotaRemaining != 5 {
t.Error("Should reset quota:", api3Limit.QuotaRemaining)
return false
}

Expand Down

0 comments on commit f13d3d8

Please sign in to comment.