szurubooru/tests/Services/AuthServiceTest.php

106 lines
3.4 KiB
PHP
Raw Normal View History

<?php
namespace Szurubooru\Tests\Services;
2014-09-01 20:51:59 +02:00
class AuthServiceTest extends \Szurubooru\Tests\AbstractTestCase
{
private $validatorMock;
2014-09-04 19:57:06 +02:00
private $configMock;
2014-09-01 20:51:59 +02:00
private $passwordServiceMock;
private $timeServiceMock;
private $tokenDaoMock;
private $userDaoMock;
public function setUp()
{
$this->validatorMock = $this->mock(\Szurubooru\Validator::class);
2014-09-04 19:57:06 +02:00
$this->configMock = $this->mock(\Szurubooru\Config::class);
2014-09-01 20:51:59 +02:00
$this->passwordServiceMock = $this->mock(\Szurubooru\Services\PasswordService::class);
$this->timeServiceMock = $this->mock(\Szurubooru\Services\TimeService::class);
$this->tokenDaoMock = $this->mock(\Szurubooru\Dao\TokenDao::class);
$this->userDaoMock = $this->mock(\Szurubooru\Dao\UserDao::class);
}
2014-09-01 20:51:59 +02:00
public function testInvalidUser()
{
$this->setExpectedException(\InvalidArgumentException::class, 'User not found');
2014-09-01 20:51:59 +02:00
$authService = $this->getAuthService();
$authService->loginFromCredentials('dummy', 'godzilla');
}
public function testInvalidPassword()
{
2014-09-01 20:51:59 +02:00
$this->passwordServiceMock->method('getHash')->willReturn('unmatchingHash');
$testUser = new \Szurubooru\Entities\User();
$testUser->name = 'dummy';
$testUser->passwordHash = 'hash';
2014-09-01 20:51:59 +02:00
$this->userDaoMock->expects($this->once())->method('getByName')->willReturn($testUser);
2014-09-01 20:51:59 +02:00
$authService = $this->getAuthService();
$this->setExpectedException(\InvalidArgumentException::class, 'Specified password is invalid');
$authService->loginFromCredentials('dummy', 'godzilla');
}
public function testValidCredentials()
{
2014-09-01 20:51:59 +02:00
$this->tokenDaoMock->expects($this->once())->method('save');
$this->passwordServiceMock->method('getHash')->willReturn('hash');
$testUser = new \Szurubooru\Entities\User();
$testUser->name = 'dummy';
$testUser->passwordHash = 'hash';
2014-09-01 20:51:59 +02:00
$this->userDaoMock->expects($this->once())->method('getByName')->willReturn($testUser);
2014-09-01 20:51:59 +02:00
$authService = $this->getAuthService();
$authService->loginFromCredentials('dummy', 'godzilla');
$this->assertTrue($authService->isLoggedIn());
$this->assertEquals($testUser, $authService->getLoggedInUser());
2014-08-31 17:42:48 +02:00
$this->assertNotNull($authService->getLoginToken());
$this->assertNotNull($authService->getLoginToken()->name);
}
public function testInvalidToken()
{
2014-09-01 20:51:59 +02:00
$this->tokenDaoMock->expects($this->once())->method('getByName')->willReturn(null);
2014-08-31 17:42:48 +02:00
$this->setExpectedException(\Exception::class);
2014-09-01 20:51:59 +02:00
$authService = $this->getAuthService();
2014-08-31 17:42:48 +02:00
$authService->loginFromToken('');
}
public function testValidToken()
{
$testUser = new \Szurubooru\Entities\User();
$testUser->id = 5;
$testUser->name = 'dummy';
$testUser->passwordHash = 'hash';
2014-09-01 20:51:59 +02:00
$this->userDaoMock->expects($this->once())->method('getById')->willReturn($testUser);
$testToken = new \Szurubooru\Entities\Token();
$testToken->name = 'dummy_token';
$testToken->additionalData = $testUser->id;
2014-09-01 20:51:59 +02:00
$this->tokenDaoMock->expects($this->once())->method('getByName')->willReturn($testToken);
2014-09-01 20:51:59 +02:00
$authService = $this->getAuthService();
$authService->loginFromToken($testToken->name);
$this->assertTrue($authService->isLoggedIn());
$this->assertEquals($testUser, $authService->getLoggedInUser());
2014-08-31 17:42:48 +02:00
$this->assertNotNull($authService->getLoginToken());
$this->assertNotNull($authService->getLoginToken()->name);
}
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(
$this->validatorMock,
2014-09-04 19:57:06 +02:00
$this->configMock,
2014-09-01 20:51:59 +02:00
$this->passwordServiceMock,
$this->timeServiceMock,
$this->tokenDaoMock,
$this->userDaoMock);
2014-08-31 17:42:48 +02:00
}
}