2018-10-25 18:53:05 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Engelsystem\Test\Unit\Controllers;
|
|
|
|
|
2020-03-01 18:21:16 +01:00
|
|
|
use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
|
2020-03-01 03:22:52 +01:00
|
|
|
use Engelsystem\Config\Config;
|
2018-10-25 18:53:05 +02:00
|
|
|
use Engelsystem\Controllers\AuthController;
|
2018-11-27 12:01:36 +01:00
|
|
|
use Engelsystem\Helpers\Authenticator;
|
2019-07-09 22:02:07 +02:00
|
|
|
use Engelsystem\Http\Exceptions\ValidationException;
|
2020-04-07 15:08:49 +02:00
|
|
|
use Engelsystem\Http\Redirector;
|
2018-11-27 12:01:36 +01:00
|
|
|
use Engelsystem\Http\Request;
|
2018-10-25 18:53:05 +02:00
|
|
|
use Engelsystem\Http\Response;
|
2019-07-09 22:02:07 +02:00
|
|
|
use Engelsystem\Http\Validation\Validator;
|
2018-11-27 12:01:36 +01:00
|
|
|
use Engelsystem\Models\User\Settings;
|
|
|
|
use Engelsystem\Models\User\User;
|
|
|
|
use Engelsystem\Test\Unit\HasDatabase;
|
2019-10-08 13:57:50 +02:00
|
|
|
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
|
|
|
|
{
|
2020-03-01 18:21:16 +01:00
|
|
|
use ArraySubsetAsserts;
|
2018-11-27 12:01:36 +01:00
|
|
|
use HasDatabase;
|
|
|
|
|
2018-10-25 18:53:05 +02:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\AuthController::__construct
|
2018-11-27 12:01:36 +01:00
|
|
|
* @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
|
|
|
*/
|
2018-11-27 12:01:36 +01: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 */
|
2020-04-07 15:08:49 +02:00
|
|
|
/** @var Redirector|MockObject $redirect */
|
2020-03-01 03:22:52 +01:00
|
|
|
/** @var Config $config */
|
2018-11-27 12:01:36 +01:00
|
|
|
/** @var Authenticator|MockObject $auth */
|
2020-04-07 15:08:49 +02:00
|
|
|
list(, $session, $redirect, $config, $auth) = $this->getMocks();
|
2018-10-25 18:53:05 +02:00
|
|
|
|
2020-03-01 18:21:16 +01:00
|
|
|
$session->expects($this->atLeastOnce())
|
2019-07-09 22:02:07 +02:00
|
|
|
->method('get')
|
2020-03-01 18:21:16 +01:00
|
|
|
->willReturnCallback(function ($type) {
|
|
|
|
return $type == 'errors' ? ['foo' => 'bar'] : [];
|
|
|
|
});
|
2018-11-27 12:01:36 +01:00
|
|
|
$response->expects($this->once())
|
|
|
|
->method('withView')
|
|
|
|
->with('pages/login')
|
|
|
|
->willReturn($response);
|
|
|
|
|
2020-04-07 15:08:49 +02:00
|
|
|
$controller = new AuthController($response, $session, $redirect, $config, $auth);
|
2018-11-27 12:01:36 +01:00
|
|
|
$controller->login();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\AuthController::postLogin
|
|
|
|
*/
|
|
|
|
public function testPostLogin()
|
|
|
|
{
|
|
|
|
$this->initDatabase();
|
2018-10-25 18:53:05 +02:00
|
|
|
|
2018-11-27 12:01:36 +01:00
|
|
|
$request = new Request();
|
|
|
|
/** @var Response|MockObject $response */
|
|
|
|
$response = $this->createMock(Response::class);
|
2020-04-07 15:08:49 +02:00
|
|
|
/** @var Redirector|MockObject $redirect */
|
2020-03-01 03:22:52 +01:00
|
|
|
/** @var Config $config */
|
2018-11-27 12:01:36 +01:00
|
|
|
/** @var Authenticator|MockObject $auth */
|
2020-04-07 15:08:49 +02:00
|
|
|
list(, , $redirect, $config, $auth) = $this->getMocks();
|
2019-07-09 22:02:07 +02:00
|
|
|
$session = new Session(new MockArraySessionStorage());
|
|
|
|
/** @var Validator|MockObject $validator */
|
2019-07-10 13:34:15 +02:00
|
|
|
$validator = new Validator();
|
2019-10-08 14:18:51 +02:00
|
|
|
$session->set('errors', [['bar' => 'some.bar.error']]);
|
2020-03-01 18:21:16 +01:00
|
|
|
$this->app->instance('session', $session);
|
2018-11-27 12:01:36 +01:00
|
|
|
|
|
|
|
$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]);
|
2018-11-27 12:01:36 +01:00
|
|
|
$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())
|
2018-11-27 12:01:36 +01:00
|
|
|
->method('withView')
|
2020-03-01 18:21:16 +01:00
|
|
|
->willReturnCallback(function ($view, $data = []) use ($response) {
|
|
|
|
$this->assertEquals('pages/login', $view);
|
|
|
|
$this->assertArraySubset(['errors' => collect(['some.bar.error', 'auth.not-found'])], $data);
|
|
|
|
return $response;
|
|
|
|
});
|
2020-04-07 15:08:49 +02:00
|
|
|
$redirect->expects($this->once())
|
|
|
|
->method('to')
|
2018-11-27 12:01:36 +01:00
|
|
|
->with('news')
|
|
|
|
->willReturn($response);
|
|
|
|
|
2019-07-09 22:02:07 +02:00
|
|
|
// No credentials
|
2020-04-07 15:08:49 +02:00
|
|
|
$controller = new AuthController($response, $session, $redirect, $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) {
|
|
|
|
}
|
2018-11-27 12:01:36 +01:00
|
|
|
|
|
|
|
// No user found
|
2019-07-09 22:02:07 +02:00
|
|
|
$request = new Request([], ['login' => 'foo', 'password' => 'bar']);
|
2018-11-27 12:01:36 +01:00
|
|
|
$controller->postLogin($request);
|
2019-07-09 22:02:07 +02:00
|
|
|
$this->assertEquals([], $session->all());
|
|
|
|
|
2018-11-27 12:01:36 +01:00
|
|
|
// 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());
|
2018-11-27 12:01:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\AuthController::logout
|
|
|
|
*/
|
|
|
|
public function testLogout()
|
|
|
|
{
|
|
|
|
/** @var Response $response */
|
|
|
|
/** @var SessionInterface|MockObject $session */
|
2020-04-07 15:08:49 +02:00
|
|
|
/** @var Redirector|MockObject $redirect */
|
2020-03-01 03:22:52 +01:00
|
|
|
/** @var Config $config */
|
2018-11-27 12:01:36 +01:00
|
|
|
/** @var Authenticator|MockObject $auth */
|
2020-04-07 15:08:49 +02:00
|
|
|
list($response, $session, $redirect, $config, $auth) = $this->getMocks();
|
2018-11-27 12:01:36 +01:00
|
|
|
|
|
|
|
$session->expects($this->once())
|
|
|
|
->method('invalidate');
|
2018-10-25 18:53:05 +02:00
|
|
|
|
2020-04-07 15:08:49 +02:00
|
|
|
$redirect->expects($this->once())
|
2018-10-25 18:53:05 +02:00
|
|
|
->method('to')
|
|
|
|
->with('/')
|
2020-04-07 15:08:49 +02:00
|
|
|
->willReturn($response);
|
2018-10-25 18:53:05 +02:00
|
|
|
|
2020-04-07 15:08:49 +02:00
|
|
|
$controller = new AuthController($response, $session, $redirect, $config, $auth);
|
2018-11-27 12:01:36 +01:00
|
|
|
$return = $controller->logout();
|
|
|
|
|
2020-04-07 15:08:49 +02:00
|
|
|
$this->assertEquals($response, $return);
|
2018-11-27 12:01:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function getMocks()
|
|
|
|
{
|
|
|
|
$response = new Response();
|
|
|
|
/** @var SessionInterface|MockObject $session */
|
|
|
|
$session = $this->getMockForAbstractClass(SessionInterface::class);
|
2020-04-07 15:08:49 +02:00
|
|
|
/** @var Redirector|MockObject $redirect */
|
|
|
|
$redirect = $this->createMock(Redirector::class);
|
2020-03-01 03:22:52 +01:00
|
|
|
$config = new Config(['home_site' => 'news']);
|
2018-11-27 12:01:36 +01:00
|
|
|
/** @var Authenticator|MockObject $auth */
|
|
|
|
$auth = $this->createMock(Authenticator::class);
|
|
|
|
|
2020-03-01 18:21:16 +01:00
|
|
|
$this->app->instance('session', $session);
|
|
|
|
|
2020-04-07 15:08:49 +02:00
|
|
|
return [$response, $session, $redirect, $config, $auth];
|
2018-10-25 18:53:05 +02:00
|
|
|
}
|
|
|
|
}
|