Skip to content

Commit 1fb14e1

Browse files
authored
[1.x] fix(testing): use cookie for testing authentication (#4631)
1 parent 282fb7f commit 1fb14e1

4 files changed

Lines changed: 65 additions & 30 deletions

File tree

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Flarum.
5+
*
6+
* For detailed copyright and license information, please view the
7+
* LICENSE file that was distributed with this source code.
8+
*/
9+
10+
namespace Flarum\Tests\integration\admin;
11+
12+
use Flarum\Testing\integration\RetrievesAuthorizedUsers;
13+
use Flarum\Testing\integration\TestCase;
14+
15+
class IndexTest extends TestCase
16+
{
17+
use RetrievesAuthorizedUsers;
18+
19+
/**
20+
* @inheritDoc
21+
*/
22+
protected function setUp(): void
23+
{
24+
$this->prepareDatabase([
25+
'users' => [
26+
$this->normalUser()
27+
]
28+
]);
29+
}
30+
31+
public function test_admin_can_access_admin_route(): void
32+
{
33+
$response = $this->send(
34+
$this->request('GET', '/admin', [
35+
'authenticatedAs' => 1,
36+
])
37+
);
38+
39+
$this->assertEquals(200, $response->getStatusCode());
40+
}
41+
42+
public function test_user_cannot_access_admin_route(): void
43+
{
44+
$response = $this->send(
45+
$this->request('GET', '/admin', [
46+
'authenticatedAs' => 2,
47+
])
48+
);
49+
50+
$this->assertEquals(403, $response->getStatusCode());
51+
}
52+
}

framework/core/tests/integration/forum/GlobalLogoutTest.php

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,12 @@ protected function setUp(): void
5959
* @dataProvider canGloballyLogoutDataProvider
6060
* @test
6161
*/
62-
public function can_globally_log_out(int $authenticatedAs, string $identification, string $password)
62+
public function can_globally_log_out(int $authenticatedAs)
6363
{
64-
$loginResponse = $this->send(
65-
$this->request('POST', '/login', [
66-
'json' => compact('identification', 'password')
67-
])
68-
);
69-
7064
$response = $this->send(
71-
$this->requestWithCookiesFrom(
72-
$this->request('POST', '/global-logout'),
73-
$loginResponse,
74-
)
65+
$this->request('POST', '/global-logout', [
66+
'authenticatedAs' => $authenticatedAs,
67+
]),
7568
);
7669

7770
$this->assertEquals(204, $response->getStatusCode());
@@ -85,10 +78,10 @@ public function canGloballyLogoutDataProvider(): array
8578
{
8679
return [
8780
// Admin
88-
[1, 'admin', 'password'],
81+
[1],
8982

9083
// Normal user
91-
[2, 'normal', 'too-obscure'],
84+
[2],
9285
];
9386
}
9487
}

framework/core/tests/integration/forum/IndexTest.php

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
namespace Flarum\Tests\integration\forum;
1111

12-
use Flarum\Extend;
1312
use Flarum\Testing\integration\RetrievesAuthorizedUsers;
1413
use Flarum\Testing\integration\TestCase;
1514

@@ -22,10 +21,6 @@ class IndexTest extends TestCase
2221
*/
2322
protected function setUp(): void
2423
{
25-
$this->extend(
26-
(new Extend\Csrf)->exemptRoute('login')
27-
);
28-
2924
$this->prepareDatabase([
3025
'users' => [
3126
$this->normalUser()
@@ -51,18 +46,9 @@ public function guest_not_serialized_by_current_user_serializer()
5146
*/
5247
public function user_serialized_by_current_user_serializer()
5348
{
54-
$login = $this->send(
55-
$this->request('POST', '/login', [
56-
'json' => [
57-
'identification' => 'normal',
58-
'password' => 'too-obscure'
59-
]
60-
])
61-
);
62-
6349
$response = $this->send(
6450
$this->request('GET', '/', [
65-
'cookiesFrom' => $login
51+
'authenticatedAs' => 2,
6652
])
6753
);
6854

php-packages/testing/src/integration/BuildsHttpRequests.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use Carbon\Carbon;
1313
use Dflydev\FigCookies\SetCookie;
14+
use Flarum\Http\CookieFactory;
1415
use Illuminate\Support\Str;
1516
use Laminas\Diactoros\CallbackStream;
1617
use Psr\Http\Message\ResponseInterface as Response;
@@ -46,11 +47,14 @@ protected function requestAsUser(Request $req, int $userId): Request
4647
'user_id' => $userId,
4748
'created_at' => Carbon::now()->toDateTimeString(),
4849
'last_activity_at' => Carbon::now()->toDateTimeString(),
49-
'type' => 'session'
50+
'type' => 'session_remember'
5051
]);
5152

53+
$cookies = $this->app()->getContainer()->make(CookieFactory::class);
54+
5255
return $req
53-
->withAddedHeader('Authorization', "Token {$token}")
56+
->withAttribute('bypassCsrfToken', true)
57+
->withCookieParams([$cookies->getName('remember') => $token])
5458
// We save the token as an attribute so that we can retrieve it for test purposes.
5559
->withAttribute('tests_token', $token);
5660
}

0 commit comments

Comments
 (0)