-
Notifications
You must be signed in to change notification settings - Fork 2
/
api_test.go
108 lines (100 loc) · 2.38 KB
/
api_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
package main
import (
"encoding/json"
"github.com/labstack/echo/v4"
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"
)
func Test_handleSign(t *testing.T) {
mockJwt := func(req LogRequest) (string, error) {
return "atoken", nil
}
mockUrl := func(c echo.Context, token string) (string, error) {
return "aurl", nil
}
tests := []struct {
name string
payload string
want map[string]string
}{
{
name: "missing namespace",
payload: `{"namespace": "", "pod": "web", "container": "app"}`,
want: map[string]string{
"error": "namespace is required",
},
},
{
name: "namespace + pod",
payload: `{"namespace": "ns", "pod": "p"}`,
want: map[string]string{
"url": "aurl",
"token": "atoken",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
handler := handleSign(signRequestValidator, mockJwt, mockUrl)
req, _ := http.NewRequest(http.MethodPost, "/", strings.NewReader(tt.payload))
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()
c := echo.New().NewContext(req, rec)
_ = handler(c)
output := make(map[string]string)
_ = json.Unmarshal(rec.Body.Bytes(), &output)
if !reflect.DeepEqual(output, tt.want) {
t.Errorf("handleSign() response = %v, want %v", output, tt.want)
}
})
}
}
func Test_signRequestValidator(t *testing.T) {
type args struct {
req *signRequest
}
tests := []struct {
name string
args args
wantErr bool
}{
{name: "without namespace", args: args{
req: &signRequest{
Namespace: "",
Pod: "web",
Container: "app",
},
}, wantErr: true},
{name: "without pod, with container", args: args{
req: &signRequest{
Namespace: "ns",
Pod: "",
Container: "app",
},
}, wantErr: true},
{name: "with namespace, with pod, with container", args: args{
req: &signRequest{
Namespace: "ns",
Pod: "web",
Container: "app",
},
}, wantErr: false},
{name: "with namespace, with pod", args: args{
req: &signRequest{
Namespace: "ns",
Pod: "web",
Container: "app",
},
}, wantErr: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := signRequestValidator(tt.args.req); (err != nil) != tt.wantErr {
t.Errorf("signRequestValidator() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}