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 test ( E2E ) #880

Merged
merged 23 commits into from
Dec 2, 2020
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
262 changes: 244 additions & 18 deletions api/test/e2e/consumer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@
package e2e

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

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

func TestConsumer_with_key_auth(t *testing.T) {
Expand All @@ -29,19 +35,19 @@ func TestConsumer_with_key_auth(t *testing.T) {
Method: http.MethodPut,
Path: "/apisix/admin/routes/r1",
Body: `{
"uri": "/hello",
"plugins": {
"key-auth": {}
},
"upstream": {
"type": "roundrobin",
"nodes": [{
"uri": "/hello",
"plugins": {
"key-auth": {}
},
"upstream": {
"type": "roundrobin",
"nodes": [{
"host": "172.16.238.20",
"port": 1980,
"weight": 1
}]
}
}`,
}
}`,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
},
Expand All @@ -52,22 +58,22 @@ func TestConsumer_with_key_auth(t *testing.T) {
Path: "/hello",
ExpectStatus: http.StatusUnauthorized,
ExpectBody: "Missing API key found in request",
Sleep: sleepTime, //sleep x millisecond before verify route
Sleep: sleepTime * 2,
},
{
caseDesc: "create consumer",
Object: ManagerApiExpect(t),
Path: "/apisix/admin/consumers",
Method: http.MethodPut,
Body: `{
"username": "jack",
"plugins": {
"key-auth": {
"key": "auth-one"
}
},
"desc": "test description"
}`,
"username": "jack",
"plugins": {
"key-auth": {
"key": "auth-one"
}
},
"desc": "test description"
}`,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
},
Expand Down Expand Up @@ -131,3 +137,223 @@ func TestConsumer_with_key_auth(t *testing.T) {
testCaseCheck(tc)
}
}

func TestConsumer_with_notexist_plugin(t *testing.T) {
tests := []HttpTestCase{
{
caseDesc: "create consumer with not exist plugin",
Object: ManagerApiExpect(t),
Path: "/apisix/admin/consumers",
Method: http.MethodPut,
Body: `{
"username": "jack",
"plugins": {
"key-authaa": {
"key": "auth-one"
}
},
"desc": "test description"
}`,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusBadRequest,
ExpectBody: "schema validate failed: schema not found, path: plugins.key-authaa",
},
{
caseDesc: "verify the consumer",
Object: ManagerApiExpect(t),
Path: "/apisix/admin/consumers/jack",
Method: http.MethodGet,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusNotFound,
Sleep: sleepTime,
},
}

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

func TestConsumer_add_consumer_with_labels(t *testing.T) {
tests := []HttpTestCase{
{
caseDesc: "create the consumer",
Object: ManagerApiExpect(t),
Path: "/apisix/admin/consumers",
Method: http.MethodPut,
Body: `{
"username": "jack",
"labels": {
"build":"16",
"env":"production",
"version":"v2"
},
"plugins": {
"key-auth": {
"key": "auth-two"
}
membphis marked this conversation as resolved.
Show resolved Hide resolved
},
"desc": "test description"
}`,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
},
{
caseDesc: "verify the consumer",
Object: ManagerApiExpect(t),
Method: http.MethodGet,
Path: "/apisix/admin/consumers/jack",
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
ExpectBody: "\"username\":\"jack\",\"desc\":\"test description\",\"plugins\":{\"key-auth\":{\"key\":\"auth-two\"}},\"labels\":{\"build\":\"16\",\"env\":\"production\",\"version\":\"v2\"}",
Sleep: sleepTime,
},
{
caseDesc: "create the route",
Object: ManagerApiExpect(t),
Method: http.MethodPut,
Path: "/apisix/admin/routes/r1",
Body: `{
"uri": "/hello",
"plugins": {
"key-auth": {}
},
"upstream": {
"type": "roundrobin",
"nodes": [{
"host": "172.16.238.20",
"port": 1980,
"weight": 1
}]
}
}`,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
},
{
caseDesc: "hit the route with correct apikey",
Object: APISIXExpect(t),
Method: http.MethodGet,
Path: "/hello",
Headers: map[string]string{"apikey": "auth-two"},
ExpectStatus: http.StatusOK,
Sleep: sleepTime,
},
{
caseDesc: "delete the consumer",
Object: ManagerApiExpect(t),
Method: http.MethodDelete,
Path: "/apisix/admin/consumers/jack",
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
},
{
caseDesc: "delete the route",
Object: ManagerApiExpect(t),
Method: http.MethodDelete,
Path: "/apisix/admin/routes/r1",
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
},
}

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

func TestConsumer_with_createtime_updatetime(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

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

Why not use the testing framework? Is this test related to the title?

Copy link
Contributor Author

@idbeta idbeta Nov 26, 2020

Choose a reason for hiding this comment

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

This case is used to test when the consumer is updated, it's create_time should not be updated and update_time should be updated. The current framework does not support obtaining update_time and create_time from the response, so I use standard library and assert to achieve it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ping @moonming

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ping @moonming Can you review it when you have time?

tests := []HttpTestCase{
{
caseDesc: "create the consumer",
Object: ManagerApiExpect(t),
Path: "/apisix/admin/consumers",
Method: http.MethodPut,
Body: `{
"username":"jack",
"desc": "new consumer"
}`,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
Sleep: sleepTime,
},
}
for _, tc := range tests {
testCaseCheck(tc)
}

basepath := "http://127.0.0.1:9000/apisix/admin/consumers"
time.Sleep(time.Duration(1) * time.Second)

// get the consumer, save createtime and updatetime
request, _ := http.NewRequest("GET", basepath+"/jack", nil)
request.Header.Add("Authorization", token)
resp, err := http.DefaultClient.Do(request)
if err != nil {
fmt.Printf("server not responding %s", err.Error())
}
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")

// wait 1 second so the update_time should be different
time.Sleep(time.Duration(1) * time.Second)

tests = []HttpTestCase{
{
caseDesc: "update the consumer",
Object: ManagerApiExpect(t),
Path: "/apisix/admin/consumers",
Method: http.MethodPut,
Body: `{
"username":"jack",
"desc": "updated consumer"
}`,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
Sleep: sleepTime,
},
}

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

// get the consumer
request, _ = http.NewRequest("GET", basepath+"/jack", nil)
request.Header.Add("Authorization", token)
resp, _ = http.DefaultClient.Do(request)
membphis marked this conversation as resolved.
Show resolved Hide resolved
respBody, _ = ioutil.ReadAll(resp.Body)
createtime2 := gjson.Get(string(respBody), "data.create_time")
updatetime2 := gjson.Get(string(respBody), "data.update_time")

// verify the consumer and compare result
assert.Equal(t, "updated consumer", gjson.Get(string(respBody), "data.desc").String())
assert.Equal(t, createtime.String(), createtime2.String())
assert.NotEqual(t, updatetime.String(), updatetime2.String())

tests = []HttpTestCase{
{
caseDesc: "delete the consumer",
Object: ManagerApiExpect(t),
Method: http.MethodDelete,
Path: "/apisix/admin/consumers/jack",
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
},
{
caseDesc: "after delete consumer verify it again",
Object: ManagerApiExpect(t),
Method: http.MethodGet,
Path: "/apisix/admin/consumers/jack",
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusNotFound,
Sleep: sleepTime,
},
}

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