1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Damax\Bundle\ApiAuthBundle\Tests\Security\ApiKey; |
6
|
|
|
|
7
|
|
|
use Damax\Bundle\ApiAuthBundle\Extractor\Extractor; |
8
|
|
|
use Damax\Bundle\ApiAuthBundle\Security\AbstractAuthenticator; |
9
|
|
|
use Damax\Bundle\ApiAuthBundle\Security\ApiKey\ApiKeyUserProvider; |
10
|
|
|
use Damax\Bundle\ApiAuthBundle\Security\ApiKey\Authenticator; |
11
|
|
|
use Damax\Bundle\ApiAuthBundle\Security\ApiUser; |
12
|
|
|
use Damax\Bundle\ApiAuthBundle\Security\ResponseFactory; |
13
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
14
|
|
|
use PHPUnit\Framework\TestCase; |
15
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
16
|
|
|
use Symfony\Component\Security\Core\User\UserProviderInterface; |
17
|
|
|
|
18
|
|
|
class AuthenticatorTest extends TestCase |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var AbstractAuthenticator |
22
|
|
|
*/ |
23
|
|
|
private $authenticator; |
24
|
|
|
|
25
|
|
|
protected function setUp() |
26
|
|
|
{ |
27
|
|
|
/** @var Extractor $extractor */ |
28
|
|
|
$extractor = $this->createMock(Extractor::class); |
29
|
|
|
|
30
|
|
|
/** @var ResponseFactory $responseFactory */ |
31
|
|
|
$responseFactory = $this->createMock(ResponseFactory::class); |
32
|
|
|
|
33
|
|
|
$this->authenticator = new Authenticator($extractor, $responseFactory); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @test |
38
|
|
|
*/ |
39
|
|
|
public function it_retrieves_user_by_api_key() |
40
|
|
|
{ |
41
|
|
|
/** @var MockObject|ApiKeyUserProvider $provider */ |
42
|
|
|
$provider = $this->createMock(ApiKeyUserProvider::class); |
43
|
|
|
|
44
|
|
|
$provider |
45
|
|
|
->expects($this->once()) |
46
|
|
|
->method('loadUserByApiKey') |
47
|
|
|
->with('XYZ') |
48
|
|
|
->willReturn($user = new ApiUser('john.doe')) |
49
|
|
|
; |
50
|
|
|
|
51
|
|
|
$this->assertSame($user, $this->authenticator->getUser('XYZ', $provider)); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @test |
56
|
|
|
*/ |
57
|
|
|
public function it_retrieves_user_by_username() |
58
|
|
|
{ |
59
|
|
|
/** @var UserInterface $user */ |
60
|
|
|
$user = $this->createMock(UserInterface::class); |
61
|
|
|
|
62
|
|
|
/** @var MockObject|UserProviderInterface $provider */ |
63
|
|
|
$provider = $this->createMock(UserProviderInterface::class); |
64
|
|
|
|
65
|
|
|
$provider |
66
|
|
|
->expects($this->once()) |
67
|
|
|
->method('loadUserByUsername') |
68
|
|
|
->with('XYZ') |
69
|
|
|
->willReturn($user) |
70
|
|
|
; |
71
|
|
|
|
72
|
|
|
$this->assertSame($user, $this->authenticator->getUser('XYZ', $provider)); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|