Skip to content

Commit 7d79a57

Browse files
Style: Add name to test cases
1 parent f4587e0 commit 7d79a57

File tree

1 file changed

+115
-91
lines changed

1 file changed

+115
-91
lines changed

signature/signature_test.go

Lines changed: 115 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const testKey = "other-secret"
1919

2020
func TestCalculateSignature(t *testing.T) {
2121
var cases = []struct {
22+
name string
2223
sKey string
2324
ts string
2425
qp string
@@ -27,6 +28,7 @@ func TestCalculateSignature(t *testing.T) {
2728
e bool
2829
}{
2930
{
31+
name: "Succesful",
3032
sKey: testKey,
3133
ts: testTs,
3234
qp: testQp,
@@ -35,6 +37,7 @@ func TestCalculateSignature(t *testing.T) {
3537
e: true,
3638
},
3739
{
40+
name: "Wrong signature",
3841
sKey: testKey,
3942
ts: testTs,
4043
qp: testQp,
@@ -43,6 +46,7 @@ func TestCalculateSignature(t *testing.T) {
4346
e: false,
4447
},
4548
{
49+
name: "Empty query params and body",
4650
sKey: "secret",
4751
ts: testTs,
4852
qp: "",
@@ -51,6 +55,7 @@ func TestCalculateSignature(t *testing.T) {
5155
e: true,
5256
},
5357
{
58+
name: "Empty query params",
5459
sKey: "secret",
5560
ts: testTs,
5661
qp: "",
@@ -59,6 +64,7 @@ func TestCalculateSignature(t *testing.T) {
5964
e: true,
6065
},
6166
{
67+
name: "Empty body",
6268
sKey: "secret",
6369
ts: testTs,
6470
qp: testQp,
@@ -67,7 +73,7 @@ func TestCalculateSignature(t *testing.T) {
6773
e: true,
6874
},
6975
}
70-
for i, tt := range cases {
76+
for _, tt := range cases {
7177
v := NewValidator(tt.sKey, nil)
7278
s, err := v.CalculateSignature(tt.ts, tt.qp, []byte(tt.b))
7379
if err != nil {
@@ -76,7 +82,7 @@ func TestCalculateSignature(t *testing.T) {
7682
drs, _ := base64.StdEncoding.DecodeString(tt.es)
7783
e := bool(bytes.Compare(s, drs) == 0)
7884
if e != tt.e {
79-
t.Errorf("Unexpected signature: %s, test case: %d", s, i)
85+
t.Errorf("Unexpected signature: %s, test case: %s", s, tt.name)
8086
}
8187
}
8288
}
@@ -85,150 +91,168 @@ func TestValidTimestamp(t *testing.T) {
8591
now := time.Now()
8692
nowts := fmt.Sprintf("%d", now.Unix())
8793
var cases = []struct {
88-
ts string
89-
p ValidityPeriod
90-
e bool
94+
name string
95+
ts string
96+
p ValidityPeriod
97+
e bool
9198
}{
9299
{
93-
ts: nowts,
94-
p: nil,
95-
e: true,
100+
name: "Succesful",
101+
ts: nowts,
102+
p: nil,
103+
e: true,
96104
},
97105
{
98-
ts: "",
99-
p: nil,
100-
e: false,
106+
name: "Empty time stamp",
107+
ts: "",
108+
p: nil,
109+
e: false,
101110
},
102111
{
103-
ts: "wrongTs",
104-
p: nil,
105-
e: false,
112+
name: "Invalid time stamp",
113+
ts: "wrongTs",
114+
p: nil,
115+
e: false,
106116
},
107117
{
108-
ts: nowts,
109-
p: &p,
110-
e: true,
118+
name: "Succesful",
119+
ts: nowts,
120+
p: &p,
121+
e: true,
111122
},
112123
{
113-
ts: fmt.Sprintf("%d", now.AddDate(0, 0, 1).Unix()),
114-
p: &p,
115-
e: false,
124+
name: "Time stamp 24 hours in the futute",
125+
ts: fmt.Sprintf("%d", now.AddDate(0, 0, 1).Unix()),
126+
p: &p,
127+
e: false,
116128
},
117129
{
118-
ts: fmt.Sprintf("%d", now.AddDate(0, 0, -1).Unix()),
119-
p: &p,
120-
e: false,
130+
name: "Time stamp 24 hours in the past",
131+
ts: fmt.Sprintf("%d", now.AddDate(0, 0, -1).Unix()),
132+
p: &p,
133+
e: false,
121134
},
122135
}
123136

124-
for i, tt := range cases {
137+
for _, tt := range cases {
125138
v := NewValidator(testKey, tt.p)
126139
r := v.ValidTimestamp(tt.ts)
127140
if r != tt.e {
128-
t.Errorf("Unexpected error validating ts: %s, test case: %d", tt.ts, i)
141+
t.Errorf("Unexpected error validating ts: %s, test case: %s", tt.ts, tt.name)
129142
}
130143
}
131144
}
132145

133146
func TestValidSignature(t *testing.T) {
134147
var cases = []struct {
135-
ts string
136-
qp string
137-
b string
138-
s string
139-
e bool
148+
name string
149+
ts string
150+
qp string
151+
b string
152+
s string
153+
e bool
140154
}{
141155
{
142-
ts: testTs,
143-
qp: testQp,
144-
b: testBody,
145-
s: testSignature,
146-
e: true,
156+
name: "succesful",
157+
ts: testTs,
158+
qp: testQp,
159+
b: testBody,
160+
s: testSignature,
161+
e: true,
147162
},
148163
{
149-
ts: testTs,
150-
qp: "def=bar&abc=foo",
151-
b: testBody,
152-
s: testSignature,
153-
e: true,
164+
name: "Unorganized query params",
165+
ts: testTs,
166+
qp: "def=bar&abc=foo",
167+
b: testBody,
168+
s: testSignature,
169+
e: true,
154170
},
155171
{
156-
ts: testTs,
157-
qp: testQp,
158-
b: testBody,
159-
s: "wrong signature",
160-
e: false,
172+
name: "Wrong signature received",
173+
ts: testTs,
174+
qp: testQp,
175+
b: testBody,
176+
s: "wrong signature",
177+
e: false,
161178
},
162179
}
163180

164-
for i, tt := range cases {
181+
for _, tt := range cases {
165182
v := NewValidator(testKey, nil)
166183
r := v.ValidSignature(tt.ts, tt.qp, []byte(tt.b), tt.s)
167184
if r != tt.e {
168-
t.Errorf("Unexpected error validating signature: %s, test case: %d", tt.s, i)
185+
t.Errorf("Unexpected error validating signature: %s, test case: %s", tt.s, tt.name)
169186
}
170187
}
171188
}
172189
func TestValidate(t *testing.T) {
173190
var cases = []struct {
174-
k string
175-
ts string
176-
s string
177-
sh string
178-
tsh string
179-
e int
191+
name string
192+
k string
193+
ts string
194+
s string
195+
sh string
196+
tsh string
197+
e int
180198
}{
181199
{
182-
k: testKey,
183-
ts: testTs,
184-
s: testSignature,
185-
sh: sHeader,
186-
tsh: tsHeader,
187-
e: http.StatusOK,
200+
name: "Succesful",
201+
k: testKey,
202+
ts: testTs,
203+
s: testSignature,
204+
sh: sHeader,
205+
tsh: tsHeader,
206+
e: http.StatusOK,
188207
},
189208
{
190-
k: "",
191-
ts: testTs,
192-
s: testSignature,
193-
sh: sHeader,
194-
tsh: tsHeader,
195-
e: http.StatusUnauthorized,
209+
name: "NO Access key",
210+
k: "",
211+
ts: testTs,
212+
s: testSignature,
213+
sh: sHeader,
214+
tsh: tsHeader,
215+
e: http.StatusUnauthorized,
196216
},
197217
{
198-
k: testKey,
199-
ts: "",
200-
s: testSignature,
201-
sh: sHeader,
202-
tsh: tsHeader,
203-
e: http.StatusUnauthorized,
218+
name: "Request with empty time stamp",
219+
k: testKey,
220+
ts: "",
221+
s: testSignature,
222+
sh: sHeader,
223+
tsh: tsHeader,
224+
e: http.StatusUnauthorized,
204225
},
205226
{
206-
k: testKey,
207-
ts: testTs,
208-
s: "",
209-
sh: sHeader,
210-
tsh: tsHeader,
211-
e: http.StatusUnauthorized,
227+
name: "Request with empty signature",
228+
k: testKey,
229+
ts: testTs,
230+
s: "",
231+
sh: sHeader,
232+
tsh: tsHeader,
233+
e: http.StatusUnauthorized,
212234
},
213235
{
214-
k: testKey,
215-
ts: testTs,
216-
s: testSignature,
217-
sh: "wrong-header",
218-
tsh: tsHeader,
219-
e: http.StatusUnauthorized,
236+
name: "Request with wrong signature header",
237+
k: testKey,
238+
ts: testTs,
239+
s: testSignature,
240+
sh: "wrong-header",
241+
tsh: tsHeader,
242+
e: http.StatusUnauthorized,
220243
},
221244
{
222-
k: testKey,
223-
ts: testTs,
224-
s: testSignature,
225-
sh: sHeader,
226-
tsh: "wrong-header",
227-
e: http.StatusUnauthorized,
245+
name: "Request with wrong timestamp header",
246+
k: testKey,
247+
ts: testTs,
248+
s: testSignature,
249+
sh: sHeader,
250+
tsh: "wrong-header",
251+
e: http.StatusUnauthorized,
228252
},
229253
}
230254

231-
for i, tt := range cases {
255+
for _, tt := range cases {
232256
v := NewValidator(tt.k, nil)
233257
ts := httptest.NewServer(v.Validate(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
234258
w.WriteHeader(http.StatusOK)
@@ -241,7 +265,7 @@ func TestValidate(t *testing.T) {
241265
req.Header.Set(tt.tsh, tt.ts)
242266
res, _ := client.Do(req)
243267
if res.StatusCode != tt.e {
244-
t.Errorf("Unexpected response code: %s, test case: %d", res.Status, i)
268+
t.Errorf("Unexpected response code: %s, test case: %s", res.Status, tt.name)
245269
}
246270
}
247271

0 commit comments

Comments
 (0)