1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Frontend\Modules\Profiles\Tests\Engine; |
4
|
|
|
|
5
|
|
|
use Common\WebTestCase; |
6
|
|
|
use Frontend\Core\Engine\Model as FrontendModel; |
7
|
|
|
use Frontend\Modules\Profiles\Engine\Authentication; |
8
|
|
|
use Frontend\Modules\Profiles\Tests\DataFixtures\LoadProfiles; |
9
|
|
|
use SpoonDatabase; |
10
|
|
|
use Symfony\Component\HttpFoundation\Request; |
11
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
12
|
|
|
use Symfony\Component\HttpFoundation\Session\Session; |
13
|
|
|
|
14
|
|
|
final class AuthenticationTest extends WebTestCase |
15
|
|
|
{ |
16
|
|
|
/** @var SpoonDatabase */ |
17
|
|
|
private $database; |
18
|
|
|
|
19
|
|
|
/** @var Session */ |
20
|
|
|
private $session; |
21
|
|
|
|
22
|
|
|
public function setUp(): void |
23
|
|
|
{ |
24
|
|
|
parent::setUp(); |
25
|
|
|
|
26
|
|
|
if (!defined('APPLICATION')) { |
27
|
|
|
define('APPLICATION', 'Frontend'); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
$client = self::createClient(); |
31
|
|
|
$this->loadFixtures($client, [LoadProfiles::class]); |
32
|
|
|
|
33
|
|
|
$this->database = FrontendModel::get('database'); |
34
|
|
|
$this->session = FrontendModel::getSession(); |
35
|
|
|
|
36
|
|
|
// Create a request stack for cookie stuff |
37
|
|
|
$requestStack = new RequestStack(); |
38
|
|
|
$request = new Request(); |
39
|
|
|
$request->setSession($this->session); |
40
|
|
|
$request->cookies->set('frontend_profile_secret_key', 'NotSoSecret'); |
41
|
|
|
$requestStack->push($request); |
42
|
|
|
FrontendModel::getContainer()->set('request_stack', $requestStack); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function testOldSessionCleanUp() |
46
|
|
|
{ |
47
|
|
|
$this->assertEquals('2', $this->database->getVar('SELECT COUNT(session_id) FROM profiles_sessions')); |
48
|
|
|
|
49
|
|
|
Authentication::cleanupOldSessions(); |
50
|
|
|
|
51
|
|
|
$this->assertFalse((bool) $this->database->getVar('SELECT 1 FROM profiles_sessions WHERE session_id = "1234567890"')); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testGettingLoginStatusForNonExistingUser() |
55
|
|
|
{ |
56
|
|
|
$this->assertEquals('invalid', Authentication::getLoginStatus('[email protected]', 'wrong')); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testGettingLoginStatusForUserWithWrongPassword() |
60
|
|
|
{ |
61
|
|
|
$this->assertEquals('invalid', Authentication::getLoginStatus('[email protected]', 'wrong')); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testGettingLoginStatusForActiveUserWithCorrectPassword() |
65
|
|
|
{ |
66
|
|
|
$this->assertEquals('active', Authentication::getLoginStatus('[email protected]', 'forkcms')); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function testGettingLoginStatusForInactiveUserWithCorrectPassword() |
70
|
|
|
{ |
71
|
|
|
$this->assertEquals('inactive', Authentication::getLoginStatus('[email protected]', 'forkcms')); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function testGettingLoginStatusForDeletedUserWithCorrectPassword() |
75
|
|
|
{ |
76
|
|
|
$this->assertEquals('deleted', Authentication::getLoginStatus('[email protected]', 'forkcms')); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testGettingLoginStatusForBlockedUserWithCorrectPassword() |
80
|
|
|
{ |
81
|
|
|
$this->assertEquals('blocked', Authentication::getLoginStatus('[email protected]', 'forkcms')); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testLoggingInMakesUsLoggedIn() |
85
|
|
|
{ |
86
|
|
|
Authentication::login(1); |
87
|
|
|
$this->assertTrue(Authentication::isLoggedIn()); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function testLoggingInCleansUpOldSessions() |
91
|
|
|
{ |
92
|
|
|
$this->assertEquals('2', $this->database->getVar('SELECT COUNT(session_id) FROM profiles_sessions')); |
93
|
|
|
|
94
|
|
|
Authentication::login(1); |
95
|
|
|
|
96
|
|
|
$this->assertFalse((bool) $this->database->getVar('SELECT 1 FROM profiles_sessions WHERE session_id = "1234567890"')); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function testLoggingInSetsASessionVariable() |
100
|
|
|
{ |
101
|
|
|
$this->assertNull(FrontendModel::getSession()->get('frontend_profile_logged_in')); |
102
|
|
|
|
103
|
|
|
Authentication::login(1); |
104
|
|
|
|
105
|
|
|
$this->assertTrue(FrontendModel::getSession()->get('frontend_profile_logged_in')); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function testLogginInAddsASessionToTheDatabase() |
109
|
|
|
{ |
110
|
|
|
$this->assertEquals( |
111
|
|
|
'0', |
112
|
|
|
$this->database->getVar( |
113
|
|
|
'SELECT COUNT(session_id) |
114
|
|
|
FROM profiles_sessions |
115
|
|
|
WHERE profile_id = 2' |
116
|
|
|
) |
117
|
|
|
); |
118
|
|
|
|
119
|
|
|
Authentication::login(2); |
120
|
|
|
|
121
|
|
|
$this->assertEquals( |
122
|
|
|
'1', |
123
|
|
|
$this->database->getVar( |
124
|
|
|
'SELECT COUNT(session_id) |
125
|
|
|
FROM profiles_sessions |
126
|
|
|
WHERE profile_id = 2' |
127
|
|
|
) |
128
|
|
|
); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function testProfileLastLoginGetsUpdatedWhenLoggingIn() |
132
|
|
|
{ |
133
|
|
|
$initalLastLogin = $this->database->getVar('SELECT last_login FROM profiles WHERE id = 1'); |
134
|
|
|
|
135
|
|
|
Authentication::login(1); |
136
|
|
|
|
137
|
|
|
$newLastLogin = $this->database->getVar('SELECT last_login FROM profiles WHERE id = 1'); |
138
|
|
|
|
139
|
|
|
$this->assertLessThan($newLastLogin, $initalLastLogin); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function testLogoutDeletesSessionFromDatabase(): void |
143
|
|
|
{ |
144
|
|
|
$this->database->insert( |
145
|
|
|
'profiles_sessions', |
146
|
|
|
[ |
147
|
|
|
'session_id' => $this->session->getId(), |
148
|
|
|
'profile_id' => 1, |
149
|
|
|
'secret_key' => 'Fork is da bomb', |
150
|
|
|
'date' => '1970-01-01 00:00:00', |
151
|
|
|
] |
152
|
|
|
); |
153
|
|
|
|
154
|
|
|
$this->assertTrue( |
155
|
|
|
(bool) $this->database->getVar( |
156
|
|
|
'SELECT 1 FROM profiles_sessions WHERE session_id = ?', |
157
|
|
|
$this->session->getId() |
158
|
|
|
) |
159
|
|
|
); |
160
|
|
|
|
161
|
|
|
Authentication::logout(); |
162
|
|
|
|
163
|
|
|
$this->assertFalse( |
164
|
|
|
(bool) $this->database->getVar( |
165
|
|
|
'SELECT 1 FROM profiles_sessions WHERE session_id = ?', |
166
|
|
|
$this->session->getId() |
167
|
|
|
) |
168
|
|
|
); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function testLogoutSetsLoggedInSessionToFalse(): void |
172
|
|
|
{ |
173
|
|
|
$this->session->set('frontend_profile_logged_in', true); |
174
|
|
|
$this->assertTrue($this->session->get('frontend_profile_logged_in')); |
175
|
|
|
|
176
|
|
|
Authentication::logout(); |
177
|
|
|
|
178
|
|
|
$this->assertFalse($this->session->get('frontend_profile_logged_in')); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public function testLogoutDeletesSecretKeyCookie(): void |
182
|
|
|
{ |
183
|
|
|
$cookie = FrontendModel::getContainer()->get('fork.cookie'); |
184
|
|
|
|
185
|
|
|
$this->assertTrue($cookie->has('frontend_profile_secret_key')); |
186
|
|
|
$this->assertEquals('NotSoSecret', $cookie->get('frontend_profile_secret_key')); |
187
|
|
|
|
188
|
|
|
Authentication::logout(); |
189
|
|
|
|
190
|
|
|
$this->assertFalse($cookie->has('frontend_profile_secret_key')); |
191
|
|
|
$this->assertNotEquals('NotSoSecret', $cookie->get('frontend_profile_secret_key')); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|