-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_finding_test.go
117 lines (114 loc) · 3.34 KB
/
service_finding_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package main
import (
"context"
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/ca-risken/core/proto/finding"
findingmocks "github.com/ca-risken/core/proto/finding/mocks"
"github.com/golang-jwt/jwt/v4"
"github.com/stretchr/testify/mock"
)
func TestPutPendFindingHandler(t *testing.T) {
findingMock := findingmocks.NewFindingServiceClient(t)
svc := gatewayService{
findingClient: findingMock,
}
cases := []struct {
name string
input string
claims *jwt.MapClaims
userKey *requestUser
userIdpKey string
claimsErr error
mockResp *finding.PutPendFindingResponse
mockErr error
wantStatus int
}{
{
name: "OK userID is set",
input: `{"project_id": 1, "pend_finding":{"finding_id":1, "project_id":1}}`,
claims: &jwt.MapClaims{
"user_idp_key": "uik",
},
userKey: &requestUser{sub: "uik", userID: 1},
userIdpKey: "uik",
mockResp: &finding.PutPendFindingResponse{},
wantStatus: http.StatusOK,
},
{
name: "OK userID is not set",
input: `{"project_id": 1, "pend_finding":{"finding_id":1, "project_id":1}}`,
claims: &jwt.MapClaims{
"user_idp_key": "uik",
},
userKey: &requestUser{sub: "uik", accessTokenID: 1},
userIdpKey: "uik",
mockResp: &finding.PutPendFindingResponse{},
wantStatus: http.StatusOK,
},
{
name: "NG Invalid parameter",
input: `invalid_param`,
claims: &jwt.MapClaims{
"user_idp_key": "uik",
},
userKey: &requestUser{sub: "uik", userID: 1},
userIdpKey: "uik",
wantStatus: http.StatusBadRequest,
},
{
name: "NG InvalidUser",
input: `{"project_id": 1, "pend_finding":{"finding_id":1, "project_id":1}}`,
claims: &jwt.MapClaims{
"user_idp_key": "uik",
},
userKey: nil,
userIdpKey: "uik",
wantStatus: http.StatusUnauthorized,
},
{
name: "NG Backend service error",
input: `{"project_id": 1, "pend_finding":{"finding_id":1, "project_id":1}}`,
claims: &jwt.MapClaims{
"user_idp_key": "uik",
},
userKey: &requestUser{sub: "uik", userID: 1},
userIdpKey: "uik",
wantStatus: http.StatusInternalServerError,
mockErr: errors.New("something wrong"),
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
svc.claimsClient = newMockClaimsClient(c.claims, "", c.userIdpKey, c.claimsErr)
if c.mockResp != nil || c.mockErr != nil {
findingMock.On("PutPendFinding", mock.Anything, mock.Anything).Return(c.mockResp, c.mockErr).Once()
}
// Invoke HTTP Request
rec := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodPost, "/api/v1/finding/put-pend-finding/", strings.NewReader(c.input))
req = req.WithContext(context.WithValue(req.Context(), userKey, c.userKey))
req.Header.Add("Content-Type", "application/json")
svc.putPendFindingHandler(rec, req)
// Check Response
if c.wantStatus != rec.Code {
t.Fatalf("Unexpected HTTP status code: want=%+v, got=%+v", c.wantStatus, rec.Code)
}
resp := map[string]interface{}{}
if err := json.NewDecoder(rec.Body).Decode(&resp); err != nil {
t.Fatalf("Unexpected json decode error to response body: err=%+v", err)
}
jsonKey := successJSONKey
if c.wantStatus != http.StatusOK {
jsonKey = errorJSONKey
}
if _, ok := resp[jsonKey]; !ok {
t.Fatalf("Unexpected no response key: want key=%s", jsonKey)
}
})
}
}