Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add consumer e2e test for manager-api #830

Closed
wants to merge 13 commits into from
186 changes: 3 additions & 183 deletions api/test/e2e/consumer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,8 @@
package e2e

import (
"io/ioutil"
"net/http"
"strings"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/tidwall/gjson"
)

//case 1: add consumer with username
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove useless code

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and pls remove other similar points

Expand Down Expand Up @@ -158,25 +152,7 @@ func TestConsumer_delete_consumer(t *testing.T) {
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusUnauthorized,
ExpectBody: "Missing API key found in request",
Sleep: sleepTime, //sleep x millisecond before verify route
},
}

for _, tc := range tests {
testCaseCheck(tc)
}
}

//case 4: delete consumer(id: not_found)
func TestConsumer_delete_notexit_consumer(t *testing.T) {
tests := []HttpTestCase{
{
caseDesc: "delete notexit consumer",
Object: MangerApiExpect(t),
Method: http.MethodDelete,
Path: "/apisix/admin/consumers/notexit",
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusNotFound,
Sleep: sleepTime,
},
}

Expand Down Expand Up @@ -213,7 +189,7 @@ func TestConsumer_create_consumer_with_error_key(t *testing.T) {
Method: http.MethodGet,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusNotFound,
Sleep: sleepTime, //sleep x millisecond before verify route
Sleep: sleepTime,
},
}

Expand Down Expand Up @@ -329,7 +305,7 @@ func TestConsumer_add_consumer_with_labels(t *testing.T) {
Path: "/hello",
Headers: map[string]string{"apikey": "auth-two"},
ExpectStatus: http.StatusOK,
Sleep: sleepTime, //sleep x millisecond before verify route
Sleep: sleepTime,
},
{
caseDesc: "delete consumer",
Expand All @@ -346,162 +322,6 @@ func TestConsumer_add_consumer_with_labels(t *testing.T) {
}
}

//case 8: update consumer, check if updatetime is updated
func TestConsumer_create_consumer_with_createtime_updatetime(t *testing.T) {
//create consumer, save the result_A ( create_time and update_time )
data := `{
"username":"case_8",
"desc": "new consumer"
}`
request, _ := http.NewRequest("PUT", "http://127.0.0.1:8080/apisix/admin/consumers", strings.NewReader(data))
request.Header.Add("Authorization", token)
resp, _ := http.DefaultClient.Do(request)
defer resp.Body.Close()
respBody, _ := ioutil.ReadAll(resp.Body)
code := gjson.Get(string(respBody), "code")
assert.Equal(t, code.String(), "0")

time.Sleep(1 * time.Second)

request, _ = http.NewRequest("GET", "http://127.0.0.1:8080/apisix/admin/consumers/case_8", nil)
request.Header.Add("Authorization", token)
resp, _ = http.DefaultClient.Do(request)
defer resp.Body.Close()
respBody, _ = ioutil.ReadAll(resp.Body)
createtime := gjson.Get(string(respBody), "data.create_time")
updatetime := gjson.Get(string(respBody), "data.update_time")

//create consumer again, compare the new result and the result_A
data = `{
"username":"case_8",
"desc": "new consumer haha"
}`
request, _ = http.NewRequest("PUT", "http://127.0.0.1:8080/apisix/admin/consumers", strings.NewReader(data))
request.Header.Add("Authorization", token)
resp, _ = http.DefaultClient.Do(request)
defer resp.Body.Close()
respBody, _ = ioutil.ReadAll(resp.Body)
code = gjson.Get(string(respBody), "code")
assert.Equal(t, code.String(), "0")

time.Sleep(1 * time.Second)

request, _ = http.NewRequest("GET", "http://127.0.0.1:8080/apisix/admin/consumers/case_8", nil)
request.Header.Add("Authorization", token)
resp, _ = http.DefaultClient.Do(request)
defer resp.Body.Close()
respBody, _ = ioutil.ReadAll(resp.Body)
createtime2 := gjson.Get(string(respBody), "data.create_time")
updatetime2 := gjson.Get(string(respBody), "data.update_time")

assert.Equal(t, createtime.String(), createtime2.String())
assert.NotEqual(t, updatetime.String(), updatetime2.String())

//deletea consumer
request, _ = http.NewRequest("DELETE", "http://127.0.0.1:8080/apisix/admin/consumers/case_8", nil)
request.Header.Add("Authorization", token)
_, err := http.DefaultClient.Do(request)
assert.Nil(t, err)

}

//case9: create consumers with post method
func TestConsumer_create_consumer_with_post_method(t *testing.T) {
tests := []HttpTestCase{
{
caseDesc: "create consumers with post method",
Object: MangerApiExpect(t),
Path: "/apisix/admin/consumers",
Method: http.MethodPost,
Body: `{
"username":"case_9",
"desc": "new consumer",
"create_time": 1602883670,
"update_time": 1602893670
}`,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusMethodNotAllowed,
},
{
caseDesc: "delete consumer",
Object: MangerApiExpect(t),
Method: http.MethodDelete,
Path: "/apisix/admin/consumers/case_9",
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
},
}

for _, tc := range tests {
testCaseCheck(tc)
}
}

//case10: create consumers with invalid format of label value
func TestConsumer_create_consumer_with_invalid_format_of_label(t *testing.T) {
tests := []HttpTestCase{
{
caseDesc: "create consumers with invalid format of label value",
Object: MangerApiExpect(t),
Path: "/apisix/admin/consumers",
Method: http.MethodPost,
Body: `{
"username":"case_10",
"desc": "new consumer",
"labels": {
"env": ["production", "release"]
}
}`,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusBadRequest,
},
}

for _, tc := range tests {
testCaseCheck(tc)
}
}

//case11: create consumers with two auth plugin
func TestConsumer_create_consumer_with_two_authplugin(t *testing.T) {
tests := []HttpTestCase{
{
caseDesc: "create consumers with two auth plugin",
Object: MangerApiExpect(t),
Path: "/apisix/admin/consumers",
Method: http.MethodPut,
Body: `{
"username": "case_11",
"plugins": {
"jwt-auth": {
"key": "a36c3049b36249a3c9f8891cb127243c",
"secret": "e71829c351aa4242c2719cbfbe671c09"
},
"key-auth": {
"key": "auth-one"
}
}
}`,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusBadRequest,
ExpectBody: "only one auth plugin is allowed",
},
{
caseDesc: "verify consumer",
Object: MangerApiExpect(t),
Path: "/apisix/admin/consumers/case_11",
Method: http.MethodGet,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusNotFound,
Sleep: sleepTime, //sleep x millisecond before verify route
},
}

for _, tc := range tests {
testCaseCheck(tc)
}
}

//Teardown
func TestConsumer_teardown(t *testing.T) {
_ = []HttpTestCase{
Expand Down
Loading