summaryrefslogtreecommitdiff
path: root/tests/Unit/Helpers
diff options
context:
space:
mode:
Diffstat (limited to 'tests/Unit/Helpers')
-rw-r--r--tests/Unit/Helpers/AuthenticatorServiceProviderTest.php3
-rw-r--r--tests/Unit/Helpers/AuthenticatorTest.php49
-rw-r--r--tests/Unit/Helpers/Stub/UserModelImplementation.php17
3 files changed, 68 insertions, 1 deletions
diff --git a/tests/Unit/Helpers/AuthenticatorServiceProviderTest.php b/tests/Unit/Helpers/AuthenticatorServiceProviderTest.php
index f7819da2..f42e9dff 100644
--- a/tests/Unit/Helpers/AuthenticatorServiceProviderTest.php
+++ b/tests/Unit/Helpers/AuthenticatorServiceProviderTest.php
@@ -5,7 +5,9 @@ namespace Engelsystem\Test\Unit\Helpers;
use Engelsystem\Application;
use Engelsystem\Helpers\Authenticator;
use Engelsystem\Helpers\AuthenticatorServiceProvider;
+use Engelsystem\Http\Request;
use Engelsystem\Test\Unit\ServiceProviderTest;
+use Psr\Http\Message\ServerRequestInterface;
class AuthenticatorServiceProviderTest extends ServiceProviderTest
{
@@ -15,6 +17,7 @@ class AuthenticatorServiceProviderTest extends ServiceProviderTest
public function testRegister()
{
$app = new Application();
+ $app->bind(ServerRequestInterface::class, Request::class);
$serviceProvider = new AuthenticatorServiceProvider($app);
$serviceProvider->register();
diff --git a/tests/Unit/Helpers/AuthenticatorTest.php b/tests/Unit/Helpers/AuthenticatorTest.php
index 085887c4..2c03b968 100644
--- a/tests/Unit/Helpers/AuthenticatorTest.php
+++ b/tests/Unit/Helpers/AuthenticatorTest.php
@@ -7,6 +7,7 @@ use Engelsystem\Models\User\User;
use Engelsystem\Test\Unit\Helpers\Stub\UserModelImplementation;
use Engelsystem\Test\Unit\ServiceProviderTest;
use PHPUnit\Framework\MockObject\MockObject;
+use Psr\Http\Message\ServerRequestInterface;
use Symfony\Component\HttpFoundation\Session\Session;
class AuthenticatorTest extends ServiceProviderTest
@@ -17,6 +18,8 @@ class AuthenticatorTest extends ServiceProviderTest
*/
public function testUser()
{
+ /** @var ServerRequestInterface|MockObject $request */
+ $request = $this->getMockForAbstractClass(ServerRequestInterface::class);
/** @var Session|MockObject $session */
$session = $this->createMock(Session::class);
/** @var UserModelImplementation|MockObject $userRepository */
@@ -33,7 +36,7 @@ class AuthenticatorTest extends ServiceProviderTest
1337
);
- $auth = new Authenticator($session, $userRepository);
+ $auth = new Authenticator($request, $session, $userRepository);
// Not in session
$this->assertEquals(null, $auth->user());
@@ -52,4 +55,48 @@ class AuthenticatorTest extends ServiceProviderTest
UserModelImplementation::$user = null;
$this->assertEquals($user, $auth->user());
}
+
+ /**
+ * @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
+ $this->assertEquals(null, $auth->apiUser());
+
+ // Unknown user
+ UserModelImplementation::$apiKey = 'iMaNot3xiSt1nGAp1Key!';
+ $this->assertEquals(null, $auth->apiUser());
+
+ // 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());
+ }
}
diff --git a/tests/Unit/Helpers/Stub/UserModelImplementation.php b/tests/Unit/Helpers/Stub/UserModelImplementation.php
index 934aaeb2..1b14a17e 100644
--- a/tests/Unit/Helpers/Stub/UserModelImplementation.php
+++ b/tests/Unit/Helpers/Stub/UserModelImplementation.php
@@ -3,6 +3,7 @@
namespace Engelsystem\Test\Unit\Helpers\Stub;
use Engelsystem\Models\User\User;
+use Illuminate\Database\Eloquent\Collection;
use InvalidArgumentException;
class UserModelImplementation extends User
@@ -13,6 +14,9 @@ class UserModelImplementation extends User
/** @var int */
public static $id = null;
+ /** @var int */
+ public static $apiKey = null;
+
/**
* @param mixed $id
* @param array $columns
@@ -26,4 +30,17 @@ class UserModelImplementation extends User
return self::$user;
}
+
+ /**
+ * @param string $apiKey
+ * @return User[]|Collection|\Illuminate\Database\Query\Builder
+ */
+ public static function whereApiKey($apiKey)
+ {
+ if ($apiKey != static::$apiKey) {
+ throw new InvalidArgumentException('Wrong api key searched');
+ }
+
+ return new Collection([self::$user]);
+ }
}