Skip to content

Commit 6a6c7ea

Browse files
author
Dave Wong
committed
update tests
1 parent 8a6ef49 commit 6a6c7ea

File tree

1 file changed

+71
-38
lines changed

1 file changed

+71
-38
lines changed

tests/Middlewares/Laravel/VerifySignatureTest.php

Lines changed: 71 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,17 @@ public function setup_the_middleware()
4646
$this->cache = new \Illuminate\Cache\Repository(new ArrayStore());
4747
$this->configurations = Mockery::mock(Configurations::class);
4848
$this->configurations->shouldReceive('get')
49-
->with('signed-requests.cache-prefix')
49+
->with('signed-requests.default.cache-prefix')
5050
->andReturn('prefix');
5151
$this->configurations->shouldReceive('get')
52-
->with('signed-requests.request-replay.tolerance')
52+
->with('signed-requests.default.request-replay.tolerance')
5353
->andReturn(60);
54+
$this->configurations->shouldReceive('get')
55+
->with('signed-requests')
56+
->andReturn([
57+
'default' => [],
58+
'custom' => []
59+
]);
5460
$this->middleware = new VerifySignature($this->configurations, $this->cache);
5561
}
5662

@@ -69,19 +75,19 @@ public function it_can_be_constructed()
6975
public function it_throws_an_invalid_signature_exception_if_the_request_is_not_valid()
7076
{
7177
$this->configurations->shouldReceive('get')
72-
->with('signed-requests.headers.signature')
78+
->with('signed-requests.default.headers.signature')
7379
->andReturn('HTTP_SIGNATURE');
7480

7581
$this->configurations->shouldReceive('get')
76-
->with('signed-requests.headers.algorithm')
82+
->with('signed-requests.default.headers.algorithm')
7783
->andReturn('HTTP_ALGORITHM');
7884

7985
$this->configurations->shouldReceive('get')
80-
->with('signed-requests.key')
86+
->with('signed-requests.default.key')
8187
->andReturn('key');
8288

8389
$this->configurations->shouldReceive('get')
84-
->with('signed-requests.request-replay.allow')
90+
->with('signed-requests.default.request-replay.allow')
8591
->andReturn(true);
8692

8793
$request = new Request();
@@ -98,19 +104,19 @@ public function it_should_call_our_callback_if_the_request_is_valid()
98104
$id = (string) Uuid::uuid4();
99105

100106
$this->configurations->shouldReceive('get')
101-
->with('signed-requests.headers.signature')
107+
->with('signed-requests.default.headers.signature')
102108
->andReturn('signature');
103109

104110
$this->configurations->shouldReceive('get')
105-
->with('signed-requests.headers.algorithm')
111+
->with('signed-requests.default.headers.algorithm')
106112
->andReturn('algorithm');
107113

108114
$this->configurations->shouldReceive('get')
109-
->with('signed-requests.key')
115+
->with('signed-requests.default.key')
110116
->andReturn('key');
111117

112118
$this->configurations->shouldReceive('get')
113-
->with('signed-requests.request-replay.allow')
119+
->with('signed-requests.default.request-replay.allow')
114120
->andReturn(true);
115121

116122
$query = [];
@@ -135,31 +141,31 @@ public function it_should_call_our_callback_if_the_request_is_valid()
135141
/**
136142
* @test
137143
*/
138-
public function it_should_prefix_the_configuration_keys_if_a_prefix_is_supplied()
144+
public function it_should_set_the_key_when_one_is_passed()
139145
{
140146
$id = (string) Uuid::uuid4();
141147

142148
$this->configurations->shouldReceive('get')
143-
->with('prefix-signed-requests.headers.signature')
149+
->with('signed-requests.custom.headers.signature')
144150
->andReturn('signature');
145151

146152
$this->configurations->shouldReceive('get')
147-
->with('prefix-signed-requests.headers.algorithm')
153+
->with('signed-requests.custom.headers.algorithm')
148154
->andReturn('algorithm');
149155

150156
$this->configurations->shouldReceive('get')
151-
->with('prefix-signed-requests.key')
157+
->with('signed-requests.custom.key')
152158
->andReturn('key');
153159

154160
$this->configurations->shouldReceive('get')
155-
->with('prefix-signed-requests.request-replay.allow')
161+
->with('signed-requests.custom.request-replay.allow')
156162
->andReturn(true);
157163

158164
$this->configurations->shouldReceive('get')
159-
->with('prefix-signed-requests.cache-prefix')
165+
->with('signed-requests.custom.cache-prefix')
160166
->andReturn('prefix');
161167
$this->configurations->shouldReceive('get')
162-
->with('prefix-signed-requests.request-replay.tolerance')
168+
->with('signed-requests.custom.request-replay.tolerance')
163169
->andReturn(60);
164170

165171
$query = [];
@@ -178,7 +184,34 @@ public function it_should_prefix_the_configuration_keys_if_a_prefix_is_supplied(
178184
$this->middleware->handle($request, function () {
179185
// This should be called.
180186
$this->assertTrue(true);
181-
}, 'prefix');
187+
}, 'custom');
188+
}
189+
190+
/**
191+
* @test
192+
* @expectedException \SoapBox\SignedRequests\Exceptions\InvalidConfigurationException
193+
*/
194+
public function it_should_throw_an_exception_when_it_cannot_find_the_key()
195+
{
196+
$id = (string) Uuid::uuid4();
197+
198+
$query = [];
199+
$request = [];
200+
$attributes = [];
201+
$cookies = [];
202+
$files = [];
203+
$server = [
204+
'HTTP_X-SIGNED-ID' => $id,
205+
'HTTP_ALGORITHM' => 'sha256'
206+
];
207+
208+
$request = new Request($query, $request, $attributes, $cookies, $files, $server, 'a');
209+
$request->headers->set('signature', (string) new Signature(new Payload($request), 'sha256', 'key'));
210+
211+
$this->middleware->handle($request, function () {
212+
// This should be called.
213+
$this->assertTrue(true);
214+
}, 'nope');
182215
}
183216

184217
/**
@@ -190,19 +223,19 @@ public function it_throws_an_expired_request_exception_if_the_timestamp_on_the_r
190223
$id = (string) Uuid::uuid4();
191224

192225
$this->configurations->shouldReceive('get')
193-
->with('signed-requests.headers.signature')
226+
->with('signed-requests.default.headers.signature')
194227
->andReturn('signature');
195228

196229
$this->configurations->shouldReceive('get')
197-
->with('signed-requests.headers.algorithm')
230+
->with('signed-requests.default.headers.algorithm')
198231
->andReturn('algorithm');
199232

200233
$this->configurations->shouldReceive('get')
201-
->with('signed-requests.key')
234+
->with('signed-requests.default.key')
202235
->andReturn('key');
203236

204237
$this->configurations->shouldReceive('get')
205-
->with('signed-requests.request-replay.allow')
238+
->with('signed-requests.default.request-replay.allow')
206239
->andReturn(false);
207240

208241
$query = [];
@@ -231,19 +264,19 @@ public function it_should_call_our_callback_if_the_id_has_not_previously_been_se
231264
$id = (string) Uuid::uuid4();
232265

233266
$this->configurations->shouldReceive('get')
234-
->with('signed-requests.headers.signature')
267+
->with('signed-requests.default.headers.signature')
235268
->andReturn('signature');
236269

237270
$this->configurations->shouldReceive('get')
238-
->with('signed-requests.headers.algorithm')
271+
->with('signed-requests.default.headers.algorithm')
239272
->andReturn('algorithm');
240273

241274
$this->configurations->shouldReceive('get')
242-
->with('signed-requests.key')
275+
->with('signed-requests.default.key')
243276
->andReturn('key');
244277

245278
$this->configurations->shouldReceive('get')
246-
->with('signed-requests.request-replay.allow')
279+
->with('signed-requests.default.request-replay.allow')
247280
->andReturn(false);
248281

249282
$query = [];
@@ -275,19 +308,19 @@ public function it_should_throw_an_expired_request_exception_if_the_request_id_h
275308
$id = (string) Uuid::uuid4();
276309

277310
$this->configurations->shouldReceive('get')
278-
->with('signed-requests.headers.signature')
311+
->with('signed-requests.default.headers.signature')
279312
->andReturn('signature');
280313

281314
$this->configurations->shouldReceive('get')
282-
->with('signed-requests.headers.algorithm')
315+
->with('signed-requests.default.headers.algorithm')
283316
->andReturn('algorithm');
284317

285318
$this->configurations->shouldReceive('get')
286-
->with('signed-requests.key')
319+
->with('signed-requests.default.key')
287320
->andReturn('key');
288321

289322
$this->configurations->shouldReceive('get')
290-
->with('signed-requests.request-replay.allow')
323+
->with('signed-requests.default.request-replay.allow')
291324
->andReturn(false);
292325

293326
$key = sprintf('prefix.%s', $id);
@@ -321,19 +354,19 @@ public function it_should_throw_an_expired_request_exception_if_the_same_request
321354
$id = (string) Uuid::uuid4();
322355

323356
$this->configurations->shouldReceive('get')
324-
->with('signed-requests.headers.signature')
357+
->with('signed-requests.default.headers.signature')
325358
->andReturn('signature');
326359

327360
$this->configurations->shouldReceive('get')
328-
->with('signed-requests.headers.algorithm')
361+
->with('signed-requests.default.headers.algorithm')
329362
->andReturn('algorithm');
330363

331364
$this->configurations->shouldReceive('get')
332-
->with('signed-requests.key')
365+
->with('signed-requests.default.key')
333366
->andReturn('key');
334367

335368
$this->configurations->shouldReceive('get')
336-
->with('signed-requests.request-replay.allow')
369+
->with('signed-requests.default.request-replay.allow')
337370
->andReturn(false);
338371

339372
$query = [];
@@ -368,19 +401,19 @@ public function it_throws_an_expired_request_exception_if_the_timestamp_on_the_r
368401
$id = (string) Uuid::uuid4();
369402

370403
$this->configurations->shouldReceive('get')
371-
->with('signed-requests.headers.signature')
404+
->with('signed-requests.default.headers.signature')
372405
->andReturn('signature');
373406

374407
$this->configurations->shouldReceive('get')
375-
->with('signed-requests.headers.algorithm')
408+
->with('signed-requests.default.headers.algorithm')
376409
->andReturn('algorithm');
377410

378411
$this->configurations->shouldReceive('get')
379-
->with('signed-requests.key')
412+
->with('signed-requests.default.key')
380413
->andReturn('key');
381414

382415
$this->configurations->shouldReceive('get')
383-
->with('signed-requests.request-replay.allow')
416+
->with('signed-requests.default.request-replay.allow')
384417
->andReturn(false);
385418

386419
$query = [];

0 commit comments

Comments
 (0)