summaryrefslogtreecommitdiff
path: root/tests/Unit/Helpers/AuthenticatorTest.php
diff options
context:
space:
mode:
authorIgor Scheller <igor.scheller@igorshp.de>2018-11-12 14:41:23 +0100
committermsquare <msquare@notrademark.de>2018-12-02 12:53:31 +0100
commitc33940f64a1e5b59afd700010247382f5b7b2df3 (patch)
tree453b8810c90cd78e75a1425a4f4f002e585d121a /tests/Unit/Helpers/AuthenticatorTest.php
parent951828a4f1175f99666a48629ea125640cc7c598 (diff)
Moved permission checks to Authenticator class
Diffstat (limited to 'tests/Unit/Helpers/AuthenticatorTest.php')
-rw-r--r--tests/Unit/Helpers/AuthenticatorTest.php52
1 files changed, 52 insertions, 0 deletions
diff --git a/tests/Unit/Helpers/AuthenticatorTest.php b/tests/Unit/Helpers/AuthenticatorTest.php
index 2c03b968..05c7d16e 100644
--- a/tests/Unit/Helpers/AuthenticatorTest.php
+++ b/tests/Unit/Helpers/AuthenticatorTest.php
@@ -99,4 +99,56 @@ class AuthenticatorTest extends ServiceProviderTest
UserModelImplementation::$user = null;
$this->assertEquals($user, $auth->apiUser());
}
+
+ /**
+ * @covers \Engelsystem\Helpers\Authenticator::can
+ */
+ public function testCan()
+ {
+ /** @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);
+
+ $user->expects($this->once())
+ ->method('save');
+
+ $session->expects($this->exactly(2))
+ ->method('get')
+ ->with('uid')
+ ->willReturn(42);
+ $session->expects($this->once())
+ ->method('remove')
+ ->with('uid');
+
+ /** @var Authenticator|MockObject $auth */
+ $auth = $this->getMockBuilder(Authenticator::class)
+ ->setConstructorArgs([$request, $session, $userRepository])
+ ->setMethods(['getPermissionsByGroup', 'getPermissionsByUser', 'user'])
+ ->getMock();
+ $auth->expects($this->exactly(1))
+ ->method('getPermissionsByGroup')
+ ->with(-10)
+ ->willReturn([]);
+ $auth->expects($this->exactly(1))
+ ->method('getPermissionsByUser')
+ ->with($user)
+ ->willReturn(['bar']);
+ $auth->expects($this->exactly(2))
+ ->method('user')
+ ->willReturnOnConsecutiveCalls(null, $user);
+
+ // No user, no permissions
+ $this->assertFalse($auth->can('foo'));
+
+ // User exists, has permissions
+ $this->assertTrue($auth->can('bar'));
+
+ // Permissions cached
+ $this->assertTrue($auth->can('bar'));
+ }
}