2014-08-30 18:11:32 +02:00
|
|
|
<?php
|
|
|
|
namespace Szurubooru\Tests\Services;
|
|
|
|
|
2014-09-01 20:51:59 +02:00
|
|
|
class AuthServiceTest extends \Szurubooru\Tests\AbstractTestCase
|
2014-08-30 18:11:32 +02:00
|
|
|
{
|
2014-09-08 13:06:32 +02:00
|
|
|
private $configMock;
|
2014-09-01 20:51:59 +02:00
|
|
|
private $passwordServiceMock;
|
|
|
|
private $timeServiceMock;
|
2014-09-08 08:20:31 +02:00
|
|
|
private $tokenServiceMock;
|
|
|
|
private $userServiceMock;
|
2014-09-01 20:51:59 +02:00
|
|
|
|
|
|
|
public function setUp()
|
2014-08-30 18:11:32 +02:00
|
|
|
{
|
2014-09-10 17:42:28 +02:00
|
|
|
parent::setUp();
|
2014-09-08 13:06:32 +02:00
|
|
|
$this->configMock = $this->mockConfig();
|
2014-09-01 20:51:59 +02:00
|
|
|
$this->passwordServiceMock = $this->mock(\Szurubooru\Services\PasswordService::class);
|
|
|
|
$this->timeServiceMock = $this->mock(\Szurubooru\Services\TimeService::class);
|
2014-09-08 08:20:31 +02:00
|
|
|
$this->tokenServiceMock = $this->mock(\Szurubooru\Services\TokenService::class);
|
|
|
|
$this->userServiceMock = $this->mock(\Szurubooru\Services\UserService::class);
|
2014-08-30 18:11:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testInvalidPassword()
|
|
|
|
{
|
2014-09-08 13:06:32 +02:00
|
|
|
$this->configMock->set('security/needEmailActivationToRegister', false);
|
2014-09-01 20:51:59 +02:00
|
|
|
$this->passwordServiceMock->method('getHash')->willReturn('unmatchingHash');
|
2014-08-30 18:11:32 +02:00
|
|
|
|
|
|
|
$testUser = new \Szurubooru\Entities\User();
|
|
|
|
$testUser->name = 'dummy';
|
|
|
|
$testUser->passwordHash = 'hash';
|
2014-09-08 13:06:32 +02:00
|
|
|
$this->userServiceMock->expects($this->once())->method('getByNameOrEmail')->willReturn($testUser);
|
2014-08-30 18:11:32 +02:00
|
|
|
|
2014-09-08 08:20:31 +02:00
|
|
|
$this->setExpectedException(\Exception::class, 'Specified password is invalid');
|
2014-09-09 19:38:16 +02:00
|
|
|
$authService = $this->getAuthService();
|
|
|
|
$formData = new \Szurubooru\FormData\LoginFormData();
|
|
|
|
$formData->userNameOrEmail = 'dummy';
|
|
|
|
$formData->password = 'godzilla';
|
|
|
|
$authService->loginFromCredentials($formData);
|
2014-08-30 18:11:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidCredentials()
|
|
|
|
{
|
2014-09-08 13:06:32 +02:00
|
|
|
$this->configMock->set('security/needEmailActivationToRegister', false);
|
2014-09-01 20:51:59 +02:00
|
|
|
$this->passwordServiceMock->method('getHash')->willReturn('hash');
|
2014-08-30 18:11:32 +02:00
|
|
|
|
|
|
|
$testUser = new \Szurubooru\Entities\User();
|
2014-09-08 13:06:32 +02:00
|
|
|
$testUser->id = 'an unusual database identifier';
|
2014-08-30 18:11:32 +02:00
|
|
|
$testUser->name = 'dummy';
|
|
|
|
$testUser->passwordHash = 'hash';
|
2014-09-08 13:06:32 +02:00
|
|
|
$this->userServiceMock->expects($this->once())->method('getByNameOrEmail')->willReturn($testUser);
|
2014-09-08 08:20:31 +02:00
|
|
|
|
|
|
|
$testToken = new \Szurubooru\Entities\Token();
|
|
|
|
$testToken->name = 'mummy';
|
|
|
|
$this->tokenServiceMock->expects($this->once())->method('createAndSaveToken')->with(
|
2014-09-08 13:06:32 +02:00
|
|
|
$testUser->id,
|
2014-09-08 08:20:31 +02:00
|
|
|
\Szurubooru\Entities\Token::PURPOSE_LOGIN)->willReturn($testToken);
|
2014-08-30 18:11:32 +02:00
|
|
|
|
2014-09-01 20:51:59 +02:00
|
|
|
$authService = $this->getAuthService();
|
2014-09-09 19:38:16 +02:00
|
|
|
$formData = new \Szurubooru\FormData\LoginFormData();
|
|
|
|
$formData->userNameOrEmail = 'dummy';
|
|
|
|
$formData->password = 'godzilla';
|
|
|
|
$authService->loginFromCredentials($formData);
|
2014-08-30 18:11:32 +02:00
|
|
|
|
|
|
|
$this->assertTrue($authService->isLoggedIn());
|
|
|
|
$this->assertEquals($testUser, $authService->getLoggedInUser());
|
2014-08-31 17:42:48 +02:00
|
|
|
$this->assertNotNull($authService->getLoginToken());
|
2014-09-08 08:20:31 +02:00
|
|
|
$this->assertEquals('mummy', $authService->getLoginToken()->name);
|
2014-08-31 17:42:48 +02:00
|
|
|
}
|
|
|
|
|
2014-09-08 13:06:32 +02:00
|
|
|
public function testValidCredentialsUnconfirmedEmail()
|
|
|
|
{
|
|
|
|
$this->configMock->set('security/needEmailActivationToRegister', true);
|
|
|
|
$this->passwordServiceMock->method('getHash')->willReturn('hash');
|
|
|
|
|
|
|
|
$testUser = new \Szurubooru\Entities\User();
|
|
|
|
$testUser->name = 'dummy';
|
|
|
|
$testUser->passwordHash = 'hash';
|
|
|
|
$this->userServiceMock->expects($this->once())->method('getByNameOrEmail')->willReturn($testUser);
|
|
|
|
|
|
|
|
$this->setExpectedException(\Exception::class, 'User didn\'t confirm mail yet');
|
|
|
|
$authService = $this->getAuthService();
|
2014-09-09 19:38:16 +02:00
|
|
|
$formData = new \Szurubooru\FormData\LoginFormData();
|
|
|
|
$formData->userNameOrEmail = 'dummy';
|
|
|
|
$formData->password = 'godzilla';
|
|
|
|
$authService->loginFromCredentials($formData);
|
2014-09-08 13:06:32 +02:00
|
|
|
|
|
|
|
$this->assertFalse($authService->isLoggedIn());
|
|
|
|
$this->assertNull($testUser, $authService->getLoggedInUser());
|
|
|
|
$this->assertNull($authService->getLoginToken());
|
|
|
|
}
|
|
|
|
|
2014-08-31 17:42:48 +02:00
|
|
|
public function testInvalidToken()
|
|
|
|
{
|
2014-09-08 13:06:32 +02:00
|
|
|
$this->configMock->set('security/needEmailActivationToRegister', false);
|
2014-08-31 17:42:48 +02:00
|
|
|
|
|
|
|
$this->setExpectedException(\Exception::class);
|
2014-09-01 20:51:59 +02:00
|
|
|
$authService = $this->getAuthService();
|
2014-09-09 19:38:16 +02:00
|
|
|
$testToken = new \Szurubooru\Entities\Token();
|
|
|
|
$authService->loginFromToken($testToken);
|
2014-08-30 18:11:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidToken()
|
|
|
|
{
|
2014-09-08 13:06:32 +02:00
|
|
|
$this->configMock->set('security/needEmailActivationToRegister', false);
|
2014-08-30 18:11:32 +02:00
|
|
|
$testUser = new \Szurubooru\Entities\User();
|
|
|
|
$testUser->id = 5;
|
|
|
|
$testUser->name = 'dummy';
|
2014-09-08 08:20:31 +02:00
|
|
|
$this->userServiceMock->expects($this->once())->method('getById')->willReturn($testUser);
|
2014-08-30 18:11:32 +02:00
|
|
|
|
|
|
|
$testToken = new \Szurubooru\Entities\Token();
|
|
|
|
$testToken->name = 'dummy_token';
|
|
|
|
$testToken->additionalData = $testUser->id;
|
2014-09-07 18:07:24 +02:00
|
|
|
$testToken->purpose = \Szurubooru\Entities\Token::PURPOSE_LOGIN;
|
2014-08-30 18:11:32 +02:00
|
|
|
|
2014-09-01 20:51:59 +02:00
|
|
|
$authService = $this->getAuthService();
|
2014-09-09 19:38:16 +02:00
|
|
|
$authService->loginFromToken($testToken);
|
2014-08-30 18:11:32 +02:00
|
|
|
|
|
|
|
$this->assertTrue($authService->isLoggedIn());
|
|
|
|
$this->assertEquals($testUser, $authService->getLoggedInUser());
|
2014-08-31 17:42:48 +02:00
|
|
|
$this->assertNotNull($authService->getLoginToken());
|
2014-09-08 08:20:31 +02:00
|
|
|
$this->assertEquals('dummy_token', $authService->getLoginToken()->name);
|
2014-08-30 18:11:32 +02:00
|
|
|
}
|
|
|
|
|
2014-09-08 13:06:32 +02:00
|
|
|
public function testValidTokenInvalidPurpose()
|
|
|
|
{
|
|
|
|
$this->configMock->set('security/needEmailActivationToRegister', false);
|
|
|
|
$testToken = new \Szurubooru\Entities\Token();
|
|
|
|
$testToken->name = 'dummy_token';
|
|
|
|
$testToken->additionalData = 'whatever';
|
|
|
|
$testToken->purpose = null;
|
|
|
|
|
|
|
|
$this->setExpectedException(\Exception::class, 'This token is not a login token');
|
|
|
|
$authService = $this->getAuthService();
|
2014-09-09 19:38:16 +02:00
|
|
|
$authService->loginFromToken($testToken);
|
2014-09-08 13:06:32 +02:00
|
|
|
|
|
|
|
$this->assertFalse($authService->isLoggedIn());
|
|
|
|
$this->assertNull($authService->getLoggedInUser());
|
|
|
|
$this->assertNull($authService->getLoginToken());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidTokenUnconfirmedEmail()
|
|
|
|
{
|
|
|
|
$this->configMock->set('security/needEmailActivationToRegister', true);
|
|
|
|
$testUser = new \Szurubooru\Entities\User();
|
|
|
|
$testUser->id = 5;
|
|
|
|
$testUser->name = 'dummy';
|
|
|
|
$this->userServiceMock->expects($this->once())->method('getById')->willReturn($testUser);
|
|
|
|
|
|
|
|
$testToken = new \Szurubooru\Entities\Token();
|
|
|
|
$testToken->name = 'dummy_token';
|
|
|
|
$testToken->additionalData = $testUser->id;
|
|
|
|
$testToken->purpose = \Szurubooru\Entities\Token::PURPOSE_LOGIN;
|
|
|
|
|
|
|
|
$this->setExpectedException(\Exception::class, 'User didn\'t confirm mail yet');
|
|
|
|
$authService = $this->getAuthService();
|
2014-09-09 19:38:16 +02:00
|
|
|
$authService->loginFromToken($testToken);
|
2014-09-08 13:06:32 +02:00
|
|
|
|
|
|
|
$this->assertFalse($authService->isLoggedIn());
|
|
|
|
$this->assertNull($testUser, $authService->getLoggedInUser());
|
|
|
|
$this->assertNull($authService->getLoginToken());
|
|
|
|
}
|
|
|
|
|
2014-09-01 20:51:59 +02:00
|
|
|
private function getAuthService()
|
2014-08-31 17:42:48 +02:00
|
|
|
{
|
2014-09-01 20:51:59 +02:00
|
|
|
return new \Szurubooru\Services\AuthService(
|
2014-09-08 13:06:32 +02:00
|
|
|
$this->configMock,
|
2014-09-01 20:51:59 +02:00
|
|
|
$this->passwordServiceMock,
|
|
|
|
$this->timeServiceMock,
|
2014-09-08 08:20:31 +02:00
|
|
|
$this->tokenServiceMock,
|
|
|
|
$this->userServiceMock);
|
2014-08-31 17:42:48 +02:00
|
|
|
}
|
2014-08-30 18:11:32 +02:00
|
|
|
}
|