|
| 1 | +/* |
| 2 | +Copyright 2024 The west2-online Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
1 | 17 | package api |
| 18 | + |
| 19 | +import ( |
| 20 | + "bytes" |
| 21 | + "context" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/bytedance/mockey" |
| 25 | + "github.com/cloudwego/hertz/pkg/common/config" |
| 26 | + "github.com/cloudwego/hertz/pkg/common/ut" |
| 27 | + "github.com/cloudwego/hertz/pkg/protocol/consts" |
| 28 | + "github.com/cloudwego/hertz/pkg/route" |
| 29 | + "github.com/stretchr/testify/assert" |
| 30 | + |
| 31 | + "github.com/west2-online/fzuhelper-server/api/rpc" |
| 32 | + oa "github.com/west2-online/fzuhelper-server/kitex_gen/oa" |
| 33 | + "github.com/west2-online/fzuhelper-server/pkg/errno" |
| 34 | +) |
| 35 | + |
| 36 | +func TestCreateFeedback(t *testing.T) { |
| 37 | + type testCase struct { |
| 38 | + name string |
| 39 | + body string |
| 40 | + mockRPCError error |
| 41 | + expectingError bool |
| 42 | + expectingMsg string |
| 43 | + url string |
| 44 | + } |
| 45 | + |
| 46 | + okBody := `{ |
| 47 | + "reportId": 1, |
| 48 | + "stuId": "102301000", |
| 49 | + "name": "张三", |
| 50 | + "college": "计算机与大数据学院", |
| 51 | + "contactPhone": "13800000000", |
| 52 | + "contactQQ": "10001", |
| 53 | + "contactEmail": "a@b.com", |
| 54 | + "networkEnv": "wifi", |
| 55 | + "isOnCampus": true, |
| 56 | + "osName": "Android", |
| 57 | + "osVersion": "14", |
| 58 | + "manufacturer": "Xiaomi", |
| 59 | + "deviceModel": "Mi 14", |
| 60 | + "problemDesc": "登录白屏", |
| 61 | + "screenshots": "[]", |
| 62 | + "appVersion": "1.2.3", |
| 63 | + "versionHistory": "[]", |
| 64 | + "networkTraces": "[]", |
| 65 | + "events": "[]", |
| 66 | + "userSettings": "{}" |
| 67 | + }` |
| 68 | + |
| 69 | + testCases := []testCase{ |
| 70 | + { |
| 71 | + name: "success", |
| 72 | + body: okBody, |
| 73 | + mockRPCError: nil, |
| 74 | + expectingError: false, |
| 75 | + expectingMsg: `{"code":"10000","message":`, |
| 76 | + url: "/api/v1/feedback/create", |
| 77 | + }, |
| 78 | + { |
| 79 | + name: "invalid json", |
| 80 | + body: `{"reportId": 1,`, // 非法 JSON |
| 81 | + mockRPCError: nil, |
| 82 | + expectingError: true, |
| 83 | + expectingMsg: `{"code":"20001","message":`, |
| 84 | + url: "/api/v1/feedback/create", |
| 85 | + }, |
| 86 | + { |
| 87 | + name: "rpc error", |
| 88 | + body: okBody, |
| 89 | + mockRPCError: errno.InternalServiceError, |
| 90 | + expectingError: true, |
| 91 | + expectingMsg: `{"code":"50001","message":`, |
| 92 | + url: "/api/v1/feedback/create", |
| 93 | + }, |
| 94 | + } |
| 95 | + |
| 96 | + router := route.NewEngine(&config.Options{}) |
| 97 | + router.POST("/api/v1/feedback/create", CreateFeedback) |
| 98 | + |
| 99 | + defer mockey.UnPatchAll() |
| 100 | + for _, tc := range testCases { |
| 101 | + mockey.PatchConvey(tc.name, t, func() { |
| 102 | + mockey.Mock(rpc.CreateFeedbackRPC).To(func(ctx context.Context, req *oa.CreateFeedbackRequest) error { |
| 103 | + return tc.mockRPCError |
| 104 | + }).Build() |
| 105 | + |
| 106 | + result := ut.PerformRequest(router, "POST", tc.url, |
| 107 | + &ut.Body{Body: bytes.NewBufferString(tc.body), Len: len(tc.body)}, |
| 108 | + ut.Header{Key: "Content-Type", Value: "application/json"}) |
| 109 | + assert.Equal(t, consts.StatusOK, result.Result().StatusCode()) |
| 110 | + assert.Contains(t, string(result.Result().Body()), tc.expectingMsg) |
| 111 | + }) |
| 112 | + } |
| 113 | +} |
0 commit comments