juzhiyuan commented on a change in pull request #859: URL: https://github.com/apache/apisix-dashboard/pull/859#discussion_r533123881
########## File path: api/internal/handler/consumer/consumer_test.go ########## @@ -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) - -} +} Review comment: new line ########## File path: api/internal/handler/consumer/consumer_test.go ########## @@ -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) - -} +} Review comment: new line ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org