Merge pull request #5877 from nextcloud/typehint_middleware

Prop argument type for Middleware
This commit is contained in:
Morris Jobke 2017-08-01 14:28:16 +02:00 committed by GitHub
commit 6010c4f267
18 changed files with 140 additions and 120 deletions

View File

@ -26,6 +26,7 @@ namespace OCA\Federation\Middleware;
use OC\HintException;
use OCA\Federation\Controller\SettingsController;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Middleware;
@ -52,12 +53,13 @@ class AddServerMiddleware extends Middleware {
/**
* Log error message and return a response which can be displayed to the user
*
* @param \OCP\AppFramework\Controller $controller
* @param Controller $controller
* @param string $methodName
* @param \Exception $exception
* @return JSONResponse
* @throws \Exception
*/
public function afterException($controller, $methodName, \Exception $exception) {
public function afterException(Controller $controller, $methodName, \Exception $exception) {
if (($controller instanceof SettingsController) === false) {
throw $exception;
}

View File

@ -3,6 +3,7 @@
namespace OCA\Files_Sharing\Middleware;
use OCA\Files_Sharing\Controller\ShareAPIController;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\Response;
use OCP\AppFramework\Middleware;
use OCP\AppFramework\OCS\OCSNotFoundException;
@ -22,12 +23,12 @@ class OCSShareAPIMiddleware extends Middleware {
}
/**
* @param \OCP\AppFramework\Controller $controller
* @param Controller $controller
* @param string $methodName
*
* @throws OCSNotFoundException
*/
public function beforeController($controller, $methodName) {
public function beforeController(Controller $controller, $methodName) {
if ($controller instanceof ShareAPIController) {
if (!$this->shareManager->shareApiEnabled()) {
throw new OCSNotFoundException($this->l->t('Share API is disabled'));
@ -36,12 +37,12 @@ class OCSShareAPIMiddleware extends Middleware {
}
/**
* @param \OCP\AppFramework\Controller $controller
* @param Controller $controller
* @param string $methodName
* @param Response $response
* @return Response
*/
public function afterController($controller, $methodName, Response $response) {
public function afterController(Controller $controller, $methodName, Response $response) {
if ($controller instanceof ShareAPIController) {
/** @var ShareAPIController $controller */
$controller->cleanup();

View File

@ -28,6 +28,7 @@ namespace OCA\Files_Sharing\Middleware;
use OCA\Files_Sharing\Controller\ExternalSharesController;
use OCA\Files_Sharing\Controller\ShareController;
use OCP\App\IAppManager;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\NotFoundResponse;
use OCP\AppFramework\Middleware;
use OCP\Files\NotFoundException;
@ -85,13 +86,13 @@ class SharingCheckMiddleware extends Middleware {
/**
* Check if sharing is enabled before the controllers is executed
*
* @param \OCP\AppFramework\Controller $controller
* @param Controller $controller
* @param string $methodName
* @throws NotFoundException
* @throws S2SException
* @throws ShareNotFound
*/
public function beforeController($controller, $methodName) {
public function beforeController(Controller $controller, $methodName) {
if(!$this->isSharingEnabled()) {
throw new NotFoundException('Sharing is disabled.');
}
@ -112,13 +113,13 @@ class SharingCheckMiddleware extends Middleware {
/**
* Return 404 page in case of a not found exception
*
* @param \OCP\AppFramework\Controller $controller
* @param Controller $controller
* @param string $methodName
* @param \Exception $exception
* @return NotFoundResponse
* @throws \Exception
*/
public function afterException($controller, $methodName, \Exception $exception) {
public function afterException(Controller $controller, $methodName, \Exception $exception) {
if(is_a($exception, '\OCP\Files\NotFoundException')) {
return new NotFoundResponse();
}

View File

@ -3,6 +3,7 @@
namespace OCA\Provisioning_API\Middleware;
use OCA\Provisioning_API\Middleware\Exceptions\NotSubAdminException;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\Response;
use OCP\AppFramework\Middleware;
use OCP\AppFramework\OCS\OCSException;
@ -36,29 +37,29 @@ class ProvisioningApiMiddleware extends Middleware {
}
/**
* @param \OCP\AppFramework\Controller $controller
* @param Controller $controller
* @param string $methodName
*
* @throws NotSubAdminException
*/
public function beforeController($controller, $methodName) {
public function beforeController(Controller $controller, $methodName) {
if (!$this->isAdmin && !$this->reflector->hasAnnotation('NoSubAdminRequired') && !$this->isSubAdmin) {
throw new NotSubAdminException();
}
}
/**
* @param \OCP\AppFramework\Controller $controller
* @param Controller $controller
* @param string $methodName
* @param \Exception $exception
* @throws \Exception
* @return Response
*/
public function afterException($controller, $methodName, \Exception $exception) {
public function afterException(Controller $controller, $methodName, \Exception $exception) {
if ($exception instanceof NotSubAdminException) {
throw new OCSException($exception->getMessage(), \OCP\API::RESPOND_UNAUTHORISED);
}
throw $exception;
}
}
}

View File

@ -79,7 +79,7 @@ class TwoFactorMiddleware extends Middleware {
* @param Controller $controller
* @param string $methodName
*/
public function beforeController($controller, $methodName) {
public function beforeController(Controller $controller, $methodName) {
if ($this->reflector->hasAnnotation('PublicPage')) {
// Don't block public pages
return;
@ -104,7 +104,7 @@ class TwoFactorMiddleware extends Middleware {
// TODO: dont check/enforce 2FA if a auth token is used
}
private function checkTwoFactor($controller, $methodName, IUser $user) {
private function checkTwoFactor(Controller $controller, $methodName, IUser $user) {
// If two-factor auth is in progress disallow access to any controllers
// defined within "LoginController".
$needsSecondFactor = $this->twoFactorManager->needsSecondFactor($user);
@ -122,7 +122,7 @@ class TwoFactorMiddleware extends Middleware {
}
}
public function afterException($controller, $methodName, Exception $exception) {
public function afterException(Controller $controller, $methodName, Exception $exception) {
if ($exception instanceof TwoFactorAuthRequiredException) {
$params = [];
if (isset($this->request->server['REQUEST_URI'])) {

View File

@ -52,10 +52,10 @@ class OCSMiddleware extends Middleware {
}
/**
* @param \OCP\AppFramework\Controller $controller
* @param Controller $controller
* @param string $methodName
*/
public function beforeController($controller, $methodName) {
public function beforeController(Controller $controller, $methodName) {
if ($controller instanceof OCSController) {
if (substr_compare($this->request->getScriptName(), '/ocs/v2.php', -strlen('/ocs/v2.php')) === 0) {
$this->ocsVersion = 2;
@ -67,13 +67,13 @@ class OCSMiddleware extends Middleware {
}
/**
* @param \OCP\AppFramework\Controller $controller
* @param Controller $controller
* @param string $methodName
* @param \Exception $exception
* @throws \Exception
* @return BaseResponse
*/
public function afterException($controller, $methodName, \Exception $exception) {
public function afterException(Controller $controller, $methodName, \Exception $exception) {
if ($controller instanceof OCSController && $exception instanceof OCSException) {
$code = $exception->getCode();
if ($code === 0) {
@ -87,12 +87,12 @@ class OCSMiddleware extends Middleware {
}
/**
* @param \OCP\AppFramework\Controller $controller
* @param Controller $controller
* @param string $methodName
* @param Response $response
* @return \OCP\AppFramework\Http\Response
*/
public function afterController($controller, $methodName, Response $response) {
public function afterController(Controller $controller, $methodName, Response $response) {
/*
* If a different middleware has detected that a request unauthorized or forbidden
* we need to catch the response and convert it to a proper OCS response.
@ -120,7 +120,7 @@ class OCSMiddleware extends Middleware {
* @param string $message
* @return V1Response|V2Response
*/
private function buildNewResponse($controller, $code, $message) {
private function buildNewResponse(Controller $controller, $code, $message) {
$format = $this->getFormat($controller);
$data = new DataResponse();
@ -135,10 +135,10 @@ class OCSMiddleware extends Middleware {
}
/**
* @param \OCP\AppFramework\Controller $controller
* @param Controller $controller
* @return string
*/
private function getFormat($controller) {
private function getFormat(Controller $controller) {
// get format from the url format or request format parameter
$format = $this->request->getParam('format');

View File

@ -23,6 +23,7 @@ namespace OC\AppFramework\Middleware\Security;
use OC\AppFramework\Utility\ControllerMethodReflector;
use OC\Security\Bruteforce\Throttler;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\Response;
use OCP\AppFramework\Middleware;
use OCP\IRequest;
@ -58,7 +59,7 @@ class BruteForceMiddleware extends Middleware {
/**
* {@inheritDoc}
*/
public function beforeController($controller, $methodName) {
public function beforeController(Controller $controller, $methodName) {
parent::beforeController($controller, $methodName);
if($this->reflector->hasAnnotation('BruteForceProtection')) {
@ -70,7 +71,7 @@ class BruteForceMiddleware extends Middleware {
/**
* {@inheritDoc}
*/
public function afterController($controller, $methodName, Response $response) {
public function afterController(Controller $controller, $methodName, Response $response) {
if($this->reflector->hasAnnotation('BruteForceProtection') && $response->isThrottled()) {
$action = $this->reflector->getAnnotationParameter('BruteForceProtection', 'action');
$ip = $this->request->getRemoteAddress();

View File

@ -80,7 +80,7 @@ class CORSMiddleware extends Middleware {
* @throws SecurityException
* @since 6.0.0
*/
public function beforeController($controller, $methodName){
public function beforeController(Controller $controller, $methodName){
// ensure that @CORS annotated API routes are not used in conjunction
// with session authentication since this enables CSRF attack vectors
if ($this->reflector->hasAnnotation('CORS') &&
@ -110,7 +110,7 @@ class CORSMiddleware extends Middleware {
* @return Response a Response object
* @throws SecurityException
*/
public function afterController($controller, $methodName, Response $response){
public function afterController(Controller $controller, $methodName, Response $response){
// only react if its a CORS request and if the request sends origin and
if(isset($this->request->server['HTTP_ORIGIN']) &&
@ -143,7 +143,7 @@ class CORSMiddleware extends Middleware {
* @throws \Exception the passed in exception if it can't handle it
* @return Response a Response object or null in case that the exception could not be handled
*/
public function afterException($controller, $methodName, \Exception $exception){
public function afterException(Controller $controller, $methodName, \Exception $exception){
if($exception instanceof SecurityException){
$response = new JSONResponse(['message' => $exception->getMessage()]);
if($exception->getCode() !== 0) {

View File

@ -24,6 +24,7 @@ namespace OC\AppFramework\Middleware\Security;
use OC\AppFramework\Utility\ControllerMethodReflector;
use OC\Security\RateLimiting\Exception\RateLimitExceededException;
use OC\Security\RateLimiting\Limiter;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Middleware;
@ -76,7 +77,7 @@ class RateLimitingMiddleware extends Middleware {
* {@inheritDoc}
* @throws RateLimitExceededException
*/
public function beforeController($controller, $methodName) {
public function beforeController(Controller $controller, $methodName) {
parent::beforeController($controller, $methodName);
$anonLimit = $this->reflector->getAnnotationParameter('AnonRateThrottle', 'limit');
@ -104,7 +105,7 @@ class RateLimitingMiddleware extends Middleware {
/**
* {@inheritDoc}
*/
public function afterException($controller, $methodName, \Exception $exception) {
public function afterException(Controller $controller, $methodName, \Exception $exception) {
if($exception instanceof RateLimitExceededException) {
if (stripos($this->request->getHeader('Accept'),'html') === false) {
$response = new JSONResponse(

View File

@ -136,7 +136,7 @@ class SecurityMiddleware extends Middleware {
* @param string $methodName the name of the method
* @throws SecurityException when a security check fails
*/
public function beforeController($controller, $methodName) {
public function beforeController(Controller $controller, $methodName) {
// this will set the current navigation entry of the app, use this only
// for normal HTML requests and not for AJAX requests
@ -205,7 +205,7 @@ class SecurityMiddleware extends Middleware {
* @param Response $response
* @return Response
*/
public function afterController($controller, $methodName, Response $response) {
public function afterController(Controller $controller, $methodName, Response $response) {
$policy = !is_null($response->getContentSecurityPolicy()) ? $response->getContentSecurityPolicy() : new ContentSecurityPolicy();
if (get_class($policy) === EmptyContentSecurityPolicy::class) {
@ -234,7 +234,7 @@ class SecurityMiddleware extends Middleware {
* @throws \Exception the passed in exception if it can't handle it
* @return Response a Response object or null in case that the exception could not be handled
*/
public function afterException($controller, $methodName, \Exception $exception) {
public function afterException(Controller $controller, $methodName, \Exception $exception) {
if($exception instanceof SecurityException) {
if($exception instanceof StrictCookieMissingException) {
return new RedirectResponse(\OC::$WEBROOT);

View File

@ -24,6 +24,7 @@
namespace OC\AppFramework\Middleware;
use OC\AppFramework\Utility\ControllerMethodReflector;
use OCP\AppFramework\Controller;
use OCP\IRequest;
use OCP\AppFramework\Http\Response;
use OCP\AppFramework\Middleware;
@ -55,10 +56,10 @@ class SessionMiddleware extends Middleware {
}
/**
* @param \OCP\AppFramework\Controller $controller
* @param Controller $controller
* @param string $methodName
*/
public function beforeController($controller, $methodName) {
public function beforeController(Controller $controller, $methodName) {
$useSession = $this->reflector->hasAnnotation('UseSession');
if (!$useSession) {
$this->session->close();
@ -66,12 +67,12 @@ class SessionMiddleware extends Middleware {
}
/**
* @param \OCP\AppFramework\Controller $controller
* @param Controller $controller
* @param string $methodName
* @param Response $response
* @return Response
*/
public function afterController($controller, $methodName, Response $response){
public function afterController(Controller $controller, $methodName, Response $response){
$useSession = $this->reflector->hasAnnotation('UseSession');
if ($useSession) {
$this->session->close();

View File

@ -52,7 +52,7 @@ abstract class Middleware {
* the controller
* @since 6.0.0
*/
public function beforeController($controller, $methodName){
public function beforeController(Controller $controller, $methodName){
}
@ -72,7 +72,7 @@ abstract class Middleware {
* @return Response a Response object in case that the exception was handled
* @since 6.0.0
*/
public function afterException($controller, $methodName, \Exception $exception){
public function afterException(Controller $controller, $methodName, \Exception $exception){
throw $exception;
}
@ -88,7 +88,7 @@ abstract class Middleware {
* @return Response a Response object
* @since 6.0.0
*/
public function afterController($controller, $methodName, Response $response){
public function afterController(Controller $controller, $methodName, Response $response){
return $response;
}
@ -104,7 +104,7 @@ abstract class Middleware {
* @return string the output that should be printed
* @since 6.0.0
*/
public function beforeOutput($controller, $methodName, $output){
public function beforeOutput(Controller $controller, $methodName, $output){
return $output;
}

View File

@ -27,6 +27,7 @@ namespace OC\Settings\Middleware;
use OC\AppFramework\Http;
use OC\AppFramework\Middleware\Security\Exceptions\NotAdminException;
use OC\AppFramework\Utility\ControllerMethodReflector;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Middleware;
@ -54,11 +55,11 @@ class SubadminMiddleware extends Middleware {
/**
* Check if sharing is enabled before the controllers is executed
* @param \OCP\AppFramework\Controller $controller
* @param Controller $controller
* @param string $methodName
* @throws \Exception
*/
public function beforeController($controller, $methodName) {
public function beforeController(Controller $controller, $methodName) {
if(!$this->reflector->hasAnnotation('NoSubadminRequired')) {
if(!$this->isSubAdmin) {
throw new NotAdminException('Logged in user must be a subadmin');
@ -68,13 +69,13 @@ class SubadminMiddleware extends Middleware {
/**
* Return 403 page in case of an exception
* @param \OCP\AppFramework\Controller $controller
* @param Controller $controller
* @param string $methodName
* @param \Exception $exception
* @return TemplateResponse
* @throws \Exception
*/
public function afterException($controller, $methodName, \Exception $exception) {
public function afterException(Controller $controller, $methodName, \Exception $exception) {
if($exception instanceof NotAdminException) {
$response = new TemplateResponse('core', '403', array(), 'guest');
$response->setStatus(Http::STATUS_FORBIDDEN);

View File

@ -24,6 +24,7 @@ namespace Test\Core\Middleware;
use OC\Core\Middleware\TwoFactorMiddleware;
use OC\AppFramework\Http\Request;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Utility\IControllerMethodReflector;
use OCP\IConfig;
use OCP\ISession;
@ -44,6 +45,9 @@ class TwoFactorMiddlewareTest extends TestCase {
/** @var TwoFactorMiddleware */
private $middleware;
/** @var Controller */
private $controller;
protected function setUp() {
parent::setUp();
@ -67,6 +71,7 @@ class TwoFactorMiddlewareTest extends TestCase {
);
$this->middleware = new TwoFactorMiddleware($this->twoFactorManager, $this->userSession, $this->session, $this->urlGenerator, $this->reflector, $this->request);
$this->controller = $this->createMock(Controller::class);
}
public function testBeforeControllerNotLoggedIn() {
@ -81,7 +86,7 @@ class TwoFactorMiddlewareTest extends TestCase {
$this->userSession->expects($this->never())
->method('getUser');
$this->middleware->beforeController(null, 'index');
$this->middleware->beforeController($this->controller, 'index');
}
public function testBeforeControllerPublicPage() {
@ -92,7 +97,7 @@ class TwoFactorMiddlewareTest extends TestCase {
$this->userSession->expects($this->never())
->method('isLoggedIn');
$this->middleware->beforeController(null, 'create');
$this->middleware->beforeController($this->controller, 'create');
}
public function testBeforeControllerNoTwoFactorCheckNeeded() {
@ -113,7 +118,7 @@ class TwoFactorMiddlewareTest extends TestCase {
->with($user)
->will($this->returnValue(false));
$this->middleware->beforeController(null, 'index');
$this->middleware->beforeController($this->controller, 'index');
}
/**
@ -141,7 +146,7 @@ class TwoFactorMiddlewareTest extends TestCase {
->with($user)
->will($this->returnValue(true));
$this->middleware->beforeController(null, 'index');
$this->middleware->beforeController($this->controller, 'index');
}
/**
@ -184,7 +189,7 @@ class TwoFactorMiddlewareTest extends TestCase {
->will($this->returnValue('test/url'));
$expected = new \OCP\AppFramework\Http\RedirectResponse('test/url');
$this->assertEquals($expected, $this->middleware->afterException(null, 'index', $ex));
$this->assertEquals($expected, $this->middleware->afterException($this->controller, 'index', $ex));
}
public function testAfterException() {
@ -196,7 +201,7 @@ class TwoFactorMiddlewareTest extends TestCase {
->will($this->returnValue('redirect/url'));
$expected = new \OCP\AppFramework\Http\RedirectResponse('redirect/url');
$this->assertEquals($expected, $this->middleware->afterException(null, 'index', $ex));
$this->assertEquals($expected, $this->middleware->afterException($this->controller, 'index', $ex));
}
}

View File

@ -26,6 +26,7 @@ namespace Test\AppFramework\Middleware;
use OC\AppFramework\Http\Request;
use OC\AppFramework\Middleware\MiddlewareDispatcher;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Middleware;
use OCP\AppFramework\Http\Response;
@ -61,7 +62,7 @@ class TestMiddleware extends Middleware {
$this->beforeControllerThrowsEx = $beforeControllerThrowsEx;
}
public function beforeController($controller, $methodName){
public function beforeController(Controller $controller, $methodName){
self::$beforeControllerCalled++;
$this->beforeControllerOrder = self::$beforeControllerCalled;
$this->controller = $controller;
@ -71,7 +72,7 @@ class TestMiddleware extends Middleware {
}
}
public function afterException($controller, $methodName, \Exception $exception){
public function afterException(Controller $controller, $methodName, \Exception $exception){
self::$afterExceptionCalled++;
$this->afterExceptionOrder = self::$afterExceptionCalled;
$this->controller = $controller;
@ -80,7 +81,7 @@ class TestMiddleware extends Middleware {
parent::afterException($controller, $methodName, $exception);
}
public function afterController($controller, $methodName, Response $response){
public function afterController(Controller $controller, $methodName, Response $response){
self::$afterControllerCalled++;
$this->afterControllerOrder = self::$afterControllerCalled;
$this->controller = $controller;
@ -89,7 +90,7 @@ class TestMiddleware extends Middleware {
return parent::afterController($controller, $methodName, $response);
}
public function beforeOutput($controller, $methodName, $output){
public function beforeOutput(Controller $controller, $methodName, $output){
self::$beforeOutputCalled++;
$this->beforeOutputOrder = self::$beforeOutputCalled;
$this->controller = $controller;

View File

@ -17,26 +17,30 @@ use OC\AppFramework\Middleware\Security\CORSMiddleware;
use OC\AppFramework\Utility\ControllerMethodReflector;
use OC\AppFramework\Middleware\Security\Exceptions\SecurityException;
use OC\Security\Bruteforce\Throttler;
use OC\User\Session;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Http\Response;
use OCP\IConfig;
use OCP\Security\ISecureRandom;
class CORSMiddlewareTest extends \Test\TestCase {
/** @var ControllerMethodReflector */
private $reflector;
/** @var Session|\PHPUnit_Framework_MockObject_MockObject */
private $session;
/** @var Throttler */
private $throttler;
/** @var Controller */
private $controller;
protected function setUp() {
parent::setUp();
$this->reflector = new ControllerMethodReflector();
$this->session = $this->getMockBuilder('\OC\User\Session')
->disableOriginalConstructor()
->getMock();
$this->throttler = $this->getMockBuilder('\OC\Security\Bruteforce\Throttler')
->disableOriginalConstructor()
->getMock();
$this->session = $this->createMock(Session::class);
$this->throttler = $this->createMock(Throttler::class);
$this->controller = $this->createMock(Controller::class);
}
/**
@ -49,13 +53,13 @@ class CORSMiddlewareTest extends \Test\TestCase {
'HTTP_ORIGIN' => 'test'
]
],
$this->getMockBuilder('\OCP\Security\ISecureRandom')->getMock(),
$this->getMockBuilder('\OCP\IConfig')->getMock()
$this->createMock(ISecureRandom::class),
$this->createMock(IConfig::class)
);
$this->reflector->reflect($this, __FUNCTION__);
$middleware = new CORSMiddleware($request, $this->reflector, $this->session, $this->throttler);
$response = $middleware->afterController($this, __FUNCTION__, new Response());
$response = $middleware->afterController($this->controller, __FUNCTION__, new Response());
$headers = $response->getHeaders();
$this->assertEquals('test', $headers['Access-Control-Allow-Origin']);
}
@ -68,12 +72,12 @@ class CORSMiddlewareTest extends \Test\TestCase {
'HTTP_ORIGIN' => 'test'
]
],
$this->getMockBuilder('\OCP\Security\ISecureRandom')->getMock(),
$this->getMockBuilder('\OCP\IConfig')->getMock()
$this->createMock(ISecureRandom::class),
$this->createMock(IConfig::class)
);
$middleware = new CORSMiddleware($request, $this->reflector, $this->session, $this->throttler);
$response = $middleware->afterController($this, __FUNCTION__, new Response());
$response = $middleware->afterController($this->controller, __FUNCTION__, new Response());
$headers = $response->getHeaders();
$this->assertFalse(array_key_exists('Access-Control-Allow-Origin', $headers));
}
@ -85,13 +89,13 @@ class CORSMiddlewareTest extends \Test\TestCase {
public function testNoOriginHeaderNoCORSHEADER() {
$request = new Request(
[],
$this->getMockBuilder('\OCP\Security\ISecureRandom')->getMock(),
$this->getMockBuilder('\OCP\IConfig')->getMock()
$this->createMock(ISecureRandom::class),
$this->createMock(IConfig::class)
);
$this->reflector->reflect($this, __FUNCTION__);
$middleware = new CORSMiddleware($request, $this->reflector, $this->session, $this->throttler);
$response = $middleware->afterController($this, __FUNCTION__, new Response());
$response = $middleware->afterController($this->controller, __FUNCTION__, new Response());
$headers = $response->getHeaders();
$this->assertFalse(array_key_exists('Access-Control-Allow-Origin', $headers));
}
@ -108,15 +112,15 @@ class CORSMiddlewareTest extends \Test\TestCase {
'HTTP_ORIGIN' => 'test'
]
],
$this->getMockBuilder('\OCP\Security\ISecureRandom')->getMock(),
$this->getMockBuilder('\OCP\IConfig')->getMock()
$this->createMock(ISecureRandom::class),
$this->createMock(IConfig::class)
);
$this->reflector->reflect($this, __FUNCTION__);
$middleware = new CORSMiddleware($request, $this->reflector, $this->session, $this->throttler);
$response = new Response();
$response->addHeader('AcCess-control-Allow-Credentials ', 'TRUE');
$middleware->afterController($this, __FUNCTION__, $response);
$middleware->afterController($this->controller, __FUNCTION__, $response);
}
/**
@ -126,8 +130,8 @@ class CORSMiddlewareTest extends \Test\TestCase {
public function testNoCORSShouldAllowCookieAuth() {
$request = new Request(
[],
$this->getMockBuilder('\OCP\Security\ISecureRandom')->getMock(),
$this->getMockBuilder('\OCP\IConfig')->getMock()
$this->createMock(ISecureRandom::class),
$this->createMock(IConfig::class)
);
$this->reflector->reflect($this, __FUNCTION__);
$middleware = new CORSMiddleware($request, $this->reflector, $this->session, $this->throttler);
@ -139,7 +143,7 @@ class CORSMiddlewareTest extends \Test\TestCase {
->will($this->returnValue(true));
$this->reflector->reflect($this, __FUNCTION__);
$middleware->beforeController($this, __FUNCTION__, new Response());
$middleware->beforeController($this->controller, __FUNCTION__);
}
/**
@ -151,8 +155,8 @@ class CORSMiddlewareTest extends \Test\TestCase {
'PHP_AUTH_USER' => 'user',
'PHP_AUTH_PW' => 'pass'
]],
$this->getMockBuilder('\OCP\Security\ISecureRandom')->getMock(),
$this->getMockBuilder('\OCP\IConfig')->getMock()
$this->createMock(ISecureRandom::class),
$this->createMock(IConfig::class)
);
$this->session->expects($this->once())
->method('logout');
@ -163,7 +167,7 @@ class CORSMiddlewareTest extends \Test\TestCase {
$this->reflector->reflect($this, __FUNCTION__);
$middleware = new CORSMiddleware($request, $this->reflector, $this->session, $this->throttler);
$middleware->beforeController($this, __FUNCTION__, new Response());
$middleware->beforeController($this->controller, __FUNCTION__);
}
/**
@ -176,8 +180,8 @@ class CORSMiddlewareTest extends \Test\TestCase {
'PHP_AUTH_USER' => 'user',
'PHP_AUTH_PW' => 'pass'
]],
$this->getMockBuilder('\OCP\Security\ISecureRandom')->getMock(),
$this->getMockBuilder('\OCP\IConfig')->getMock()
$this->createMock(ISecureRandom::class),
$this->createMock(IConfig::class)
);
$this->session->expects($this->once())
->method('logout');
@ -188,7 +192,7 @@ class CORSMiddlewareTest extends \Test\TestCase {
$this->reflector->reflect($this, __FUNCTION__);
$middleware = new CORSMiddleware($request, $this->reflector, $this->session, $this->throttler);
$middleware->beforeController($this, __FUNCTION__, new Response());
$middleware->beforeController($this->controller, __FUNCTION__);
}
/**
@ -201,8 +205,8 @@ class CORSMiddlewareTest extends \Test\TestCase {
'PHP_AUTH_USER' => 'user',
'PHP_AUTH_PW' => 'pass'
]],
$this->getMockBuilder('\OCP\Security\ISecureRandom')->getMock(),
$this->getMockBuilder('\OCP\IConfig')->getMock()
$this->createMock(ISecureRandom::class),
$this->createMock(IConfig::class)
);
$this->session->expects($this->once())
->method('logout');
@ -213,7 +217,7 @@ class CORSMiddlewareTest extends \Test\TestCase {
$this->reflector->reflect($this, __FUNCTION__);
$middleware = new CORSMiddleware($request, $this->reflector, $this->session, $this->throttler);
$middleware->beforeController($this, __FUNCTION__, new Response());
$middleware->beforeController($this->controller, __FUNCTION__);
}
public function testAfterExceptionWithSecurityExceptionNoStatus() {
@ -222,11 +226,11 @@ class CORSMiddlewareTest extends \Test\TestCase {
'PHP_AUTH_USER' => 'user',
'PHP_AUTH_PW' => 'pass'
]],
$this->getMockBuilder('\OCP\Security\ISecureRandom')->getMock(),
$this->getMockBuilder('\OCP\IConfig')->getMock()
$this->createMock(ISecureRandom::class),
$this->createMock(IConfig::class)
);
$middleware = new CORSMiddleware($request, $this->reflector, $this->session, $this->throttler);
$response = $middleware->afterException($this, __FUNCTION__, new SecurityException('A security exception'));
$response = $middleware->afterException($this->controller, __FUNCTION__, new SecurityException('A security exception'));
$expected = new JSONResponse(['message' => 'A security exception'], 500);
$this->assertEquals($expected, $response);
@ -238,11 +242,11 @@ class CORSMiddlewareTest extends \Test\TestCase {
'PHP_AUTH_USER' => 'user',
'PHP_AUTH_PW' => 'pass'
]],
$this->getMockBuilder('\OCP\Security\ISecureRandom')->getMock(),
$this->getMockBuilder('\OCP\IConfig')->getMock()
$this->createMock(ISecureRandom::class),
$this->createMock(IConfig::class)
);
$middleware = new CORSMiddleware($request, $this->reflector, $this->session, $this->throttler);
$response = $middleware->afterException($this, __FUNCTION__, new SecurityException('A security exception', 501));
$response = $middleware->afterException($this->controller, __FUNCTION__, new SecurityException('A security exception', 501));
$expected = new JSONResponse(['message' => 'A security exception'], 501);
$this->assertEquals($expected, $response);
@ -258,11 +262,11 @@ class CORSMiddlewareTest extends \Test\TestCase {
'PHP_AUTH_USER' => 'user',
'PHP_AUTH_PW' => 'pass'
]],
$this->getMockBuilder('\OCP\Security\ISecureRandom')->getMock(),
$this->getMockBuilder('\OCP\IConfig')->getMock()
$this->createMock(ISecureRandom::class),
$this->createMock(IConfig::class)
);
$middleware = new CORSMiddleware($request, $this->reflector, $this->session, $this->throttler);
$middleware->afterException($this, __FUNCTION__, new \Exception('A regular exception'));
$middleware->afterException($this->controller, __FUNCTION__, new \Exception('A regular exception'));
}
}

View File

@ -131,7 +131,7 @@ class SecurityMiddlewareTest extends \Test\TestCase {
->with($this->equalTo('files'));
$this->reader->reflect(__CLASS__, __FUNCTION__);
$this->middleware->beforeController(__CLASS__, __FUNCTION__);
$this->middleware->beforeController($this->controller, __FUNCTION__);
}
@ -152,7 +152,7 @@ class SecurityMiddlewareTest extends \Test\TestCase {
try {
$this->reader->reflect(__CLASS__, $method);
$sec->beforeController(__CLASS__, $method);
$sec->beforeController($this->controller, $method);
} catch (SecurityException $ex){
$this->assertEquals($status, $ex->getCode());
}
@ -234,7 +234,7 @@ class SecurityMiddlewareTest extends \Test\TestCase {
$sec = $this->getMiddleware(false, false);
$this->reader->reflect(__CLASS__, __FUNCTION__);
$sec->beforeController(__CLASS__, __FUNCTION__);
$sec->beforeController($this->controller, __FUNCTION__);
}
@ -261,7 +261,7 @@ class SecurityMiddlewareTest extends \Test\TestCase {
}
$this->reader->reflect(__CLASS__, $method);
$sec->beforeController(__CLASS__, $method);
$sec->beforeController($this->controller, $method);
}
@ -277,7 +277,7 @@ class SecurityMiddlewareTest extends \Test\TestCase {
->method('passesStrictCookieCheck')
->will($this->returnValue(true));
$this->reader->reflect(__CLASS__, __FUNCTION__);
$this->middleware->beforeController(__CLASS__, __FUNCTION__);
$this->middleware->beforeController($this->controller, __FUNCTION__);
}
@ -291,7 +291,7 @@ class SecurityMiddlewareTest extends \Test\TestCase {
->will($this->returnValue(false));
$this->reader->reflect(__CLASS__, __FUNCTION__);
$this->middleware->beforeController(__CLASS__, __FUNCTION__);
$this->middleware->beforeController($this->controller, __FUNCTION__);
}
/**
@ -306,7 +306,7 @@ class SecurityMiddlewareTest extends \Test\TestCase {
->will($this->returnValue(true));
$this->reader->reflect(__CLASS__, __FUNCTION__);
$this->middleware->beforeController(__CLASS__, __FUNCTION__);
$this->middleware->beforeController($this->controller, __FUNCTION__);
}
/**
@ -322,7 +322,7 @@ class SecurityMiddlewareTest extends \Test\TestCase {
->will($this->returnValue(true));
$this->reader->reflect(__CLASS__, __FUNCTION__);
$this->middleware->beforeController(__CLASS__, __FUNCTION__);
$this->middleware->beforeController($this->controller, __FUNCTION__);
}
/**
@ -338,7 +338,7 @@ class SecurityMiddlewareTest extends \Test\TestCase {
->will($this->returnValue(false));
$this->reader->reflect(__CLASS__, __FUNCTION__);
$this->middleware->beforeController(__CLASS__, __FUNCTION__);
$this->middleware->beforeController($this->controller, __FUNCTION__);
}
@ -352,7 +352,7 @@ class SecurityMiddlewareTest extends \Test\TestCase {
->will($this->returnValue(false));
$this->reader->reflect(__CLASS__, __FUNCTION__);
$this->middleware->beforeController(__CLASS__, __FUNCTION__);
$this->middleware->beforeController($this->controller, __FUNCTION__);
}
/**
@ -367,7 +367,7 @@ class SecurityMiddlewareTest extends \Test\TestCase {
->willReturn(true);
$this->reader->reflect(__CLASS__, __FUNCTION__);
$this->middleware->beforeController(__CLASS__, __FUNCTION__);
$this->middleware->beforeController($this->controller, __FUNCTION__);
}
public function dataCsrfOcsController() {

View File

@ -15,21 +15,21 @@ namespace Test\AppFramework\Middleware;
use OC\AppFramework\Http\Request;
use OC\AppFramework\Middleware\SessionMiddleware;
use OC\AppFramework\Utility\ControllerMethodReflector;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\Response;
class SessionMiddlewareTest extends \Test\TestCase {
/**
* @var ControllerMethodReflector
*/
/** @var ControllerMethodReflector */
private $reflector;
/**
* @var Request
*/
/** @var Request */
private $request;
/** @var Controller */
private $controller;
protected function setUp() {
parent::setUp();
@ -39,6 +39,7 @@ class SessionMiddlewareTest extends \Test\TestCase {
$this->getMockBuilder('\OCP\IConfig')->getMock()
);
$this->reflector = new ControllerMethodReflector();
$this->controller = $this->createMock(Controller::class);
}
/**
@ -49,7 +50,7 @@ class SessionMiddlewareTest extends \Test\TestCase {
$this->reflector->reflect($this, __FUNCTION__);
$middleware = new SessionMiddleware($this->request, $this->reflector, $session);
$middleware->beforeController($this, __FUNCTION__);
$middleware->beforeController($this->controller, __FUNCTION__);
}
/**
@ -60,7 +61,7 @@ class SessionMiddlewareTest extends \Test\TestCase {
$this->reflector->reflect($this, __FUNCTION__);
$middleware = new SessionMiddleware($this->request, $this->reflector, $session);
$middleware->afterController($this, __FUNCTION__, new Response());
$middleware->afterController($this->controller, __FUNCTION__, new Response());
}
public function testSessionClosedOnBeforeController() {
@ -68,7 +69,7 @@ class SessionMiddlewareTest extends \Test\TestCase {
$this->reflector->reflect($this, __FUNCTION__);
$middleware = new SessionMiddleware($this->request, $this->reflector, $session);
$middleware->beforeController($this, __FUNCTION__);
$middleware->beforeController($this->controller, __FUNCTION__);
}
public function testSessionNotClosedOnAfterController() {
@ -76,7 +77,7 @@ class SessionMiddlewareTest extends \Test\TestCase {
$this->reflector->reflect($this, __FUNCTION__);
$middleware = new SessionMiddleware($this->request, $this->reflector, $session);
$middleware->afterController($this, __FUNCTION__, new Response());
$middleware->afterController($this->controller, __FUNCTION__, new Response());
}
/**