1
0
mirror of https://github.com/flarum/core.git synced 2025-08-05 16:07:34 +02:00

fix(testing): use cookie for testing authentication (#3924)

This commit is contained in:
Sami Mazouz
2023-11-10 22:35:24 +01:00
committed by GitHub
parent 3107319812
commit 693bce912a
4 changed files with 65 additions and 30 deletions

View File

@@ -0,0 +1,52 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Tests\integration\admin;
use Flarum\Testing\integration\RetrievesAuthorizedUsers;
use Flarum\Testing\integration\TestCase;
class IndexTest extends TestCase
{
use RetrievesAuthorizedUsers;
/**
* @inheritDoc
*/
protected function setUp(): void
{
$this->prepareDatabase([
'users' => [
$this->normalUser()
]
]);
}
public function admin_can_access_admin_route(): void
{
$response = $this->send(
$this->request('GET', '/admin', [
'authenticatedAs' => 1,
])
);
$this->assertEquals(200, $response->getStatusCode());
}
public function user_cannot_access_admin_route(): void
{
$response = $this->send(
$this->request('GET', '/admin', [
'authenticatedAs' => 2,
])
);
$this->assertEquals(403, $response->getStatusCode());
}
}

View File

@@ -59,19 +59,12 @@ class GlobalLogoutTest extends TestCase
* @dataProvider canGloballyLogoutDataProvider
* @test
*/
public function can_globally_log_out(int $authenticatedAs, string $identification, string $password)
public function can_globally_log_out(int $authenticatedAs)
{
$loginResponse = $this->send(
$this->request('POST', '/login', [
'json' => compact('identification', 'password')
])
);
$response = $this->send(
$this->requestWithCookiesFrom(
$this->request('POST', '/global-logout'),
$loginResponse,
)
$this->request('POST', '/global-logout', [
'authenticatedAs' => $authenticatedAs,
]),
);
$this->assertEquals(204, $response->getStatusCode());
@@ -85,10 +78,10 @@ class GlobalLogoutTest extends TestCase
{
return [
// Admin
[1, 'admin', 'password'],
[1],
// Normal user
[2, 'normal', 'too-obscure'],
[2],
];
}
}

View File

@@ -9,7 +9,6 @@
namespace Flarum\Tests\integration\forum;
use Flarum\Extend;
use Flarum\Testing\integration\RetrievesAuthorizedUsers;
use Flarum\Testing\integration\TestCase;
@@ -22,10 +21,6 @@ class IndexTest extends TestCase
*/
protected function setUp(): void
{
$this->extend(
(new Extend\Csrf)->exemptRoute('login')
);
$this->prepareDatabase([
'users' => [
$this->normalUser()
@@ -51,18 +46,9 @@ class IndexTest extends TestCase
*/
public function user_serialized_by_current_user_serializer()
{
$login = $this->send(
$this->request('POST', '/login', [
'json' => [
'identification' => 'normal',
'password' => 'too-obscure'
]
])
);
$response = $this->send(
$this->request('GET', '/', [
'cookiesFrom' => $login
'authenticatedAs' => 2,
])
);