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 2020/12/11 07:48:20 UTC

[apisix-dashboard] branch master updated: Revert "fix: delete POST method in /apisix/admin/consumer (#852) (#868)" (#1019)

This is an automated email from the ASF dual-hosted git repository.

juzhiyuan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix-dashboard.git


The following commit(s) were added to refs/heads/master by this push:
     new 84ff9f8  Revert "fix: delete POST method in /apisix/admin/consumer (#852) (#868)" (#1019)
84ff9f8 is described below

commit 84ff9f8c87746f236f71b49b4ae77a07722f0f5e
Author: 琚致远 <ju...@apache.org>
AuthorDate: Fri Dec 11 15:48:12 2020 +0800

    Revert "fix: delete POST method in /apisix/admin/consumer (#852) (#868)" (#1019)
    
    This reverts commit 78db532402a86cbdc4e8d3a58396f126bf313695.
---
 api/internal/handler/consumer/consumer.go      |  40 ++++--
 api/internal/handler/consumer/consumer_test.go |  22 +--
 api/test/e2e/consumer_test.go                  | 179 -------------------------
 3 files changed, 43 insertions(+), 198 deletions(-)

diff --git a/api/internal/handler/consumer/consumer.go b/api/internal/handler/consumer/consumer.go
index ab31aa3..79d6f8b 100644
--- a/api/internal/handler/consumer/consumer.go
+++ b/api/internal/handler/consumer/consumer.go
@@ -49,10 +49,12 @@ func (h *Handler) ApplyRoute(r *gin.Engine) {
 		wrapper.InputType(reflect.TypeOf(GetInput{}))))
 	r.GET("/apisix/admin/consumers", wgin.Wraps(h.List,
 		wrapper.InputType(reflect.TypeOf(ListInput{}))))
-	r.PUT("/apisix/admin/consumers/:username", wgin.Wraps(h.Set,
-		wrapper.InputType(reflect.TypeOf(SetInput{}))))
-	r.PUT("/apisix/admin/consumers", wgin.Wraps(h.Set,
-		wrapper.InputType(reflect.TypeOf(SetInput{}))))
+	r.POST("/apisix/admin/consumers", wgin.Wraps(h.Create,
+		wrapper.InputType(reflect.TypeOf(entity.Consumer{}))))
+	r.PUT("/apisix/admin/consumers/:username", wgin.Wraps(h.Update,
+		wrapper.InputType(reflect.TypeOf(UpdateInput{}))))
+	r.PUT("/apisix/admin/consumers", wgin.Wraps(h.Update,
+		wrapper.InputType(reflect.TypeOf(UpdateInput{}))))
 	r.DELETE("/apisix/admin/consumers/:usernames", wgin.Wraps(h.BatchDelete,
 		wrapper.InputType(reflect.TypeOf(BatchDelete{}))))
 }
@@ -96,13 +98,35 @@ func (h *Handler) List(c droplet.Context) (interface{}, error) {
 	return ret, nil
 }
 
-type SetInput struct {
-	entity.Consumer
+func (h *Handler) Create(c droplet.Context) (interface{}, error) {
+	input := c.Input().(*entity.Consumer)
+	if input.ID != nil && utils.InterfaceToString(input.ID) != input.Username {
+		return &data.SpecCodeResponse{StatusCode: http.StatusBadRequest},
+			fmt.Errorf("consumer's id and username must be a same value")
+	}
+	input.ID = input.Username
+
+	if _, ok := input.Plugins["jwt-auth"]; ok {
+		jwt := input.Plugins["jwt-auth"].(map[string]interface{})
+		jwt["exp"] = 86400
+
+		input.Plugins["jwt-auth"] = jwt
+	}
+
+	if err := h.consumerStore.Create(c.Context(), input); err != nil {
+		return handler.SpecCodeResponse(err), err
+	}
+
+	return nil, nil
+}
+
+type UpdateInput struct {
 	Username string `auto_read:"username,path"`
+	entity.Consumer
 }
 
-func (h *Handler) Set(c droplet.Context) (interface{}, error) {
-	input := c.Input().(*SetInput)
+func (h *Handler) Update(c droplet.Context) (interface{}, error) {
+	input := c.Input().(*UpdateInput)
 	if input.ID != nil && utils.InterfaceToString(input.ID) != input.Username {
 		return &data.SpecCodeResponse{StatusCode: http.StatusBadRequest},
 			fmt.Errorf("consumer's id and username must be a same value")
diff --git a/api/internal/handler/consumer/consumer_test.go b/api/internal/handler/consumer/consumer_test.go
index 185fad8..ba02f28 100644
--- a/api/internal/handler/consumer/consumer_test.go
+++ b/api/internal/handler/consumer/consumer_test.go
@@ -46,7 +46,7 @@ func TestConsumer(t *testing.T) {
 
 	//create consumer
 	ctx := droplet.NewContext()
-	consumer := &SetInput{}
+	consumer := &entity.Consumer{}
 	reqBody := `{
       "username": "jack",
       "plugins": {
@@ -62,11 +62,11 @@ func TestConsumer(t *testing.T) {
 	err = json.Unmarshal([]byte(reqBody), consumer)
 	assert.Nil(t, err)
 	ctx.SetInput(consumer)
-	_, err = handler.Set(ctx)
+	_, err = handler.Create(ctx)
 	assert.Nil(t, err)
 
 	//create consumer 2
-	consumer2 := &SetInput{}
+	consumer2 := &entity.Consumer{}
 	reqBody = `{
 		"username": "pony",
 		"plugins": {
@@ -82,7 +82,7 @@ func TestConsumer(t *testing.T) {
 	err = json.Unmarshal([]byte(reqBody), consumer2)
 	assert.Nil(t, err)
 	ctx.SetInput(consumer2)
-	_, err = handler.Set(ctx)
+	_, err = handler.Create(ctx)
 	assert.Nil(t, err)
 
 	//sleep
@@ -98,10 +98,10 @@ func TestConsumer(t *testing.T) {
 	stored := ret.(*entity.Consumer)
 	assert.Nil(t, err)
 	assert.Equal(t, stored.ID, consumer.ID)
-	assert.Equal(t, stored.Username, consumer.Consumer.Username)
+	assert.Equal(t, stored.Username, consumer.Username)
 
 	//update consumer
-	consumer3 := &SetInput{}
+	consumer3 := &UpdateInput{}
 	consumer3.Username = "pony"
 	reqBody = `{
 		"username": "pony",
@@ -118,7 +118,7 @@ func TestConsumer(t *testing.T) {
 	err = json.Unmarshal([]byte(reqBody), consumer3)
 	assert.Nil(t, err)
 	ctx.SetInput(consumer3)
-	_, err = handler.Set(ctx)
+	_, err = handler.Update(ctx)
 	assert.Nil(t, err)
 
 	//sleep
@@ -197,7 +197,7 @@ func TestConsumer(t *testing.T) {
 	assert.Nil(t, err)
 
 	//create consumer fail
-	consumer_fail := &SetInput{}
+	consumer_fail := &entity.Consumer{}
 	reqBody = `{
       "plugins": {
           "limit-count": {
@@ -212,11 +212,11 @@ func TestConsumer(t *testing.T) {
 	err = json.Unmarshal([]byte(reqBody), consumer_fail)
 	assert.Nil(t, err)
 	ctx.SetInput(consumer_fail)
-	_, err = handler.Set(ctx)
+	_, err = handler.Create(ctx)
 	assert.NotNil(t, err)
 
 	//create consumer using Update
-	consumer6 := &SetInput{}
+	consumer6 := &UpdateInput{}
 	reqBody = `{
       "username": "nnn",
       "plugins": {
@@ -232,7 +232,7 @@ func TestConsumer(t *testing.T) {
 	err = json.Unmarshal([]byte(reqBody), consumer6)
 	assert.Nil(t, err)
 	ctx.SetInput(consumer6)
-	_, err = handler.Set(ctx)
+	_, err = handler.Update(ctx)
 	assert.Nil(t, err)
 
 	//sleep
diff --git a/api/test/e2e/consumer_test.go b/api/test/e2e/consumer_test.go
index 21d6f45..e7ba231 100644
--- a/api/test/e2e/consumer_test.go
+++ b/api/test/e2e/consumer_test.go
@@ -27,185 +27,6 @@ import (
 	"github.com/tidwall/gjson"
 )
 
-func TestConsumer_Create_And_Get(t *testing.T) {
-	tests := []HttpTestCase{
-		{
-			caseDesc:     "check consumer is not exist",
-			Object:       ManagerApiExpect(t),
-			Path:         "/apisix/admin/consumers/consumer_1",
-			Method:       http.MethodGet,
-			Headers:      map[string]string{"Authorization": token},
-			ExpectStatus: http.StatusNotFound,
-			ExpectBody:   "data not found",
-		},
-		{
-			caseDesc:     "check consumer is not exist",
-			Object:       ManagerApiExpect(t),
-			Path:         "/apisix/admin/consumers/consumer_2",
-			Method:       http.MethodGet,
-			Headers:      map[string]string{"Authorization": token},
-			ExpectStatus: http.StatusNotFound,
-			ExpectBody:   "data not found",
-		},
-		{
-			caseDesc: "create consumer by POST",
-			Object:   ManagerApiExpect(t),
-			Path:     "/apisix/admin/consumers",
-			Method:   http.MethodPost,
-			Body: `{
-				"username": "consumer_1",
-				"plugins": {
-					"limit-count": {
-					"count": 2,
-					"time_window": 60,
-					"rejected_code": 503,
-					"key": "remote_addr"
-					}
-				},
-				"desc": "test description"
-			}`,
-			Headers:      map[string]string{"Authorization": token},
-			ExpectStatus: http.StatusNotFound,
-			ExpectBody:   "404 page not found",
-		},
-		{
-			caseDesc: "create consumer by PUT",
-			Object:   ManagerApiExpect(t),
-			Path:     "/apisix/admin/consumers",
-			Method:   http.MethodPut,
-			Body: `{
-				"username": "consumer_2",
-				"plugins": {
-					"limit-count": {
-					"count": 2,
-					"time_window": 60,
-					"rejected_code": 503,
-					"key": "remote_addr"
-					}
-				},
-				"desc": "test description"
-			}`,
-			Headers:      map[string]string{"Authorization": token},
-			ExpectStatus: http.StatusOK,
-			ExpectBody:   "\"code\":0",
-			Sleep:        sleepTime,
-		},
-		{
-			caseDesc:     "get consumer",
-			Object:       ManagerApiExpect(t),
-			Path:         "/apisix/admin/consumers/consumer_2",
-			Method:       http.MethodGet,
-			Headers:      map[string]string{"Authorization": token},
-			ExpectStatus: http.StatusOK,
-			ExpectBody:   "\"username\":\"consumer_2\"",
-		},
-		{
-			caseDesc: "create consumer without username",
-			Object:   ManagerApiExpect(t),
-			Path:     "/apisix/admin/consumers",
-			Method:   http.MethodPut,
-			Body: `{
-				"plugins": {
-					"limit-count": {
-					"count": 2,
-					"time_window": 60,
-					"rejected_code": 503,
-					"key": "remote_addr"
-					}
-				},
-				"desc": "test description"
-			}`,
-			Headers:      map[string]string{"Authorization": token},
-			ExpectStatus: http.StatusBadRequest,
-			ExpectBody:   "\"code\":10000",
-		},
-		{
-			caseDesc:     "delete consumer",
-			Object:       ManagerApiExpect(t),
-			Path:         "/apisix/admin/consumers/consumer_2",
-			Method:       http.MethodDelete,
-			Headers:      map[string]string{"Authorization": token},
-			ExpectStatus: http.StatusOK,
-			ExpectBody:   "\"code\":0",
-		},
-	}
-
-	for _, tc := range tests {
-		testCaseCheck(tc)
-	}
-}
-
-func TestConsumer_Update_And_Get(t *testing.T) {
-	tests := []HttpTestCase{
-		{
-			caseDesc: "create consumer by PUT",
-			Object:   ManagerApiExpect(t),
-			Path:     "/apisix/admin/consumers",
-			Method:   http.MethodPut,
-			Body: `{
-				"username": "consumer_3",
-				"plugins": {
-					"limit-count": {
-					"count": 2,
-					"time_window": 60,
-					"rejected_code": 503,
-					"key": "remote_addr"
-					}
-				},
-				"desc": "test description"
-			}`,
-			Headers:      map[string]string{"Authorization": token},
-			ExpectStatus: http.StatusOK,
-			ExpectBody:   "\"code\":0",
-			Sleep:        sleepTime,
-		},
-		{
-			caseDesc: "update consumer by PUT",
-			Object:   ManagerApiExpect(t),
-			Path:     "/apisix/admin/consumers/consumer_3",
-			Method:   http.MethodPut,
-			Body: `{
-				"username": "consumer_3",
-				"plugins": {
-					"limit-count": {
-					"count": 2,
-					"time_window": 60,
-					"rejected_code": 504,
-					"key": "remote_addr"
-					}
-				},
-				"desc": "test description"
-			}`,
-			Headers:      map[string]string{"Authorization": token},
-			ExpectStatus: http.StatusOK,
-			ExpectBody:   "\"code\":0",
-			Sleep:        sleepTime,
-		},
-		{
-			caseDesc:     "get consumer",
-			Object:       ManagerApiExpect(t),
-			Path:         "/apisix/admin/consumers/consumer_3",
-			Method:       http.MethodGet,
-			Headers:      map[string]string{"Authorization": token},
-			ExpectStatus: http.StatusOK,
-			ExpectBody:   "\"rejected_code\":504",
-		},
-		{
-			caseDesc:     "delete consumer",
-			Object:       ManagerApiExpect(t),
-			Path:         "/apisix/admin/consumers/consumer_3",
-			Method:       http.MethodDelete,
-			Headers:      map[string]string{"Authorization": token},
-			ExpectStatus: http.StatusOK,
-			ExpectBody:   "\"code\":0",
-		},
-	}
-
-	for _, tc := range tests {
-		testCaseCheck(tc)
-	}
-}
-
 func TestConsumer_with_key_auth(t *testing.T) {
 	tests := []HttpTestCase{
 		{