-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecurityControllerTest.php
More file actions
164 lines (139 loc) · 5.52 KB
/
Copy pathSecurityControllerTest.php
File metadata and controls
164 lines (139 loc) · 5.52 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
declare(strict_types=1);
namespace App\Tests\Integration\Controller;
use App\Controller\SecurityController;
use App\Entity\User;
use App\Enum\UserStatus;
use App\Security\UserManager;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
/**
* End-to-end login / logout flow against the real `form_login`
* authenticator wired in `security.yaml`.
*
* Relies on the baseline `UserFixtures` (alice + bob with password
* `password`) loaded by `tests/bootstrap_integration.php`.
*/
final class SecurityControllerTest extends WebTestCase
{
private KernelBrowser $client;
protected function setUp(): void
{
$this->client = self::createClient();
}
public function testLoginPageRenders(): void
{
$this->client->request('GET', '/login');
self::assertResponseIsSuccessful();
self::assertSelectorExists('input[name="_username"]');
self::assertSelectorExists('input[name="_password"]');
self::assertSelectorExists('input[name="_csrf_token"]');
}
public function testSuccessfulLoginRedirectsToFrontpage(): void
{
$crawler = $this->client->request('GET', '/login');
$form = $crawler->filter('form')->form();
$form['_username'] = 'alice@example.test';
$form['_password'] = 'password';
$this->client->submit($form);
self::assertResponseRedirects('/');
$this->client->followRedirect();
self::assertResponseIsSuccessful();
/** @var User|null $token */
$token = $this->client->getContainer()->get('security.token_storage')->getToken()?->getUser();
self::assertInstanceOf(User::class, $token);
self::assertSame('alice@example.test', $token->getUserIdentifier());
}
public function testFailedLoginShowsErrorAndStaysOnLoginPage(): void
{
$crawler = $this->client->request('GET', '/login');
$form = $crawler->filter('form')->form();
$form['_username'] = 'alice@example.test';
$form['_password'] = 'wrong';
$this->client->submit($form);
// Form login redirects back to the login page on failure.
self::assertResponseRedirects('/login');
$this->client->followRedirect();
self::assertResponseIsSuccessful();
self::assertNull(
$this->client->getContainer()->get('security.token_storage')->getToken(),
);
}
public function testLogoutActionThrowsWhenInvokedDirectly(): void
{
// The firewall intercepts /logout in production, so the method body
// is unreachable through HTTP. Calling it directly proves the
// defensive throw is wired correctly.
$controller = new SecurityController();
$this->expectException(\LogicException::class);
$controller->logout();
}
public function testLogoutClearsTheSession(): void
{
// Sign in first.
$crawler = $this->client->request('GET', '/login');
$form = $crawler->filter('form')->form();
$form['_username'] = 'alice@example.test';
$form['_password'] = 'password';
$this->client->submit($form);
$this->client->followRedirect();
$this->client->request('GET', '/logout');
// Symfony intercepts /logout and redirects to the configured target.
self::assertResponseRedirects('/');
$this->client->followRedirect();
self::assertNull(
$this->client->getContainer()->get('security.token_storage')->getToken(),
);
}
// Tests that a Pending user is rejected at login with the localised pending message.
public function testPendingUserCannotLogIn(): void
{
$this->client->getContainer()->get(UserManager::class)->createUser(
'carol@example.test',
'Carol',
'password',
status: UserStatus::Pending,
);
$crawler = $this->client->request('GET', '/login');
$form = $crawler->filter('form')->form();
$form['_username'] = 'carol@example.test';
$form['_password'] = 'password';
$this->client->submit($form);
self::assertResponseRedirects('/login');
$crawler = $this->client->followRedirect();
self::assertResponseIsSuccessful();
// Localised pending message is rendered on the form.
self::assertStringContainsString(
'venter på godkendelse',
$crawler->filter('body')->text(),
);
self::assertNull(
$this->client->getContainer()->get('security.token_storage')->getToken(),
);
}
// Tests that a Blocked user is rejected at login with the localised blocked message.
public function testBlockedUserCannotLogIn(): void
{
$this->client->getContainer()->get(UserManager::class)->createUser(
'dora@example.test',
'Dora',
'password',
status: UserStatus::Blocked,
);
$crawler = $this->client->request('GET', '/login');
$form = $crawler->filter('form')->form();
$form['_username'] = 'dora@example.test';
$form['_password'] = 'password';
$this->client->submit($form);
self::assertResponseRedirects('/login');
$crawler = $this->client->followRedirect();
self::assertResponseIsSuccessful();
self::assertStringContainsString(
'spærret',
$crawler->filter('body')->text(),
);
self::assertNull(
$this->client->getContainer()->get('security.token_storage')->getToken(),
);
}
}