-
Notifications
You must be signed in to change notification settings - Fork 28
/
AuthenticationTest.php
147 lines (121 loc) · 3.98 KB
/
AuthenticationTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
declare(strict_types=1);
namespace PhpList\Core\Tests\Integration\Security;
use PhpList\Core\Domain\Model\Identity\Administrator;
use PhpList\Core\Security\Authentication;
use PhpList\Core\TestingSupport\Traits\DatabaseTestTrait;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
/**
* Testcase.
*
* @author Oliver Klee <oliver@phplist.com>
*/
class AuthenticationTest extends TestCase
{
use DatabaseTestTrait;
/**
* @var string
*/
const ADMINISTRATOR_TABLE_NAME = 'phplist_admin';
/**
* @var string
*/
const TOKEN_TABLE_NAME = 'phplist_admintoken';
/**
* @var Authentication
*/
private $subject = null;
protected function setUp()
{
$this->setUpDatabaseTest();
$this->subject = $this->container->get(Authentication::class);
}
/**
* @test
*/
public function subjectIsAvailableViaContainer()
{
static::assertInstanceOf(Authentication::class, $this->subject);
}
/**
* @test
*/
public function classIsRegisteredAsSingletonInContainer()
{
static::assertSame($this->subject, $this->container->get(Authentication::class));
}
/**
* @test
*/
public function authenticateByApiKeyWithValidApiKeyReturnsMatchingAdministrator()
{
$this->getDataSet()->addTable(
static::ADMINISTRATOR_TABLE_NAME,
__DIR__ . '/../Domain/Repository/Fixtures/Administrator.csv'
);
$this->getDataSet()->addTable(
static::TOKEN_TABLE_NAME,
__DIR__ . '/../Domain/Repository/Fixtures/AdministratorTokenWithAdministrator.csv'
);
$this->applyDatabaseChanges();
$apiKey = 'cfdf64eecbbf336628b0f3071adba762';
$request = new Request();
$request->headers->add(['php-auth-pw' => $apiKey]);
$result = $this->subject->authenticateByApiKey($request);
static::assertInstanceOf(Administrator::class, $result);
static::assertSame(1, $result->getId());
}
/**
* @test
*/
public function authenticateByApiKeyWithValidApiKeyAndDisabledAdministratorReturnsNull()
{
$this->getDataSet()->addTable(
static::ADMINISTRATOR_TABLE_NAME,
__DIR__ . '/../Domain/Repository/Fixtures/Administrator.csv'
);
$this->getDataSet()->addTable(
static::TOKEN_TABLE_NAME,
__DIR__ . '/../Domain/Repository/Fixtures/AdministratorTokenWithAdministrator.csv'
);
$this->applyDatabaseChanges();
$apiKey = 'cfdf64eecbbf336628b0f3071adba765';
$request = new Request();
$request->headers->add(['php-auth-pw' => $apiKey]);
$result = $this->subject->authenticateByApiKey($request);
static::assertNull($result);
}
/**
* @test
*/
public function authenticateByApiKeyWithValidApiKeyForInexistentAdministratorReturnsNull()
{
$this->getDataSet()->addTable(
static::TOKEN_TABLE_NAME,
__DIR__ . '/../Domain/Repository/Fixtures/AdministratorTokenWithAdministrator.csv'
);
$this->applyDatabaseChanges();
$apiKey = 'cfdf64eecbbf336628b0f3071adba763';
$request = new Request();
$request->headers->add(['php-auth-pw' => $apiKey]);
$result = $this->subject->authenticateByApiKey($request);
static::assertNull($result);
}
/**
* @test
*/
public function authenticateByApiKeyWithValidApiKeyForNonSuperUserAdministratorReturnsNull()
{
$this->getDataSet()->addTable(
static::TOKEN_TABLE_NAME,
__DIR__ . '/../Domain/Repository/Fixtures/AdministratorTokenWithAdministrator.csv'
);
$this->applyDatabaseChanges();
$apiKey = 'cfdf64eecbbf336628b0f3071adba764';
$request = new Request();
$request->headers->add(['php-auth-pw' => $apiKey]);
$result = $this->subject->authenticateByApiKey($request);
static::assertNull($result);
}
}