|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Damax\Bundle\ApiAuthBundle\Tests\Security; |
|
6
|
|
|
|
|
7
|
|
|
use Damax\Bundle\ApiAuthBundle\Extractor\Extractor; |
|
8
|
|
|
use Damax\Bundle\ApiAuthBundle\Security\AbstractAuthenticator; |
|
9
|
|
|
use Damax\Bundle\ApiAuthBundle\Security\JsonResponseFactory; |
|
10
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
|
11
|
|
|
use PHPUnit\Framework\TestCase; |
|
12
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
13
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
14
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
|
15
|
|
|
use Symfony\Component\Security\Core\Exception\AuthenticationException; |
|
16
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
|
17
|
|
|
use Symfony\Component\Security\Core\User\UserProviderInterface; |
|
18
|
|
|
|
|
19
|
|
|
class AbstractAuthenticatorTest extends TestCase |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var Request |
|
23
|
|
|
*/ |
|
24
|
|
|
private $request; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var Extractor|MockObject |
|
28
|
|
|
*/ |
|
29
|
|
|
private $extractor; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var AbstractAuthenticator |
|
33
|
|
|
*/ |
|
34
|
|
|
private $authenticator; |
|
35
|
|
|
|
|
36
|
|
|
protected function setUp() |
|
37
|
|
|
{ |
|
38
|
|
|
$this->request = new Request(); |
|
39
|
|
|
$this->extractor = $extractor = $this->createMock(Extractor::class); |
|
40
|
|
|
$this->authenticator = new class($extractor, new JsonResponseFactory()) extends AbstractAuthenticator { |
|
41
|
|
|
public function getUser($credentials, UserProviderInterface $userProvider) |
|
42
|
|
|
{ |
|
43
|
|
|
} |
|
44
|
|
|
}; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @test |
|
49
|
|
|
*/ |
|
50
|
|
|
public function it_supports_authentication() |
|
51
|
|
|
{ |
|
52
|
|
|
$this->extractor |
|
53
|
|
|
->expects($this->once()) |
|
54
|
|
|
->method('extractKey') |
|
55
|
|
|
->with($this->identicalTo($this->request)) |
|
56
|
|
|
->willReturn('ABC') |
|
57
|
|
|
; |
|
58
|
|
|
|
|
59
|
|
|
$this->assertTrue($this->authenticator->supports($this->request)); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @test |
|
64
|
|
|
*/ |
|
65
|
|
|
public function it_does_not_support_authentication() |
|
66
|
|
|
{ |
|
67
|
|
|
$this->extractor |
|
68
|
|
|
->expects($this->once()) |
|
69
|
|
|
->method('extractKey') |
|
70
|
|
|
->with($this->identicalTo($this->request)) |
|
71
|
|
|
; |
|
72
|
|
|
|
|
73
|
|
|
$this->assertFalse($this->authenticator->supports($this->request)); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @test |
|
78
|
|
|
*/ |
|
79
|
|
|
public function it_starts_authentication() |
|
80
|
|
|
{ |
|
81
|
|
|
$response = $this->authenticator->start($this->request); |
|
82
|
|
|
|
|
83
|
|
|
$this->assertInstanceOf(JsonResponse::class, $response); |
|
84
|
|
|
$this->assertEquals(401, $response->getStatusCode()); |
|
85
|
|
|
$this->assertEquals('{"error":{"code":401,"message":"Unauthorized"}}', $response->getContent()); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @test |
|
90
|
|
|
*/ |
|
91
|
|
|
public function it_retrieves_credentials() |
|
92
|
|
|
{ |
|
93
|
|
|
$this->extractor |
|
94
|
|
|
->expects($this->once()) |
|
95
|
|
|
->method('extractKey') |
|
96
|
|
|
->with($this->identicalTo($this->request)) |
|
97
|
|
|
->willReturn('ABC') |
|
98
|
|
|
; |
|
99
|
|
|
$this->assertEquals('ABC', $this->authenticator->getCredentials($this->request)); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @test |
|
104
|
|
|
*/ |
|
105
|
|
|
public function it_always_validates_credentials() |
|
106
|
|
|
{ |
|
107
|
|
|
/** @var UserInterface $user */ |
|
108
|
|
|
$user = $this->createMock(UserInterface::class); |
|
109
|
|
|
|
|
110
|
|
|
$this->assertTrue($this->authenticator->checkCredentials('password', $user)); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @test |
|
115
|
|
|
*/ |
|
116
|
|
|
public function it_allows_authentication() |
|
117
|
|
|
{ |
|
118
|
|
|
/** @var TokenInterface $token */ |
|
119
|
|
|
$token = $this->createMock(TokenInterface::class); |
|
120
|
|
|
|
|
121
|
|
|
$this->assertNull($this->authenticator->onAuthenticationSuccess($this->request, $token, 'main')); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @test |
|
126
|
|
|
*/ |
|
127
|
|
|
public function it_denies_authentication() |
|
128
|
|
|
{ |
|
129
|
|
|
$response = $this->authenticator->onAuthenticationFailure(new Request(), new AuthenticationException('Authentication error.')); |
|
130
|
|
|
|
|
131
|
|
|
$this->assertInstanceOf(JsonResponse::class, $response); |
|
132
|
|
|
$this->assertEquals(403, $response->getStatusCode()); |
|
133
|
|
|
$this->assertEquals('{"error":{"code":403,"message":"Forbidden"}}', $response->getContent()); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @test |
|
138
|
|
|
*/ |
|
139
|
|
|
public function it_does_not_support_remember_me() |
|
140
|
|
|
{ |
|
141
|
|
|
$this->assertFalse($this->authenticator->supportsRememberMe()); |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|