nextcloud/tests/lib/AppFramework/Middleware/Security/SecurityMiddlewareTest.php

701 lines
20 KiB
PHP
Raw Normal View History

2013-08-17 05:16:48 -04:00
<?php
/**
* @author Bernhard Posselt <dev@bernhard-posselt.com>
* @author Lukas Reschke <lukas@owncloud.com>
2013-08-17 05:16:48 -04:00
*
* @copyright Copyright (c) 2015, ownCloud, Inc.
* @license AGPL-3.0
2013-08-17 05:16:48 -04:00
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
2013-08-17 05:16:48 -04:00
*
* This program is distributed in the hope that it will be useful,
2013-08-17 05:16:48 -04:00
* 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.
2013-08-17 05:16:48 -04:00
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
2013-08-17 05:16:48 -04:00
*
*/
2016-05-18 12:40:34 -04:00
namespace Test\AppFramework\Middleware\Security;
2013-08-17 05:16:48 -04:00
use OC\AppFramework\Http;
2013-08-17 05:16:48 -04:00
use OC\AppFramework\Http\Request;
use OC\AppFramework\Middleware\Security\Exceptions\AppNotEnabledException;
use OC\AppFramework\Middleware\Security\Exceptions\CrossSiteRequestForgeryException;
use OC\AppFramework\Middleware\Security\Exceptions\NotAdminException;
use OC\AppFramework\Middleware\Security\Exceptions\NotLoggedInException;
use OC\AppFramework\Middleware\Security\Exceptions\SecurityException;
use OC\Appframework\Middleware\Security\Exceptions\StrictCookieMissingException;
2016-05-18 12:40:34 -04:00
use OC\AppFramework\Middleware\Security\SecurityMiddleware;
use OC\AppFramework\Utility\ControllerMethodReflector;
use OC\Settings\AuthorizedGroupMapper;
use OCP\App\IAppManager;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Http\RedirectResponse;
use OCP\AppFramework\Http\TemplateResponse;
2016-09-15 06:12:30 -04:00
use OCP\IConfig;
use OCP\IL10N;
use OCP\INavigationManager;
use OCP\IRequest;
use OCP\IRequestId;
use OCP\IURLGenerator;
use OCP\IUserSession;
use Psr\Log\LoggerInterface;
use Test\AppFramework\Middleware\Security\Mock\NormalController;
use Test\AppFramework\Middleware\Security\Mock\OCSController;
use Test\AppFramework\Middleware\Security\Mock\SecurityMiddlewareController;
2013-08-17 05:16:48 -04:00
class SecurityMiddlewareTest extends \Test\TestCase {
/** @var SecurityMiddleware|\PHPUnit\Framework\MockObject\MockObject */
2013-08-17 05:16:48 -04:00
private $middleware;
/** @var SecurityMiddlewareController */
2013-08-17 05:16:48 -04:00
private $controller;
/** @var SecurityException */
2013-08-17 05:16:48 -04:00
private $secException;
/** @var SecurityException */
2013-08-17 05:16:48 -04:00
private $secAjaxException;
/** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
2013-08-17 05:16:48 -04:00
private $request;
/** @var ControllerMethodReflector */
private $reader;
/** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */
private $logger;
/** @var INavigationManager|\PHPUnit\Framework\MockObject\MockObject */
private $navigationManager;
/** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */
private $urlGenerator;
/** @var IAppManager|\PHPUnit\Framework\MockObject\MockObject */
private $appManager;
/** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */
private $l10n;
/** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
private $userSession;
/** @var AuthorizedGroupMapper|\PHPUnit\Framework\MockObject\MockObject */
private $authorizedGroupMapper;
2013-08-17 05:16:48 -04:00
protected function setUp(): void {
parent::setUp();
$this->authorizedGroupMapper = $this->createMock(AuthorizedGroupMapper::class);
$this->userSession = $this->createMock(IUserSession::class);
$this->request = $this->createMock(IRequest::class);
$this->controller = new SecurityMiddlewareController(
'test',
$this->request
);
$this->reader = new ControllerMethodReflector();
$this->logger = $this->createMock(LoggerInterface::class);
2016-09-15 06:12:30 -04:00
$this->navigationManager = $this->createMock(INavigationManager::class);
$this->urlGenerator = $this->createMock(IURLGenerator::class);
$this->l10n = $this->createMock(IL10N::class);
$this->middleware = $this->getMiddleware(true, true, false);
2013-08-17 05:16:48 -04:00
$this->secException = new SecurityException('hey', false);
$this->secAjaxException = new SecurityException('hey', true);
}
private function getMiddleware(bool $isLoggedIn, bool $isAdminUser, bool $isSubAdmin, bool $isAppEnabledForUser = true): SecurityMiddleware {
$this->appManager = $this->createMock(IAppManager::class);
$this->appManager->expects($this->any())
->method('isEnabledForUser')
->willReturn($isAppEnabledForUser);
return new SecurityMiddleware(
$this->request,
$this->reader,
$this->navigationManager,
$this->urlGenerator,
$this->logger,
'files',
$isLoggedIn,
Add public API to give developers the possibility to adjust the global CSP defaults Allows to inject something into the default content policy. This is for example useful when you're injecting Javascript code into a view belonging to another controller and cannot modify its Content-Security-Policy itself. Note that the adjustment is only applied to applications that use AppFramework controllers. To use this from your `app.php` use `\OC::$server->getContentSecurityPolicyManager()->addDefaultPolicy($policy)`, $policy has to be of type `\OCP\AppFramework\Http\ContentSecurityPolicy`. To test this add something like the following into an `app.php` of any enabled app: ``` $manager = \OC::$server->getContentSecurityPolicyManager(); $policy = new \OCP\AppFramework\Http\ContentSecurityPolicy(false); $policy->addAllowedFrameDomain('asdf'); $policy->addAllowedScriptDomain('yolo.com'); $policy->allowInlineScript(false); $manager->addDefaultPolicy($policy); $policy = new \OCP\AppFramework\Http\ContentSecurityPolicy(false); $policy->addAllowedFontDomain('yolo.com'); $manager->addDefaultPolicy($policy); $policy = new \OCP\AppFramework\Http\ContentSecurityPolicy(false); $policy->addAllowedFrameDomain('banana.com'); $manager->addDefaultPolicy($policy); ``` If you now open the files app the policy should be: ``` Content-Security-Policy:default-src 'none';script-src yolo.com 'self' 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data: blob:;font-src yolo.com 'self';connect-src 'self';media-src 'self';frame-src asdf banana.com 'self' ```
2016-01-28 08:33:02 -05:00
$isAdminUser,
$isSubAdmin,
$this->appManager,
$this->l10n,
$this->authorizedGroupMapper,
$this->userSession
);
2013-08-17 05:16:48 -04:00
}
public function dataNoCSRFRequiredPublicPage(): array {
return [
['testAnnotationNoCSRFRequiredPublicPage'],
['testAnnotationNoCSRFRequiredAttributePublicPage'],
['testAnnotationPublicPageAttributeNoCSRFRequired'],
['testAttributeNoCSRFRequiredPublicPage'],
];
}
public function dataPublicPage(): array {
return [
['testAnnotationPublicPage'],
['testAttributePublicPage'],
];
}
public function dataNoCSRFRequired(): array {
return [
['testAnnotationNoCSRFRequired'],
['testAttributeNoCSRFRequired'],
];
}
public function dataPublicPageStrictCookieRequired(): array {
return [
['testAnnotationPublicPageStrictCookieRequired'],
['testAnnotationStrictCookieRequiredAttributePublicPage'],
['testAnnotationPublicPageAttributeStrictCookiesRequired'],
['testAttributePublicPageStrictCookiesRequired'],
];
}
public function dataNoCSRFRequiredPublicPageStrictCookieRequired(): array {
return [
['testAnnotationNoCSRFRequiredPublicPageStrictCookieRequired'],
['testAttributeNoCSRFRequiredPublicPageStrictCookiesRequired'],
];
}
public function dataNoAdminRequiredNoCSRFRequired(): array {
return [
['testAnnotationNoAdminRequiredNoCSRFRequired'],
['testAttributeNoAdminRequiredNoCSRFRequired'],
];
}
public function dataNoAdminRequiredNoCSRFRequiredPublicPage(): array {
return [
['testAnnotationNoAdminRequiredNoCSRFRequiredPublicPage'],
['testAttributeNoAdminRequiredNoCSRFRequiredPublicPage'],
];
}
public function dataNoCSRFRequiredSubAdminRequired(): array {
return [
['testAnnotationNoCSRFRequiredSubAdminRequired'],
['testAnnotationNoCSRFRequiredAttributeSubAdminRequired'],
['testAnnotationSubAdminRequiredAttributeNoCSRFRequired'],
['testAttributeNoCSRFRequiredSubAdminRequired'],
];
}
2013-08-17 05:16:48 -04:00
/**
* @dataProvider dataNoCSRFRequiredPublicPage
2013-08-17 05:16:48 -04:00
*/
public function testSetNavigationEntry(string $method): void {
$this->navigationManager->expects($this->once())
->method('setActiveEntry')
->with($this->equalTo('files'));
$this->reader->reflect($this->controller, $method);
$this->middleware->beforeController($this->controller, $method);
2013-08-17 05:16:48 -04:00
}
/**
* @param string $method
* @param string $test
*/
2013-08-17 05:16:48 -04:00
private function ajaxExceptionStatus($method, $test, $status) {
$isLoggedIn = false;
$isAdminUser = false;
2013-08-17 05:16:48 -04:00
// isAdminUser requires isLoggedIn call to return true
if ($test === 'isAdminUser') {
$isLoggedIn = true;
}
$sec = $this->getMiddleware($isLoggedIn, $isAdminUser, false);
2013-08-17 05:16:48 -04:00
try {
$this->reader->reflect($this->controller, $method);
$sec->beforeController($this->controller, $method);
} catch (SecurityException $ex) {
2013-08-17 05:16:48 -04:00
$this->assertEquals($status, $ex->getCode());
}
2014-05-28 09:23:57 -04:00
// add assertion if everything should work fine otherwise phpunit will
// complain
if ($status === 0) {
$this->addToAssertionCount(1);
2014-05-28 09:23:57 -04:00
}
2013-08-17 05:16:48 -04:00
}
public function testAjaxStatusLoggedInCheck(): void {
2013-08-17 05:16:48 -04:00
$this->ajaxExceptionStatus(
'testNoAnnotationNorAttribute',
2013-08-17 05:16:48 -04:00
'isLoggedIn',
Http::STATUS_UNAUTHORIZED
);
}
/**
* @dataProvider dataNoCSRFRequired
2013-08-17 05:16:48 -04:00
*/
public function testAjaxNotAdminCheck(string $method): void {
2013-08-17 05:16:48 -04:00
$this->ajaxExceptionStatus(
$method,
2013-08-17 05:16:48 -04:00
'isAdminUser',
Http::STATUS_FORBIDDEN
);
}
/**
* @dataProvider dataPublicPage
2013-08-17 05:16:48 -04:00
*/
public function testAjaxStatusCSRFCheck(string $method): void {
2013-08-17 05:16:48 -04:00
$this->ajaxExceptionStatus(
$method,
2013-08-17 05:16:48 -04:00
'passesCSRFCheck',
Http::STATUS_PRECONDITION_FAILED
);
}
/**
* @dataProvider dataNoCSRFRequiredPublicPage
2013-08-17 05:16:48 -04:00
*/
public function testAjaxStatusAllGood(string $method): void {
2013-08-17 05:16:48 -04:00
$this->ajaxExceptionStatus(
$method,
2013-08-17 05:16:48 -04:00
'isLoggedIn',
0
);
$this->ajaxExceptionStatus(
$method,
2013-08-17 05:16:48 -04:00
'isAdminUser',
0
);
$this->ajaxExceptionStatus(
$method,
2013-08-17 05:16:48 -04:00
'passesCSRFCheck',
0
);
}
/**
* @dataProvider dataNoCSRFRequiredPublicPage
2013-08-17 05:16:48 -04:00
*/
public function testNoChecks(string $method): void {
$this->request->expects($this->never())
->method('passesCSRFCheck')
->willReturn(false);
$sec = $this->getMiddleware(false, false, false);
$this->reader->reflect($this->controller, $method);
$sec->beforeController($this->controller, $method);
2013-08-17 05:16:48 -04:00
}
/**
* @param string $method
* @param string $expects
*/
private function securityCheck($method, $expects, $shouldFail = false) {
// admin check requires login
if ($expects === 'isAdminUser') {
$isLoggedIn = true;
$isAdminUser = !$shouldFail;
} else {
$isLoggedIn = !$shouldFail;
$isAdminUser = false;
}
$sec = $this->getMiddleware($isLoggedIn, $isAdminUser, false);
2013-08-17 05:16:48 -04:00
if ($shouldFail) {
$this->expectException(SecurityException::class);
} else {
$this->addToAssertionCount(1);
2013-08-17 05:16:48 -04:00
}
$this->reader->reflect($this->controller, $method);
$sec->beforeController($this->controller, $method);
2013-08-17 05:16:48 -04:00
}
/**
* @dataProvider dataPublicPage
2013-08-17 05:16:48 -04:00
*/
public function testCsrfCheck(string $method): void {
$this->expectException(\OC\AppFramework\Middleware\Security\Exceptions\CrossSiteRequestForgeryException::class);
$this->request->expects($this->once())
2013-10-07 05:25:50 -04:00
->method('passesCSRFCheck')
->willReturn(false);
$this->request->expects($this->once())
->method('passesStrictCookieCheck')
->willReturn(true);
$this->reader->reflect($this->controller, $method);
$this->middleware->beforeController($this->controller, $method);
2013-10-07 05:25:50 -04:00
}
/**
* @dataProvider dataNoCSRFRequiredPublicPage
2013-10-07 05:25:50 -04:00
*/
public function testNoCsrfCheck(string $method) {
$this->request->expects($this->never())
2013-10-07 05:25:50 -04:00
->method('passesCSRFCheck')
->willReturn(false);
2013-10-07 05:25:50 -04:00
$this->reader->reflect($this->controller, $method);
$this->middleware->beforeController($this->controller, $method);
2013-08-17 05:16:48 -04:00
}
/**
* @dataProvider dataPublicPage
*/
public function testPassesCsrfCheck(string $method): void {
$this->request->expects($this->once())
->method('passesCSRFCheck')
->willReturn(true);
$this->request->expects($this->once())
->method('passesStrictCookieCheck')
->willReturn(true);
$this->reader->reflect($this->controller, $method);
$this->middleware->beforeController($this->controller, $method);
}
2013-08-17 05:16:48 -04:00
/**
* @dataProvider dataPublicPage
2013-08-17 05:16:48 -04:00
*/
public function testFailCsrfCheck(string $method): void {
$this->expectException(\OC\AppFramework\Middleware\Security\Exceptions\CrossSiteRequestForgeryException::class);
$this->request->expects($this->once())
2013-10-07 05:25:50 -04:00
->method('passesCSRFCheck')
->willReturn(false);
$this->request->expects($this->once())
->method('passesStrictCookieCheck')
->willReturn(true);
2013-10-07 05:25:50 -04:00
$this->reader->reflect($this->controller, $method);
$this->middleware->beforeController($this->controller, $method);
2013-08-17 05:16:48 -04:00
}
/**
* @dataProvider dataPublicPageStrictCookieRequired
*/
public function testStrictCookieRequiredCheck(string $method): void {
$this->expectException(\OC\AppFramework\Middleware\Security\Exceptions\StrictCookieMissingException::class);
$this->request->expects($this->never())
->method('passesCSRFCheck');
$this->request->expects($this->once())
->method('passesStrictCookieCheck')
->willReturn(false);
$this->reader->reflect($this->controller, $method);
$this->middleware->beforeController($this->controller, $method);
}
/**
* @dataProvider dataNoCSRFRequiredPublicPage
*/
public function testNoStrictCookieRequiredCheck(string $method): void {
$this->request->expects($this->never())
->method('passesStrictCookieCheck')
->willReturn(false);
$this->reader->reflect($this->controller, $method);
$this->middleware->beforeController($this->controller, $method);
}
/**
* @dataProvider dataNoCSRFRequiredPublicPageStrictCookieRequired
*/
public function testPassesStrictCookieRequiredCheck(string $method): void {
$this->request
->expects($this->once())
->method('passesStrictCookieCheck')
->willReturn(true);
$this->reader->reflect($this->controller, $method);
$this->middleware->beforeController($this->controller, $method);
}
2013-08-17 05:16:48 -04:00
public function dataCsrfOcsController(): array {
return [
[NormalController::class, false, false, true],
[NormalController::class, false, true, true],
[NormalController::class, true, false, true],
[NormalController::class, true, true, true],
[OCSController::class, false, false, true],
[OCSController::class, false, true, false],
[OCSController::class, true, false, false],
[OCSController::class, true, true, false],
];
}
/**
* @dataProvider dataCsrfOcsController
* @param string $controllerClass
* @param bool $hasOcsApiHeader
* @param bool $hasBearerAuth
* @param bool $exception
*/
public function testCsrfOcsController(string $controllerClass, bool $hasOcsApiHeader, bool $hasBearerAuth, bool $exception): void {
$this->request
->method('getHeader')
->willReturnCallback(function ($header) use ($hasOcsApiHeader, $hasBearerAuth) {
if ($header === 'OCS-APIREQUEST' && $hasOcsApiHeader) {
return 'true';
}
if ($header === 'Authorization' && $hasBearerAuth) {
return 'Bearer TOKEN!';
}
return '';
});
$this->request->expects($this->once())
->method('passesStrictCookieCheck')
->willReturn(true);
$controller = new $controllerClass('test', $this->request);
try {
$this->middleware->beforeController($controller, 'foo');
$this->assertFalse($exception);
} catch (CrossSiteRequestForgeryException $e) {
$this->assertTrue($exception);
}
}
2013-08-17 05:16:48 -04:00
/**
* @dataProvider dataNoAdminRequiredNoCSRFRequired
2013-08-17 05:16:48 -04:00
*/
public function testLoggedInCheck(string $method): void {
$this->securityCheck($method, 'isLoggedIn');
2013-08-17 05:16:48 -04:00
}
/**
* @dataProvider dataNoAdminRequiredNoCSRFRequired
2013-08-17 05:16:48 -04:00
*/
public function testFailLoggedInCheck(string $method): void {
$this->securityCheck($method, 'isLoggedIn', true);
2013-08-17 05:16:48 -04:00
}
/**
* @dataProvider dataNoCSRFRequired
2013-08-17 05:16:48 -04:00
*/
public function testIsAdminCheck(string $method): void {
$this->securityCheck($method, 'isAdminUser');
2013-08-17 05:16:48 -04:00
}
/**
* @dataProvider dataNoCSRFRequiredSubAdminRequired
*/
public function testIsNotSubAdminCheck(string $method): void {
$this->reader->reflect($this->controller, $method);
$sec = $this->getMiddleware(true, false, false);
$this->expectException(SecurityException::class);
$sec->beforeController($this->controller, $method);
}
/**
* @dataProvider dataNoCSRFRequiredSubAdminRequired
*/
public function testIsSubAdminCheck(string $method): void {
$this->reader->reflect($this->controller, $method);
$sec = $this->getMiddleware(true, false, true);
$sec->beforeController($this->controller, $method);
$this->addToAssertionCount(1);
}
/**
* @dataProvider dataNoCSRFRequiredSubAdminRequired
*/
public function testIsSubAdminAndAdminCheck(string $method): void {
$this->reader->reflect($this->controller, $method);
$sec = $this->getMiddleware(true, true, true);
$sec->beforeController($this->controller, $method);
$this->addToAssertionCount(1);
}
2013-08-17 05:16:48 -04:00
/**
* @dataProvider dataNoCSRFRequired
2013-08-17 05:16:48 -04:00
*/
public function testFailIsAdminCheck(string $method): void {
$this->securityCheck($method, 'isAdminUser', true);
}
/**
* @dataProvider dataNoAdminRequiredNoCSRFRequiredPublicPage
*/
public function testRestrictedAppLoggedInPublicPage(string $method): void {
$middleware = $this->getMiddleware(true, false, false);
$this->reader->reflect($this->controller, $method);
$this->appManager->method('getAppPath')
->with('files')
->willReturn('foo');
$this->appManager->method('isEnabledForUser')
->with('files')
->willReturn(false);
$middleware->beforeController($this->controller, $method);
$this->addToAssertionCount(1);
}
/**
* @dataProvider dataNoAdminRequiredNoCSRFRequiredPublicPage
*/
public function testRestrictedAppNotLoggedInPublicPage(string $method): void {
$middleware = $this->getMiddleware(false, false, false);
$this->reader->reflect($this->controller, $method);
$this->appManager->method('getAppPath')
->with('files')
->willReturn('foo');
$this->appManager->method('isEnabledForUser')
->with('files')
->willReturn(false);
$middleware->beforeController($this->controller, $method);
$this->addToAssertionCount(1);
}
/**
* @dataProvider dataNoAdminRequiredNoCSRFRequired
*/
public function testRestrictedAppLoggedIn(string $method): void {
$middleware = $this->getMiddleware(true, false, false, false);
$this->reader->reflect($this->controller, $method);
$this->appManager->method('getAppPath')
->with('files')
->willReturn('foo');
$this->expectException(AppNotEnabledException::class);
$middleware->beforeController($this->controller, $method);
2013-08-17 05:16:48 -04:00
}
public function testAfterExceptionNotCaughtThrowsItAgain() {
2013-08-17 05:16:48 -04:00
$ex = new \Exception();
$this->expectException(\Exception::class);
2013-08-17 05:16:48 -04:00
$this->middleware->afterException($this->controller, 'test', $ex);
}
public function testAfterExceptionReturnsRedirectForNotLoggedInUser() {
2013-08-17 05:16:48 -04:00
$this->request = new Request(
[
'server' =>
[
'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'REQUEST_URI' => 'nextcloud/index.php/apps/specialapp'
]
],
$this->createMock(IRequestId::class),
2016-09-15 06:12:30 -04:00
$this->createMock(IConfig::class)
);
$this->middleware = $this->getMiddleware(false, false, false);
$this->urlGenerator
->expects($this->once())
->method('linkToRoute')
->with(
'core.login.showLoginForm',
[
'redirect_url' => 'nextcloud/index.php/apps/specialapp',
]
)
->willReturn('http://localhost/nextcloud/index.php/login?redirect_url=nextcloud/index.php/apps/specialapp');
$this->logger
->expects($this->once())
->method('debug');
$response = $this->middleware->afterException(
$this->controller,
'test',
new NotLoggedInException()
);
$expected = new RedirectResponse('http://localhost/nextcloud/index.php/login?redirect_url=nextcloud/index.php/apps/specialapp');
$this->assertEquals($expected, $response);
}
public function testAfterExceptionRedirectsToWebRootAfterStrictCookieFail() {
$this->request = new Request(
[
'server' => [
'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'REQUEST_URI' => 'nextcloud/index.php/apps/specialapp',
],
],
$this->createMock(IRequestId::class),
2016-09-15 06:12:30 -04:00
$this->createMock(IConfig::class)
);
$this->middleware = $this->getMiddleware(false, false, false);
$response = $this->middleware->afterException(
$this->controller,
'test',
new StrictCookieMissingException()
);
$expected = new RedirectResponse(\OC::$WEBROOT . '/');
$this->assertEquals($expected, $response);
}
/**
* @return array
*/
public function exceptionProvider() {
return [
[
new AppNotEnabledException(),
],
[
new CrossSiteRequestForgeryException(),
],
[
new NotAdminException(''),
],
];
}
/**
* @dataProvider exceptionProvider
* @param SecurityException $exception
*/
public function testAfterExceptionReturnsTemplateResponse(SecurityException $exception) {
$this->request = new Request(
[
'server' =>
[
'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'REQUEST_URI' => 'nextcloud/index.php/apps/specialapp'
]
],
$this->createMock(IRequestId::class),
2016-09-15 06:12:30 -04:00
$this->createMock(IConfig::class)
);
$this->middleware = $this->getMiddleware(false, false, false);
$this->logger
->expects($this->once())
->method('debug');
$response = $this->middleware->afterException(
$this->controller,
'test',
$exception
);
$expected = new TemplateResponse('core', '403', ['message' => $exception->getMessage()], 'guest');
$expected->setStatus($exception->getCode());
$this->assertEquals($expected, $response);
2013-08-17 05:16:48 -04:00
}
public function testAfterAjaxExceptionReturnsJSONError() {
2013-08-17 05:16:48 -04:00
$response = $this->middleware->afterException($this->controller, 'test',
$this->secAjaxException);
2013-08-17 05:16:48 -04:00
$this->assertTrue($response instanceof JSONResponse);
}
}