|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace SimpleSAML\Test\Module\oidc\unit\Admin; |
| 6 | + |
| 7 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 8 | +use PHPUnit\Framework\MockObject\MockObject; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | +use SimpleSAML\Error\Exception; |
| 11 | +use SimpleSAML\Module\oidc\Admin\Authorization; |
| 12 | +use SimpleSAML\Module\oidc\Bridges\SspBridge; |
| 13 | +use SimpleSAML\Module\oidc\Bridges\SspBridge\Utils; |
| 14 | +use SimpleSAML\Module\oidc\Exceptions\AuthorizationException; |
| 15 | +use SimpleSAML\Module\oidc\Services\AuthContextService; |
| 16 | +use SimpleSAML\Utils\Auth; |
| 17 | + |
| 18 | +#[CoversClass(Authorization::class)] |
| 19 | +class AuthorizationTest extends TestCase |
| 20 | +{ |
| 21 | + protected MockObject $sspBridgeMock; |
| 22 | + protected MockObject $sspBridgeUtilsMock; |
| 23 | + protected MockObject $sspBridgeUtilsAuthMock; |
| 24 | + protected MockObject $authContextServiceMock; |
| 25 | + |
| 26 | + protected function setUp(): void |
| 27 | + { |
| 28 | + $this->sspBridgeMock = $this->createMock(SspBridge::class); |
| 29 | + $this->sspBridgeUtilsMock = $this->createMock(Utils::class); |
| 30 | + $this->sspBridgeMock->method('utils')->willReturn($this->sspBridgeUtilsMock); |
| 31 | + $this->sspBridgeUtilsAuthMock = $this->createMock(Auth::class); |
| 32 | + $this->sspBridgeUtilsMock->method('auth')->willReturn($this->sspBridgeUtilsAuthMock); |
| 33 | + |
| 34 | + $this->authContextServiceMock = $this->createMock(AuthContextService::class); |
| 35 | + } |
| 36 | + |
| 37 | + protected function sut( |
| 38 | + ?SspBridge $sspBridge = null, |
| 39 | + ?AuthContextService $authContextService = null, |
| 40 | + ): Authorization { |
| 41 | + $sspBridge ??= $this->sspBridgeMock; |
| 42 | + $authContextService ??= $this->authContextServiceMock; |
| 43 | + |
| 44 | + return new Authorization($sspBridge, $authContextService); |
| 45 | + } |
| 46 | + |
| 47 | + public function testCanCreateInstance(): void |
| 48 | + { |
| 49 | + $this->assertInstanceOf(Authorization::class, $this->sut()); |
| 50 | + } |
| 51 | + |
| 52 | + public function testCanCheckIsAdmin(): void |
| 53 | + { |
| 54 | + $this->assertFalse($this->sut()->isAdmin()); |
| 55 | + $this->sspBridgeUtilsAuthMock->method('isAdmin')->willReturn(true); |
| 56 | + $this->assertTrue($this->sut()->isAdmin()); |
| 57 | + } |
| 58 | + |
| 59 | + public function testCanRequireAdmin(): void |
| 60 | + { |
| 61 | + $this->expectException(AuthorizationException::class); |
| 62 | + $this->expectExceptionMessage('admin'); |
| 63 | + |
| 64 | + $this->sspBridgeUtilsAuthMock->method('isAdmin')->willReturn(false); |
| 65 | + |
| 66 | + $this->sut()->requireAdmin(); |
| 67 | + } |
| 68 | + |
| 69 | + public function testCanForceRequireAdmin(): void |
| 70 | + { |
| 71 | + $this->sspBridgeUtilsAuthMock->expects($this->once())->method('requireAdmin'); |
| 72 | + $this->sspBridgeUtilsAuthMock->expects($this->once())->method('isAdmin')->willReturn(true); |
| 73 | + |
| 74 | + $this->sut()->requireAdmin(true); |
| 75 | + } |
| 76 | + |
| 77 | + public function testThrowsOnForceRequireAdminError(): void |
| 78 | + { |
| 79 | + $this->sspBridgeUtilsAuthMock->expects($this->once())->method('requireAdmin') |
| 80 | + ->willThrowException(new Exception('error')); |
| 81 | + |
| 82 | + $this->expectException(AuthorizationException::class); |
| 83 | + $this->expectExceptionMessage('admin'); |
| 84 | + |
| 85 | + $this->sut()->requireAdmin(true); |
| 86 | + } |
| 87 | + |
| 88 | + public function testRequireAdminOrUserWithPermissionReturnsIfAdmin(): void |
| 89 | + { |
| 90 | + $this->sspBridgeUtilsAuthMock->expects($this->once())->method('isAdmin')->willReturn(true); |
| 91 | + $this->authContextServiceMock->expects($this->never())->method('requirePermission'); |
| 92 | + |
| 93 | + $this->sut()->requireAdminOrUserWithPermission('permission'); |
| 94 | + } |
| 95 | + |
| 96 | + public function testRequireAdminOrUserWithPermissionReturnsIfUser(): void |
| 97 | + { |
| 98 | + $this->sspBridgeUtilsAuthMock->expects($this->once())->method('isAdmin')->willReturn(false); |
| 99 | + $this->authContextServiceMock->expects($this->once())->method('requirePermission'); |
| 100 | + |
| 101 | + $this->sut()->requireAdminOrUserWithPermission('permission'); |
| 102 | + } |
| 103 | + |
| 104 | + public function testRequireUserWithPermissionThrowsIfUserNotAuthorized(): void |
| 105 | + { |
| 106 | + $this->expectException(AuthorizationException::class); |
| 107 | + $this->expectExceptionMessage('not authorized'); |
| 108 | + |
| 109 | + $this->sspBridgeUtilsAuthMock->expects($this->once())->method('isAdmin')->willReturn(false); |
| 110 | + $this->authContextServiceMock->expects($this->once())->method('requirePermission') |
| 111 | + ->willThrowException(new Exception('error')); |
| 112 | + |
| 113 | + $this->sut()->requireAdminOrUserWithPermission('permission'); |
| 114 | + } |
| 115 | + |
| 116 | + public function testCanGetUserId(): void |
| 117 | + { |
| 118 | + $this->authContextServiceMock->expects($this->once())->method('getAuthUserId')->willReturn('id'); |
| 119 | + |
| 120 | + $this->assertSame('id', $this->sut()->getUserId()); |
| 121 | + } |
| 122 | +} |
0 commit comments