|
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\Authenticator; |
|
10
|
|
|
use Damax\Bundle\ApiAuthBundle\Security\ApiKey\TokenUserProvider; |
|
11
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
|
12
|
|
|
use PHPUnit\Framework\TestCase; |
|
13
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
|
14
|
|
|
use Symfony\Component\Security\Core\User\UserProviderInterface; |
|
15
|
|
|
|
|
16
|
|
|
class AuthenticatorTest extends TestCase |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var AbstractAuthenticator |
|
20
|
|
|
*/ |
|
21
|
|
|
private $authenticator; |
|
22
|
|
|
|
|
23
|
|
|
protected function setUp() |
|
24
|
|
|
{ |
|
25
|
|
|
/** @var Extractor $extractor */ |
|
26
|
|
|
$extractor = $this->createMock(Extractor::class); |
|
27
|
|
|
|
|
28
|
|
|
$this->authenticator = new Authenticator($extractor); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @test |
|
33
|
|
|
*/ |
|
34
|
|
|
public function it_retrieves_user_by_api_key() |
|
35
|
|
|
{ |
|
36
|
|
|
$user = $this->authenticator->getUser('ABC', new TokenUserProvider(['user' => 'ABC'])); |
|
37
|
|
|
|
|
38
|
|
|
$this->assertEquals('user', $user->getUsername()); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @test |
|
43
|
|
|
*/ |
|
44
|
|
|
public function it_retrieves_user_by_username() |
|
45
|
|
|
{ |
|
46
|
|
|
/** @var UserInterface $user */ |
|
47
|
|
|
$user = $this->createMock(UserInterface::class); |
|
48
|
|
|
|
|
49
|
|
|
/** @var UserProviderInterface|MockObject $provider */ |
|
50
|
|
|
$provider = $this->createMock(UserProviderInterface::class); |
|
51
|
|
|
|
|
52
|
|
|
$provider |
|
53
|
|
|
->expects($this->once()) |
|
54
|
|
|
->method('loadUserByUsername') |
|
55
|
|
|
->with('ABC') |
|
56
|
|
|
->willReturn($user) |
|
57
|
|
|
; |
|
58
|
|
|
|
|
59
|
|
|
$this->assertSame($user, $this->authenticator->getUser('ABC', $provider)); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|