You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by ju...@apache.org on 2022/07/12 07:08:54 UTC
[apisix-dashboard] branch next updated: test: move consumer unit cases to e2e (#2512)
This is an automated email from the ASF dual-hosted git repository.
juzhiyuan pushed a commit to branch next
in repository https://gitbox.apache.org/repos/asf/apisix-dashboard.git
The following commit(s) were added to refs/heads/next by this push:
new 6f085d7f test: move consumer unit cases to e2e (#2512)
6f085d7f is described below
commit 6f085d7f949d24b8bfc3652423653d3019b9b3f7
Author: Zeping Bai <bz...@apache.org>
AuthorDate: Tue Jul 12 15:08:50 2022 +0800
test: move consumer unit cases to e2e (#2512)
---
api/internal/handler/consumer/consumer_test.go | 461 -------------------------
api/test/e2e/base/base.go | 8 +-
api/test/e2e/consumer/consumer_test.go | 98 ++++--
3 files changed, 80 insertions(+), 487 deletions(-)
diff --git a/api/internal/handler/consumer/consumer_test.go b/api/internal/handler/consumer/consumer_test.go
deleted file mode 100644
index 08289f48..00000000
--- a/api/internal/handler/consumer/consumer_test.go
+++ /dev/null
@@ -1,461 +0,0 @@
-/*
- * 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 consumer
-
-import (
- "context"
- "fmt"
- "net/http"
- "testing"
-
- "github.com/shiningrush/droplet"
- "github.com/shiningrush/droplet/data"
- "github.com/stretchr/testify/assert"
- "github.com/stretchr/testify/mock"
-
- "github.com/apache/apisix-dashboard/api/internal/core/entity"
- "github.com/apache/apisix-dashboard/api/internal/core/store"
-)
-
-func TestHandler_Get(t *testing.T) {
- tests := []struct {
- caseDesc string
- giveInput *GetInput
- giveRet interface{}
- giveErr error
- wantErr error
- wantGetKey string
- wantRet interface{}
- }{
- {
- caseDesc: "normal",
- giveInput: &GetInput{Username: "test"},
- wantGetKey: "test",
- giveRet: "hello",
- wantRet: "hello",
- },
- {
- caseDesc: "store get failed",
- giveInput: &GetInput{Username: "failed key"},
- wantGetKey: "failed key",
- giveErr: fmt.Errorf("get failed"),
- wantErr: fmt.Errorf("get failed"),
- wantRet: &data.SpecCodeResponse{
- StatusCode: http.StatusInternalServerError,
- },
- },
- }
-
- for _, tc := range tests {
- t.Run(tc.caseDesc, func(t *testing.T) {
- getCalled := false
- mStore := &store.MockInterface{}
- mStore.On("Get", mock.Anything).Run(func(args mock.Arguments) {
- getCalled = true
- assert.Equal(t, tc.wantGetKey, args.Get(0))
- }).Return(tc.giveRet, tc.giveErr)
-
- h := Handler{consumerStore: mStore}
- ctx := droplet.NewContext()
- ctx.SetInput(tc.giveInput)
- ret, err := h.Get(ctx)
- assert.True(t, getCalled)
- assert.Equal(t, tc.wantRet, ret)
- assert.Equal(t, tc.wantErr, err)
- })
- }
-}
-
-func TestHandler_List(t *testing.T) {
- tests := []struct {
- caseDesc string
- giveInput *ListInput
- giveData []*entity.Consumer
- giveErr error
- wantErr error
- wantInput store.ListInput
- wantRet interface{}
- }{
- {
- caseDesc: "list all condition",
- giveInput: &ListInput{
- Username: "testUser",
- Pagination: store.Pagination{
- PageSize: 10,
- PageNumber: 10,
- },
- },
- wantInput: store.ListInput{
- PageSize: 10,
- PageNumber: 10,
- },
- giveData: []*entity.Consumer{
- {Username: "user1"},
- {Username: "testUser"},
- {Username: "iam-testUser"},
- {Username: "testUser-is-me"},
- },
- wantRet: &store.ListOutput{
- Rows: []interface{}{
- &entity.Consumer{Username: "iam-testUser"},
- &entity.Consumer{Username: "testUser"},
- &entity.Consumer{Username: "testUser-is-me"},
- },
- TotalSize: 3,
- },
- },
- {
- caseDesc: "store list failed",
- giveInput: &ListInput{
- Username: "testUser",
- Pagination: store.Pagination{
- PageSize: 10,
- PageNumber: 10,
- },
- },
- wantInput: store.ListInput{
- PageSize: 10,
- PageNumber: 10,
- },
- giveData: []*entity.Consumer{},
- giveErr: fmt.Errorf("list failed"),
- wantErr: fmt.Errorf("list failed"),
- },
- }
-
- for _, tc := range tests {
- t.Run(tc.caseDesc, func(t *testing.T) {
- getCalled := false
- mStore := &store.MockInterface{}
- mStore.On("List", mock.Anything).Run(func(args mock.Arguments) {
- getCalled = true
- input := args.Get(0).(store.ListInput)
- assert.Equal(t, tc.wantInput.PageSize, input.PageSize)
- assert.Equal(t, tc.wantInput.PageNumber, input.PageNumber)
- }).Return(func(input store.ListInput) *store.ListOutput {
- var returnData []interface{}
- for _, c := range tc.giveData {
- if input.Predicate(c) {
- returnData = append(returnData, c)
- }
- }
- return &store.ListOutput{
- Rows: returnData,
- TotalSize: len(returnData),
- }
- }, tc.giveErr)
-
- h := Handler{consumerStore: mStore}
- ctx := droplet.NewContext()
- ctx.SetInput(tc.giveInput)
- ret, err := h.List(ctx)
- assert.True(t, getCalled)
- assert.Equal(t, tc.wantRet, ret)
- assert.Equal(t, tc.wantErr, err)
- })
- }
-}
-
-func TestHandler_Create(t *testing.T) {
- tests := []struct {
- caseDesc string
- giveInput *SetInput
- giveCtx context.Context
- giveErr error
- giveRet interface{}
- wantErr error
- wantInput *SetInput
- wantRet interface{}
- wantCalled bool
- }{
- {
- caseDesc: "normal",
- giveInput: &SetInput{
- Consumer: entity.Consumer{
- Username: "name",
- Plugins: map[string]interface{}{
- "jwt-auth": map[string]interface{}{},
- },
- },
- },
- giveCtx: context.WithValue(context.Background(), "test", "value"),
- giveRet: &entity.Consumer{
- Username: "name",
- Plugins: map[string]interface{}{
- "jwt-auth": map[string]interface{}{
- "exp": 86400,
- },
- },
- },
- wantInput: &SetInput{
- Consumer: entity.Consumer{
- Username: "name",
- Plugins: map[string]interface{}{
- "jwt-auth": map[string]interface{}{
- "exp": 86400,
- },
- },
- },
- },
- wantRet: &entity.Consumer{
- Username: "name",
- Plugins: map[string]interface{}{
- "jwt-auth": map[string]interface{}{
- "exp": 86400,
- },
- },
- },
- wantCalled: true,
- },
- {
- caseDesc: "store create failed",
- giveInput: &SetInput{
- Consumer: entity.Consumer{
- Username: "name",
- Plugins: map[string]interface{}{
- "jwt-auth": map[string]interface{}{
- "exp": 5000,
- },
- },
- },
- },
- giveRet: &data.SpecCodeResponse{
- StatusCode: http.StatusInternalServerError,
- },
- giveErr: fmt.Errorf("create failed"),
- wantInput: &SetInput{
- Consumer: entity.Consumer{
- Username: "name",
- Plugins: map[string]interface{}{
- "jwt-auth": map[string]interface{}{
- "exp": 5000,
- },
- },
- },
- },
- wantErr: fmt.Errorf("create failed"),
- wantRet: &data.SpecCodeResponse{
- StatusCode: http.StatusInternalServerError,
- },
- wantCalled: true,
- },
- }
-
- for _, tc := range tests {
- t.Run(tc.caseDesc, func(t *testing.T) {
- methodCalled := true
- mStore := &store.MockInterface{}
- mStore.On("Update", mock.Anything, mock.Anything, mock.Anything).Run(func(args mock.Arguments) {
- methodCalled = true
- assert.Equal(t, tc.giveCtx, args.Get(0))
- assert.True(t, args.Bool(2))
- }).Return(tc.giveRet, tc.giveErr)
-
- mStore.On("Get", mock.Anything).Run(func(args mock.Arguments) {
- }).Return(nil, nil)
-
- h := Handler{consumerStore: mStore}
- ctx := droplet.NewContext()
- ctx.SetInput(tc.giveInput)
- ctx.SetContext(tc.giveCtx)
- ret, err := h.Set(ctx)
- assert.Equal(t, tc.wantCalled, methodCalled)
- assert.Equal(t, tc.wantRet, ret)
- assert.Equal(t, tc.wantErr, err)
- })
- }
-}
-
-func TestHandler_Update(t *testing.T) {
- tests := []struct {
- caseDesc string
- giveInput *SetInput
- giveCtx context.Context
- giveRet interface{}
- giveErr error
- wantErr error
- wantInput *entity.Consumer
- wantRet interface{}
- wantCalled bool
- getRet interface{}
- }{
- {
- caseDesc: "normal",
- giveInput: &SetInput{
- Username: "name",
- Consumer: entity.Consumer{
- Plugins: map[string]interface{}{
- "jwt-auth": map[string]interface{}{
- "exp": 500,
- },
- },
- },
- },
- giveCtx: context.WithValue(context.Background(), "test", "value"),
- giveRet: &entity.Consumer{
- Username: "name",
- Plugins: map[string]interface{}{
- "jwt-auth": map[string]interface{}{
- "exp": 500,
- },
- },
- CreateTime: 1618648423,
- },
- wantInput: &entity.Consumer{
- Username: "name",
- Plugins: map[string]interface{}{
- "jwt-auth": map[string]interface{}{
- "exp": 500,
- },
- },
- },
- wantRet: &entity.Consumer{
- Username: "name",
- Plugins: map[string]interface{}{
- "jwt-auth": map[string]interface{}{
- "exp": 500,
- },
- },
- CreateTime: 1618648423,
- },
- wantCalled: true,
- getRet: &entity.Consumer{
- Username: "name",
- CreateTime: 1618648423,
- UpdateTime: 1618648423,
- },
- },
- {
- caseDesc: "store update failed",
- giveInput: &SetInput{
- Username: "name",
- Consumer: entity.Consumer{
- Plugins: map[string]interface{}{
- "jwt-auth": map[string]interface{}{},
- },
- },
- },
- giveRet: &data.SpecCodeResponse{
- StatusCode: http.StatusInternalServerError,
- },
- giveErr: fmt.Errorf("create failed"),
- wantInput: &entity.Consumer{
- Username: "name",
- Plugins: map[string]interface{}{
- "jwt-auth": map[string]interface{}{
- "exp": 86400,
- },
- },
- },
- wantErr: fmt.Errorf("create failed"),
- wantRet: &data.SpecCodeResponse{
- StatusCode: http.StatusInternalServerError,
- },
- wantCalled: true,
- },
- }
-
- for _, tc := range tests {
- t.Run(tc.caseDesc, func(t *testing.T) {
- methodCalled := true
- mStore := &store.MockInterface{}
- mStore.On("Update", mock.Anything, mock.Anything, mock.Anything).Run(func(args mock.Arguments) {
- methodCalled = true
- assert.Equal(t, tc.giveCtx, args.Get(0))
- assert.True(t, args.Bool(2))
- }).Return(tc.giveRet, tc.giveErr)
-
- mStore.On("Get", mock.Anything).Run(func(args mock.Arguments) {
- }).Return(tc.getRet, nil)
-
- h := Handler{consumerStore: mStore}
- ctx := droplet.NewContext()
- ctx.SetInput(tc.giveInput)
- ctx.SetContext(tc.giveCtx)
- ret, err := h.Set(ctx)
- assert.Equal(t, tc.wantCalled, methodCalled)
- assert.Equal(t, tc.wantRet, ret)
- assert.Equal(t, tc.wantErr, err)
- if err == nil {
- assert.Equal(t, tc.getRet.(*entity.Consumer).CreateTime, ret.(*entity.Consumer).CreateTime)
- assert.NotEqual(t, tc.getRet.(*entity.Consumer).UpdateTime, ret.(*entity.Consumer).UpdateTime)
- }
- })
- }
-}
-
-func TestHandler_BatchDelete(t *testing.T) {
- tests := []struct {
- caseDesc string
- giveInput *BatchDeleteInput
- giveCtx context.Context
- giveErr error
- wantErr error
- wantInput []string
- wantRet interface{}
- }{
- {
- caseDesc: "normal",
- giveInput: &BatchDeleteInput{
- UserNames: "user1,user2",
- },
- giveCtx: context.WithValue(context.Background(), "test", "value"),
- wantInput: []string{
- "user1",
- "user2",
- },
- },
- {
- caseDesc: "store delete failed",
- giveInput: &BatchDeleteInput{
- UserNames: "user1,user2",
- },
- giveCtx: context.WithValue(context.Background(), "test", "value"),
- giveErr: fmt.Errorf("delete failed"),
- wantInput: []string{
- "user1",
- "user2",
- },
- wantErr: fmt.Errorf("delete failed"),
- wantRet: &data.SpecCodeResponse{
- StatusCode: http.StatusInternalServerError,
- },
- },
- }
-
- for _, tc := range tests {
- t.Run(tc.caseDesc, func(t *testing.T) {
- methodCalled := true
- mStore := &store.MockInterface{}
- mStore.On("BatchDelete", mock.Anything, mock.Anything, mock.Anything).Run(func(args mock.Arguments) {
- methodCalled = true
- assert.Equal(t, tc.giveCtx, args.Get(0))
- assert.Equal(t, tc.wantInput, args.Get(1))
- }).Return(tc.giveErr)
-
- h := Handler{consumerStore: mStore}
- ctx := droplet.NewContext()
- ctx.SetInput(tc.giveInput)
- ctx.SetContext(tc.giveCtx)
- ret, err := h.BatchDelete(ctx)
- assert.True(t, methodCalled)
- assert.Equal(t, tc.wantErr, err)
- assert.Equal(t, tc.wantRet, ret)
- })
- }
-}
diff --git a/api/test/e2e/base/base.go b/api/test/e2e/base/base.go
index a3aa943b..e0c53eba 100644
--- a/api/test/e2e/base/base.go
+++ b/api/test/e2e/base/base.go
@@ -307,11 +307,15 @@ func CleanResource(resource string) {
list := gjson.Get(resources, "data.rows").Value().([]interface{})
for _, item := range list {
resourceObj := item.(map[string]interface{})
+ idTag := "id"
+ if resource == "consumers" {
+ idTag = "username"
+ }
tc := HttpTestCase{
- Desc: "delete " + resource + "/" + resourceObj["id"].(string),
+ Desc: "delete " + resource + "/" + resourceObj[idTag].(string),
Object: ManagerApiExpect(),
Method: http.MethodDelete,
- Path: "/apisix/admin/" + resource + "/" + resourceObj["id"].(string),
+ Path: "/apisix/admin/" + resource + "/" + resourceObj[idTag].(string),
Headers: map[string]string{"Authorization": GetToken()},
}
RunTestCase(tc)
diff --git a/api/test/e2e/consumer/consumer_test.go b/api/test/e2e/consumer/consumer_test.go
index ccb596d4..6daf697c 100644
--- a/api/test/e2e/consumer/consumer_test.go
+++ b/api/test/e2e/consumer/consumer_test.go
@@ -26,11 +26,26 @@ import (
)
var _ = Describe("Consumer", func() {
- DescribeTable("test consumer curd",
+ DescribeTable("Test consumer CURD",
func(tc base.HttpTestCase) {
base.RunTestCase(tc)
},
- Entry("create consumer", base.HttpTestCase{
+ Entry("Get consumer (Not Exist)", base.HttpTestCase{
+ Object: base.ManagerApiExpect(),
+ Method: http.MethodGet,
+ Path: "/apisix/admin/consumers/1",
+ Headers: map[string]string{"Authorization": base.GetToken()},
+ ExpectStatus: http.StatusNotFound,
+ }),
+ Entry("List consumer (Empty)", base.HttpTestCase{
+ Object: base.ManagerApiExpect(),
+ Method: http.MethodGet,
+ Path: "/apisix/admin/consumers",
+ Headers: map[string]string{"Authorization": base.GetToken()},
+ ExpectStatus: http.StatusOK,
+ ExpectBody: `"total_size":0`,
+ }),
+ Entry("Create consumer #1", base.HttpTestCase{
Object: base.ManagerApiExpect(),
Method: http.MethodPut,
Path: "/apisix/admin/consumers",
@@ -44,65 +59,100 @@ var _ = Describe("Consumer", func() {
"key": "remote_addr",
"policy": "local"
}
- },
- "desc": "test description"
+ }
}`,
Headers: map[string]string{"Authorization": base.GetToken()},
ExpectStatus: http.StatusOK,
- ExpectBody: []string{"\"code\":0", "\"username\":\"consumer_1\""},
+ ExpectBody: `"username":"consumer_1"`,
}),
- Entry("get consumer #1", base.HttpTestCase{
+ Entry("Get consumer (Exist)", base.HttpTestCase{
Object: base.ManagerApiExpect(),
Method: http.MethodGet,
Path: "/apisix/admin/consumers/consumer_1",
Headers: map[string]string{"Authorization": base.GetToken()},
ExpectStatus: http.StatusOK,
- ExpectBody: "\"username\":\"consumer_1\"",
+ ExpectBody: `"username":"consumer_1"`,
}),
- Entry("update consumer", base.HttpTestCase{
+ Entry("List consumer (1 item)", base.HttpTestCase{
+ Object: base.ManagerApiExpect(),
+ Method: http.MethodGet,
+ Path: "/apisix/admin/consumers",
+ Headers: map[string]string{"Authorization": base.GetToken()},
+ ExpectStatus: http.StatusOK,
+ ExpectBody: `"total_size":1`,
+ }),
+ Entry("Create consumer #2", base.HttpTestCase{
Object: base.ManagerApiExpect(),
Method: http.MethodPut,
- Path: "/apisix/admin/consumers/consumer_1",
+ Path: "/apisix/admin/consumers",
Body: `{
- "username": "consumer_1",
+ "username": "consumer_2",
"plugins": {
"limit-count": {
"count": 2,
"time_window": 60,
- "rejected_code": 504,
+ "rejected_code": 503,
"key": "remote_addr",
"policy": "local"
}
- },
- "desc": "test description"
+ }
}`,
Headers: map[string]string{"Authorization": base.GetToken()},
ExpectStatus: http.StatusOK,
- ExpectBody: []string{"\"code\":0", "\"username\":\"consumer_1\"", "\"rejected_code\":504"},
+ ExpectBody: `"username":"consumer_2"`,
}),
- Entry("get consumer #2", base.HttpTestCase{
+ Entry("List consumer (2 items)", base.HttpTestCase{
Object: base.ManagerApiExpect(),
Method: http.MethodGet,
- Path: "/apisix/admin/consumers/consumer_1",
+ Path: "/apisix/admin/consumers",
+ Headers: map[string]string{"Authorization": base.GetToken()},
+ ExpectStatus: http.StatusOK,
+ ExpectBody: `"total_size":2`,
+ }),
+ Entry("List consumer (Paginate)", base.HttpTestCase{
+ Object: base.ManagerApiExpect(),
+ Method: http.MethodGet,
+ Path: "/apisix/admin/consumers",
+ Query: "page=2&page_size=1",
+ Headers: map[string]string{"Authorization": base.GetToken()},
+ ExpectStatus: http.StatusOK,
+ ExpectBody: `"username":"consumer_2"`,
+ }),
+ Entry("Update consumer", base.HttpTestCase{
+ Object: base.ManagerApiExpect(),
+ Method: http.MethodPut,
+ Path: "/apisix/admin/consumers/consumer_1",
+ Body: `{
+ "username": "consumer_1",
+ "plugins": {
+ "limit-count": {
+ "count": 2,
+ "time_window": 60,
+ "rejected_code": 504,
+ "key": "remote_addr",
+ "policy": "local"
+ }
+ }
+ }`,
Headers: map[string]string{"Authorization": base.GetToken()},
ExpectStatus: http.StatusOK,
- ExpectBody: "\"rejected_code\":504",
+ ExpectBody: []string{`"code":0`, `"username":"consumer_1"`, `"rejected_code":504`},
}),
- Entry("delete consumer", base.HttpTestCase{
+ Entry("Batch Delete consumer", base.HttpTestCase{
Object: base.ManagerApiExpect(),
Method: http.MethodDelete,
- Path: "/apisix/admin/consumers/consumer_1",
+ Path: "/apisix/admin/consumers/consumer_1,consumer_2",
Headers: map[string]string{"Authorization": base.GetToken()},
ExpectStatus: http.StatusOK,
- ExpectBody: "\"code\":0",
+ ExpectBody: `"code":0`,
}),
)
- DescribeTable("test consumer curd exception",
+ DescribeTable("Test consumer CURD Exception",
func(tc base.HttpTestCase) {
base.RunTestCase(tc)
},
- Entry("create consumer by POST method", base.HttpTestCase{
+ Entry("Create consumer (POST method)", base.HttpTestCase{
Object: base.ManagerApiExpect(),
Method: http.MethodPost,
Path: "/apisix/admin/consumers",
@@ -123,7 +173,7 @@ var _ = Describe("Consumer", func() {
ExpectStatus: http.StatusNotFound,
ExpectBody: "404 page not found",
}),
- Entry("create consumer with not exist plugin", base.HttpTestCase{
+ Entry("Create consumer (Not Exist Plugin)", base.HttpTestCase{
Object: base.ManagerApiExpect(),
Method: http.MethodPut,
Path: "/apisix/admin/consumers",
@@ -140,7 +190,7 @@ var _ = Describe("Consumer", func() {
ExpectStatus: http.StatusBadRequest,
ExpectBody: "schema validate failed: schema not found, path: plugins.key-authnotexist",
}),
- Entry("delete consumer (as delete not exist consumer)", base.HttpTestCase{
+ Entry("Delete consumer (as delete not exist consumer)", base.HttpTestCase{
Object: base.ManagerApiExpect(),
Method: http.MethodDelete,
Path: "/apisix/admin/consumers/test",