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
172 changes: 172 additions & 0 deletions api/test/e2e/consumer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
/*
* 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
func TestConsumer_add_consumer_with_username(t *testing.T) {
tests := []HttpTestCase{
{
caseDesc: "create consumer",
Object: MangerApiExpect(t),
Path: "/apisix/admin/consumers",
Method: http.MethodPut,
Body: `{
"username": "jack",
"plugins": {
"key-auth": {
"key": "auth-one"
}
},
"desc": "test description"
}`,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
},
{
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",
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,
Sleep: sleepTime, //sleep x millisecond before 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.

need to check body.

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 check before consumer created.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok, thanks for the suggestion, I already add it now.

},
{
caseDesc: "verify route without auth",
Object: APISIXExpect(t),
Method: http.MethodGet,
Path: "/hello",
ExpectStatus: http.StatusUnauthorized,
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,
Sleep: sleepTime,
},
}

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

//CASE 3: add consumer with labels

idbeta marked this conversation as resolved.
Show resolved Hide resolved
//CASE 4: delete consumer
func TestConsumer_delete_consumer(t *testing.T) {
tests := []HttpTestCase{
{
caseDesc: "delete consumer",
Object: MangerApiExpect(t),
Method: http.MethodDelete,
Path: "/apisix/admin/consumers/jack",
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,
Sleep: sleepTime, //sleep x millisecond before verify route
},
}

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

//CASE 5: 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,
},
{
caseDesc: "delete consumer",
Object: MangerApiExpect(t),
Method: http.MethodDelete,
Path: "/apisix/admin/consumers/jack",
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
},
}
}