Skip to content

Commit

Permalink
fix: delete POST method in /apisix/admin/consumer (#852)
Browse files Browse the repository at this point in the history
  • Loading branch information
starsz committed Nov 26, 2020
1 parent 1a38fa6 commit 6f837a7
Show file tree
Hide file tree
Showing 4 changed files with 235 additions and 44 deletions.
38 changes: 7 additions & 31 deletions api/internal/handler/consumer/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,10 @@ 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.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.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.DELETE("/apisix/admin/consumers/:usernames", wgin.Wraps(h.BatchDelete,
wrapper.InputType(reflect.TypeOf(BatchDelete{}))))
}
Expand Down Expand Up @@ -97,35 +95,13 @@ func (h *Handler) List(c droplet.Context) (interface{}, error) {
return ret, nil
}

func (h *Handler) Create(c droplet.Context) (interface{}, error) {
input := c.Input().(*entity.Consumer)
if input.ID != "" && 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 {
type SetInput struct {
Username string `auto_read:"username,path"`
entity.Consumer
}

func (h *Handler) Update(c droplet.Context) (interface{}, error) {
input := c.Input().(*UpdateInput)
func (h *Handler) Set(c droplet.Context) (interface{}, error) {
input := c.Input().(*SetInput)
if input.ID != "" && input.ID != input.Username {
return &data.SpecCodeResponse{StatusCode: http.StatusBadRequest},
fmt.Errorf("consumer's id and username must be a same value")
Expand Down
22 changes: 11 additions & 11 deletions api/internal/handler/consumer/consumer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestConsumer(t *testing.T) {

//create consumer
ctx := droplet.NewContext()
consumer := &entity.Consumer{}
consumer := &SetInput{}
reqBody := `{
"username": "jack",
"plugins": {
Expand All @@ -60,11 +60,11 @@ func TestConsumer(t *testing.T) {
err = json.Unmarshal([]byte(reqBody), consumer)
assert.Nil(t, err)
ctx.SetInput(consumer)
_, err = handler.Create(ctx)
_, err = handler.Set(ctx)
assert.Nil(t, err)

//create consumer 2
consumer2 := &entity.Consumer{}
consumer2 := &SetInput{}
reqBody = `{
"username": "pony",
"plugins": {
Expand All @@ -80,7 +80,7 @@ func TestConsumer(t *testing.T) {
err = json.Unmarshal([]byte(reqBody), consumer2)
assert.Nil(t, err)
ctx.SetInput(consumer2)
_, err = handler.Create(ctx)
_, err = handler.Set(ctx)
assert.Nil(t, err)

//sleep
Expand All @@ -96,10 +96,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.Username)
assert.Equal(t, stored.Username, consumer.Consumer.Username)

//update consumer
consumer3 := &UpdateInput{}
consumer3 := &SetInput{}
consumer3.Username = "pony"
reqBody = `{
"username": "pony",
Expand All @@ -116,7 +116,7 @@ func TestConsumer(t *testing.T) {
err = json.Unmarshal([]byte(reqBody), consumer3)
assert.Nil(t, err)
ctx.SetInput(consumer3)
_, err = handler.Update(ctx)
_, err = handler.Set(ctx)
assert.Nil(t, err)

//sleep
Expand Down Expand Up @@ -195,7 +195,7 @@ func TestConsumer(t *testing.T) {
assert.Nil(t, err)

//create consumer fail
consumer_fail := &entity.Consumer{}
consumer_fail := &SetInput{}
reqBody = `{
"plugins": {
"limit-count": {
Expand All @@ -210,11 +210,11 @@ func TestConsumer(t *testing.T) {
err = json.Unmarshal([]byte(reqBody), consumer_fail)
assert.Nil(t, err)
ctx.SetInput(consumer_fail)
_, err = handler.Create(ctx)
_, err = handler.Set(ctx)
assert.NotNil(t, err)

//create consumer using Update
consumer6 := &UpdateInput{}
consumer6 := &SetInput{}
reqBody = `{
"username": "nnn",
"plugins": {
Expand All @@ -230,7 +230,7 @@ func TestConsumer(t *testing.T) {
err = json.Unmarshal([]byte(reqBody), consumer6)
assert.Nil(t, err)
ctx.SetInput(consumer6)
_, err = handler.Update(ctx)
_, err = handler.Set(ctx)
assert.Nil(t, err)

//sleep
Expand Down
18 changes: 16 additions & 2 deletions api/test/e2e/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"io/ioutil"
"net"
"net/http"
"sync"
"testing"
"time"

Expand All @@ -32,6 +33,11 @@ import (

var token string

var MExpect *httpexpect.Expect
var AExpect *httpexpect.Expect
var MOnce sync.Once
var AOnce sync.Once

func init() {
//login to get auth token
requestBody := []byte(`{
Expand Down Expand Up @@ -63,11 +69,19 @@ func init() {
}

func MangerApiExpect(t *testing.T) *httpexpect.Expect {
return httpexpect.New(t, "http://127.0.0.1:8080")
MOnce.Do(func() {
MExpect = httpexpect.New(t, "http://127.0.0.1:8080")
})

return MExpect
}

func APISIXExpect(t *testing.T) *httpexpect.Expect {
return httpexpect.New(t, "http://127.0.0.1:9080")
AOnce.Do(func() {
AExpect = httpexpect.New(t, "http://127.0.0.1:9080")
})

return AExpect
}

func APISIXHTTPSExpect(t *testing.T) *httpexpect.Expect {
Expand Down
201 changes: 201 additions & 0 deletions api/test/e2e/consumer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
/*
* 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"
)

func TestConsumer_Create_And_Get(t *testing.T) {
tests := []HttpTestCase{
{
caseDesc: "check consumer is not exist",
Object: MangerApiExpect(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: MangerApiExpect(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: MangerApiExpect(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: MangerApiExpect(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: MangerApiExpect(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: MangerApiExpect(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: MangerApiExpect(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: MangerApiExpect(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: MangerApiExpect(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: MangerApiExpect(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: MangerApiExpect(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)
}
}

0 comments on commit 6f837a7

Please sign in to comment.