summaryrefslogtreecommitdiff
path: root/tests/Unit/Helpers/AuthenticatorTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'tests/Unit/Helpers/AuthenticatorTest.php')
-rw-r--r--tests/Unit/Helpers/AuthenticatorTest.php125
1 files changed, 118 insertions, 7 deletions
diff --git a/tests/Unit/Helpers/AuthenticatorTest.php b/tests/Unit/Helpers/AuthenticatorTest.php
index 400278f2..83dc72ad 100644
--- a/tests/Unit/Helpers/AuthenticatorTest.php
+++ b/tests/Unit/Helpers/AuthenticatorTest.php
@@ -4,6 +4,7 @@ namespace Engelsystem\Test\Unit\Helpers;
use Engelsystem\Helpers\Authenticator;
use Engelsystem\Models\User\User;
+use Engelsystem\Test\Unit\HasDatabase;
use Engelsystem\Test\Unit\Helpers\Stub\UserModelImplementation;
use Engelsystem\Test\Unit\ServiceProviderTest;
use PHPUnit\Framework\MockObject\MockObject;
@@ -12,6 +13,8 @@ use Symfony\Component\HttpFoundation\Session\Session;
class AuthenticatorTest extends ServiceProviderTest
{
+ use HasDatabase;
+
/**
* @covers \Engelsystem\Helpers\Authenticator::__construct(
* @covers \Engelsystem\Helpers\Authenticator::user
@@ -29,7 +32,7 @@ class AuthenticatorTest extends ServiceProviderTest
$session->expects($this->exactly(3))
->method('get')
- ->with('uid')
+ ->with('user_id')
->willReturnOnConsecutiveCalls(
null,
42,
@@ -114,16 +117,13 @@ class AuthenticatorTest extends ServiceProviderTest
/** @var User|MockObject $user */
$user = $this->createMock(User::class);
- $user->expects($this->once())
- ->method('save');
-
- $session->expects($this->exactly(2))
+ $session->expects($this->once())
->method('get')
- ->with('uid')
+ ->with('user_id')
->willReturn(42);
$session->expects($this->once())
->method('remove')
- ->with('uid');
+ ->with('user_id');
/** @var Authenticator|MockObject $auth */
$auth = $this->getMockBuilder(Authenticator::class)
@@ -151,4 +151,115 @@ class AuthenticatorTest extends ServiceProviderTest
// Permissions cached
$this->assertTrue($auth->can('bar'));
}
+
+ /**
+ * @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() { }
+ };
+ }
}