Skip to content

Commit

Permalink
Fix backend Unit and e2e test
Browse files Browse the repository at this point in the history
Signed-off-by: imjoey <[email protected]>
  • Loading branch information
imjoey committed Jan 18, 2021
1 parent a623b98 commit dda9c8c
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 38 deletions.
56 changes: 34 additions & 22 deletions api/internal/handler/consumer/consumer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ func TestHandler_Create(t *testing.T) {
giveInput *SetInput
giveCtx context.Context
giveErr error
giveRet interface{}
wantErr error
wantInput *SetInput
wantRet interface{}
Expand All @@ -193,6 +194,17 @@ func TestHandler_Create(t *testing.T) {
},
},
giveCtx: context.WithValue(context.Background(), "test", "value"),
giveRet: &entity.Consumer{
BaseInfo: entity.BaseInfo{
ID: "name",
},
Username: "name",
Plugins: map[string]interface{}{
"jwt-auth": map[string]interface{}{
"exp": 86400,
},
},
},
wantInput: &SetInput{
Consumer: entity.Consumer{
BaseInfo: entity.BaseInfo{
Expand Down Expand Up @@ -231,6 +243,9 @@ func TestHandler_Create(t *testing.T) {
},
},
},
giveRet: &data.SpecCodeResponse{
StatusCode: http.StatusInternalServerError,
},
giveErr: fmt.Errorf("create failed"),
wantInput: &SetInput{
Consumer: entity.Consumer{
Expand Down Expand Up @@ -262,24 +277,15 @@ func TestHandler_Create(t *testing.T) {
assert.Equal(t, tc.giveCtx, args.Get(0))
assert.Equal(t, &tc.wantInput.Consumer, args.Get(1))
assert.True(t, args.Bool(2))
}).Return(tc.giveErr)
}).Return(tc.giveRet, tc.giveErr)

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)
// if ret is entity.Consumer, need to ignore
// create_time and update_time before assertion
if retObj, ok := ret.(*entity.Consumer); ok {
retObj.BaseInfo.CreateTime = 0
retObj.BaseInfo.UpdateTime = 0
assert.Equal(t, tc.wantRet, retObj)
} else {
// tc.wantRet is *data.SpecCodeResponse
assert.Equal(t, tc.wantRet, ret)
}
assert.Equal(t, tc.wantRet, ret)
assert.Equal(t, tc.wantErr, err)
})
}
Expand All @@ -290,6 +296,7 @@ func TestHandler_Update(t *testing.T) {
caseDesc string
giveInput *SetInput
giveCtx context.Context
giveRet interface{}
giveErr error
wantErr error
wantInput *entity.Consumer
Expand All @@ -309,6 +316,17 @@ func TestHandler_Update(t *testing.T) {
},
},
giveCtx: context.WithValue(context.Background(), "test", "value"),
giveRet: &entity.Consumer{
BaseInfo: entity.BaseInfo{
ID: "name",
},
Username: "name",
Plugins: map[string]interface{}{
"jwt-auth": map[string]interface{}{
"exp": 500,
},
},
},
wantInput: &entity.Consumer{
BaseInfo: entity.BaseInfo{
ID: "name",
Expand Down Expand Up @@ -343,6 +361,9 @@ func TestHandler_Update(t *testing.T) {
},
},
},
giveRet: &data.SpecCodeResponse{
StatusCode: http.StatusInternalServerError,
},
giveErr: fmt.Errorf("create failed"),
wantInput: &entity.Consumer{
BaseInfo: entity.BaseInfo{
Expand Down Expand Up @@ -372,24 +393,15 @@ func TestHandler_Update(t *testing.T) {
assert.Equal(t, tc.giveCtx, args.Get(0))
assert.Equal(t, tc.wantInput, args.Get(1))
assert.True(t, args.Bool(2))
}).Return(tc.giveErr)
}).Return(tc.giveRet, tc.giveErr)

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)
// if ret is entity.Consumer, need to ignore
// create_time and update_time before assertion
if retObj, ok := ret.(*entity.Consumer); ok {
retObj.BaseInfo.CreateTime = 0
retObj.BaseInfo.UpdateTime = 0
assert.Equal(t, tc.wantRet, retObj)
} else {
// tc.wantRet is *data.SpecCodeResponse
assert.Equal(t, tc.wantRet, ret)
}
assert.Equal(t, tc.wantRet, ret)
assert.Equal(t, tc.wantErr, err)
})
}
Expand Down
23 changes: 12 additions & 11 deletions api/internal/handler/global_rule/global_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ func TestHandler_Set(t *testing.T) {
caseDesc string
giveInput *SetInput
giveCtx context.Context
giveRet interface{}
giveErr error
wantErr error
wantInput *entity.GlobalPlugins
Expand All @@ -228,6 +229,12 @@ func TestHandler_Set(t *testing.T) {
},
},
giveCtx: context.WithValue(context.Background(), "test", "value"),
giveRet: &entity.GlobalPlugins{
BaseInfo: entity.BaseInfo{ID: "name"},
Plugins: map[string]interface{}{
"jwt-auth": map[string]interface{}{},
},
},
wantInput: &entity.GlobalPlugins{
BaseInfo: entity.BaseInfo{ID: "name"},
Plugins: map[string]interface{}{
Expand All @@ -249,6 +256,9 @@ func TestHandler_Set(t *testing.T) {
GlobalPlugins: entity.GlobalPlugins{},
},
giveErr: fmt.Errorf("create failed"),
giveRet: &data.SpecCodeResponse{
StatusCode: http.StatusInternalServerError,
},
wantInput: &entity.GlobalPlugins{
BaseInfo: entity.BaseInfo{ID: "name"},
Plugins: map[string]interface{}(nil),
Expand All @@ -270,24 +280,15 @@ func TestHandler_Set(t *testing.T) {
assert.Equal(t, tc.giveCtx, args.Get(0))
assert.Equal(t, tc.wantInput, args.Get(1))
assert.True(t, args.Bool(2))
}).Return(tc.giveErr)
}).Return(tc.giveRet, tc.giveErr)

h := Handler{globalRuleStore: mStore}
ctx := droplet.NewContext()
ctx.SetInput(tc.giveInput)
ctx.SetContext(tc.giveCtx)
ret, err := h.Set(ctx)
assert.Equal(t, tc.wantCalled, methodCalled)
// if ret is entity.GlobalPlugins, need to ignore
// create_time and update_time before assertion
if retObj, ok := ret.(*entity.GlobalPlugins); ok {
retObj.BaseInfo.CreateTime = 0
retObj.BaseInfo.UpdateTime = 0
assert.Equal(t, tc.wantRet, retObj)
} else {
// tc.wantRet is *data.SpecCodeResponse
assert.Equal(t, tc.wantRet, ret)
}
assert.Equal(t, tc.wantRet, ret)
assert.Equal(t, tc.wantErr, err)
})
}
Expand Down
2 changes: 1 addition & 1 deletion api/internal/handler/route/route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1350,7 +1350,7 @@ func Test_Route_With_Script_Dag2lua(t *testing.T) {
err = json.Unmarshal([]byte(reqBody), route2)
assert.Nil(t, err)
ctx.SetInput(route2)
_, err = handler.Update(ctx)
ret, err = handler.Update(ctx)
assert.Nil(t, err)
// check the returned value
objRet, ok = ret.(*entity.Route)
Expand Down
2 changes: 1 addition & 1 deletion api/internal/handler/ssl/ssl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func TestSSL(t *testing.T) {
err = json.Unmarshal([]byte(reqBody), ssl2)
assert.Nil(t, err)
ctx.SetInput(ssl2)
_, err = handler.Update(ctx)
ret, err = handler.Update(ctx)
assert.Nil(t, err)
// check the returned value
objRet, ok = ret.(*entity.SSL)
Expand Down
6 changes: 3 additions & 3 deletions api/test/e2e/route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func TestRoute_Create_With_Hosts(t *testing.T) {
}`,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
ExpectBody: []string{"\"id\":\"1\"", "\"uri\":\"/hello_\""},
ExpectBody: []string{"\"id\":\"r1\"", "\"uri\":\"/hello_\""},
},
{
Desc: "create route with int uri",
Expand Down Expand Up @@ -228,7 +228,7 @@ func TestRoute_Update_Routes_With_Hosts(t *testing.T) {
}`,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
ExpectBody: []string{"\"id\":\"1\"", "\"hosts\":[\"foo.com\"]"},
ExpectBody: []string{"\"id\":\"r1\"", "\"hosts\":[\"foo.com\"]"},
},
{
Desc: "hit the route just create",
Expand Down Expand Up @@ -256,7 +256,7 @@ func TestRoute_Update_Routes_With_Hosts(t *testing.T) {
}`,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
ExpectBody: []string{"\"id\":\"1\"", "\"hosts\":[\"bar.com\"]"},
ExpectBody: []string{"\"id\":\"r1\"", "\"hosts\":[\"bar.com\"]"},
},
{
Desc: "hit the route with host foo.com",
Expand Down

0 comments on commit dda9c8c

Please sign in to comment.