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 5 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
250 changes: 250 additions & 0 deletions api/test/e2e/consumer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,250 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package e2e

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

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

func TestConsumer_without_username(t *testing.T) {
tests := []HttpTestCase{
{
caseDesc: "create consumer without username",
Object: MangerApiExpect(t),
Path: "/apisix/admin/consumers",
Method: http.MethodPut,
Body: `{
"plugins": {
"key-auth": {
"key": "auth-new"
}
},
"desc": "test description"
}`,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusBadRequest,
ExpectBody: "scheme validate fail",
membphis marked this conversation as resolved.
Show resolved Hide resolved
},
}

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

func TestConsumer_delete_notexit_consumer(t *testing.T) {
tests := []HttpTestCase{
{
caseDesc: "delete notexit consumer",
membphis marked this conversation as resolved.
Show resolved Hide resolved
Object: MangerApiExpect(t),
Method: http.MethodDelete,
Path: "/apisix/admin/consumers/notexit",
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusNotFound,
},
}

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

func TestConsumer_with_error_key(t *testing.T) {
tests := []HttpTestCase{
{
caseDesc: "create consumer with error key",
Object: MangerApiExpect(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: "scheme validate failed",
},
{
caseDesc: "verify consumer",
Object: MangerApiExpect(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 consumer",
Object: MangerApiExpect(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 consumer",
Object: MangerApiExpect(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 route",
Object: MangerApiExpect(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: "verify route",
Object: APISIXExpect(t),
Method: http.MethodGet,
Path: "/hello",
Headers: map[string]string{"apikey": "auth-two"},
ExpectStatus: http.StatusOK,
Sleep: sleepTime,
},
{
caseDesc: "delete consumer",
Object: MangerApiExpect(t),
Method: http.MethodDelete,
Path: "/apisix/admin/consumers/jack",
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
},
{
caseDesc: "delete route",
Object: MangerApiExpect(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?

//create consumer
basepath := "http://127.0.0.1:8080/apisix/admin/consumers"
data := `{
"username":"jack",
"desc": "new consumer"
}`
membphis marked this conversation as resolved.
Show resolved Hide resolved
request, _ := http.NewRequest("PUT", basepath, 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)
membphis marked this conversation as resolved.
Show resolved Hide resolved

request, _ = http.NewRequest("GET", basepath+"/jack", nil)
request.Header.Add("Authorization", token)
resp, _ = http.DefaultClient.Do(request)
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 create_time and update_time
data = `{
"username":"jack",
"desc": "new consumer haha"
}`
request, _ = http.NewRequest("PUT", basepath, strings.NewReader(data))
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)
code = gjson.Get(string(respBody), "code")
assert.Equal(t, code.String(), "0")

time.Sleep(1 * time.Second)

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")

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

//deletea consumer
request, _ = http.NewRequest("DELETE", basepath+"/jack", nil)
request.Header.Add("Authorization", token)
_, err := http.DefaultClient.Do(request)
membphis marked this conversation as resolved.
Show resolved Hide resolved
assert.Nil(t, err)
}