1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Rezzza\SecurityBundle\Tests\Units\Security\Firewall; |
4
|
|
|
|
5
|
|
|
use mageekguy\atoum; |
6
|
|
|
|
7
|
|
|
use Rezzza\SecurityBundle\Security\Firewall\SignedRequest as SUT; |
8
|
|
|
|
9
|
|
|
class SignedRequest extends atoum\test |
10
|
|
|
{ |
11
|
|
View Code Duplication |
public function test_authenticate_invalid_signature_lead_to_exception() |
|
|
|
|
12
|
|
|
{ |
13
|
|
|
$this |
|
|
|
|
14
|
|
|
->given( |
15
|
|
|
$mockSignatureConfig = new \mock\Rezzza\SecurityBundle\Security\Firewall\SignatureConfig(true, 'sha1', 's3cr3t'), |
16
|
|
|
$mockReplayProtection = new \mock\Rezzza\SecurityBundle\Security\Firewall\ReplayProtection(true, 100), |
17
|
|
|
$sut = new SUT('GET', 'localhost', '/url', 'content', 123) |
18
|
|
|
) |
19
|
|
|
->exception(function () use ($sut, $mockSignatureConfig, $mockReplayProtection) { |
20
|
|
|
$sut->authenticateSignature('LALALALAAL', $mockSignatureConfig, $mockReplayProtection); |
21
|
|
|
}) |
22
|
|
|
->isInstanceOf('Rezzza\SecurityBundle\Security\Firewall\InvalidSignatureException') |
23
|
|
|
; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
View Code Duplication |
public function test_replay_protected_denied_lead_to_exception() |
|
|
|
|
27
|
|
|
{ |
28
|
|
|
$this |
|
|
|
|
29
|
|
|
->given( |
30
|
|
|
$mockSignatureConfig = new \mock\Rezzza\SecurityBundle\Security\Firewall\SignatureConfig(true, 'sha1', 's3cr3t'), |
31
|
|
|
$mockReplayProtection = new \mock\Rezzza\SecurityBundle\Security\Firewall\ReplayProtection(true, 100), |
32
|
|
|
$mockReplayProtection->getMockController()->accept = false, |
33
|
|
|
$sut = new SUT('GET', 'localhost', '/url', 'content', 123) |
34
|
|
|
) |
35
|
|
|
->exception(function () use ($sut, $mockSignatureConfig, $mockReplayProtection) { |
36
|
|
|
$sut->authenticateSignature('68a9f810beed3c8bbbf98096a60d36ade5f81d42', $mockSignatureConfig, $mockReplayProtection); |
37
|
|
|
}) |
38
|
|
|
->isInstanceOf('Rezzza\SecurityBundle\Security\Firewall\ExpiredSignatureException') |
39
|
|
|
; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function test_it_should_authenticated_valid_signature_not_expired() |
43
|
|
|
{ |
44
|
|
|
$this |
|
|
|
|
45
|
|
|
->given( |
46
|
|
|
$mockSignatureConfig = new \mock\Rezzza\SecurityBundle\Security\Firewall\SignatureConfig(true, 'sha1', 's3cr3t'), |
47
|
|
|
$mockReplayProtection = new \mock\Rezzza\SecurityBundle\Security\Firewall\ReplayProtection(true, 100), |
48
|
|
|
$mockReplayProtection->getMockController()->accept = true, |
49
|
|
|
$sut = new SUT('GET', 'localhost', '/url', 'content', 123) |
50
|
|
|
) |
51
|
|
|
->when( |
52
|
|
|
$authenticated = $sut->authenticateSignature('68a9f810beed3c8bbbf98096a60d36ade5f81d42', $mockSignatureConfig, $mockReplayProtection) |
53
|
|
|
) |
54
|
|
|
->boolean($authenticated) |
55
|
|
|
->isTrue() |
56
|
|
|
; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
View Code Duplication |
public function test_signature_generated_with_replay_protection_should_not_be_the_same_without() |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
$this |
|
|
|
|
62
|
|
|
->given( |
63
|
|
|
$mockSignatureConfig = new \mock\Rezzza\SecurityBundle\Security\Firewall\SignatureConfig(false, 'sha1', 's3cr3t'), |
64
|
|
|
$mockReplayProtection = new \mock\Rezzza\SecurityBundle\Security\Firewall\ReplayProtection(true, 100), |
65
|
|
|
$mockReplayProtection->getMockController()->accept = true, |
66
|
|
|
$sut = new SUT('GET', 'localhost', '/url', 'content', 123) |
67
|
|
|
) |
68
|
|
|
->exception(function () use ($sut, $mockSignatureConfig, $mockReplayProtection) { |
69
|
|
|
$sut->authenticateSignature('68a9f810beed3c8bbbf98096a60d36ade5f81d42', $mockSignatureConfig, $mockReplayProtection); |
70
|
|
|
}) |
71
|
|
|
->isInstanceOf('Rezzza\SecurityBundle\Security\Firewall\InvalidSignatureException') |
72
|
|
|
; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.