diff options
author | Olivier Paroz <github@oparoz.com> | 2015-08-20 18:47:49 +0300 |
---|---|---|
committer | Olivier Paroz <github@oparoz.com> | 2015-08-20 18:47:49 +0300 |
commit | 7354cf79f1c5cef9a4beee9990358d3c45de98f7 (patch) | |
tree | b0b4be530e51fe854173beed16dbc37ca5c83718 /tests/unit | |
parent | 7b62b80b3c9da17cf697209d25ff5c05ce56b689 (diff) |
Some EnvCheckMiddleware response tests
Diffstat (limited to 'tests/unit')
-rw-r--r-- | tests/unit/middleware/EnvCheckMiddlewareTest.php | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/tests/unit/middleware/EnvCheckMiddlewareTest.php b/tests/unit/middleware/EnvCheckMiddlewareTest.php index 238c902b..51610636 100644 --- a/tests/unit/middleware/EnvCheckMiddlewareTest.php +++ b/tests/unit/middleware/EnvCheckMiddlewareTest.php @@ -24,6 +24,9 @@ use OCP\Share; use OCP\AppFramework\Controller; use OCP\AppFramework\Http; use OCP\AppFramework\Utility\IControllerMethodReflector; +use OCP\AppFramework\Http\JSONResponse; +use OCP\AppFramework\Http\RedirectResponse; +use OCP\AppFramework\Http\TemplateResponse; use OCA\Gallery\Environment\Environment; @@ -336,6 +339,45 @@ class EnvCheckMiddlewareTest extends \Test\TestCase { self::invokePrivate($this->middleware, 'noTokenFound'); } + public function testAfterExceptionWithCheckExceptionAndHtmlAcceptAnd401Code() { + $message = 'fail'; + $code = Http::STATUS_UNAUTHORIZED; + $exception = new CheckException($message, $code); + + $template = $this->mockHtml401Response(); + + $response = + $this->middleware->afterException($this->controller, 'checkSession', $exception); + + $this->assertEquals($template, $response); + } + + public function testAfterExceptionWithCheckExceptionAndHtmlAcceptAnd404Code() { + $message = 'fail'; + $code = Http::STATUS_NOT_FOUND; + $exception = new CheckException($message, $code); + + $template = $this->mockHtml404Response($message, $code); + + $response = + $this->middleware->afterException($this->controller, 'authenticate', $exception); + + $this->assertEquals($template, $response); + } + + public function testAfterExceptionWithCheckExceptionAndJsonAccept() { + $message = 'fail'; + $code = Http::STATUS_NOT_FOUND; + $exception = new CheckException($message, $code); + + $template = $this->mockJsonResponse($message, $code); + + $response = + $this->middleware->afterException($this->controller, 'checkLinkItemIsValid', $exception); + + $this->assertEquals($template, $response); + } + /** * Mocks ISession->exists('public_link_authenticated') * @@ -376,4 +418,70 @@ class EnvCheckMiddlewareTest extends \Test\TestCase { ->willReturn($valid); } + private function mockHtml401Response() { + $this->mockAcceptHeader('html'); + $this->mockGetParams(); + + return new TemplateResponse($this->appName, 'authenticate', [], 'guest'); + } + + private function mockHtml404Response($message, $code) { + $this->mockAcceptHeader('html'); + $redirectUrl = 'http://newroute.com'; + $this->mockUrlToErrorPage($message, $code, $redirectUrl); + + return new RedirectResponse($redirectUrl); + } + + private function mockJsonResponse($message, $code) { + $this->mockAcceptHeader('json'); + $jsonData = [ + 'message' => $message, + 'success' => false + ]; + + return new JSONResponse($jsonData, $code); + } + + /** + * Mocks IRequest->getHeader('Accept') + * + * @param string $type + */ + private function mockAcceptHeader($type) { + $this->request->expects($this->once()) + ->method('getHeader') + ->with('Accept') + ->willReturn($type); + } + + /** + * Mocks IRequest->getParams() + */ + private function mockGetParams() { + $this->request->expects($this->once()) + ->method('getParams') + ->willReturn([]); + } + + /** + * Mocks IURLGenerator->linkToRoute() + * + * @param string $message + * @param int $code + * @param string $url + */ + private function mockUrlToErrorPage($message, $code, $url) { + $this->urlGenerator->expects($this->once()) + ->method('linkToRoute') + ->with( + $this->appName . '.page.error_page', + [ + 'message' => $message, + 'code' => $code + ] + ) + ->willReturn($url); + } + } |