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
8 changes: 8 additions & 0 deletions api/test/e2e/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ type HttpTestCase struct {
ExpectCode int
ExpectMessage string
ExpectBody string
ExpectJSON map[string]interface{}
ExpectHeaders map[string]string
Sleep time.Duration //ms
}
Expand Down Expand Up @@ -167,4 +168,11 @@ func testCaseCheck(tc HttpTestCase) {
resp.Body().Contains(tc.ExpectBody)
}

//verify result by json
if tc.ExpectJSON != nil {
for key, val := range tc.ExpectJSON {
resp.Status(http.StatusOK).JSON().Object().ContainsKey(key).ValueEqual(key, val)
}
}

}
337 changes: 337 additions & 0 deletions api/test/e2e/consumer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,337 @@
/*
* 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 (
"net/http"
"testing"
)

//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

func TestConsumer_add_consumer_with_username(t *testing.T) {
tests := []HttpTestCase{
{
caseDesc: "create route",
Object: MangerApiExpect(t),
Method: http.MethodPut,
Path: "/apisix/admin/routes/r1",
Body: `{
"uri": "/hello",
"plugins": {
"key-auth": {}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"172.16.238.20:1980": 1
}
}
}`,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
},
{
caseDesc: "verify route",
Object: APISIXExpect(t),
Method: http.MethodGet,
Path: "/hello",
ExpectStatus: http.StatusUnauthorized,
ExpectBody: "Missing API key found in request",
Sleep: sleepTime, //sleep x millisecond before verify route
},
{
caseDesc: "create consumer",
Object: MangerApiExpect(t),
Path: "/apisix/admin/consumers",
Method: http.MethodPut,
Body: `{
"username": "case",
"plugins": {
"key-auth": {
"key": "auth-one"
}
},
"desc": "test description"
}`,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
},
{
caseDesc: "verify route",
Copy link
Member

Choose a reason for hiding this comment

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

desc: hit route with correct key

Object: APISIXExpect(t),
Method: http.MethodGet,
Path: "/hello",
Headers: map[string]string{"apikey": "auth-one"},
ExpectStatus: http.StatusOK,
ExpectBody: "hello world",
Sleep: sleepTime,
},
{
caseDesc: "verify route",
Copy link
Member

Choose a reason for hiding this comment

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

desc: hit route with incorrect key

Object: APISIXExpect(t),
Method: http.MethodGet,
Path: "/hello",
Headers: map[string]string{"apikey": "auth-new"},
ExpectStatus: http.StatusUnauthorized,
ExpectBody: "Invalid API key in request",
Sleep: sleepTime,
},
Copy link
Member

Choose a reason for hiding this comment

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

need to delete the consumer and check it in dataplane

}

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

//case 2: add consumer without username
func TestConsumer_add_consumer_without_username(t *testing.T) {
tests := []HttpTestCase{
{
caseDesc: "create consumer",
Copy link
Member

Choose a reason for hiding this comment

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

bad title.

create consumer without user name

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,
},
{
caseDesc: "verify route",
Copy link
Member

Choose a reason for hiding this comment

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

I think we need to create the route first

Object: APISIXExpect(t),
Method: http.MethodGet,
Path: "/hello",
Headers: map[string]string{"apikey": "auth-new"},
ExpectStatus: http.StatusUnauthorized,
ExpectBody: "Invalid API key in request",
Sleep: sleepTime,
},
}

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

//case 3: delete consumer
func TestConsumer_delete_consumer(t *testing.T) {
tests := []HttpTestCase{
{
caseDesc: "delete consumer",
Object: MangerApiExpect(t),
Method: http.MethodDelete,
Path: "/apisix/admin/consumers/case",
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
},
idbeta marked this conversation as resolved.
Show resolved Hide resolved
{
caseDesc: "verify route",
Object: APISIXExpect(t),
Method: http.MethodGet,
Path: "/hello",
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusUnauthorized,
ExpectBody: "Missing API key found in request",
Sleep: sleepTime,
},
}

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

//case 5: create consumer with error key
func TestConsumer_create_consumer_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": "case_2",
"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/case_2",
Method: http.MethodGet,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusNotFound,
Sleep: sleepTime,
},
}

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

//case 6: create consumer with no value
func TestConsumer_create_consumer_with_no_value(t *testing.T) {
tests := []HttpTestCase{
{
caseDesc: "create consumer with no value",
Object: MangerApiExpect(t),
Path: "/apisix/admin/consumers",
Method: http.MethodPut,
Body: `{
"username": "case_3",
"plugins": {
"key-auth": {
"key": ""
}
},
"desc": "test description"
}`,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
},
{
caseDesc: "verify consumer",
Object: MangerApiExpect(t),
Path: "/apisix/admin/consumers/case_3",
Method: http.MethodGet,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
ExpectBody: "\"username\":\"case_3\",\"desc\":\"test description\",\"plugins\":{\"key-auth\":{\"key\":\"\"}}}",
Sleep: sleepTime,
},
{
caseDesc: "delete consumer",
Object: MangerApiExpect(t),
Method: http.MethodDelete,
Path: "/apisix/admin/consumers/case_3",
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
},
}

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

//case 7: create consumer with labels
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": "case_7",
"labels": {
"build":"16",
"env":"production",
"version":"v2"
},
"plugins": {
"key-auth": {
"key": "auth-two"
}
},
"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/case_7",
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
ExpectBody: "\"username\":\"case_7\",\"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": {
"172.16.238.20:1980": 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/case_7",
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
},
}

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

//Teardown
func TestConsumer_teardown(t *testing.T) {
_ = []HttpTestCase{
{
caseDesc: "delete route",
Object: MangerApiExpect(t),
Method: http.MethodDelete,
Path: "/apisix/admin/routes/r1",
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
},
}
}
Loading