|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace SimpleSAML\Test\Module\oidc\unit\Controllers\Federation; |
| 6 | + |
| 7 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 8 | +use PHPUnit\Framework\MockObject\MockObject; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | +use SimpleSAML\Module\oidc\Controllers\Federation\SubordinateListingsController; |
| 11 | +use SimpleSAML\Module\oidc\Entities\Interfaces\ClientEntityInterface; |
| 12 | +use SimpleSAML\Module\oidc\ModuleConfig; |
| 13 | +use SimpleSAML\Module\oidc\Repositories\ClientRepository; |
| 14 | +use SimpleSAML\Module\oidc\Server\Exceptions\OidcServerException; |
| 15 | +use SimpleSAML\Module\oidc\Utils\Routes; |
| 16 | +use SimpleSAML\OpenID\Codebooks\ErrorsEnum; |
| 17 | +use SimpleSAML\OpenID\Codebooks\ParamsEnum; |
| 18 | +use Symfony\Component\HttpFoundation\ParameterBag; |
| 19 | +use Symfony\Component\HttpFoundation\Request; |
| 20 | + |
| 21 | +#[CoversClass(SubordinateListingsController::class)] |
| 22 | +final class SubordinateListingsControllerTest extends TestCase |
| 23 | +{ |
| 24 | + private MockObject $moduleConfigMock; |
| 25 | + private MockObject $clientRepositoryMock; |
| 26 | + private MockObject $routesMock; |
| 27 | + |
| 28 | + private bool $isFederationEnabled; |
| 29 | + private MockObject $requestMock; |
| 30 | + private MockObject $requestQueryMock; |
| 31 | + |
| 32 | + |
| 33 | + protected function setUp(): void |
| 34 | + { |
| 35 | + $this->moduleConfigMock = $this->createMock(ModuleConfig::class); |
| 36 | + $this->clientRepositoryMock = $this->createMock(ClientRepository::class); |
| 37 | + $this->routesMock = $this->createMock(Routes::class); |
| 38 | + |
| 39 | + $this->isFederationEnabled = true; |
| 40 | + |
| 41 | + $this->requestMock = $this->createMock(Request::class); |
| 42 | + $this->requestQueryMock = $this->createMock(ParameterBag::class); |
| 43 | + $this->requestMock->query = $this->requestQueryMock; |
| 44 | + } |
| 45 | + |
| 46 | + public function sut( |
| 47 | + ?ModuleConfig $moduleConfig = null, |
| 48 | + ?ClientRepository $clientRepository = null, |
| 49 | + ?Routes $routes = null, |
| 50 | + ?bool $federationEnabled = null, |
| 51 | + ): SubordinateListingsController { |
| 52 | + $federationEnabled = $federationEnabled ?? $this->isFederationEnabled; |
| 53 | + $this->moduleConfigMock->method('getFederationEnabled')->willReturn($federationEnabled); |
| 54 | + |
| 55 | + $moduleConfig = $moduleConfig ?? $this->moduleConfigMock; |
| 56 | + $clientRepository = $clientRepository ?? $this->clientRepositoryMock; |
| 57 | + $routes = $routes ?? $this->routesMock; |
| 58 | + |
| 59 | + return new SubordinateListingsController( |
| 60 | + $moduleConfig, |
| 61 | + $clientRepository, |
| 62 | + $routes, |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + public function testCanConstruct(): void |
| 67 | + { |
| 68 | + $this->assertInstanceOf(SubordinateListingsController::class, $this->sut()); |
| 69 | + } |
| 70 | + |
| 71 | + public function testThrowsIfFederationNotEnabled(): void |
| 72 | + { |
| 73 | + $this->expectException(OidcServerException::class); |
| 74 | + $this->expectExceptionMessage('refused'); |
| 75 | + |
| 76 | + $this->sut(federationEnabled: false); |
| 77 | + } |
| 78 | + |
| 79 | + public function testCanListFederatedEntities(): void |
| 80 | + { |
| 81 | + $client = $this->createMock(ClientEntityInterface::class); |
| 82 | + $client->method('getEntityIdentifier')->willReturn('entity-id'); |
| 83 | + |
| 84 | + $federatedEntities = [ |
| 85 | + $client, |
| 86 | + ]; |
| 87 | + |
| 88 | + $this->clientRepositoryMock->expects($this->once())->method('findAllFederated') |
| 89 | + ->willReturn($federatedEntities); |
| 90 | + |
| 91 | + $this->routesMock->expects($this->once())->method('newJsonResponse') |
| 92 | + ->with([ |
| 93 | + $client->getEntityIdentifier(), |
| 94 | + ]); |
| 95 | + |
| 96 | + $this->sut()->list($this->requestMock); |
| 97 | + } |
| 98 | + |
| 99 | + public function testListReturnsErrorOnUnsuportedQueryParameter(): void |
| 100 | + { |
| 101 | + $this->requestQueryMock->method('all')->willReturn([ |
| 102 | + ParamsEnum::EntityType->value => 'something', |
| 103 | + ]); |
| 104 | + |
| 105 | + $this->routesMock->expects($this->once())->method('newJsonErrorResponse') |
| 106 | + ->with(ErrorsEnum::UnsupportedParameter->value); |
| 107 | + |
| 108 | + $this->sut()->list($this->requestMock); |
| 109 | + } |
| 110 | +} |
0 commit comments