diff options
author | Christoph Wurst <christoph@winzerhof-wurst.at> | 2020-12-14 15:56:07 +0100 |
---|---|---|
committer | Christoph Wurst <christoph@winzerhof-wurst.at> | 2020-12-16 13:13:05 +0100 |
commit | 6995223b1ed202c7f8e920e83cb5b53efd7ce761 (patch) | |
tree | 76e839b9c3de3b4751a993f24f35f0cb93c0dbbd /tests/lib | |
parent | d37034f1612279b07c78284771ac73fbd5a2a407 (diff) | |
download | nextcloud-server-6995223b1ed202c7f8e920e83cb5b53efd7ce761.tar.gz nextcloud-server-6995223b1ed202c7f8e920e83cb5b53efd7ce761.zip |
Add well known handlers API
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
Diffstat (limited to 'tests/lib')
-rw-r--r-- | tests/lib/Http/WellKnown/GenericResponseTest.php | 40 | ||||
-rw-r--r-- | tests/lib/Http/WellKnown/JrdResponseTest.php | 108 | ||||
-rw-r--r-- | tests/lib/Http/WellKnown/RequestManagerTest.php | 176 |
3 files changed, 324 insertions, 0 deletions
diff --git a/tests/lib/Http/WellKnown/GenericResponseTest.php b/tests/lib/Http/WellKnown/GenericResponseTest.php new file mode 100644 index 00000000000..1e84a509c1b --- /dev/null +++ b/tests/lib/Http/WellKnown/GenericResponseTest.php @@ -0,0 +1,40 @@ +<?php + +declare(strict_types=1); + +/* + * @copyright 2020 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2020 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +namespace Tests\Http\WellKnown; + +use OCP\AppFramework\Http\JSONResponse; +use OCP\Http\WellKnown\GenericResponse; +use Test\TestCase; + +class GenericResponseTest extends TestCase { + public function testToHttpResponse(): void { + $httpResponse = $this->createMock(JSONResponse::class); + + $response = new GenericResponse($httpResponse); + + self::assertSame($httpResponse, $response->toHttpResponse()); + } +} diff --git a/tests/lib/Http/WellKnown/JrdResponseTest.php b/tests/lib/Http/WellKnown/JrdResponseTest.php new file mode 100644 index 00000000000..f540ac1862e --- /dev/null +++ b/tests/lib/Http/WellKnown/JrdResponseTest.php @@ -0,0 +1,108 @@ +<?php + +declare(strict_types=1); + +/* + * @copyright 2020 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2020 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +namespace Test\Http\WellKnown; + +use OCP\AppFramework\Http\JSONResponse; +use OCP\Http\WellKnown\JrdResponse; +use Test\TestCase; + +class JrdResponseTest extends TestCase { + public function testEmptyToHttpResponse(): void { + $response = new JrdResponse("subject"); + $httpResponse = $response->toHttpResponse(); + + self::assertTrue($response->isEmpty()); + self::assertInstanceOf(JSONResponse::class, $httpResponse); + /** @var JSONResponse $httpResponse */ + self::assertEquals( + [ + 'subject' => 'subject', + ], + $httpResponse->getData() + ); + } + + public function testComplexToHttpResponse(): void { + $response = new JrdResponse("subject"); + $response->addAlias('alias'); + $response->addAlias('blias'); + $response->addProperty('propa', 'a'); + $response->addProperty('propb', null); + $response->setExpires('tomorrow'); + $response->addLink('rel', null, null); + $response->addLink('rel', 'type', null); + $response->addLink('rel', 'type', 'href', ['title' => 'titlevalue']); + $response->addLink('rel', 'type', 'href', ['title' => 'titlevalue'], ['propx' => 'valx']); + $httpResponse = $response->toHttpResponse(); + + self::assertFalse($response->isEmpty()); + self::assertInstanceOf(JSONResponse::class, $httpResponse); + /** @var JSONResponse $httpResponse */ + self::assertEquals( + [ + 'subject' => 'subject', + 'aliases' => [ + 'alias', + 'blias', + ], + 'properties' => [ + 'propa' => 'a', + 'propb' => null, + ], + 'expires' => 'tomorrow', + 'links' => [ + [ + 'rel' => 'rel', + ], + [ + 'rel' => 'rel', + 'type' => 'type', + ], + [ + 'rel' => 'rel', + 'type' => 'type', + 'href' => 'href', + 'titles' => [ + 'title' => 'titlevalue', + ], + ], + [ + 'rel' => 'rel', + 'type' => 'type', + 'href' => 'href', + 'titles' => [ + 'title' => 'titlevalue', + ], + 'properties' => [ + 'propx' => 'valx', + ], + ], + ] + ], + $httpResponse->getData() + ); + } +} diff --git a/tests/lib/Http/WellKnown/RequestManagerTest.php b/tests/lib/Http/WellKnown/RequestManagerTest.php new file mode 100644 index 00000000000..b947df49496 --- /dev/null +++ b/tests/lib/Http/WellKnown/RequestManagerTest.php @@ -0,0 +1,176 @@ +<?php + +declare(strict_types=1); + +/* + * @copyright 2020 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2020 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +namespace Test\Http\WellKnown; + +use OC\AppFramework\Bootstrap\Coordinator; +use OC\AppFramework\Bootstrap\RegistrationContext; +use OC\Http\WellKnown\RequestManager; +use OCP\AppFramework\QueryException; +use OCP\Http\WellKnown\IHandler; +use OCP\Http\WellKnown\IRequestContext; +use OCP\Http\WellKnown\IResponse; +use OCP\Http\WellKnown\JrdResponse; +use OCP\IRequest; +use OCP\IServerContainer; +use PHPUnit\Framework\MockObject\MockObject; +use Psr\Log\LoggerInterface; +use RuntimeException; +use Test\TestCase; +use function get_class; + +class RequestManagerTest extends TestCase { + + /** @var Coordinator|MockObject */ + private $coordinator; + + /** @var IServerContainer|MockObject */ + private $container; + + /** @var MockObject|LoggerInterface */ + private $logger; + + /** @var RequestManager */ + private $manager; + + protected function setUp(): void { + parent::setUp(); + + $this->coordinator = $this->createMock(Coordinator::class); + $this->container = $this->createMock(IServerContainer::class); + $this->logger = $this->createMock(LoggerInterface::class); + + $this->manager = new RequestManager( + $this->coordinator, + $this->container, + $this->logger, + ); + } + + public function testProcessAppsNotRegistered(): void { + $request = $this->createMock(IRequest::class); + $this->expectException(RuntimeException::class); + + $this->manager->process("webfinger", $request); + } + + public function testProcessNoHandlersRegistered(): void { + $request = $this->createMock(IRequest::class); + $registrationContext = $this->createMock(RegistrationContext::class); + $this->coordinator->expects(self::once()) + ->method('getRegistrationContext') + ->willReturn($registrationContext); + $registrationContext->expects(self::once()) + ->method('getWellKnownHandlers') + ->willReturn([]); + + $response = $this->manager->process("webfinger", $request); + + self::assertNull($response); + } + + public function testProcessHandlerNotLoadable(): void { + $request = $this->createMock(IRequest::class); + $registrationContext = $this->createMock(RegistrationContext::class); + $this->coordinator->expects(self::once()) + ->method('getRegistrationContext') + ->willReturn($registrationContext); + $handler = new class { + }; + $registrationContext->expects(self::once()) + ->method('getWellKnownHandlers') + ->willReturn([ + [ + 'class' => get_class($handler), + ], + ]); + $this->container->expects(self::once()) + ->method('get') + ->with(get_class($handler)) + ->willThrowException(new QueryException("")); + $this->logger->expects(self::once()) + ->method('error'); + + $response = $this->manager->process("webfinger", $request); + + self::assertNull($response); + } + + public function testProcessHandlerOfWrongType(): void { + $request = $this->createMock(IRequest::class); + $registrationContext = $this->createMock(RegistrationContext::class); + $this->coordinator->expects(self::once()) + ->method('getRegistrationContext') + ->willReturn($registrationContext); + $handler = new class { + }; + $registrationContext->expects(self::once()) + ->method('getWellKnownHandlers') + ->willReturn([ + [ + 'class' => get_class($handler), + ], + ]); + $this->container->expects(self::once()) + ->method('get') + ->with(get_class($handler)) + ->willReturn($handler); + $this->logger->expects(self::once()) + ->method('error'); + + $response = $this->manager->process("webfinger", $request); + + self::assertNull($response); + } + + public function testProcess(): void { + $request = $this->createMock(IRequest::class); + $registrationContext = $this->createMock(RegistrationContext::class); + $this->coordinator->expects(self::once()) + ->method('getRegistrationContext') + ->willReturn($registrationContext); + $handler = new class implements IHandler { + public function handle(string $service, IRequestContext $context, ?IResponse $previousResponse): ?IResponse { + return (new JrdResponse($service))->addAlias('alias'); + } + }; + $registrationContext->expects(self::once()) + ->method('getWellKnownHandlers') + ->willReturn([ + [ + 'class' => get_class($handler), + ], + ]); + $this->container->expects(self::once()) + ->method('get') + ->with(get_class($handler)) + ->willReturn($handler); + + $response = $this->manager->process("webfinger", $request); + + self::assertNotNull($response); + self::assertInstanceOf(JrdResponse::class, $response); + } +} |