@@ -19,6 +19,7 @@ const testKey = "other-secret"
19
19
20
20
func TestCalculateSignature (t * testing.T ) {
21
21
var cases = []struct {
22
+ name string
22
23
sKey string
23
24
ts string
24
25
qp string
@@ -27,6 +28,7 @@ func TestCalculateSignature(t *testing.T) {
27
28
e bool
28
29
}{
29
30
{
31
+ name : "Succesful" ,
30
32
sKey : testKey ,
31
33
ts : testTs ,
32
34
qp : testQp ,
@@ -35,6 +37,7 @@ func TestCalculateSignature(t *testing.T) {
35
37
e : true ,
36
38
},
37
39
{
40
+ name : "Wrong signature" ,
38
41
sKey : testKey ,
39
42
ts : testTs ,
40
43
qp : testQp ,
@@ -43,6 +46,7 @@ func TestCalculateSignature(t *testing.T) {
43
46
e : false ,
44
47
},
45
48
{
49
+ name : "Empty query params and body" ,
46
50
sKey : "secret" ,
47
51
ts : testTs ,
48
52
qp : "" ,
@@ -51,6 +55,7 @@ func TestCalculateSignature(t *testing.T) {
51
55
e : true ,
52
56
},
53
57
{
58
+ name : "Empty query params" ,
54
59
sKey : "secret" ,
55
60
ts : testTs ,
56
61
qp : "" ,
@@ -59,6 +64,7 @@ func TestCalculateSignature(t *testing.T) {
59
64
e : true ,
60
65
},
61
66
{
67
+ name : "Empty body" ,
62
68
sKey : "secret" ,
63
69
ts : testTs ,
64
70
qp : testQp ,
@@ -67,7 +73,7 @@ func TestCalculateSignature(t *testing.T) {
67
73
e : true ,
68
74
},
69
75
}
70
- for i , tt := range cases {
76
+ for _ , tt := range cases {
71
77
v := NewValidator (tt .sKey , nil )
72
78
s , err := v .CalculateSignature (tt .ts , tt .qp , []byte (tt .b ))
73
79
if err != nil {
@@ -76,7 +82,7 @@ func TestCalculateSignature(t *testing.T) {
76
82
drs , _ := base64 .StdEncoding .DecodeString (tt .es )
77
83
e := bool (bytes .Compare (s , drs ) == 0 )
78
84
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 )
80
86
}
81
87
}
82
88
}
@@ -85,150 +91,168 @@ func TestValidTimestamp(t *testing.T) {
85
91
now := time .Now ()
86
92
nowts := fmt .Sprintf ("%d" , now .Unix ())
87
93
var cases = []struct {
88
- ts string
89
- p ValidityPeriod
90
- e bool
94
+ name string
95
+ ts string
96
+ p ValidityPeriod
97
+ e bool
91
98
}{
92
99
{
93
- ts : nowts ,
94
- p : nil ,
95
- e : true ,
100
+ name : "Succesful" ,
101
+ ts : nowts ,
102
+ p : nil ,
103
+ e : true ,
96
104
},
97
105
{
98
- ts : "" ,
99
- p : nil ,
100
- e : false ,
106
+ name : "Empty time stamp" ,
107
+ ts : "" ,
108
+ p : nil ,
109
+ e : false ,
101
110
},
102
111
{
103
- ts : "wrongTs" ,
104
- p : nil ,
105
- e : false ,
112
+ name : "Invalid time stamp" ,
113
+ ts : "wrongTs" ,
114
+ p : nil ,
115
+ e : false ,
106
116
},
107
117
{
108
- ts : nowts ,
109
- p : & p ,
110
- e : true ,
118
+ name : "Succesful" ,
119
+ ts : nowts ,
120
+ p : & p ,
121
+ e : true ,
111
122
},
112
123
{
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 ,
116
128
},
117
129
{
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 ,
121
134
},
122
135
}
123
136
124
- for i , tt := range cases {
137
+ for _ , tt := range cases {
125
138
v := NewValidator (testKey , tt .p )
126
139
r := v .ValidTimestamp (tt .ts )
127
140
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 )
129
142
}
130
143
}
131
144
}
132
145
133
146
func TestValidSignature (t * testing.T ) {
134
147
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
140
154
}{
141
155
{
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 ,
147
162
},
148
163
{
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 ,
154
170
},
155
171
{
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 ,
161
178
},
162
179
}
163
180
164
- for i , tt := range cases {
181
+ for _ , tt := range cases {
165
182
v := NewValidator (testKey , nil )
166
183
r := v .ValidSignature (tt .ts , tt .qp , []byte (tt .b ), tt .s )
167
184
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 )
169
186
}
170
187
}
171
188
}
172
189
func TestValidate (t * testing.T ) {
173
190
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
180
198
}{
181
199
{
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 ,
188
207
},
189
208
{
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 ,
196
216
},
197
217
{
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 ,
204
225
},
205
226
{
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 ,
212
234
},
213
235
{
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 ,
220
243
},
221
244
{
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 ,
228
252
},
229
253
}
230
254
231
- for i , tt := range cases {
255
+ for _ , tt := range cases {
232
256
v := NewValidator (tt .k , nil )
233
257
ts := httptest .NewServer (v .Validate (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
234
258
w .WriteHeader (http .StatusOK )
@@ -241,7 +265,7 @@ func TestValidate(t *testing.T) {
241
265
req .Header .Set (tt .tsh , tt .ts )
242
266
res , _ := client .Do (req )
243
267
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 )
245
269
}
246
270
}
247
271
0 commit comments