engelsystem/tests/Unit/Controllers/AuthControllerTest.php

174 lines
5.9 KiB
PHP
Raw Normal View History

2018-10-25 18:53:05 +02:00
<?php
namespace Engelsystem\Test\Unit\Controllers;
use Engelsystem\Config\Config;
2018-10-25 18:53:05 +02:00
use Engelsystem\Controllers\AuthController;
use Engelsystem\Helpers\Authenticator;
2019-07-09 22:02:07 +02:00
use Engelsystem\Http\Exceptions\ValidationException;
use Engelsystem\Http\Request;
2018-10-25 18:53:05 +02:00
use Engelsystem\Http\Response;
use Engelsystem\Http\UrlGeneratorInterface;
2019-07-09 22:02:07 +02:00
use Engelsystem\Http\Validation\Validator;
use Engelsystem\Models\User\Settings;
use Engelsystem\Models\User\User;
use Engelsystem\Test\Unit\HasDatabase;
use Engelsystem\Test\Unit\TestCase;
2018-10-25 18:53:05 +02:00
use PHPUnit\Framework\MockObject\MockObject;
2019-07-09 22:02:07 +02:00
use Symfony\Component\HttpFoundation\Session\Session;
2018-10-25 18:53:05 +02:00
use Symfony\Component\HttpFoundation\Session\SessionInterface;
2019-07-09 22:02:07 +02:00
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
2018-10-25 18:53:05 +02:00
class AuthControllerTest extends TestCase
{
use HasDatabase;
2018-10-25 18:53:05 +02:00
/**
* @covers \Engelsystem\Controllers\AuthController::__construct
* @covers \Engelsystem\Controllers\AuthController::login
2019-07-09 22:02:07 +02:00
* @covers \Engelsystem\Controllers\AuthController::showLogin
2018-10-25 18:53:05 +02:00
*/
public function testLogin()
2018-10-25 18:53:05 +02:00
{
/** @var Response|MockObject $response */
$response = $this->createMock(Response::class);
/** @var SessionInterface|MockObject $session */
/** @var UrlGeneratorInterface|MockObject $url */
/** @var Config $config */
/** @var Authenticator|MockObject $auth */
list(, $session, $url, $config, $auth) = $this->getMocks();
2018-10-25 18:53:05 +02:00
2019-07-09 22:02:07 +02:00
$session->expects($this->once())
->method('get')
->with('errors', [])
->willReturn(['foo' => 'bar']);
$response->expects($this->once())
->method('withView')
->with('pages/login')
->willReturn($response);
$controller = new AuthController($response, $session, $url, $config, $auth);
$controller->login();
}
/**
* @covers \Engelsystem\Controllers\AuthController::postLogin
*/
public function testPostLogin()
{
$this->initDatabase();
2018-10-25 18:53:05 +02:00
$request = new Request();
/** @var Response|MockObject $response */
$response = $this->createMock(Response::class);
/** @var UrlGeneratorInterface|MockObject $url */
/** @var Config $config */
/** @var Authenticator|MockObject $auth */
list(, , $url, $config, $auth) = $this->getMocks();
2019-07-09 22:02:07 +02:00
$session = new Session(new MockArraySessionStorage());
/** @var Validator|MockObject $validator */
$validator = new Validator();
$session->set('errors', [['bar' => 'some.bar.error']]);
$user = new User([
'name' => 'foo',
'password' => '',
'email' => '',
'api_key' => '',
'last_login_at' => null,
]);
2019-07-09 22:02:07 +02:00
$user->forceFill(['id' => 42]);
$user->save();
$settings = new Settings(['language' => 'de_DE', 'theme' => '']);
$settings->user()
->associate($user)
->save();
$auth->expects($this->exactly(2))
->method('authenticate')
->with('foo', 'bar')
->willReturnOnConsecutiveCalls(null, $user);
2019-07-09 22:02:07 +02:00
$response->expects($this->once())
->method('withView')
->with('pages/login', ['errors' => collect(['some.bar.error', 'auth.not-found'])])
->willReturn($response);
2018-10-25 18:53:05 +02:00
$response->expects($this->once())
->method('redirectTo')
->with('news')
->willReturn($response);
2019-07-09 22:02:07 +02:00
// No credentials
$controller = new AuthController($response, $session, $url, $config, $auth);
2019-07-09 22:02:07 +02:00
$controller->setValidator($validator);
try {
$controller->postLogin($request);
$this->fail('Login without credentials possible');
} catch (ValidationException $e) {
}
// Missing password
$request = new Request([], ['login' => 'foo']);
try {
$controller->postLogin($request);
$this->fail('Login without password possible');
} catch (ValidationException $e) {
}
// No user found
2019-07-09 22:02:07 +02:00
$request = new Request([], ['login' => 'foo', 'password' => 'bar']);
$controller->postLogin($request);
2019-07-09 22:02:07 +02:00
$this->assertEquals([], $session->all());
// Authenticated user
$controller->postLogin($request);
$this->assertNotNull($user->last_login_at);
2019-07-09 22:02:07 +02:00
$this->assertEquals(['user_id' => 42, 'locale' => 'de_DE'], $session->all());
}
/**
* @covers \Engelsystem\Controllers\AuthController::logout
*/
public function testLogout()
{
/** @var Response $response */
/** @var SessionInterface|MockObject $session */
/** @var UrlGeneratorInterface|MockObject $url */
/** @var Config $config */
/** @var Authenticator|MockObject $auth */
list($response, $session, $url, $config, $auth) = $this->getMocks();
$session->expects($this->once())
->method('invalidate');
2018-10-25 18:53:05 +02:00
$url->expects($this->once())
->method('to')
->with('/')
->willReturn('https://foo.bar/');
$controller = new AuthController($response, $session, $url, $config, $auth);
$return = $controller->logout();
$this->assertEquals(['https://foo.bar/'], $return->getHeader('location'));
}
/**
* @return array
*/
protected function getMocks()
{
$response = new Response();
/** @var SessionInterface|MockObject $session */
$session = $this->getMockForAbstractClass(SessionInterface::class);
/** @var UrlGeneratorInterface|MockObject $url */
$url = $this->getMockForAbstractClass(UrlGeneratorInterface::class);
$config = new Config(['home_site' => 'news']);
/** @var Authenticator|MockObject $auth */
$auth = $this->createMock(Authenticator::class);
return [$response, $session, $url, $config, $auth];
2018-10-25 18:53:05 +02:00
}
}