2018-10-08 19:30:37 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Engelsystem\Test\Unit\Helpers;
|
|
|
|
|
|
|
|
use Engelsystem\Helpers\Authenticator;
|
|
|
|
use Engelsystem\Models\User\User;
|
2018-11-27 12:01:36 +01:00
|
|
|
use Engelsystem\Test\Unit\HasDatabase;
|
2018-10-08 19:30:37 +02:00
|
|
|
use Engelsystem\Test\Unit\Helpers\Stub\UserModelImplementation;
|
|
|
|
use Engelsystem\Test\Unit\ServiceProviderTest;
|
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2018-10-11 01:26:34 +02:00
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
2018-10-08 19:30:37 +02:00
|
|
|
use Symfony\Component\HttpFoundation\Session\Session;
|
|
|
|
|
|
|
|
class AuthenticatorTest extends ServiceProviderTest
|
|
|
|
{
|
2018-11-27 12:01:36 +01:00
|
|
|
use HasDatabase;
|
|
|
|
|
2018-10-08 19:30:37 +02:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Helpers\Authenticator::__construct(
|
|
|
|
* @covers \Engelsystem\Helpers\Authenticator::user
|
|
|
|
*/
|
|
|
|
public function testUser()
|
|
|
|
{
|
2018-10-11 01:26:34 +02:00
|
|
|
/** @var ServerRequestInterface|MockObject $request */
|
|
|
|
$request = $this->getMockForAbstractClass(ServerRequestInterface::class);
|
2018-10-08 19:30:37 +02:00
|
|
|
/** @var Session|MockObject $session */
|
|
|
|
$session = $this->createMock(Session::class);
|
|
|
|
/** @var UserModelImplementation|MockObject $userRepository */
|
|
|
|
$userRepository = new UserModelImplementation();
|
|
|
|
/** @var User|MockObject $user */
|
|
|
|
$user = $this->createMock(User::class);
|
|
|
|
|
|
|
|
$session->expects($this->exactly(3))
|
|
|
|
->method('get')
|
2018-11-27 12:01:36 +01:00
|
|
|
->with('user_id')
|
2018-10-08 19:30:37 +02:00
|
|
|
->willReturnOnConsecutiveCalls(
|
|
|
|
null,
|
|
|
|
42,
|
|
|
|
1337
|
|
|
|
);
|
|
|
|
|
2018-10-11 01:26:34 +02:00
|
|
|
$auth = new Authenticator($request, $session, $userRepository);
|
2018-10-08 19:30:37 +02:00
|
|
|
|
|
|
|
// Not in session
|
2019-04-24 10:45:00 +02:00
|
|
|
$this->assertNull($auth->user());
|
2018-10-08 19:30:37 +02:00
|
|
|
|
|
|
|
// Unknown user
|
|
|
|
UserModelImplementation::$id = 42;
|
2019-04-24 10:45:00 +02:00
|
|
|
$this->assertNull($auth->user());
|
2018-10-08 19:30:37 +02:00
|
|
|
|
|
|
|
// User found
|
|
|
|
UserModelImplementation::$id = 1337;
|
|
|
|
UserModelImplementation::$user = $user;
|
|
|
|
$this->assertEquals($user, $auth->user());
|
|
|
|
|
|
|
|
// User cached
|
|
|
|
UserModelImplementation::$id = null;
|
|
|
|
UserModelImplementation::$user = null;
|
|
|
|
$this->assertEquals($user, $auth->user());
|
|
|
|
}
|
2018-10-11 01:26:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Helpers\Authenticator::apiUser
|
|
|
|
*/
|
|
|
|
public function testApiUser()
|
|
|
|
{
|
|
|
|
/** @var ServerRequestInterface|MockObject $request */
|
|
|
|
$request = $this->getMockForAbstractClass(ServerRequestInterface::class);
|
|
|
|
/** @var Session|MockObject $session */
|
|
|
|
$session = $this->createMock(Session::class);
|
|
|
|
/** @var UserModelImplementation|MockObject $userRepository */
|
|
|
|
$userRepository = new UserModelImplementation();
|
|
|
|
/** @var User|MockObject $user */
|
|
|
|
$user = $this->createMock(User::class);
|
|
|
|
|
|
|
|
$request->expects($this->exactly(3))
|
|
|
|
->method('getQueryParams')
|
|
|
|
->with()
|
|
|
|
->willReturnOnConsecutiveCalls(
|
|
|
|
[],
|
|
|
|
['api_key' => 'iMaNot3xiSt1nGAp1Key!'],
|
|
|
|
['foo_key' => 'SomeSecretApiKey']
|
|
|
|
);
|
|
|
|
|
|
|
|
/** @var Authenticator|MockObject $auth */
|
|
|
|
$auth = new Authenticator($request, $session, $userRepository);
|
|
|
|
|
|
|
|
// No key
|
2019-04-24 10:45:00 +02:00
|
|
|
$this->assertNull($auth->apiUser());
|
2018-10-11 01:26:34 +02:00
|
|
|
|
|
|
|
// Unknown user
|
|
|
|
UserModelImplementation::$apiKey = 'iMaNot3xiSt1nGAp1Key!';
|
2019-04-24 10:45:00 +02:00
|
|
|
$this->assertNull($auth->apiUser());
|
2018-10-11 01:26:34 +02:00
|
|
|
|
|
|
|
// User found
|
|
|
|
UserModelImplementation::$apiKey = 'SomeSecretApiKey';
|
|
|
|
UserModelImplementation::$user = $user;
|
|
|
|
$this->assertEquals($user, $auth->apiUser('foo_key'));
|
|
|
|
|
|
|
|
// User cached
|
|
|
|
UserModelImplementation::$apiKey = null;
|
|
|
|
UserModelImplementation::$user = null;
|
|
|
|
$this->assertEquals($user, $auth->apiUser());
|
|
|
|
}
|
2018-11-12 14:41:23 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Helpers\Authenticator::can
|
|
|
|
*/
|
|
|
|
public function testCan()
|
|
|
|
{
|
|
|
|
/** @var ServerRequestInterface|MockObject $request */
|
|
|
|
$request = $this->getMockForAbstractClass(ServerRequestInterface::class);
|
|
|
|
/** @var Session|MockObject $session */
|
|
|
|
$session = $this->createMock(Session::class);
|
|
|
|
/** @var UserModelImplementation|MockObject $userRepository */
|
|
|
|
$userRepository = new UserModelImplementation();
|
|
|
|
/** @var User|MockObject $user */
|
|
|
|
$user = $this->createMock(User::class);
|
|
|
|
|
2018-11-27 12:01:36 +01:00
|
|
|
$session->expects($this->once())
|
2018-11-12 14:41:23 +01:00
|
|
|
->method('get')
|
2018-11-27 12:01:36 +01:00
|
|
|
->with('user_id')
|
2018-11-12 14:41:23 +01:00
|
|
|
->willReturn(42);
|
|
|
|
$session->expects($this->once())
|
|
|
|
->method('remove')
|
2018-11-27 12:01:36 +01:00
|
|
|
->with('user_id');
|
2018-11-12 14:41:23 +01:00
|
|
|
|
|
|
|
/** @var Authenticator|MockObject $auth */
|
|
|
|
$auth = $this->getMockBuilder(Authenticator::class)
|
|
|
|
->setConstructorArgs([$request, $session, $userRepository])
|
|
|
|
->setMethods(['getPermissionsByGroup', 'getPermissionsByUser', 'user'])
|
|
|
|
->getMock();
|
|
|
|
$auth->expects($this->exactly(1))
|
|
|
|
->method('getPermissionsByGroup')
|
|
|
|
->with(-10)
|
|
|
|
->willReturn([]);
|
|
|
|
$auth->expects($this->exactly(1))
|
|
|
|
->method('getPermissionsByUser')
|
|
|
|
->with($user)
|
|
|
|
->willReturn(['bar']);
|
|
|
|
$auth->expects($this->exactly(2))
|
|
|
|
->method('user')
|
|
|
|
->willReturnOnConsecutiveCalls(null, $user);
|
|
|
|
|
|
|
|
// No user, no permissions
|
|
|
|
$this->assertFalse($auth->can('foo'));
|
|
|
|
|
|
|
|
// User exists, has permissions
|
|
|
|
$this->assertTrue($auth->can('bar'));
|
|
|
|
|
|
|
|
// Permissions cached
|
|
|
|
$this->assertTrue($auth->can('bar'));
|
|
|
|
}
|
2018-11-27 12:01:36 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Helpers\Authenticator::authenticate
|
|
|
|
*/
|
|
|
|
public function testAuthenticate()
|
|
|
|
{
|
|
|
|
$this->initDatabase();
|
|
|
|
|
|
|
|
/** @var ServerRequestInterface|MockObject $request */
|
|
|
|
$request = $this->getMockForAbstractClass(ServerRequestInterface::class);
|
|
|
|
/** @var Session|MockObject $session */
|
|
|
|
$session = $this->createMock(Session::class);
|
|
|
|
$userRepository = new User();
|
|
|
|
|
|
|
|
(new User([
|
|
|
|
'name' => 'lorem',
|
|
|
|
'password' => password_hash('testing', PASSWORD_DEFAULT),
|
|
|
|
'email' => 'lorem@foo.bar',
|
|
|
|
'api_key' => '',
|
|
|
|
]))->save();
|
|
|
|
(new User([
|
|
|
|
'name' => 'ipsum',
|
|
|
|
'password' => '',
|
|
|
|
'email' => 'ipsum@foo.bar',
|
|
|
|
'api_key' => '',
|
|
|
|
]))->save();
|
|
|
|
|
|
|
|
$auth = new Authenticator($request, $session, $userRepository);
|
|
|
|
$this->assertNull($auth->authenticate('not-existing', 'foo'));
|
|
|
|
$this->assertNull($auth->authenticate('ipsum', 'wrong-password'));
|
|
|
|
$this->assertInstanceOf(User::class, $auth->authenticate('lorem', 'testing'));
|
|
|
|
$this->assertInstanceOf(User::class, $auth->authenticate('lorem@foo.bar', 'testing'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Helpers\Authenticator::verifyPassword
|
|
|
|
*/
|
|
|
|
public function testVerifyPassword()
|
|
|
|
{
|
|
|
|
$this->initDatabase();
|
|
|
|
$password = password_hash('testing', PASSWORD_ARGON2I);
|
|
|
|
$user = new User([
|
|
|
|
'name' => 'lorem',
|
|
|
|
'password' => $password,
|
|
|
|
'email' => 'lorem@foo.bar',
|
|
|
|
'api_key' => '',
|
|
|
|
]);
|
|
|
|
$user->save();
|
|
|
|
|
|
|
|
/** @var Authenticator|MockObject $auth */
|
|
|
|
$auth = $this->getMockBuilder(Authenticator::class)
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->setMethods(['setPassword'])
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$auth->expects($this->once())
|
|
|
|
->method('setPassword')
|
|
|
|
->with($user, 'testing');
|
|
|
|
$auth->setPasswordAlgorithm(PASSWORD_BCRYPT);
|
|
|
|
|
|
|
|
$this->assertFalse($auth->verifyPassword($user, 'randomStuff'));
|
|
|
|
$this->assertTrue($auth->verifyPassword($user, 'testing'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Helpers\Authenticator::setPassword
|
|
|
|
*/
|
|
|
|
public function testSetPassword()
|
|
|
|
{
|
|
|
|
$this->initDatabase();
|
|
|
|
$user = new User([
|
|
|
|
'name' => 'ipsum',
|
|
|
|
'password' => '',
|
|
|
|
'email' => 'ipsum@foo.bar',
|
|
|
|
'api_key' => '',
|
|
|
|
]);
|
|
|
|
$user->save();
|
|
|
|
|
|
|
|
$auth = $this->getAuthenticator();
|
|
|
|
$auth->setPasswordAlgorithm(PASSWORD_ARGON2I);
|
|
|
|
|
|
|
|
$auth->setPassword($user, 'FooBar');
|
|
|
|
$this->assertTrue($user->isClean());
|
|
|
|
|
|
|
|
$this->assertTrue(password_verify('FooBar', $user->password));
|
|
|
|
$this->assertFalse(password_needs_rehash($user->password, PASSWORD_ARGON2I));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Helpers\Authenticator::setPasswordAlgorithm
|
|
|
|
* @covers \Engelsystem\Helpers\Authenticator::getPasswordAlgorithm
|
|
|
|
*/
|
|
|
|
public function testPasswordAlgorithm()
|
|
|
|
{
|
|
|
|
$auth = $this->getAuthenticator();
|
|
|
|
|
|
|
|
$auth->setPasswordAlgorithm(PASSWORD_ARGON2I);
|
|
|
|
$this->assertEquals(PASSWORD_ARGON2I, $auth->getPasswordAlgorithm());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Authenticator
|
|
|
|
*/
|
|
|
|
protected function getAuthenticator()
|
|
|
|
{
|
|
|
|
return new class extends Authenticator
|
|
|
|
{
|
|
|
|
/** @noinspection PhpMissingParentConstructorInspection */
|
|
|
|
public function __construct() { }
|
|
|
|
};
|
|
|
|
}
|
2018-10-08 19:30:37 +02:00
|
|
|
}
|