Completed
Branch master (910c19)
by Dmitri
01:43
created

StorageUserProviderTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 84
rs 10
c 0
b 0
f 0
wmc 7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Damax\Bundle\ApiAuthBundle\Tests\Security\ApiKey;
6
7
use Damax\Bundle\ApiAuthBundle\Key\Storage\InMemoryStorage;
8
use Damax\Bundle\ApiAuthBundle\Security\ApiKey\InvalidApiKey;
9
use Damax\Bundle\ApiAuthBundle\Security\ApiKey\StorageUserProvider;
10
use Damax\Bundle\ApiAuthBundle\Security\ApiUser;
11
use PHPUnit\Framework\TestCase;
12
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
13
use Symfony\Component\Security\Core\User\User as SecurityUser;
14
15
class StorageUserProviderTest extends TestCase
16
{
17
    /**
18
     * @var StorageUserProvider
19
     */
20
    private $userProvider;
21
22
    protected function setUp()
23
    {
24
        $this->userProvider = new StorageUserProvider(new InMemoryStorage(['foo' => 'ABC', 'bar' => 'XYZ']));
25
    }
26
27
    /**
28
     * @test
29
     */
30
    public function it_supports_class()
31
    {
32
        $this->assertTrue($this->userProvider->supportsClass(ApiUser::class));
33
        $this->assertFalse($this->userProvider->supportsClass(SecurityUser::class));
34
    }
35
36
    /**
37
     * @test
38
     */
39
    public function it_loads_user()
40
    {
41
        $user = $this->userProvider->loadUserByUsername('foo');
42
43
        $this->assertInstanceOf(ApiUser::class, $user);
44
        $this->assertEquals('foo', $user->getUsername());
45
46
        $user = $this->userProvider->loadUserByUsername('bar');
47
48
        $this->assertInstanceOf(ApiUser::class, $user);
49
        $this->assertEquals('bar', $user->getUsername());
50
    }
51
52
    /**
53
     * @test
54
     */
55
    public function it_loads_user_by_api_key()
56
    {
57
        $user = $this->userProvider->loadUserByApiKey('ABC');
58
59
        $this->assertInstanceOf(ApiUser::class, $user);
60
        $this->assertEquals('foo', $user->getUsername());
61
62
        $user = $this->userProvider->loadUserByApiKey('XYZ');
63
64
        $this->assertInstanceOf(ApiUser::class, $user);
65
        $this->assertEquals('bar', $user->getUsername());
66
    }
67
68
    /**
69
     * @test
70
     */
71
    public function it_throws_exception_when_loading_user_with_unregistered_api_key()
72
    {
73
        $this->expectException(InvalidApiKey::class);
74
75
        $this->userProvider->loadUserByApiKey('123');
76
    }
77
78
    /**
79
     * @test
80
     */
81
    public function it_throws_exception_when_loading_user_with_expired_key()
82
    {
83
        $this->expectException(InvalidApiKey::class);
84
85
        $storage = new InMemoryStorage(['foo' => 'ABC', 'bar' => 'XYZ'], 0);
86
87
        (new StorageUserProvider($storage))->loadUserByApiKey('ABC');
88
    }
89
90
    /**
91
     * @test
92
     */
93
    public function it_throws_exception_on_refreshing_user()
94
    {
95
        $this->expectException(UnsupportedUserException::class);
96
        $this->expectExceptionMessage('Provider "Damax\Bundle\ApiAuthBundle\Security\ApiKey\StorageUserProvider" must be configured as stateless.');
97
98
        $this->userProvider->refreshUser(new ApiUser('foo'));
99
    }
100
}
101