From a98376f38cb2f2933495b16eaf5ba096fd1d1ebe Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Fri, 12 Aug 2016 15:42:26 +0200 Subject: [PATCH] Add tests --- .../Middleware/OCSMiddlewareTest.php | 76 +++++++++++++++++-- 1 file changed, 70 insertions(+), 6 deletions(-) diff --git a/tests/lib/AppFramework/Middleware/OCSMiddlewareTest.php b/tests/lib/AppFramework/Middleware/OCSMiddlewareTest.php index 7d8cadc677f..b2295fdc26d 100644 --- a/tests/lib/AppFramework/Middleware/OCSMiddlewareTest.php +++ b/tests/lib/AppFramework/Middleware/OCSMiddlewareTest.php @@ -27,14 +27,14 @@ use OCP\AppFramework\OCS\OCSBadRequestException; use OCP\AppFramework\OCS\OCSException; use OCP\AppFramework\OCS\OCSForbiddenException; use OCP\AppFramework\OCS\OCSNotFoundException; -use OC\AppFramework\Http\Request; +use OCP\IRequest; use OC\AppFramework\Middleware\OCSMiddleware; class OCSMiddlewareTest extends \Test\TestCase { /** - * @var Request + * @var IRequest */ private $request; @@ -101,8 +101,18 @@ class OCSMiddlewareTest extends \Test\TestCase { $this->assertInstanceOf('OCP\AppFramework\Http\OCSResponse', $result); $this->assertSame($message, $this->invokePrivate($result, 'message')); - $this->assertSame($code, $this->invokePrivate($result, 'statuscode')); - $this->assertSame(200, $result->getStatus()); + + if ($exception->getCode() === 0) { + $this->assertSame(\OCP\API::RESPOND_UNKNOWN_ERROR, $this->invokePrivate($result, 'statuscode')); + } else { + $this->assertSame($code, $this->invokePrivate($result, 'statuscode')); + } + + if ($exception instanceof OCSForbiddenException) { + $this->assertSame(Http::STATUS_UNAUTHORIZED, $result->getStatus()); + } else { + $this->assertSame(200, $result->getStatus()); + } } catch (\Exception $e) { $this->assertTrue($forward); $this->assertEquals($exception, $e); @@ -131,7 +141,11 @@ class OCSMiddlewareTest extends \Test\TestCase { $this->assertInstanceOf('OCP\AppFramework\Http\OCSResponse', $result); $this->assertSame($message, $this->invokePrivate($result, 'message')); - $this->assertSame($code, $this->invokePrivate($result, 'statuscode')); + if ($exception->getCode() === 0) { + $this->assertSame(\OCP\API::RESPOND_UNKNOWN_ERROR, $this->invokePrivate($result, 'statuscode')); + } else { + $this->assertSame($code, $this->invokePrivate($result, 'statuscode')); + } $this->assertSame($code, $result->getStatus()); } catch (\Exception $e) { $this->assertTrue($forward); @@ -161,7 +175,11 @@ class OCSMiddlewareTest extends \Test\TestCase { $this->assertInstanceOf('OCP\AppFramework\Http\OCSResponse', $result); $this->assertSame($message, $this->invokePrivate($result, 'message')); - $this->assertSame($code, $this->invokePrivate($result, 'statuscode')); + if ($exception->getCode() === 0) { + $this->assertSame(\OCP\API::RESPOND_UNKNOWN_ERROR, $this->invokePrivate($result, 'statuscode')); + } else { + $this->assertSame($code, $this->invokePrivate($result, 'statuscode')); + } $this->assertSame($code, $result->getStatus()); } catch (\Exception $e) { $this->assertTrue($forward); @@ -169,4 +187,50 @@ class OCSMiddlewareTest extends \Test\TestCase { } } + public function dataAfterController() { + $OCSController = $this->getMockBuilder('OCP\AppFramework\OCSController') + ->disableOriginalConstructor() + ->getMock(); + $controller = $this->getMockBuilder('OCP\AppFramework\Controller') + ->disableOriginalConstructor() + ->getMock(); + + return [ + [$OCSController, new Http\Response(), false], + [$OCSController, new Http\JSONResponse(), false], + [$OCSController, new Http\JSONResponse(['message' => 'foo']), false], + [$OCSController, new Http\JSONResponse(['message' => 'foo'], Http::STATUS_UNAUTHORIZED), true], + [$OCSController, new Http\JSONResponse(['message' => 'foo'], Http::STATUS_FORBIDDEN), true], + + [$controller, new Http\Response(), false], + [$controller, new Http\JSONResponse(), false], + [$controller, new Http\JSONResponse(['message' => 'foo']), false], + [$controller, new Http\JSONResponse(['message' => 'foo'], Http::STATUS_UNAUTHORIZED), false], + [$controller, new Http\JSONResponse(['message' => 'foo'], Http::STATUS_FORBIDDEN), false], + + ]; + } + + /** + * @dataProvider dataAfterController + * + * @param Controller $controller + * @param Http\Response $response + * @param bool $converted + */ + public function testAfterController($controller, $response, $converted) { + $OCSMiddleware = new OCSMiddleware($this->request); + $newResponse = $OCSMiddleware->afterController($controller, 'foo', $response); + + if ($converted === false) { + $this->assertSame($response, $newResponse); + } else { + $this->assertInstanceOf('\OCP\AppFramework\Http\OCSResponse', $newResponse); + /** @var Http\OCSResponse $newResponse */ + $this->assertSame($response->getData()['message'], $this->invokePrivate($newResponse, 'message')); + $this->assertSame(\OCP\API::RESPOND_UNAUTHORISED, $this->invokePrivate($newResponse, 'statuscode')); + $this->assertSame(Http::STATUS_UNAUTHORIZED, $newResponse->getStatus()); + } + } + } -- 2.39.5