Merge pull request #272 from SergiX44/login-tests
tests: add Login tests
This commit is contained in:
commit
9a5335ae7f
7 changed files with 150 additions and 103 deletions
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
(PHP_MAJOR_VERSION >= 7 && PHP_MINOR_VERSION >= 1) ?: die('Sorry, PHP 7.1 or above is required to run XBackBone.');
|
||||
((PHP_MAJOR_VERSION >= 7 && PHP_MINOR_VERSION >= 1) || PHP_MAJOR_VERSION > 7) ?: die('Sorry, PHP 7.1 or above is required to run XBackBone.');
|
||||
require __DIR__.'/vendor/autoload.php';
|
||||
|
||||
define('BASE_DIR', realpath(__DIR__).DIRECTORY_SEPARATOR);
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
(PHP_MAJOR_VERSION >= 7 && PHP_MINOR_VERSION >= 1) ?: die('Sorry, PHP 7.1 or above is required to run XBackBone.');
|
||||
((PHP_MAJOR_VERSION >= 7 && PHP_MINOR_VERSION >= 1) || PHP_MAJOR_VERSION > 7) ?: die('Sorry, PHP 7.1 or above is required to run XBackBone.');
|
||||
require __DIR__.'/../vendor/autoload.php';
|
||||
|
||||
use App\Database\Migrator;
|
||||
|
|
|
@ -1,34 +0,0 @@
|
|||
<?php
|
||||
|
||||
|
||||
namespace Tests;
|
||||
|
||||
use GuzzleHttp\Psr7\ServerRequest;
|
||||
use Slim\App;
|
||||
use Symfony\Component\BrowserKit\AbstractBrowser;
|
||||
use Symfony\Component\BrowserKit\CookieJar;
|
||||
use Symfony\Component\BrowserKit\History;
|
||||
use Symfony\Component\BrowserKit\Response;
|
||||
|
||||
class Client extends AbstractBrowser
|
||||
{
|
||||
private $app;
|
||||
|
||||
public function __construct(App $app, $server = [], History $history = null, CookieJar $cookieJar = null)
|
||||
{
|
||||
parent::__construct($server, $history, $cookieJar);
|
||||
$this->app = $app;
|
||||
}
|
||||
|
||||
protected function doRequest($request)
|
||||
{
|
||||
$response = $this->app->handle(new ServerRequest($request->getMethod(), $request->getUri(), [], $request->getContent()));
|
||||
|
||||
$body = $response->getBody();
|
||||
|
||||
if ($body->isSeekable()) {
|
||||
$body->rewind();
|
||||
}
|
||||
return new Response($body->getContents(), $response->getStatusCode(), $response->getHeaders());
|
||||
}
|
||||
}
|
132
tests/Feature/Auth/LoginControllerTest.php
Normal file
132
tests/Feature/Auth/LoginControllerTest.php
Normal file
|
@ -0,0 +1,132 @@
|
|||
<?php
|
||||
|
||||
|
||||
namespace Tests\Feature\Auth;
|
||||
|
||||
use Tests\TestCase;
|
||||
|
||||
class LoginControllerTest extends TestCase
|
||||
{
|
||||
|
||||
/** @test */
|
||||
public function it_loads_the_login_page()
|
||||
{
|
||||
$response = $this->get(route('login.show'));
|
||||
|
||||
$this->assertSame(200, $response->getStatusCode());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_redirect_to_login_with_no_data()
|
||||
{
|
||||
$response = $this->post(route('login.show'));
|
||||
|
||||
$this->assertSame(302, $response->getStatusCode());
|
||||
$this->assertSame(route('login.show'), $response->getHeaderLine('Location'));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_login_with_correct_data()
|
||||
{
|
||||
$this->createAdminUser();
|
||||
|
||||
$response = $this->get(route('login.show'));
|
||||
$form = $this->getCrawler($response)
|
||||
->selectButton('Login')
|
||||
->form([
|
||||
'username' => 'admin@example.com',
|
||||
'password' => 'admin',
|
||||
], 'POST');
|
||||
|
||||
$response = $this->submitForm($form);
|
||||
$this->assertSame(302, $response->getStatusCode());
|
||||
$this->assertSame(route('home'), $response->getHeaderLine('Location'));
|
||||
|
||||
$response = $this->get(route('home'));
|
||||
$this->assertSame(200, $response->getStatusCode());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_hide_register_by_default()
|
||||
{
|
||||
$response = $this->get(route('login.show'));
|
||||
$this->assertSame(200, $response->getStatusCode());
|
||||
$this->assertStringNotContainsString('Register', $this->getCrawler($response)->text());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_show_register_when_enabled()
|
||||
{
|
||||
$this->updateSetting('register_enabled', 'on');
|
||||
|
||||
$response = $this->get(route('login.show'));
|
||||
$this->assertSame(200, $response->getStatusCode());
|
||||
$this->assertStringContainsString('Register', $this->getCrawler($response)->text());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_redirect_to_home_if_logged_in()
|
||||
{
|
||||
$this->createAdminUser();
|
||||
|
||||
$response = $this->get(route('login.show'));
|
||||
$form = $this->getCrawler($response)
|
||||
->selectButton('Login')
|
||||
->form([
|
||||
'username' => 'admin@example.com',
|
||||
'password' => 'admin',
|
||||
'remember' => 'on',
|
||||
], 'POST');
|
||||
|
||||
$this->submitForm($form);
|
||||
|
||||
$response = $this->get(route('login'));
|
||||
$this->assertSame(302, $response->getStatusCode());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_redirects_to()
|
||||
{
|
||||
$this->app->getContainer()->get('session')->set('redirectTo', route('profile'));
|
||||
$this->createAdminUser();
|
||||
|
||||
$response = $this->get(route('login.show'));
|
||||
$form = $this->getCrawler($response)
|
||||
->selectButton('Login')
|
||||
->form([
|
||||
'username' => 'admin@example.com',
|
||||
'password' => 'admin',
|
||||
'remember' => 'on',
|
||||
], 'POST');
|
||||
|
||||
$redirect = $this->submitForm($form)->getHeaderLine('Location');
|
||||
$this->assertSame(route('profile'), $redirect);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_logout_the_user()
|
||||
{
|
||||
$this->createAdminUser();
|
||||
|
||||
$response = $this->get(route('login.show'));
|
||||
$form = $this->getCrawler($response)
|
||||
->selectButton('Login')
|
||||
->form([
|
||||
'username' => 'admin@example.com',
|
||||
'password' => 'admin',
|
||||
'remember' => 'on',
|
||||
], 'POST');
|
||||
|
||||
$this->submitForm($form);
|
||||
$this->assertSame(200, $response->getStatusCode());
|
||||
|
||||
$response = $this->post(route('logout'));
|
||||
$this->assertSame(302, $response->getStatusCode());
|
||||
|
||||
$response = $this->get(route('home'));
|
||||
$this->assertSame(302, $response->getStatusCode());
|
||||
$this->assertSame(route('login.show'), $response->getHeaderLine('Location'));
|
||||
|
||||
$this->assertFalse($this->app->getContainer()->get('session')->get('logged'));
|
||||
}
|
||||
}
|
|
@ -1,66 +0,0 @@
|
|||
<?php
|
||||
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use Tests\TestCase;
|
||||
|
||||
class LoginControllerTest extends TestCase
|
||||
{
|
||||
|
||||
/** @test */
|
||||
public function it_loads_the_login_page()
|
||||
{
|
||||
$response = $this->get(route('login.show'));
|
||||
|
||||
$this->assertSame(200, $response->getStatusCode());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_redirect_to_login_with_no_data()
|
||||
{
|
||||
$response = $this->post(route('login.show'));
|
||||
|
||||
$this->assertSame(302, $response->getStatusCode());
|
||||
$this->assertSame(route('login.show'), $response->getHeaderLine('Location'));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_login_with_correct_data()
|
||||
{
|
||||
$this->database()->query("INSERT INTO `users` (`email`, `username`, `password`, `is_admin`, `user_code`) VALUES ('admin@example.com', 'admin', ?, 1, ?)", [password_hash('admin', PASSWORD_DEFAULT), humanRandomString(5)]);
|
||||
|
||||
$response = $this->get(route('login.show'));
|
||||
$form = $this->getCrawler($response)
|
||||
->selectButton('Login')
|
||||
->form([
|
||||
'username' => 'admin@example.com',
|
||||
'password' => 'admin',
|
||||
], 'POST');
|
||||
|
||||
$response = $this->submitForm($form);
|
||||
$this->assertSame(302, $response->getStatusCode());
|
||||
$this->assertSame(route('home'), $response->getHeaderLine('Location'));
|
||||
|
||||
$response = $this->get(route('home'));
|
||||
$this->assertSame(200, $response->getStatusCode());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_hide_register_by_default()
|
||||
{
|
||||
$response = $this->get(route('login.show'));
|
||||
$this->assertSame(200, $response->getStatusCode());
|
||||
$this->assertStringNotContainsString('Register', $this->getCrawler($response)->text());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_show_register_when_enabled()
|
||||
{
|
||||
$this->database()->query("INSERT INTO `settings`(`key`, `value`) VALUES ('register_enabled', 'on')");
|
||||
|
||||
$response = $this->get(route('login.show'));
|
||||
$this->assertSame(200, $response->getStatusCode());
|
||||
$this->assertStringContainsString('Register', $this->getCrawler($response)->text());
|
||||
}
|
||||
}
|
|
@ -14,4 +14,19 @@ abstract class TestCase extends BaseTestCase
|
|||
$_SESSION = []; // ugly workaround to the the session superglobal between tests
|
||||
$this->createApplication();
|
||||
}
|
||||
|
||||
public function updateSetting($key, $value = null)
|
||||
{
|
||||
if (!$this->database()->query('SELECT `value` FROM `settings` WHERE `key` = '.$this->database()->getPdo()->quote($key))->fetch()) {
|
||||
$this->database()->query('INSERT INTO `settings`(`key`, `value`) VALUES ('.$this->database()->getPdo()->quote($key).', ?)', $value);
|
||||
} else {
|
||||
$this->database()->query('UPDATE `settings` SET `value`=? WHERE `key` = '.$this->database()->getPdo()->quote($key), $value);
|
||||
}
|
||||
}
|
||||
|
||||
public function createAdminUser($email = 'admin@example.com', $username = 'admin', $password = 'admin')
|
||||
{
|
||||
$this->database()->query("INSERT INTO `users` (`email`, `username`, `password`, `is_admin`, `user_code`) VALUES (?, ?, ?, 1, ?)", [$email, $username, password_hash($password, PASSWORD_DEFAULT), humanRandomString(5)]);
|
||||
return $this->database()->getPdo()->lastInsertId();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
|
||||
return [
|
||||
'base_path' => 'http://localhost',
|
||||
'base_url' => 'http://localhost',
|
||||
'debug' => true,
|
||||
'db' =>
|
||||
[
|
||||
|
|
Loading…
Add table
Reference in a new issue