diff options
author | Igor Scheller <igor.scheller@igorshp.de> | 2018-11-12 14:41:23 +0100 |
---|---|---|
committer | msquare <msquare@notrademark.de> | 2018-12-02 12:53:31 +0100 |
commit | c33940f64a1e5b59afd700010247382f5b7b2df3 (patch) | |
tree | 453b8810c90cd78e75a1425a4f4f002e585d121a /tests/Unit | |
parent | 951828a4f1175f99666a48629ea125640cc7c598 (diff) |
Moved permission checks to Authenticator class
Diffstat (limited to 'tests/Unit')
-rw-r--r-- | tests/Unit/Helpers/AuthenticatorTest.php | 52 | ||||
-rw-r--r-- | tests/Unit/Middleware/LegacyMiddlewareTest.php | 3 | ||||
-rw-r--r-- | tests/Unit/Renderer/Twig/Extensions/AuthenticationTest.php | 21 |
3 files changed, 56 insertions, 20 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')); + } } diff --git a/tests/Unit/Middleware/LegacyMiddlewareTest.php b/tests/Unit/Middleware/LegacyMiddlewareTest.php index caea483d..8dd2f417 100644 --- a/tests/Unit/Middleware/LegacyMiddlewareTest.php +++ b/tests/Unit/Middleware/LegacyMiddlewareTest.php @@ -76,6 +76,9 @@ class LegacyMiddlewareTest extends TestCase $auth->expects($this->atLeastOnce()) ->method('user') ->willReturn(false); + $auth->expects($this->atLeastOnce()) + ->method('can') + ->willReturn(false); $translator->expects($this->exactly(2)) ->method('translate') diff --git a/tests/Unit/Renderer/Twig/Extensions/AuthenticationTest.php b/tests/Unit/Renderer/Twig/Extensions/AuthenticationTest.php index b67d4eed..266b038e 100644 --- a/tests/Unit/Renderer/Twig/Extensions/AuthenticationTest.php +++ b/tests/Unit/Renderer/Twig/Extensions/AuthenticationTest.php @@ -23,7 +23,7 @@ class AuthenticationTest extends ExtensionTest $this->assertExtensionExists('is_user', [$extension, 'isAuthenticated'], $functions); $this->assertExtensionExists('is_guest', [$extension, 'isGuest'], $functions); - $this->assertExtensionExists('has_permission_to', [$extension, 'checkAuth'], $functions); + $this->assertExtensionExists('has_permission_to', [$auth, 'can'], $functions); } /** @@ -53,23 +53,4 @@ class AuthenticationTest extends ExtensionTest $this->assertTrue($extension->isAuthenticated()); $this->assertFalse($extension->isGuest()); } - - /** - * @covers \Engelsystem\Renderer\Twig\Extensions\Authentication::checkAuth - */ - public function testCheckAuth() - { - global $privileges; - $privileges = []; - - /** @var Authenticator|MockObject $auth */ - $auth = $this->createMock(Authenticator::class); - - $extension = new Authentication($auth); - - $this->assertFalse($extension->checkAuth('foo.bar')); - - $privileges = ['foo.bar']; - $this->assertTrue($extension->checkAuth('foo.bar')); - } } |