2014-08-30 18:11:32 +02:00
|
|
|
<?php
|
|
|
|
namespace Szurubooru\Tests\Services;
|
|
|
|
|
|
|
|
class AuthServiceTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
|
|
|
public function testInvalidUser()
|
|
|
|
{
|
|
|
|
$passwordServiceMock = $this->getPasswordServiceMock();
|
2014-08-31 17:42:48 +02:00
|
|
|
$timeServiceMock = $this->getTimeServiceMock();
|
2014-08-30 18:11:32 +02:00
|
|
|
$tokenDaoMock = $this->getTokenDaoMock();
|
|
|
|
$userDaoMock = $this->getUserDaoMock();
|
|
|
|
|
|
|
|
$this->setExpectedException(\InvalidArgumentException::class, 'User not found');
|
|
|
|
|
2014-08-31 17:42:48 +02:00
|
|
|
$authService = new \Szurubooru\Services\AuthService($passwordServiceMock, $timeServiceMock, $tokenDaoMock, $userDaoMock);
|
2014-08-30 18:11:32 +02:00
|
|
|
$authService->loginFromCredentials('dummy', 'godzilla');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testInvalidPassword()
|
|
|
|
{
|
|
|
|
$passwordServiceMock = $this->getPasswordServiceMock();
|
2014-08-31 17:42:48 +02:00
|
|
|
$timeServiceMock = $this->getTimeServiceMock();
|
2014-08-30 18:11:32 +02:00
|
|
|
$tokenDaoMock = $this->getTokenDaoMock();
|
|
|
|
$userDaoMock = $this->getUserDaoMock();
|
|
|
|
|
|
|
|
$passwordServiceMock->method('getHash')->willReturn('unmatchingHash');
|
|
|
|
|
|
|
|
$testUser = new \Szurubooru\Entities\User();
|
|
|
|
$testUser->name = 'dummy';
|
|
|
|
$testUser->passwordHash = 'hash';
|
|
|
|
$userDaoMock->expects($this->once())->method('getByName')->willReturn($testUser);
|
|
|
|
|
2014-08-31 17:42:48 +02:00
|
|
|
$authService = new \Szurubooru\Services\AuthService($passwordServiceMock, $timeServiceMock, $tokenDaoMock, $userDaoMock);
|
2014-08-30 18:11:32 +02:00
|
|
|
$this->setExpectedException(\InvalidArgumentException::class, 'Specified password is invalid');
|
|
|
|
$authService->loginFromCredentials('dummy', 'godzilla');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidCredentials()
|
|
|
|
{
|
|
|
|
$passwordServiceMock = $this->getPasswordServiceMock();
|
2014-08-31 17:42:48 +02:00
|
|
|
$timeServiceMock = $this->getTimeServiceMock();
|
2014-08-30 18:11:32 +02:00
|
|
|
$tokenDaoMock = $this->getTokenDaoMock();
|
|
|
|
$userDaoMock = $this->getUserDaoMock();
|
|
|
|
|
|
|
|
$tokenDaoMock->expects($this->once())->method('save');
|
|
|
|
$passwordServiceMock->method('getHash')->willReturn('hash');
|
|
|
|
|
|
|
|
$testUser = new \Szurubooru\Entities\User();
|
|
|
|
$testUser->name = 'dummy';
|
|
|
|
$testUser->passwordHash = 'hash';
|
|
|
|
$userDaoMock->expects($this->once())->method('getByName')->willReturn($testUser);
|
|
|
|
|
2014-08-31 17:42:48 +02:00
|
|
|
$authService = new \Szurubooru\Services\AuthService($passwordServiceMock, $timeServiceMock, $tokenDaoMock, $userDaoMock);
|
2014-08-30 18:11:32 +02:00
|
|
|
$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()
|
|
|
|
{
|
|
|
|
$passwordServiceMock = $this->getPasswordServiceMock();
|
|
|
|
$timeServiceMock = $this->getTimeServiceMock();
|
|
|
|
$tokenDaoMock = $this->getTokenDaoMock();
|
|
|
|
$userDaoMock = $this->getUserDaoMock();
|
|
|
|
|
|
|
|
$tokenDaoMock->expects($this->once())->method('getByName')->willReturn(null);
|
|
|
|
|
|
|
|
$this->setExpectedException(\Exception::class);
|
|
|
|
$authService = new \Szurubooru\Services\AuthService($passwordServiceMock, $timeServiceMock, $tokenDaoMock, $userDaoMock);
|
|
|
|
$authService->loginFromToken('');
|
2014-08-30 18:11:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidToken()
|
|
|
|
{
|
|
|
|
$passwordServiceMock = $this->getPasswordServiceMock();
|
2014-08-31 17:42:48 +02:00
|
|
|
$timeServiceMock = $this->getTimeServiceMock();
|
2014-08-30 18:11:32 +02:00
|
|
|
$tokenDaoMock = $this->getTokenDaoMock();
|
|
|
|
$userDaoMock = $this->getUserDaoMock();
|
|
|
|
|
|
|
|
$testUser = new \Szurubooru\Entities\User();
|
|
|
|
$testUser->id = 5;
|
|
|
|
$testUser->name = 'dummy';
|
|
|
|
$testUser->passwordHash = 'hash';
|
|
|
|
$userDaoMock->expects($this->once())->method('getById')->willReturn($testUser);
|
|
|
|
|
|
|
|
$testToken = new \Szurubooru\Entities\Token();
|
|
|
|
$testToken->name = 'dummy_token';
|
|
|
|
$testToken->additionalData = $testUser->id;
|
|
|
|
$tokenDaoMock->expects($this->once())->method('getByName')->willReturn($testToken);
|
|
|
|
|
2014-08-31 17:42:48 +02:00
|
|
|
$authService = new \Szurubooru\Services\AuthService($passwordServiceMock, $timeServiceMock, $tokenDaoMock, $userDaoMock);
|
2014-08-30 18:11:32 +02:00
|
|
|
$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-08-30 18:11:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private function getTokenDaoMock()
|
|
|
|
{
|
|
|
|
return $this->getMockBuilder(\Szurubooru\Dao\TokenDao::class)->disableOriginalConstructor()->getMock();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getUserDaoMock()
|
|
|
|
{
|
|
|
|
return $this->getMockBuilder(\Szurubooru\Dao\UserDao::class)->disableOriginalConstructor()->getMock();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getPasswordServiceMock()
|
|
|
|
{
|
|
|
|
return $this->getMockBuilder(\Szurubooru\Services\PasswordService::class)->disableOriginalConstructor()->getMock();
|
|
|
|
}
|
2014-08-31 17:42:48 +02:00
|
|
|
|
|
|
|
private function getTimeServiceMock()
|
|
|
|
{
|
|
|
|
return $this->getMockBuilder(\Szurubooru\Services\TimeService::class)->disableOriginalConstructor()->getMock();
|
|
|
|
}
|
2014-08-30 18:11:32 +02:00
|
|
|
}
|