-
Notifications
You must be signed in to change notification settings - Fork 2
/
routes_test.go
178 lines (165 loc) · 4.57 KB
/
routes_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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package main_test
import (
"bytes"
"encoding/json"
"io"
"mime/multipart"
"net/http"
"net/http/httptest"
"testing"
main "github.com/ivarprudnikov/cose-and-receipt-playground"
"github.com/stretchr/testify/require"
"github.com/veraison/go-cose"
)
func TestIndex(t *testing.T) {
type test struct {
name string
path string
status int
}
tests := []test{
{
name: "toot reponds with 200",
path: "/",
status: http.StatusOK,
},
{
name: "index.html reponds with 200",
path: "/index.html",
status: http.StatusOK,
},
{
name: "unexpected path responds with 404",
path: "/xxx",
status: http.StatusNotFound,
},
}
handler := main.IndexHandler()
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, tc.path, nil)
w := httptest.NewRecorder()
handler.ServeHTTP(w, req)
res := w.Result()
require.Equal(t, tc.status, res.StatusCode)
})
}
}
func TestDidDoc(t *testing.T) {
handler := main.DidHandler()
req := httptest.NewRequest(http.MethodGet, "/", nil)
w := httptest.NewRecorder()
handler.ServeHTTP(w, req)
res := w.Result()
require.Equal(t, http.StatusOK, res.StatusCode)
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
require.NoError(t, err)
var diddoc map[string]any
err = json.Unmarshal(body, &diddoc)
require.NoError(t, err)
if _, ok := diddoc["assertionMethod"]; !ok {
t.Errorf("expected id key in diddoc %v", diddoc)
}
}
func TestSignatureCreate(t *testing.T) {
type test struct {
name string
formValues map[string][]string
formFiles map[string][]byte
status int
coseHeaderKeys []any
coseHeaderVals []any
}
tests := []test{
{
name: "fails without payload",
status: http.StatusBadRequest,
},
{
name: "create from text",
formValues: map[string][]string{"payload": {"hello"}},
status: http.StatusOK,
coseHeaderKeys: []any{int64(1), int64(3), int64(4), int64(391), int64(392), int64(393)},
coseHeaderVals: []any{"", "text/plain", "", "did:web:localhost%3A8080", "demo", ""},
},
{
name: "create from hex",
formValues: map[string][]string{"payloadhex": {"68656c6c6f"}},
status: http.StatusOK,
},
{
name: "fails with too many payloads",
formValues: map[string][]string{"payload": {"hello"}, "payloadhex": {"68656c6c6f"}},
status: http.StatusBadRequest,
},
{
name: "create from file",
formFiles: map[string][]byte{"payloadfile": []byte("hello")},
status: http.StatusOK,
},
{
name: "fails with too many payloads including file",
formValues: map[string][]string{"payload": {"hello"}},
formFiles: map[string][]byte{"payloadfile": []byte("hello")},
status: http.StatusBadRequest,
},
{
name: "uses custom content type",
formValues: map[string][]string{"payload": {"hello"}, "contenttype": {"foo/bar"}},
status: http.StatusOK,
coseHeaderKeys: []any{int64(3)},
coseHeaderVals: []any{"foo/bar"},
},
{
name: "adds custom headers",
formValues: map[string][]string{"payload": {"hello"}, "headerkey": {"33[0]", "33[1]", "33[2]"}, "headerval": {"a", "b", "c", "d", "e"}},
status: http.StatusOK,
coseHeaderKeys: []any{int64(33)},
coseHeaderVals: []any{[]any{"a", "b", "c"}},
},
}
handler := main.SigCreateHandler()
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
reqBody := new(bytes.Buffer)
mp := multipart.NewWriter(reqBody)
for key, val := range tc.formValues {
for _, v := range val {
mp.WriteField(key, v)
}
}
for key, val := range tc.formFiles {
part, err := mp.CreateFormFile(key, key)
require.NoError(t, err)
_, err = part.Write(val)
require.NoError(t, err)
}
mp.Close()
req := httptest.NewRequest(http.MethodPost, "/", reqBody)
req.Header.Set("Content-Type", mp.FormDataContentType())
w := httptest.NewRecorder()
handler.ServeHTTP(w, req)
res := w.Result()
require.Equal(t, tc.status, res.StatusCode)
if res.StatusCode != http.StatusOK {
return
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
require.NoError(t, err)
var sig cose.Sign1Message
err = sig.UnmarshalCBOR(body)
require.NoError(t, err)
for idx, key := range tc.coseHeaderKeys {
v, ok := sig.Headers.Protected[key]
if !ok {
t.Errorf("expected key %v in protected headers %v", key, sig.Headers.Protected)
}
if len(tc.coseHeaderVals) > 0 && tc.coseHeaderVals[idx] != "" {
require.Equal(t, tc.coseHeaderVals[idx], v)
}
}
})
}
}