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

feat: add a unit test for consumer and remove implicit init #859

Merged
merged 16 commits into from
Dec 11, 2020
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
226 changes: 3 additions & 223 deletions api/internal/handler/consumer/consumer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,15 @@ package consumer

import (
"context"
"encoding/json"
"fmt"
"github.com/apisix/manager-api/internal/core/storage"
"github.com/apisix/manager-api/internal/core/entity"
"github.com/apisix/manager-api/internal/core/store"
"github.com/shiningrush/droplet"
"github.com/shiningrush/droplet/data"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"net/http"
"testing"
"time"

"github.com/apisix/manager-api/internal/core/entity"
"github.com/apisix/manager-api/internal/core/store"
)

func TestHandler_Get(t *testing.T) {
Expand Down Expand Up @@ -399,220 +395,4 @@ func TestHandler_BatchDelete(t *testing.T) {
assert.Equal(t, tc.wantErr, err, tc.caseDesc)
assert.Equal(t, tc.wantRet, ret, tc.caseDesc)
}
}

func TestConsumer(t *testing.T) {
// init
err := storage.InitETCDClient([]string{"127.0.0.1:2379"})
assert.Nil(t, err)
err = store.InitStores()
assert.Nil(t, err)

handler := &Handler{
consumerStore: store.GetStore(store.HubKeyConsumer),
}
assert.NotNil(t, handler)

//create consumer
ctx := droplet.NewContext()
consumer := &entity.Consumer{}
reqBody := `{
"username": "jack",
"plugins": {
"limit-count": {
"count": 2,
"time_window": 60,
"rejected_code": 503,
"key": "remote_addr"
}
},
"desc": "test description"
}`
err = json.Unmarshal([]byte(reqBody), consumer)
assert.Nil(t, err)
ctx.SetInput(consumer)
_, err = handler.Create(ctx)
assert.Nil(t, err)

//create consumer 2
consumer2 := &entity.Consumer{}
reqBody = `{
"username": "pony",
"plugins": {
"limit-count": {
"count": 2,
"time_window": 60,
"rejected_code": 503,
"key": "remote_addr"
}
},
"desc": "test description"
}`
err = json.Unmarshal([]byte(reqBody), consumer2)
assert.Nil(t, err)
ctx.SetInput(consumer2)
_, err = handler.Create(ctx)
assert.Nil(t, err)

//sleep
time.Sleep(time.Duration(100) * time.Millisecond)

//get consumer
input := &GetInput{}
reqBody = `{"username": "jack"}`
err = json.Unmarshal([]byte(reqBody), input)
assert.Nil(t, err)
ctx.SetInput(input)
ret, err := handler.Get(ctx)
stored := ret.(*entity.Consumer)
assert.Nil(t, err)
assert.Equal(t, stored.ID, consumer.ID)
assert.Equal(t, stored.Username, consumer.Username)

//update consumer
consumer3 := &UpdateInput{}
consumer3.Username = "pony"
reqBody = `{
"username": "pony",
"plugins": {
"limit-count": {
"count": 2,
"time_window": 60,
"rejected_code": 503,
"key": "remote_addr"
}
},
"desc": "test description2"
}`
err = json.Unmarshal([]byte(reqBody), consumer3)
assert.Nil(t, err)
ctx.SetInput(consumer3)
_, err = handler.Update(ctx)
assert.Nil(t, err)

//sleep
time.Sleep(time.Duration(100) * time.Millisecond)

//check update
input3 := &GetInput{}
reqBody = `{"username": "pony"}`
err = json.Unmarshal([]byte(reqBody), input3)
assert.Nil(t, err)
ctx.SetInput(input3)
ret3, err := handler.Get(ctx)
stored3 := ret3.(*entity.Consumer)
assert.Nil(t, err)
assert.Equal(t, stored3.Desc, "test description2") //consumer3.Desc)
assert.Equal(t, stored3.Username, consumer3.Username)

//list page 1
listInput := &ListInput{}
reqBody = `{"page_size": 1, "page": 1}`
err = json.Unmarshal([]byte(reqBody), listInput)
assert.Nil(t, err)
ctx.SetInput(listInput)
retPage1, err := handler.List(ctx)
assert.Nil(t, err)
dataPage1 := retPage1.(*store.ListOutput)
assert.Equal(t, len(dataPage1.Rows), 1)

//list page 2
listInput2 := &ListInput{}
reqBody = `{"page_size": 1, "page": 2}`
err = json.Unmarshal([]byte(reqBody), listInput2)
assert.Nil(t, err)
ctx.SetInput(listInput2)
retPage2, err := handler.List(ctx)
assert.Nil(t, err)
dataPage2 := retPage2.(*store.ListOutput)
assert.Equal(t, len(dataPage2.Rows), 1)

//list search match
listInput3 := &ListInput{}
reqBody = `{"page_size": 1, "page": 1, "username": "pony"}`
err = json.Unmarshal([]byte(reqBody), listInput3)
assert.Nil(t, err)
ctx.SetInput(listInput3)
retPage, err := handler.List(ctx)
assert.Nil(t, err)
dataPage := retPage.(*store.ListOutput)
assert.Equal(t, len(dataPage.Rows), 1)

//list search not match
listInput4 := &ListInput{}
reqBody = `{"page_size": 1, "page": 1, "username": "not-exists"}`
err = json.Unmarshal([]byte(reqBody), listInput4)
assert.Nil(t, err)
ctx.SetInput(listInput4)
retPage, err = handler.List(ctx)
assert.Nil(t, err)
dataPage = retPage.(*store.ListOutput)
assert.Equal(t, len(dataPage.Rows), 0)

//delete consumer
inputDel := &BatchDeleteInput{}
reqBody = `{"usernames": "jack"}`
err = json.Unmarshal([]byte(reqBody), inputDel)
assert.Nil(t, err)
ctx.SetInput(inputDel)
_, err = handler.BatchDelete(ctx)
assert.Nil(t, err)

reqBody = `{"usernames": "pony"}`
err = json.Unmarshal([]byte(reqBody), inputDel)
assert.Nil(t, err)
ctx.SetInput(inputDel)
_, err = handler.BatchDelete(ctx)
assert.Nil(t, err)

//create consumer fail
consumer_fail := &entity.Consumer{}
reqBody = `{
"plugins": {
"limit-count": {
"count": 2,
"time_window": 60,
"rejected_code": 503,
"key": "remote_addr"
}
},
"desc": "test description"
}`
err = json.Unmarshal([]byte(reqBody), consumer_fail)
assert.Nil(t, err)
ctx.SetInput(consumer_fail)
_, err = handler.Create(ctx)
assert.NotNil(t, err)

//create consumer using Update
consumer6 := &UpdateInput{}
reqBody = `{
"username": "nnn",
"plugins": {
"limit-count": {
"count": 2,
"time_window": 60,
"rejected_code": 503,
"key": "remote_addr"
}
},
"desc": "test description"
}`
err = json.Unmarshal([]byte(reqBody), consumer6)
assert.Nil(t, err)
ctx.SetInput(consumer6)
_, err = handler.Update(ctx)
assert.Nil(t, err)

//sleep
time.Sleep(time.Duration(100) * time.Millisecond)

//delete consumer
reqBody = `{"usernames": "nnn"}`
err = json.Unmarshal([]byte(reqBody), inputDel)
assert.Nil(t, err)
ctx.SetInput(inputDel)
_, err = handler.BatchDelete(ctx)
assert.Nil(t, err)

}
}
juzhiyuan marked this conversation as resolved.
Show resolved Hide resolved
juzhiyuan marked this conversation as resolved.
Show resolved Hide resolved