1
0
mirror of https://github.com/flarum/core.git synced 2025-07-30 21:20:24 +02:00

Run API Client requests through middleware (#2783)

- Add integration tests for login and registration
- Use URL instead of controller
- Add fluent API
- Allow setting parent request, user, session
This commit is contained in:
Alexander Skvortsov
2021-05-10 17:41:38 -04:00
committed by GitHub
parent b0a26eb78c
commit 104a31ba30
13 changed files with 427 additions and 93 deletions

View File

@@ -0,0 +1,72 @@
<?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\forum;
use Flarum\Extend;
use Flarum\Testing\integration\RetrievesAuthorizedUsers;
use Flarum\Testing\integration\TestCase;
class IndexTest extends TestCase
{
use RetrievesAuthorizedUsers;
/**
* @inheritDoc
*/
protected function setUp(): void
{
$this->extend(
(new Extend\Csrf)->exemptRoute('login')
);
$this->prepareDatabase([
'users' => [
$this->normalUser()
]
]);
}
/**
* @test
*/
public function guest_not_serialized_by_current_user_serializer()
{
$response = $this->send(
$this->request('GET', '/')
);
$this->assertEquals(200, $response->getStatusCode());
$this->assertStringNotContainsString('preferences', $response->getBody()->getContents());
}
/**
* @test
*/
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
])
);
$this->assertEquals(200, $response->getStatusCode());
$this->assertStringContainsString('preferences', $response->getBody()->getContents());
}
}

View File

@@ -0,0 +1,95 @@
<?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\forum;
use Flarum\Extend;
use Flarum\Http\AccessToken;
use Flarum\Testing\integration\RetrievesAuthorizedUsers;
use Flarum\Testing\integration\TestCase;
class LoginTest extends TestCase
{
use RetrievesAuthorizedUsers;
/**
* @inheritDoc
*/
protected function setUp(): void
{
$this->extend(
(new Extend\Csrf)->exemptRoute('login')
);
$this->prepareDatabase([
'users' => [
$this->normalUser()
]
]);
}
/**
* @test
*/
public function cant_login_without_data()
{
$response = $this->send(
$this->request('POST', '/login', [
'json' => []
])
);
$this->assertEquals(401, $response->getStatusCode());
}
/**
* @test
*/
public function cant_login_with_wrong_password()
{
$response = $this->send(
$this->request('POST', '/login', [
'json' => [
'identification' => 'normal',
'password' => 'incorrect'
]
])
);
$this->assertEquals(401, $response->getStatusCode());
}
/**
* @test
*/
public function can_login_with_data()
{
$response = $this->send(
$this->request('POST', '/login', [
'json' => [
'identification' => 'normal',
'password' => 'too-obscure'
]
])
);
$this->assertEquals(200, $response->getStatusCode());
// The response body should contain the user ID...
$body = (string) $response->getBody();
$this->assertJson($body);
$data = json_decode($body, true);
$this->assertEquals(2, $data['userId']);
// ...and an access token belonging to this user.
$token = $data['token'];
$this->assertEquals(2, AccessToken::whereToken($token)->firstOrFail()->user_id);
}
}

View File

@@ -0,0 +1,90 @@
<?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\forum;
use Flarum\Extend;
use Flarum\Testing\integration\TestCase;
use Flarum\User\User;
class RegisterTest extends TestCase
{
/**
* @inheritDoc
*/
protected function setUp(): void
{
$this->extend(
(new Extend\Csrf)->exemptRoute('register')
);
}
/**
* @test
*/
public function cant_register_without_data()
{
$response = $this->send(
$this->request('POST', '/register')
);
$this->assertEquals(422, $response->getStatusCode());
// The response body should contain details about the failed validation
$body = (string) $response->getBody();
$this->assertJson($body);
$this->assertEquals([
'errors' => [
[
'status' => '422',
'code' => 'validation_error',
'detail' => 'The username field is required.',
'source' => ['pointer' => '/data/attributes/username'],
],
[
'status' => '422',
'code' => 'validation_error',
'detail' => 'The email field is required.',
'source' => ['pointer' => '/data/attributes/email'],
],
[
'status' => '422',
'code' => 'validation_error',
'detail' => 'The password field is required.',
'source' => ['pointer' => '/data/attributes/password'],
],
],
], json_decode($body, true));
}
/**
* @test
*/
public function can_register_with_data()
{
$response = $this->send(
$this->request('POST', '/register', [
'json' => [
'username' => 'test',
'password' => 'too-obscure',
'email' => 'test@machine.local',
]
])
);
$this->assertEquals(201, $response->getStatusCode());
/** @var User $user */
$user = User::where('username', 'test')->firstOrFail();
$this->assertEquals(0, $user->is_email_confirmed);
$this->assertEquals('test', $user->username);
$this->assertEquals('test@machine.local', $user->email);
}
}