diff options
author | Joas Schilling <213943+nickvergessen@users.noreply.github.com> | 2022-03-22 12:08:45 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-22 12:08:45 +0100 |
commit | 0acd4b5f8202be8b3f3b4e6d7481e3d23e496b86 (patch) | |
tree | 06897c455b69be134fcd61b0f0130609ce7a75a1 | |
parent | b6209d61251f7abacefb8cf3c164d39bcba29100 (diff) | |
parent | 67452b94ca0b59a063c4364f5930bb5186db2d55 (diff) | |
download | nextcloud-server-0acd4b5f8202be8b3f3b4e6d7481e3d23e496b86.tar.gz nextcloud-server-0acd4b5f8202be8b3f3b4e6d7481e3d23e496b86.zip |
Merge pull request #31235 from nextcloud/techdebt/noid/extract-request-id
Extract request id handling to dedicated class so it can be injected without DB dependency
22 files changed, 421 insertions, 327 deletions
diff --git a/apps/dav/tests/unit/Connector/Sabre/FileTest.php b/apps/dav/tests/unit/Connector/Sabre/FileTest.php index 3e6a47d5854..d12a86f6e8d 100644 --- a/apps/dav/tests/unit/Connector/Sabre/FileTest.php +++ b/apps/dav/tests/unit/Connector/Sabre/FileTest.php @@ -35,14 +35,14 @@ use OC\Files\Storage\Local; use OC\Files\Storage\Temporary; use OC\Files\Storage\Wrapper\PermissionsMask; use OC\Files\View; -use OC\Security\SecureRandom; use OCA\DAV\Connector\Sabre\File; use OCP\Constants; use OCP\Files\ForbiddenException; use OCP\Files\Storage; use OCP\IConfig; +use OCP\IRequestId; use OCP\Lock\ILockingProvider; -use OCP\Security\ISecureRandom; +use PHPUnit\Framework\MockObject\MockObject; use Test\HookHelper; use Test\TestCase; use Test\Traits\MountProviderTrait; @@ -64,11 +64,11 @@ class FileTest extends TestCase { */ private $user; - /** @var IConfig | \PHPUnit\Framework\MockObject\MockObject */ + /** @var IConfig|MockObject */ protected $config; - /** @var ISecureRandom */ - protected $secureRandom; + /** @var IRequestId|MockObject */ + protected $requestId; protected function setUp(): void { parent::setUp(); @@ -83,8 +83,8 @@ class FileTest extends TestCase { $this->loginAsUser($this->user); - $this->config = $this->getMockBuilder('\OCP\IConfig')->getMock(); - $this->secureRandom = new SecureRandom(); + $this->config = $this->createMock(IConfig::class); + $this->requestId = $this->createMock(IRequestId::class); } protected function tearDown(): void { @@ -96,7 +96,7 @@ class FileTest extends TestCase { } /** - * @return \PHPUnit\Framework\MockObject\MockObject|Storage + * @return MockObject|Storage */ private function getMockStorage() { $storage = $this->getMockBuilder(Storage::class) @@ -184,7 +184,7 @@ class FileTest extends TestCase { ->setConstructorArgs([['datadir' => \OC::$server->getTempManager()->getTemporaryFolder()]]) ->getMock(); \OC\Files\Filesystem::mount($storage, [], $this->user . '/'); - /** @var View | \PHPUnit\Framework\MockObject\MockObject $view */ + /** @var View | MockObject $view */ $view = $this->getMockBuilder(View::class) ->setMethods(['getRelativePath', 'resolvePath']) ->getMock(); @@ -330,7 +330,7 @@ class FileTest extends TestCase { null ); - /** @var \OCA\DAV\Connector\Sabre\File | \PHPUnit\Framework\MockObject\MockObject $file */ + /** @var \OCA\DAV\Connector\Sabre\File | MockObject $file */ $file = $this->getMockBuilder(\OCA\DAV\Connector\Sabre\File::class) ->setConstructorArgs([$view, $info, null, $request]) ->setMethods(['header']) @@ -416,7 +416,7 @@ class FileTest extends TestCase { 'server' => [ 'HTTP_X_OC_MTIME' => $requestMtime, ] - ], $this->secureRandom, $this->config, null); + ], $this->requestId, $this->config, null); $file = 'foo.txt'; if ($resultMtime === null) { @@ -439,7 +439,7 @@ class FileTest extends TestCase { 'server' => [ 'HTTP_X_OC_MTIME' => $requestMtime, ] - ], $this->secureRandom, $this->config, null); + ], $this->requestId, $this->config, null); $_SERVER['HTTP_OC_CHUNKED'] = true; $file = 'foo.txt'; diff --git a/lib/base.php b/lib/base.php index 2dd878fdbbc..f3c3e4f31cb 100644 --- a/lib/base.php +++ b/lib/base.php @@ -161,7 +161,11 @@ class OC { 'SCRIPT_FILENAME' => $_SERVER['SCRIPT_FILENAME'], ], ]; - $fakeRequest = new \OC\AppFramework\Http\Request($params, new \OC\Security\SecureRandom(), new \OC\AllConfig(new \OC\SystemConfig(self::$config))); + $fakeRequest = new \OC\AppFramework\Http\Request( + $params, + new \OC\AppFramework\Http\RequestId($_SERVER['UNIQUE_ID'] ?? '', new \OC\Security\SecureRandom()), + new \OC\AllConfig(new \OC\SystemConfig(self::$config)) + ); $scriptName = $fakeRequest->getScriptName(); if (substr($scriptName, -1) == '/') { $scriptName .= 'index.php'; diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index b6af8b8cef4..2ce5d448518 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -412,6 +412,7 @@ return array( 'OCP\\INavigationManager' => $baseDir . '/lib/public/INavigationManager.php', 'OCP\\IPreview' => $baseDir . '/lib/public/IPreview.php', 'OCP\\IRequest' => $baseDir . '/lib/public/IRequest.php', + 'OCP\\IRequestId' => $baseDir . '/lib/public/IRequestId.php', 'OCP\\ISearch' => $baseDir . '/lib/public/ISearch.php', 'OCP\\IServerContainer' => $baseDir . '/lib/public/IServerContainer.php', 'OCP\\ISession' => $baseDir . '/lib/public/ISession.php', @@ -635,6 +636,7 @@ return array( 'OC\\AppFramework\\Http\\Dispatcher' => $baseDir . '/lib/private/AppFramework/Http/Dispatcher.php', 'OC\\AppFramework\\Http\\Output' => $baseDir . '/lib/private/AppFramework/Http/Output.php', 'OC\\AppFramework\\Http\\Request' => $baseDir . '/lib/private/AppFramework/Http/Request.php', + 'OC\\AppFramework\\Http\\RequestId' => $baseDir . '/lib/private/AppFramework/Http/RequestId.php', 'OC\\AppFramework\\Logger' => $baseDir . '/lib/private/AppFramework/Logger.php', 'OC\\AppFramework\\Middleware\\AdditionalScriptsMiddleware' => $baseDir . '/lib/private/AppFramework/Middleware/AdditionalScriptsMiddleware.php', 'OC\\AppFramework\\Middleware\\CompressionMiddleware' => $baseDir . '/lib/private/AppFramework/Middleware/CompressionMiddleware.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index b1e9912f506..4c35c1d1f85 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -441,6 +441,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c 'OCP\\INavigationManager' => __DIR__ . '/../../..' . '/lib/public/INavigationManager.php', 'OCP\\IPreview' => __DIR__ . '/../../..' . '/lib/public/IPreview.php', 'OCP\\IRequest' => __DIR__ . '/../../..' . '/lib/public/IRequest.php', + 'OCP\\IRequestId' => __DIR__ . '/../../..' . '/lib/public/IRequestId.php', 'OCP\\ISearch' => __DIR__ . '/../../..' . '/lib/public/ISearch.php', 'OCP\\IServerContainer' => __DIR__ . '/../../..' . '/lib/public/IServerContainer.php', 'OCP\\ISession' => __DIR__ . '/../../..' . '/lib/public/ISession.php', @@ -664,6 +665,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c 'OC\\AppFramework\\Http\\Dispatcher' => __DIR__ . '/../../..' . '/lib/private/AppFramework/Http/Dispatcher.php', 'OC\\AppFramework\\Http\\Output' => __DIR__ . '/../../..' . '/lib/private/AppFramework/Http/Output.php', 'OC\\AppFramework\\Http\\Request' => __DIR__ . '/../../..' . '/lib/private/AppFramework/Http/Request.php', + 'OC\\AppFramework\\Http\\RequestId' => __DIR__ . '/../../..' . '/lib/private/AppFramework/Http/RequestId.php', 'OC\\AppFramework\\Logger' => __DIR__ . '/../../..' . '/lib/private/AppFramework/Logger.php', 'OC\\AppFramework\\Middleware\\AdditionalScriptsMiddleware' => __DIR__ . '/../../..' . '/lib/private/AppFramework/Middleware/AdditionalScriptsMiddleware.php', 'OC\\AppFramework\\Middleware\\CompressionMiddleware' => __DIR__ . '/../../..' . '/lib/private/AppFramework/Middleware/CompressionMiddleware.php', diff --git a/lib/private/AppFramework/Http/Request.php b/lib/private/AppFramework/Http/Request.php index 21af2bc46f4..f896b825f2d 100644 --- a/lib/private/AppFramework/Http/Request.php +++ b/lib/private/AppFramework/Http/Request.php @@ -48,8 +48,8 @@ use OC\Security\CSRF\CsrfTokenManager; use OC\Security\TrustedDomainHelper; use OCP\IConfig; use OCP\IRequest; +use OCP\IRequestId; use OCP\Security\ICrypto; -use OCP\Security\ISecureRandom; /** * Class for accessing variables in the request. @@ -92,12 +92,10 @@ class Request implements \ArrayAccess, \Countable, IRequest { 'method', 'requesttoken', ]; - /** @var ISecureRandom */ - protected $secureRandom; + /** @var RequestId */ + protected $requestId; /** @var IConfig */ protected $config; - /** @var string */ - protected $requestId = ''; /** @var ICrypto */ protected $crypto; /** @var CsrfTokenManager|null */ @@ -117,20 +115,20 @@ class Request implements \ArrayAccess, \Countable, IRequest { * - array 'cookies' the $_COOKIE array * - string 'method' the request method (GET, POST etc) * - string|false 'requesttoken' the requesttoken or false when not available - * @param ISecureRandom $secureRandom + * @param IRequestId $requestId * @param IConfig $config * @param CsrfTokenManager|null $csrfTokenManager * @param string $stream * @see https://www.php.net/manual/en/reserved.variables.php */ public function __construct(array $vars, - ISecureRandom $secureRandom, + IRequestId $requestId, IConfig $config, CsrfTokenManager $csrfTokenManager = null, string $stream = 'php://input') { $this->inputStream = $stream; $this->items['params'] = []; - $this->secureRandom = $secureRandom; + $this->requestId = $requestId; $this->config = $config; $this->csrfTokenManager = $csrfTokenManager; @@ -571,16 +569,7 @@ class Request implements \ArrayAccess, \Countable, IRequest { * @return string */ public function getId(): string { - if (isset($this->server['UNIQUE_ID'])) { - return $this->server['UNIQUE_ID']; - } - - if (empty($this->requestId)) { - $validChars = ISecureRandom::CHAR_ALPHANUMERIC; - $this->requestId = $this->secureRandom->generate(20, $validChars); - } - - return $this->requestId; + return $this->requestId->getId(); } /** diff --git a/lib/private/AppFramework/Http/RequestId.php b/lib/private/AppFramework/Http/RequestId.php new file mode 100644 index 00000000000..70032873a75 --- /dev/null +++ b/lib/private/AppFramework/Http/RequestId.php @@ -0,0 +1,52 @@ +<?php + +declare(strict_types=1); +/** + * @copyright Copyright (c) 2022, Joas Schilling <coding@schilljs.com> + * + * @author Joas Schilling <coding@schilljs.com> + * + * @license AGPL-3.0 + * + * 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. + * + * 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, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ +namespace OC\AppFramework\Http; + +use OCP\IRequestId; +use OCP\Security\ISecureRandom; + +class RequestId implements IRequestId { + protected ISecureRandom $secureRandom; + protected string $requestId; + + public function __construct(string $uniqueId, + ISecureRandom $secureRandom) { + $this->requestId = $uniqueId; + $this->secureRandom = $secureRandom; + } + + /** + * Returns an ID for the request, value is not guaranteed to be unique and is mostly meant for logging + * If `mod_unique_id` is installed this value will be taken. + * @return string + */ + public function getId(): string { + if (empty($this->requestId)) { + $validChars = ISecureRandom::CHAR_ALPHANUMERIC; + $this->requestId = $this->secureRandom->generate(20, $validChars); + } + + return $this->requestId; + } +} diff --git a/lib/private/DB/Connection.php b/lib/private/DB/Connection.php index e32c530c19e..6f89ba64e80 100644 --- a/lib/private/DB/Connection.php +++ b/lib/private/DB/Connection.php @@ -53,6 +53,7 @@ use OC\DB\QueryBuilder\QueryBuilder; use OC\SystemConfig; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\ILogger; +use OCP\IRequestId; use OCP\PreConditionNotMetException; class Connection extends \Doctrine\DBAL\Connection { @@ -281,11 +282,16 @@ class Connection extends \Doctrine\DBAL\Connection { } protected function logQueryToFile(string $sql): void { - $logFile = $this->systemConfig->getValue('query_log_file', ''); + $logFile = $this->systemConfig->getValue('query_log_file'); if ($logFile !== '' && is_writable(dirname($logFile)) && (!file_exists($logFile) || is_writable($logFile))) { + $prefix = ''; + if ($this->systemConfig->getValue('query_log_file_requestid') === 'yes') { + $prefix .= \OC::$server->get(IRequestId::class)->getId() . "\t"; + } + file_put_contents( $this->systemConfig->getValue('query_log_file', ''), - $sql . "\n", + $prefix . $sql . "\n", FILE_APPEND ); } diff --git a/lib/private/Server.php b/lib/private/Server.php index 00afaf1d6a9..b189905cfaf 100644 --- a/lib/private/Server.php +++ b/lib/private/Server.php @@ -60,6 +60,7 @@ use OC\App\AppStore\Fetcher\AppFetcher; use OC\App\AppStore\Fetcher\CategoryFetcher; use OC\AppFramework\Bootstrap\Coordinator; use OC\AppFramework\Http\Request; +use OC\AppFramework\Http\RequestId; use OC\AppFramework\Utility\TimeFactory; use OC\Authentication\Events\LoginFailed; use OC\Authentication\Listeners\LoginFailedListener; @@ -205,6 +206,7 @@ use OCP\ILogger; use OCP\INavigationManager; use OCP\IPreview; use OCP\IRequest; +use OCP\IRequestId; use OCP\ISearch; use OCP\IServerContainer; use OCP\ISession; @@ -1033,7 +1035,7 @@ class Server extends ServerContainer implements IServerContainer { : '', 'urlParams' => $urlParams, ], - $this->get(ISecureRandom::class), + $this->get(IRequestId::class), $this->get(\OCP\IConfig::class), $this->get(CsrfTokenManager::class), $stream @@ -1042,6 +1044,13 @@ class Server extends ServerContainer implements IServerContainer { /** @deprecated 19.0.0 */ $this->registerDeprecatedAlias('Request', \OCP\IRequest::class); + $this->registerService(IRequestId::class, function (ContainerInterface $c): IRequestId { + return new RequestId( + $_SERVER['UNIQUE_ID'] ?? '', + $this->get(ISecureRandom::class) + ); + }); + $this->registerService(IMailer::class, function (Server $c) { return new Mailer( $c->get(\OCP\IConfig::class), @@ -1214,7 +1223,7 @@ class Server extends ServerContainer implements IServerContainer { $this->registerAlias(EventDispatcherInterface::class, \OC\EventDispatcher\SymfonyAdapter::class); $this->registerService('CryptoWrapper', function (ContainerInterface $c) { - // FIXME: Instantiiated here due to cyclic dependency + // FIXME: Instantiated here due to cyclic dependency $request = new Request( [ 'get' => $_GET, @@ -1227,7 +1236,7 @@ class Server extends ServerContainer implements IServerContainer { ? $_SERVER['REQUEST_METHOD'] : null, ], - $c->get(ISecureRandom::class), + $c->get(IRequestId::class), $c->get(\OCP\IConfig::class) ); diff --git a/lib/public/IRequestId.php b/lib/public/IRequestId.php new file mode 100644 index 00000000000..dba06088cc9 --- /dev/null +++ b/lib/public/IRequestId.php @@ -0,0 +1,39 @@ +<?php + +declare(strict_types=1); +/** + * @copyright Copyright (c) 2022, Joas Schilling <coding@schilljs.com> + * + * @author Joas Schilling <coding@schilljs.com> + * + * @license AGPL-3.0 + * + * 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. + * + * 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, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace OCP; + +/** + * @since 24.0.0 + */ +interface IRequestId { + /** + * Returns an ID for the request, value is not guaranteed to be unique and is mostly meant for logging + * If `mod_unique_id` is installed this value will be taken. + * + * @return string + * @since 24.0.0 + */ + public function getId(): string; +} diff --git a/tests/Core/Middleware/TwoFactorMiddlewareTest.php b/tests/Core/Middleware/TwoFactorMiddlewareTest.php index 8cc4340ad98..c5de9f81fe5 100644 --- a/tests/Core/Middleware/TwoFactorMiddlewareTest.php +++ b/tests/Core/Middleware/TwoFactorMiddlewareTest.php @@ -31,16 +31,17 @@ use OC\Core\Controller\TwoFactorChallengeController; use OC\Core\Middleware\TwoFactorMiddleware; use OC\User\Session; use OCP\AppFramework\Controller; +use OCP\AppFramework\Http\RedirectResponse; use OCP\AppFramework\Utility\IControllerMethodReflector; use OCP\Authentication\TwoFactorAuth\ALoginSetupController; use OCP\Authentication\TwoFactorAuth\IProvider; use OCP\IConfig; use OCP\IRequest; +use OCP\IRequestId; use OCP\ISession; use OCP\IURLGenerator; use OCP\IUser; use OCP\IUserSession; -use OCP\Security\ISecureRandom; use PHPUnit\Framework\MockObject\MockObject; use Test\TestCase; @@ -88,7 +89,7 @@ class TwoFactorMiddlewareTest extends TestCase { 'REQUEST_URI' => 'test/url' ] ], - $this->createMock(ISecureRandom::class), + $this->createMock(IRequestId::class), $this->createMock(IConfig::class) ); @@ -139,9 +140,9 @@ class TwoFactorMiddlewareTest extends TestCase { $this->middleware->beforeController($this->controller, 'index'); } - + public function testBeforeControllerTwoFactorAuthRequired() { - $this->expectException(\OC\Authentication\Exceptions\TwoFactorAuthRequiredException::class); + $this->expectException(TwoFactorAuthRequiredException::class); $user = $this->createMock(IUser::class); @@ -163,9 +164,9 @@ class TwoFactorMiddlewareTest extends TestCase { $this->middleware->beforeController($this->controller, 'index'); } - + public function testBeforeControllerUserAlreadyLoggedIn() { - $this->expectException(\OC\Authentication\Exceptions\UserAlreadyLoggedInException::class); + $this->expectException(UserAlreadyLoggedInException::class); $user = $this->createMock(IUser::class); @@ -187,32 +188,32 @@ class TwoFactorMiddlewareTest extends TestCase { ->with($user) ->willReturn(false); - $twoFactorChallengeController = $this->getMockBuilder('\OC\Core\Controller\TwoFactorChallengeController') + $twoFactorChallengeController = $this->getMockBuilder(TwoFactorChallengeController::class) ->disableOriginalConstructor() ->getMock(); $this->middleware->beforeController($twoFactorChallengeController, 'index'); } public function testAfterExceptionTwoFactorAuthRequired() { - $ex = new \OC\Authentication\Exceptions\TwoFactorAuthRequiredException(); + $ex = new TwoFactorAuthRequiredException(); $this->urlGenerator->expects($this->once()) ->method('linkToRoute') ->with('core.TwoFactorChallenge.selectChallenge') ->willReturn('test/url'); - $expected = new \OCP\AppFramework\Http\RedirectResponse('test/url'); + $expected = new RedirectResponse('test/url'); $this->assertEquals($expected, $this->middleware->afterException($this->controller, 'index', $ex)); } public function testAfterException() { - $ex = new \OC\Authentication\Exceptions\UserAlreadyLoggedInException(); + $ex = new UserAlreadyLoggedInException(); $this->urlGenerator->expects($this->once()) ->method('linkToRoute') ->with('files.view.index') ->willReturn('redirect/url'); - $expected = new \OCP\AppFramework\Http\RedirectResponse('redirect/url'); + $expected = new RedirectResponse('redirect/url'); $this->assertEquals($expected, $this->middleware->afterException($this->controller, 'index', $ex)); } diff --git a/tests/lib/AppFramework/Controller/ApiControllerTest.php b/tests/lib/AppFramework/Controller/ApiControllerTest.php index 71eb97b94da..975b92c5b96 100644 --- a/tests/lib/AppFramework/Controller/ApiControllerTest.php +++ b/tests/lib/AppFramework/Controller/ApiControllerTest.php @@ -26,6 +26,7 @@ namespace Test\AppFramework\Controller; use OC\AppFramework\Http\Request; use OCP\AppFramework\ApiController; use OCP\IConfig; +use OCP\IRequestId; class ChildApiController extends ApiController { }; @@ -38,12 +39,8 @@ class ApiControllerTest extends \Test\TestCase { public function testCors() { $request = new Request( ['server' => ['HTTP_ORIGIN' => 'test']], - $this->getMockBuilder('\OCP\Security\ISecureRandom') - ->disableOriginalConstructor() - ->getMock(), - $this->getMockBuilder(IConfig::class) - ->disableOriginalConstructor() - ->getMock() + $this->createMock(IRequestId::class), + $this->createMock(IConfig::class) ); $this->controller = new ChildApiController('app', $request, 'verbs', 'headers', 100); diff --git a/tests/lib/AppFramework/Controller/ControllerTest.php b/tests/lib/AppFramework/Controller/ControllerTest.php index 1d72482e75d..4d36fcadce1 100644 --- a/tests/lib/AppFramework/Controller/ControllerTest.php +++ b/tests/lib/AppFramework/Controller/ControllerTest.php @@ -29,6 +29,8 @@ use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\Http\JSONResponse; use OCP\IConfig; use OCP\IRequest; +use OCP\IRequestId; +use OC\AppFramework\DependencyInjection\DIContainer; class ChildController extends Controller { public function __construct($appName, $request) { @@ -75,15 +77,11 @@ class ControllerTest extends \Test\TestCase { 'session' => ['sezession' => 'kein'], 'method' => 'hi', ], - $this->getMockBuilder('\OCP\Security\ISecureRandom') - ->disableOriginalConstructor() - ->getMock(), - $this->getMockBuilder(IConfig::class) - ->disableOriginalConstructor() - ->getMock() + $this->createMock(IRequestId::class), + $this->createMock(IConfig::class) ); - $this->app = $this->getMockBuilder('OC\AppFramework\DependencyInjection\DIContainer') + $this->app = $this->getMockBuilder(DIContainer::class) ->setMethods(['getAppName']) ->setConstructorArgs(['test']) ->getMock(); diff --git a/tests/lib/AppFramework/Controller/OCSControllerTest.php b/tests/lib/AppFramework/Controller/OCSControllerTest.php index 91a61047871..ce110f435ef 100644 --- a/tests/lib/AppFramework/Controller/OCSControllerTest.php +++ b/tests/lib/AppFramework/Controller/OCSControllerTest.php @@ -28,7 +28,7 @@ use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\Http\EmptyContentSecurityPolicy; use OCP\AppFramework\OCSController; use OCP\IConfig; -use OCP\Security\ISecureRandom; +use OCP\IRequestId; class ChildOCSController extends OCSController { } @@ -42,12 +42,8 @@ class OCSControllerTest extends \Test\TestCase { 'HTTP_ORIGIN' => 'test', ], ], - $this->getMockBuilder(ISecureRandom::class) - ->disableOriginalConstructor() - ->getMock(), - $this->getMockBuilder(IConfig::class) - ->disableOriginalConstructor() - ->getMock() + $this->createMock(IRequestId::class), + $this->createMock(IConfig::class) ); $controller = new ChildOCSController('app', $request, 'verbs', 'headers', 100); @@ -67,12 +63,8 @@ class OCSControllerTest extends \Test\TestCase { public function testXML() { $controller = new ChildOCSController('app', new Request( [], - $this->getMockBuilder(ISecureRandom::class) - ->disableOriginalConstructor() - ->getMock(), - $this->getMockBuilder(IConfig::class) - ->disableOriginalConstructor() - ->getMock() + $this->createMock(IRequestId::class), + $this->createMock(IConfig::class) )); $controller->setOCSVersion(1); @@ -100,12 +92,8 @@ class OCSControllerTest extends \Test\TestCase { public function testJSON() { $controller = new ChildOCSController('app', new Request( [], - $this->getMockBuilder(ISecureRandom::class) - ->disableOriginalConstructor() - ->getMock(), - $this->getMockBuilder(IConfig::class) - ->disableOriginalConstructor() - ->getMock() + $this->createMock(IRequestId::class), + $this->createMock(IConfig::class) )); $controller->setOCSVersion(1); $expected = '{"ocs":{"meta":{"status":"ok","statuscode":100,"message":"OK",' . @@ -121,12 +109,8 @@ class OCSControllerTest extends \Test\TestCase { public function testXMLV2() { $controller = new ChildOCSController('app', new Request( [], - $this->getMockBuilder(ISecureRandom::class) - ->disableOriginalConstructor() - ->getMock(), - $this->getMockBuilder(IConfig::class) - ->disableOriginalConstructor() - ->getMock() + $this->createMock(IRequestId::class), + $this->createMock(IConfig::class) )); $controller->setOCSVersion(2); @@ -152,12 +136,8 @@ class OCSControllerTest extends \Test\TestCase { public function testJSONV2() { $controller = new ChildOCSController('app', new Request( [], - $this->getMockBuilder(ISecureRandom::class) - ->disableOriginalConstructor() - ->getMock(), - $this->getMockBuilder(IConfig::class) - ->disableOriginalConstructor() - ->getMock() + $this->createMock(IRequestId::class), + $this->createMock(IConfig::class) )); $controller->setOCSVersion(2); $expected = '{"ocs":{"meta":{"status":"ok","statuscode":200,"message":"OK"},"data":{"test":"hi"}}}'; diff --git a/tests/lib/AppFramework/DependencyInjection/DIContainerTest.php b/tests/lib/AppFramework/DependencyInjection/DIContainerTest.php index 3004123b81b..9a3d40d1c6b 100644 --- a/tests/lib/AppFramework/DependencyInjection/DIContainerTest.php +++ b/tests/lib/AppFramework/DependencyInjection/DIContainerTest.php @@ -30,7 +30,7 @@ use OC\AppFramework\Http\Request; use OC\AppFramework\Middleware\Security\SecurityMiddleware; use OCP\AppFramework\QueryException; use OCP\IConfig; -use OCP\Security\ISecureRandom; +use OCP\IRequestId; /** * @group DB @@ -69,7 +69,7 @@ class DIContainerTest extends \Test\TestCase { public function testMiddlewareDispatcherIncludesSecurityMiddleware() { $this->container['Request'] = new Request( ['method' => 'GET'], - $this->createMock(ISecureRandom::class), + $this->createMock(IRequestId::class), $this->createMock(IConfig::class) ); $dispatcher = $this->container['MiddlewareDispatcher']; diff --git a/tests/lib/AppFramework/Http/DispatcherTest.php b/tests/lib/AppFramework/Http/DispatcherTest.php index a76b78385e7..e1d78082a2d 100644 --- a/tests/lib/AppFramework/Http/DispatcherTest.php +++ b/tests/lib/AppFramework/Http/DispatcherTest.php @@ -37,6 +37,7 @@ use OCP\IConfig; use OCP\IRequest; use PHPUnit\Framework\MockObject\MockObject; use Psr\Log\LoggerInterface; +use OCP\IRequestId; class TestController extends Controller { /** @@ -316,12 +317,8 @@ class DispatcherTest extends \Test\TestCase { ], 'method' => 'POST' ], - $this->getMockBuilder('\OCP\Security\ISecureRandom') - ->disableOriginalConstructor() - ->getMock(), - $this->getMockBuilder(IConfig::class) - ->disableOriginalConstructor() - ->getMock() + $this->createMock(IRequestId::class), + $this->createMock(IConfig::class) ); $this->dispatcher = new Dispatcher( $this->http, $this->middlewareDispatcher, $this->reflector, @@ -351,12 +348,8 @@ class DispatcherTest extends \Test\TestCase { ], 'method' => 'POST', ], - $this->getMockBuilder('\OCP\Security\ISecureRandom') - ->disableOriginalConstructor() - ->getMock(), - $this->getMockBuilder(IConfig::class) - ->disableOriginalConstructor() - ->getMock() + $this->createMock(IRequestId::class), + $this->createMock(IConfig::class) ); $this->dispatcher = new Dispatcher( $this->http, $this->middlewareDispatcher, $this->reflector, @@ -389,12 +382,8 @@ class DispatcherTest extends \Test\TestCase { ], 'method' => 'GET' ], - $this->getMockBuilder('\OCP\Security\ISecureRandom') - ->disableOriginalConstructor() - ->getMock(), - $this->getMockBuilder(IConfig::class) - ->disableOriginalConstructor() - ->getMock() + $this->createMock(IRequestId::class), + $this->createMock(IConfig::class) ); $this->dispatcher = new Dispatcher( $this->http, $this->middlewareDispatcher, $this->reflector, @@ -426,12 +415,8 @@ class DispatcherTest extends \Test\TestCase { ], 'method' => 'GET' ], - $this->getMockBuilder('\OCP\Security\ISecureRandom') - ->disableOriginalConstructor() - ->getMock(), - $this->getMockBuilder(IConfig::class) - ->disableOriginalConstructor() - ->getMock() + $this->createMock(IRequestId::class), + $this->createMock(IConfig::class) ); $this->dispatcher = new Dispatcher( $this->http, $this->middlewareDispatcher, $this->reflector, @@ -464,12 +449,8 @@ class DispatcherTest extends \Test\TestCase { ], 'method' => 'PUT' ], - $this->getMockBuilder('\OCP\Security\ISecureRandom') - ->disableOriginalConstructor() - ->getMock(), - $this->getMockBuilder(IConfig::class) - ->disableOriginalConstructor() - ->getMock() + $this->createMock(IRequestId::class), + $this->createMock(IConfig::class) ); $this->dispatcher = new Dispatcher( $this->http, $this->middlewareDispatcher, $this->reflector, @@ -504,12 +485,8 @@ class DispatcherTest extends \Test\TestCase { ], 'method' => 'POST' ], - $this->getMockBuilder('\OCP\Security\ISecureRandom') - ->disableOriginalConstructor() - ->getMock(), - $this->getMockBuilder(IConfig::class) - ->disableOriginalConstructor() - ->getMock() + $this->createMock(IRequestId::class), + $this->createMock(IConfig::class) ); $this->dispatcher = new Dispatcher( $this->http, $this->middlewareDispatcher, $this->reflector, diff --git a/tests/lib/AppFramework/Http/RequestIdTest.php b/tests/lib/AppFramework/Http/RequestIdTest.php new file mode 100644 index 00000000000..9f9afed4b7f --- /dev/null +++ b/tests/lib/AppFramework/Http/RequestIdTest.php @@ -0,0 +1,76 @@ +<?php + +declare(strict_types=1); +/** + * @copyright Copyright (c) 2022 Joas Schilling <coding@schilljs.com> + * + * @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\AppFramework\Http; + +use OC\AppFramework\Http\RequestId; +use OCP\Security\ISecureRandom; +use PHPUnit\Framework\MockObject\MockObject; + +/** + * Class RequestIdTest + * + * @package OC\AppFramework\Http + */ +class RequestIdTest extends \Test\TestCase { + /** @var ISecureRandom|MockObject */ + protected $secureRandom; + + protected function setUp(): void { + parent::setUp(); + + $this->secureRandom = $this->createMock(ISecureRandom::class); + } + + public function testGetIdWithModUnique(): void { + $requestId = new RequestId( + 'GeneratedUniqueIdByModUnique', + $this->secureRandom + ); + + $this->secureRandom->expects($this->never()) + ->method('generate'); + + $this->assertSame('GeneratedUniqueIdByModUnique', $requestId->getId()); + $this->assertSame('GeneratedUniqueIdByModUnique', $requestId->getId()); + } + + public function testGetIdWithoutModUnique(): void { + $requestId = new RequestId( + '', + $this->secureRandom + ); + + $this->secureRandom->expects($this->once()) + ->method('generate') + ->with('20') + ->willReturnOnConsecutiveCalls( + 'GeneratedByNextcloudItself1', + 'GeneratedByNextcloudItself2', + 'GeneratedByNextcloudItself3' + ); + + $this->assertSame('GeneratedByNextcloudItself1', $requestId->getId()); + $this->assertSame('GeneratedByNextcloudItself1', $requestId->getId()); + } +} diff --git a/tests/lib/AppFramework/Http/RequestTest.php b/tests/lib/AppFramework/Http/RequestTest.php index a4a03b6479c..e15f3fe656c 100644 --- a/tests/lib/AppFramework/Http/RequestTest.php +++ b/tests/lib/AppFramework/Http/RequestTest.php @@ -14,7 +14,7 @@ use OC\AppFramework\Http\Request; use OC\Security\CSRF\CsrfToken; use OC\Security\CSRF\CsrfTokenManager; use OCP\IConfig; -use OCP\Security\ISecureRandom; +use OCP\IRequestId; /** * Class RequestTest @@ -24,8 +24,8 @@ use OCP\Security\ISecureRandom; class RequestTest extends \Test\TestCase { /** @var string */ protected $stream = 'fakeinput://data'; - /** @var ISecureRandom */ - protected $secureRandom; + /** @var IRequestId */ + protected $requestId; /** @var IConfig */ protected $config; /** @var CsrfTokenManager */ @@ -39,10 +39,11 @@ class RequestTest extends \Test\TestCase { } stream_wrapper_register('fakeinput', 'Test\AppFramework\Http\RequestStream'); - $this->secureRandom = $this->getMockBuilder('\OCP\Security\ISecureRandom')->getMock(); - $this->config = $this->getMockBuilder(IConfig::class)->getMock(); - $this->csrfTokenManager = $this->getMockBuilder('\OC\Security\CSRF\CsrfTokenManager') - ->disableOriginalConstructor()->getMock(); + $this->requestId = $this->createMock(IRequestId::class); + $this->config = $this->createMock(IConfig::class); + $this->csrfTokenManager = $this->getMockBuilder(CsrfTokenManager::class) + ->disableOriginalConstructor() + ->getMock(); } protected function tearDown(): void { @@ -58,7 +59,7 @@ class RequestTest extends \Test\TestCase { $request = new Request( $vars, - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -90,7 +91,7 @@ class RequestTest extends \Test\TestCase { $request = new Request( $vars, - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -113,7 +114,7 @@ class RequestTest extends \Test\TestCase { $request = new Request( $vars, - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -133,7 +134,7 @@ class RequestTest extends \Test\TestCase { $request = new Request( $vars, - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -153,7 +154,7 @@ class RequestTest extends \Test\TestCase { $request = new Request( $vars, - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -170,7 +171,7 @@ class RequestTest extends \Test\TestCase { $request = new Request( $vars, - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -192,7 +193,7 @@ class RequestTest extends \Test\TestCase { $request = new Request( $vars, - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -216,7 +217,7 @@ class RequestTest extends \Test\TestCase { $request = new Request( $vars, - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -238,7 +239,7 @@ class RequestTest extends \Test\TestCase { $request = new Request( $vars, - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -263,7 +264,7 @@ class RequestTest extends \Test\TestCase { $request = new Request( $vars, - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -284,7 +285,7 @@ class RequestTest extends \Test\TestCase { $request = new Request( $vars, - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -312,7 +313,7 @@ class RequestTest extends \Test\TestCase { $request = new Request( $vars, - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -341,7 +342,7 @@ class RequestTest extends \Test\TestCase { $request = new Request( $vars, - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -354,54 +355,6 @@ class RequestTest extends \Test\TestCase { $this->assertEquals('3', $request->getParams()['id']); } - public function testGetIdWithModUnique() { - $vars = [ - 'server' => [ - 'UNIQUE_ID' => 'GeneratedUniqueIdByModUnique' - ], - ]; - - $request = new Request( - $vars, - $this->secureRandom, - $this->config, - $this->csrfTokenManager, - $this->stream - ); - - $this->assertSame('GeneratedUniqueIdByModUnique', $request->getId()); - } - - public function testGetIdWithoutModUnique() { - $this->secureRandom->expects($this->once()) - ->method('generate') - ->with('20') - ->willReturn('GeneratedByOwnCloudItself'); - - $request = new Request( - [], - $this->secureRandom, - $this->config, - $this->csrfTokenManager, - $this->stream - ); - - $this->assertSame('GeneratedByOwnCloudItself', $request->getId()); - } - - public function testGetIdWithoutModUniqueStable() { - $request = new Request( - [], - \OC::$server->getSecureRandom(), - $this->config, - $this->csrfTokenManager, - $this->stream - ); - $firstId = $request->getId(); - $secondId = $request->getId(); - $this->assertSame($firstId, $secondId); - } - public function testGetRemoteAddressWithoutTrustedRemote() { $this->config ->expects($this->once()) @@ -417,7 +370,7 @@ class RequestTest extends \Test\TestCase { 'HTTP_X_FORWARDED_FOR' => '192.168.0.233' ], ], - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -446,7 +399,7 @@ class RequestTest extends \Test\TestCase { 'HTTP_X_FORWARDED_FOR' => '192.168.0.233' ], ], - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -475,7 +428,7 @@ class RequestTest extends \Test\TestCase { 'HTTP_X_FORWARDED_FOR' => '192.168.0.233' ], ], - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -504,7 +457,7 @@ class RequestTest extends \Test\TestCase { 'HTTP_X_FORWARDED_FOR' => '192.168.0.233' ], ], - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -537,7 +490,7 @@ class RequestTest extends \Test\TestCase { 'HTTP_X_FORWARDED_FOR' => '192.168.0.233' ], ], - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -570,7 +523,7 @@ class RequestTest extends \Test\TestCase { 'HTTP_X_FORWARDED_FOR' => '192.168.0.233' ], ], - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -599,7 +552,7 @@ class RequestTest extends \Test\TestCase { 'HTTP_X_FORWARDED_FOR' => '192.168.0.233' ], ], - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -623,7 +576,7 @@ class RequestTest extends \Test\TestCase { 'HTTP_X_FORWARDED_FOR' => '192.168.0.233' ], ], - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -651,7 +604,7 @@ class RequestTest extends \Test\TestCase { 'HTTP_X_FORWARDED_FOR' => '[2001:db8:85a3:8d3:1319:8a2e:370:7348]', ], ], - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -702,7 +655,7 @@ class RequestTest extends \Test\TestCase { 'SERVER_PROTOCOL' => $input, ], ], - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -730,7 +683,7 @@ class RequestTest extends \Test\TestCase { $request = new Request( [], - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -757,7 +710,7 @@ class RequestTest extends \Test\TestCase { 'REMOTE_ADDR' => '1.2.3.4', ], ], - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -769,7 +722,7 @@ class RequestTest extends \Test\TestCase { 'REMOTE_ADDR' => '1.2.3.4', ], ], - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -793,7 +746,7 @@ class RequestTest extends \Test\TestCase { 'HTTPS' => 'on' ], ], - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -814,7 +767,7 @@ class RequestTest extends \Test\TestCase { 'HTTPS' => 'off' ], ], - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -835,7 +788,7 @@ class RequestTest extends \Test\TestCase { 'HTTPS' => '' ], ], - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -852,7 +805,7 @@ class RequestTest extends \Test\TestCase { $request = new Request( [], - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -878,7 +831,7 @@ class RequestTest extends \Test\TestCase { 'REMOTE_ADDR' => '1.2.3.4', ], ], - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -900,7 +853,7 @@ class RequestTest extends \Test\TestCase { 'HTTP_USER_AGENT' => $testAgent, ] ], - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -918,7 +871,7 @@ class RequestTest extends \Test\TestCase { public function testUndefinedUserAgent($testAgent, $userAgent, $matches) { $request = new Request( [], - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -1056,7 +1009,7 @@ class RequestTest extends \Test\TestCase { 'SERVER_NAME' => 'from.server.name:8080', ] ], - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -1073,7 +1026,7 @@ class RequestTest extends \Test\TestCase { 'HTTP_HOST' => 'from.host.header:8080', ] ], - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -1102,7 +1055,7 @@ class RequestTest extends \Test\TestCase { 'REMOTE_ADDR' => '1.2.3.4', ] ], - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -1131,7 +1084,7 @@ class RequestTest extends \Test\TestCase { 'REMOTE_ADDR' => '1.2.3.4', ] ], - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -1155,7 +1108,7 @@ class RequestTest extends \Test\TestCase { $request = new Request( [], - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -1184,7 +1137,7 @@ class RequestTest extends \Test\TestCase { 'REMOTE_ADDR' => '1.2.3.4', ], ], - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -1213,7 +1166,7 @@ class RequestTest extends \Test\TestCase { 'REMOTE_ADDR' => '1.2.3.4', ], ], - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -1239,7 +1192,7 @@ class RequestTest extends \Test\TestCase { 'REMOTE_ADDR' => '1.2.3.4', ], ], - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -1285,7 +1238,7 @@ class RequestTest extends \Test\TestCase { 'REMOTE_ADDR' => '1.2.3.4', ], ], - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -1302,7 +1255,7 @@ class RequestTest extends \Test\TestCase { ->willReturn(''); $request = new Request( [], - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -1330,7 +1283,7 @@ class RequestTest extends \Test\TestCase { $request = new Request( [], - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -1351,7 +1304,7 @@ class RequestTest extends \Test\TestCase { 'SCRIPT_NAME' => '/var/www/index.php', ] ], - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -1372,7 +1325,7 @@ class RequestTest extends \Test\TestCase { 'SCRIPT_NAME' => '/var/www/index.php', ] ], - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -1395,7 +1348,7 @@ class RequestTest extends \Test\TestCase { 'SCRIPT_NAME' => $scriptName, ] ], - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -1418,7 +1371,7 @@ class RequestTest extends \Test\TestCase { 'SCRIPT_NAME' => $scriptName, ] ], - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -1441,7 +1394,7 @@ class RequestTest extends \Test\TestCase { 'SCRIPT_NAME' => $scriptName, ] ], - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -1464,7 +1417,7 @@ class RequestTest extends \Test\TestCase { 'SCRIPT_NAME' => $scriptName, ] ], - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -1519,7 +1472,7 @@ class RequestTest extends \Test\TestCase { 'REQUEST_URI' => '/test.php' ] ], - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -1559,7 +1512,7 @@ class RequestTest extends \Test\TestCase { 'SCRIPT_NAME' => '/test.php', ] ], - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -1587,7 +1540,7 @@ class RequestTest extends \Test\TestCase { 'nc_sameSiteCookielax' => 'true', ], ], - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -1617,7 +1570,7 @@ class RequestTest extends \Test\TestCase { 'nc_sameSiteCookielax' => 'true', ], ], - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -1647,7 +1600,7 @@ class RequestTest extends \Test\TestCase { 'nc_sameSiteCookielax' => 'true', ], ], - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -1673,7 +1626,7 @@ class RequestTest extends \Test\TestCase { 'requesttoken' => 'AAAHGxsTCTc3BgMQESAcNR0OAR0=:MyTotalSecretShareds', ], ], - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -1697,7 +1650,7 @@ class RequestTest extends \Test\TestCase { 'requesttoken' => 'AAAHGxsTCTc3BgMQESAcNR0OAR0=:MyTotalSecretShareds', ], ], - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -1721,7 +1674,7 @@ class RequestTest extends \Test\TestCase { 'HTTP_REQUESTTOKEN' => 'AAAHGxsTCTc3BgMQESAcNR0OAR0=:MyTotalSecretShareds', ], ], - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -1749,7 +1702,7 @@ class RequestTest extends \Test\TestCase { 'nc_sameSiteCookiestrict' => 'true', ], ], - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -1777,7 +1730,7 @@ class RequestTest extends \Test\TestCase { '__Host-nc_sameSiteCookielax' => 'true', ], ], - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -1809,7 +1762,7 @@ class RequestTest extends \Test\TestCase { 'nc_sameSiteCookielax' => 'true', ], ], - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -1832,7 +1785,7 @@ class RequestTest extends \Test\TestCase { ->setMethods(['getScriptName']) ->setConstructorArgs([ [], - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -1857,7 +1810,7 @@ class RequestTest extends \Test\TestCase { 'nc_sameSiteCookielax' => 'true', ], ], - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -1880,7 +1833,7 @@ class RequestTest extends \Test\TestCase { 'RandomCookie' => 'asdf', ], ], - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -1903,7 +1856,7 @@ class RequestTest extends \Test\TestCase { session_name() => 'asdf', ], ], - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -1926,7 +1879,7 @@ class RequestTest extends \Test\TestCase { 'nc_token' => 'asdf', ], ], - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -1950,7 +1903,7 @@ class RequestTest extends \Test\TestCase { 'foo' => 'bar', ], ], - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -1977,7 +1930,7 @@ class RequestTest extends \Test\TestCase { 'nc_sameSiteCookielax' => 'true', ], ], - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -2001,7 +1954,7 @@ class RequestTest extends \Test\TestCase { 'nc_sameSiteCookiestrict' => 'true', ], ], - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -2025,7 +1978,7 @@ class RequestTest extends \Test\TestCase { 'nc_sameSiteCookielax' => 'true', ], ], - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -2049,7 +2002,7 @@ class RequestTest extends \Test\TestCase { 'nc_sameSiteCookiestrict' => 'true', ], ], - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -2074,7 +2027,7 @@ class RequestTest extends \Test\TestCase { 'nc_sameSiteCookiestrict' => 'false', ], ], - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -2109,7 +2062,7 @@ class RequestTest extends \Test\TestCase { 'HTTP_REQUESTTOKEN' => $invalidToken, ], ], - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream @@ -2132,7 +2085,7 @@ class RequestTest extends \Test\TestCase { ->setMethods(['getScriptName']) ->setConstructorArgs([ [], - $this->secureRandom, + $this->requestId, $this->config, $this->csrfTokenManager, $this->stream diff --git a/tests/lib/AppFramework/Middleware/MiddlewareDispatcherTest.php b/tests/lib/AppFramework/Middleware/MiddlewareDispatcherTest.php index 9a5254fb4dd..f408320971d 100644 --- a/tests/lib/AppFramework/Middleware/MiddlewareDispatcherTest.php +++ b/tests/lib/AppFramework/Middleware/MiddlewareDispatcherTest.php @@ -28,6 +28,7 @@ use OC\AppFramework\Middleware\MiddlewareDispatcher; use OCP\AppFramework\Http\Response; use OCP\AppFramework\Middleware; use OCP\IConfig; +use OCP\IRequestId; // needed to test ordering class TestMiddleware extends Middleware { @@ -129,8 +130,8 @@ class MiddlewareDispatcherTest extends \Test\TestCase { ->setConstructorArgs(['app', new Request( ['method' => 'GET'], - $this->getMockBuilder('\OCP\Security\ISecureRandom')->getMock(), - $this->getMockBuilder(IConfig::class)->getMock() + $this->createMock(IRequestId::class), + $this->createMock(IConfig::class) ) ])->getMock(); } @@ -272,7 +273,7 @@ class MiddlewareDispatcherTest extends \Test\TestCase { public function testExceptionShouldRunAfterExceptionOfOnlyPreviouslyExecutedMiddlewares() { $m1 = $this->getMiddleware(); $m2 = $this->getMiddleware(true); - $m3 = $this->getMockBuilder('\OCP\AppFramework\Middleware')->getMock(); + $m3 = $this->createMock(Middleware::class); $m3->expects($this->never()) ->method('afterException'); $m3->expects($this->never()) diff --git a/tests/lib/AppFramework/Middleware/MiddlewareTest.php b/tests/lib/AppFramework/Middleware/MiddlewareTest.php index 42e38bbd999..bd61aab4673 100644 --- a/tests/lib/AppFramework/Middleware/MiddlewareTest.php +++ b/tests/lib/AppFramework/Middleware/MiddlewareTest.php @@ -24,9 +24,12 @@ namespace Test\AppFramework\Middleware; use OC\AppFramework\Http\Request; +use OCP\AppFramework\Controller; use OCP\AppFramework\Http\Response; use OCP\AppFramework\Middleware; use OCP\IConfig; +use OCP\IRequestId; +use OC\AppFramework\DependencyInjection\DIContainer; class ChildMiddleware extends Middleware { }; @@ -49,22 +52,22 @@ class MiddlewareTest extends \Test\TestCase { $this->middleware = new ChildMiddleware(); - $this->api = $this->getMockBuilder('OC\AppFramework\DependencyInjection\DIContainer') + $this->api = $this->getMockBuilder(DIContainer::class) ->disableOriginalConstructor() ->getMock(); - $this->controller = $this->getMockBuilder('OCP\AppFramework\Controller') + $this->controller = $this->getMockBuilder(Controller::class) ->setMethods([]) ->setConstructorArgs([ $this->api, new Request( [], - $this->getMockBuilder('\OCP\Security\ISecureRandom')->getMock(), - $this->getMockBuilder(IConfig::class)->getMock() + $this->createMock(IRequestId::class), + $this->createMock(IConfig::class) ) ])->getMock(); $this->exception = new \Exception(); - $this->response = $this->getMockBuilder('OCP\AppFramework\Http\Response')->getMock(); + $this->response = $this->getMockBuilder(Response::class)->getMock(); } diff --git a/tests/lib/AppFramework/Middleware/Security/CORSMiddlewareTest.php b/tests/lib/AppFramework/Middleware/Security/CORSMiddlewareTest.php index c89f38b44f5..cc6c74c16c4 100644 --- a/tests/lib/AppFramework/Middleware/Security/CORSMiddlewareTest.php +++ b/tests/lib/AppFramework/Middleware/Security/CORSMiddlewareTest.php @@ -21,7 +21,7 @@ use OCP\AppFramework\Controller; use OCP\AppFramework\Http\JSONResponse; use OCP\AppFramework\Http\Response; use OCP\IConfig; -use OCP\Security\ISecureRandom; +use OCP\IRequestId; class CORSMiddlewareTest extends \Test\TestCase { @@ -52,7 +52,7 @@ class CORSMiddlewareTest extends \Test\TestCase { 'HTTP_ORIGIN' => 'test' ] ], - $this->createMock(ISecureRandom::class), + $this->createMock(IRequestId::class), $this->createMock(IConfig::class) ); $this->reflector->reflect($this, __FUNCTION__); @@ -71,7 +71,7 @@ class CORSMiddlewareTest extends \Test\TestCase { 'HTTP_ORIGIN' => 'test' ] ], - $this->createMock(ISecureRandom::class), + $this->createMock(IRequestId::class), $this->createMock(IConfig::class) ); $middleware = new CORSMiddleware($request, $this->reflector, $this->session, $this->throttler); @@ -88,7 +88,7 @@ class CORSMiddlewareTest extends \Test\TestCase { public function testNoOriginHeaderNoCORSHEADER() { $request = new Request( [], - $this->createMock(ISecureRandom::class), + $this->createMock(IRequestId::class), $this->createMock(IConfig::class) ); $this->reflector->reflect($this, __FUNCTION__); @@ -112,7 +112,7 @@ class CORSMiddlewareTest extends \Test\TestCase { 'HTTP_ORIGIN' => 'test' ] ], - $this->createMock(ISecureRandom::class), + $this->createMock(IRequestId::class), $this->createMock(IConfig::class) ); $this->reflector->reflect($this, __FUNCTION__); @@ -130,7 +130,7 @@ class CORSMiddlewareTest extends \Test\TestCase { public function testNoCORSShouldAllowCookieAuth() { $request = new Request( [], - $this->createMock(ISecureRandom::class), + $this->createMock(IRequestId::class), $this->createMock(IConfig::class) ); $this->reflector->reflect($this, __FUNCTION__); @@ -155,7 +155,7 @@ class CORSMiddlewareTest extends \Test\TestCase { 'PHP_AUTH_USER' => 'user', 'PHP_AUTH_PW' => 'pass' ]], - $this->createMock(ISecureRandom::class), + $this->createMock(IRequestId::class), $this->createMock(IConfig::class) ); $this->session->expects($this->once()) @@ -181,7 +181,7 @@ class CORSMiddlewareTest extends \Test\TestCase { 'PHP_AUTH_USER' => 'user', 'PHP_AUTH_PW' => 'pass' ]], - $this->createMock(ISecureRandom::class), + $this->createMock(IRequestId::class), $this->createMock(IConfig::class) ); $this->session->expects($this->once()) @@ -207,7 +207,7 @@ class CORSMiddlewareTest extends \Test\TestCase { 'PHP_AUTH_USER' => 'user', 'PHP_AUTH_PW' => 'pass' ]], - $this->createMock(ISecureRandom::class), + $this->createMock(IRequestId::class), $this->createMock(IConfig::class) ); $this->session->expects($this->once()) @@ -228,7 +228,7 @@ class CORSMiddlewareTest extends \Test\TestCase { 'PHP_AUTH_USER' => 'user', 'PHP_AUTH_PW' => 'pass' ]], - $this->createMock(ISecureRandom::class), + $this->createMock(IRequestId::class), $this->createMock(IConfig::class) ); $middleware = new CORSMiddleware($request, $this->reflector, $this->session, $this->throttler); @@ -244,7 +244,7 @@ class CORSMiddlewareTest extends \Test\TestCase { 'PHP_AUTH_USER' => 'user', 'PHP_AUTH_PW' => 'pass' ]], - $this->createMock(ISecureRandom::class), + $this->createMock(IRequestId::class), $this->createMock(IConfig::class) ); $middleware = new CORSMiddleware($request, $this->reflector, $this->session, $this->throttler); @@ -264,7 +264,7 @@ class CORSMiddlewareTest extends \Test\TestCase { 'PHP_AUTH_USER' => 'user', 'PHP_AUTH_PW' => 'pass' ]], - $this->createMock(ISecureRandom::class), + $this->createMock(IRequestId::class), $this->createMock(IConfig::class) ); $middleware = new CORSMiddleware($request, $this->reflector, $this->session, $this->throttler); diff --git a/tests/lib/AppFramework/Middleware/Security/SecurityMiddlewareTest.php b/tests/lib/AppFramework/Middleware/Security/SecurityMiddlewareTest.php index 1af2b2b9f1c..276ebe8f9ac 100644 --- a/tests/lib/AppFramework/Middleware/Security/SecurityMiddlewareTest.php +++ b/tests/lib/AppFramework/Middleware/Security/SecurityMiddlewareTest.php @@ -42,9 +42,9 @@ use OCP\IConfig; use OCP\IL10N; use OCP\INavigationManager; use OCP\IRequest; +use OCP\IRequestId; use OCP\IURLGenerator; use OCP\IUserSession; -use OCP\Security\ISecureRandom; use Psr\Log\LoggerInterface; class SecurityMiddlewareTest extends \Test\TestCase { @@ -500,7 +500,7 @@ class SecurityMiddlewareTest extends \Test\TestCase { 'REQUEST_URI' => 'nextcloud/index.php/apps/specialapp' ] ], - $this->createMock(ISecureRandom::class), + $this->createMock(IRequestId::class), $this->createMock(IConfig::class) ); $this->middleware = $this->getMiddleware(false, false, false); @@ -534,7 +534,7 @@ class SecurityMiddlewareTest extends \Test\TestCase { 'REQUEST_URI' => 'nextcloud/index.php/apps/specialapp', ], ], - $this->createMock(ISecureRandom::class), + $this->createMock(IRequestId::class), $this->createMock(IConfig::class) ); @@ -580,7 +580,7 @@ class SecurityMiddlewareTest extends \Test\TestCase { 'REQUEST_URI' => 'nextcloud/index.php/apps/specialapp' ] ], - $this->createMock(ISecureRandom::class), + $this->createMock(IRequestId::class), $this->createMock(IConfig::class) ); $this->middleware = $this->getMiddleware(false, false, false); diff --git a/tests/lib/User/SessionTest.php b/tests/lib/User/SessionTest.php index 8913e2f99c1..fcd45bab5bd 100644 --- a/tests/lib/User/SessionTest.php +++ b/tests/lib/User/SessionTest.php @@ -9,10 +9,13 @@ namespace Test\User; use OC\AppFramework\Http\Request; +use OC\Authentication\Exceptions\InvalidTokenException; +use OC\Authentication\Exceptions\PasswordLoginForbiddenException; use OC\Authentication\Token\IProvider; use OC\Authentication\Token\IToken; use OC\Security\Bruteforce\Throttler; use OC\Session\Memory; +use OC\User\LoginException; use OC\User\Manager; use OC\User\Session; use OC\User\User; @@ -23,6 +26,7 @@ use OCP\ICacheFactory; use OCP\IConfig; use OCP\ILogger; use OCP\IRequest; +use OCP\IRequestId; use OCP\ISession; use OCP\IUser; use OCP\Lockdown\ILockdownManager; @@ -30,6 +34,7 @@ use OCP\Security\ISecureRandom; use OCP\User\Events\PostLoginEvent; use PHPUnit\Framework\MockObject\MockObject; use Symfony\Component\EventDispatcher\EventDispatcherInterface; +use OC\Security\CSRF\CsrfTokenManager; /** * @group DB @@ -136,7 +141,7 @@ class SessionTest extends \Test\TestCase { ->method('getUID') ->willReturn('foo'); - $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher); + $userSession = new Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher); $userSession->setUser($user); } @@ -147,7 +152,7 @@ class SessionTest extends \Test\TestCase { $this->tokenProvider->expects($this->once()) ->method('getToken') ->with('bar') - ->will($this->throwException(new \OC\Authentication\Exceptions\InvalidTokenException())); + ->will($this->throwException(new InvalidTokenException())); $session->expects($this->exactly(2)) ->method('set') ->with($this->callback(function ($key) { @@ -217,7 +222,7 @@ class SessionTest extends \Test\TestCase { public function testLoginValidPasswordDisabled() { - $this->expectException(\OC\User\LoginException::class); + $this->expectException(LoginException::class); $session = $this->getMockBuilder(Memory::class)->setConstructorArgs([''])->getMock(); $session->expects($this->never()) @@ -227,9 +232,9 @@ class SessionTest extends \Test\TestCase { $this->tokenProvider->expects($this->once()) ->method('getToken') ->with('bar') - ->will($this->throwException(new \OC\Authentication\Exceptions\InvalidTokenException())); + ->will($this->throwException(new InvalidTokenException())); - $managerMethods = get_class_methods(\OC\User\Manager::class); + $managerMethods = get_class_methods(Manager::class); //keep following methods intact in order to ensure hooks are working $mockedManagerMethods = array_diff($managerMethods, ['__construct', 'emit', 'listen']); $manager = $this->getMockBuilder(Manager::class) @@ -257,13 +262,13 @@ class SessionTest extends \Test\TestCase { $this->dispatcher->expects($this->never()) ->method('dispatch'); - $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher); + $userSession = new Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher); $userSession->login('foo', 'bar'); } public function testLoginInvalidPassword() { $session = $this->getMockBuilder(Memory::class)->setConstructorArgs([''])->getMock(); - $managerMethods = get_class_methods(\OC\User\Manager::class); + $managerMethods = get_class_methods(Manager::class); //keep following methods intact in order to ensure hooks are working $mockedManagerMethods = array_diff($managerMethods, ['__construct', 'emit', 'listen']); $manager = $this->getMockBuilder(Manager::class) @@ -276,7 +281,7 @@ class SessionTest extends \Test\TestCase { ]) ->getMock(); $backend = $this->createMock(\Test\Util\User\Dummy::class); - $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher); + $userSession = new Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher); $user = $this->createMock(IUser::class); @@ -287,7 +292,7 @@ class SessionTest extends \Test\TestCase { $this->tokenProvider->expects($this->once()) ->method('getToken') ->with('bar') - ->will($this->throwException(new \OC\Authentication\Exceptions\InvalidTokenException())); + ->will($this->throwException(new InvalidTokenException())); $user->expects($this->never()) ->method('isEnabled'); @@ -308,7 +313,7 @@ class SessionTest extends \Test\TestCase { public function testLoginNonExisting() { $session = $this->getMockBuilder(Memory::class)->setConstructorArgs([''])->getMock(); $manager = $this->createMock(Manager::class); - $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher); + $userSession = new Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher); $session->expects($this->never()) ->method('set'); @@ -317,7 +322,7 @@ class SessionTest extends \Test\TestCase { $this->tokenProvider->expects($this->once()) ->method('getToken') ->with('bar') - ->will($this->throwException(new \OC\Authentication\Exceptions\InvalidTokenException())); + ->will($this->throwException(new InvalidTokenException())); $manager->expects($this->once()) ->method('checkPasswordNoLogging') @@ -328,13 +333,13 @@ class SessionTest extends \Test\TestCase { } public function testLogClientInNoTokenPasswordWith2fa() { - $this->expectException(\OC\Authentication\Exceptions\PasswordLoginForbiddenException::class); + $this->expectException(PasswordLoginForbiddenException::class); $manager = $this->createMock(Manager::class); $session = $this->createMock(ISession::class); $request = $this->createMock(IRequest::class); - /** @var \OC\User\Session $userSession */ + /** @var Session $userSession */ $userSession = $this->getMockBuilder(Session::class) ->setConstructorArgs([$manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher]) ->setMethods(['login', 'supportsCookies', 'createSessionToken', 'getUser']) @@ -343,7 +348,7 @@ class SessionTest extends \Test\TestCase { $this->tokenProvider->expects($this->once()) ->method('getToken') ->with('doe') - ->will($this->throwException(new \OC\Authentication\Exceptions\InvalidTokenException())); + ->will($this->throwException(new InvalidTokenException())); $this->config->expects($this->once()) ->method('getSystemValue') ->with('token_auth_enforced', false) @@ -379,7 +384,7 @@ class SessionTest extends \Test\TestCase { $this->tokenProvider->expects($this->once()) ->method('getToken') ->with('doe') - ->will($this->throwException(new \OC\Authentication\Exceptions\InvalidTokenException())); + ->will($this->throwException(new InvalidTokenException())); $this->config->expects($this->once()) ->method('getSystemValue') ->with('token_auth_enforced', false) @@ -396,7 +401,7 @@ class SessionTest extends \Test\TestCase { $session = $this->createMock(ISession::class); $request = $this->createMock(IRequest::class); - /** @var \OC\User\Session $userSession */ + /** @var Session $userSession */ $userSession = $this->getMockBuilder(Session::class) ->setConstructorArgs([$manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher]) ->setMethods(['isTokenPassword', 'login', 'supportsCookies', 'createSessionToken', 'getUser']) @@ -432,13 +437,13 @@ class SessionTest extends \Test\TestCase { public function testLogClientInNoTokenPasswordNo2fa() { - $this->expectException(\OC\Authentication\Exceptions\PasswordLoginForbiddenException::class); + $this->expectException(PasswordLoginForbiddenException::class); $manager = $this->createMock(Manager::class); $session = $this->createMock(ISession::class); $request = $this->createMock(IRequest::class); - /** @var \OC\User\Session $userSession */ + /** @var Session $userSession */ $userSession = $this->getMockBuilder(Session::class) ->setConstructorArgs([$manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher]) ->setMethods(['login', 'isTwoFactorEnforced']) @@ -447,7 +452,7 @@ class SessionTest extends \Test\TestCase { $this->tokenProvider->expects($this->once()) ->method('getToken') ->with('doe') - ->will($this->throwException(new \OC\Authentication\Exceptions\InvalidTokenException())); + ->will($this->throwException(new InvalidTokenException())); $this->config->expects($this->once()) ->method('getSystemValue') ->with('token_auth_enforced', false) @@ -477,7 +482,7 @@ class SessionTest extends \Test\TestCase { public function testRememberLoginValidToken() { $session = $this->getMockBuilder(Memory::class)->setConstructorArgs([''])->getMock(); - $managerMethods = get_class_methods(\OC\User\Manager::class); + $managerMethods = get_class_methods(Manager::class); //keep following methods intact in order to ensure hooks are working $mockedManagerMethods = array_diff($managerMethods, ['__construct', 'emit', 'listen']); $manager = $this->getMockBuilder(Manager::class) @@ -567,7 +572,7 @@ class SessionTest extends \Test\TestCase { public function testRememberLoginInvalidSessionToken() { $session = $this->getMockBuilder(Memory::class)->setConstructorArgs([''])->getMock(); - $managerMethods = get_class_methods(\OC\User\Manager::class); + $managerMethods = get_class_methods(Manager::class); //keep following methods intact in order to ensure hooks are working $mockedManagerMethods = array_diff($managerMethods, ['__construct', 'emit', 'listen']); $manager = $this->getMockBuilder(Manager::class) @@ -612,7 +617,7 @@ class SessionTest extends \Test\TestCase { $this->tokenProvider->expects($this->once()) ->method('renewSessionToken') ->with($oldSessionId, $sessionId) - ->will($this->throwException(new \OC\Authentication\Exceptions\InvalidTokenException())); + ->will($this->throwException(new InvalidTokenException())); $user->expects($this->never()) ->method('getUID') @@ -632,7 +637,7 @@ class SessionTest extends \Test\TestCase { public function testRememberLoginInvalidToken() { $session = $this->getMockBuilder(Memory::class)->setConstructorArgs([''])->getMock(); - $managerMethods = get_class_methods(\OC\User\Manager::class); + $managerMethods = get_class_methods(Manager::class); //keep following methods intact in order to ensure hooks are working $mockedManagerMethods = array_diff($managerMethods, ['__construct', 'emit', 'listen']); $manager = $this->getMockBuilder(Manager::class) @@ -685,7 +690,7 @@ class SessionTest extends \Test\TestCase { public function testRememberLoginInvalidUser() { $session = $this->getMockBuilder(Memory::class)->setConstructorArgs([''])->getMock(); - $managerMethods = get_class_methods(\OC\User\Manager::class); + $managerMethods = get_class_methods(Manager::class); //keep following methods intact in order to ensure hooks are working $mockedManagerMethods = array_diff($managerMethods, ['__construct', 'emit', 'listen']); $manager = $this->getMockBuilder(Manager::class) @@ -735,7 +740,7 @@ class SessionTest extends \Test\TestCase { 'bar' => new User('bar', null, $this->createMock(EventDispatcherInterface::class)) ]; - $manager = $this->getMockBuilder('\OC\User\Manager') + $manager = $this->getMockBuilder(Manager::class) ->disableOriginalConstructor() ->getMock(); @@ -768,18 +773,18 @@ class SessionTest extends \Test\TestCase { $manager = $this->createMock(Manager::class); $session = $this->createMock(ISession::class); $user = $this->createMock(IUser::class); - $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher); + $userSession = new Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher); - $random = $this->createMock(ISecureRandom::class); + $requestId = $this->createMock(IRequestId::class); $config = $this->createMock(IConfig::class); - $csrf = $this->getMockBuilder('\OC\Security\CSRF\CsrfTokenManager') + $csrf = $this->getMockBuilder(CsrfTokenManager::class) ->disableOriginalConstructor() ->getMock(); - $request = new \OC\AppFramework\Http\Request([ + $request = new Request([ 'server' => [ 'HTTP_USER_AGENT' => 'Firefox', ] - ], $random, $config, $csrf); + ], $requestId, $config, $csrf); $uid = 'user123'; $loginName = 'User123'; @@ -796,7 +801,7 @@ class SessionTest extends \Test\TestCase { $this->tokenProvider->expects($this->once()) ->method('getToken') ->with($password) - ->will($this->throwException(new \OC\Authentication\Exceptions\InvalidTokenException())); + ->will($this->throwException(new InvalidTokenException())); $this->tokenProvider->expects($this->once()) ->method('generateToken') @@ -809,18 +814,18 @@ class SessionTest extends \Test\TestCase { $manager = $this->createMock(Manager::class); $session = $this->createMock(ISession::class); $user = $this->createMock(IUser::class); - $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher); + $userSession = new Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher); - $random = $this->createMock(ISecureRandom::class); + $requestId = $this->createMock(IRequestId::class); $config = $this->createMock(IConfig::class); - $csrf = $this->getMockBuilder('\OC\Security\CSRF\CsrfTokenManager') + $csrf = $this->getMockBuilder(CsrfTokenManager::class) ->disableOriginalConstructor() ->getMock(); - $request = new \OC\AppFramework\Http\Request([ + $request = new Request([ 'server' => [ 'HTTP_USER_AGENT' => 'Firefox', ] - ], $random, $config, $csrf); + ], $requestId, $config, $csrf); $uid = 'user123'; $loginName = 'User123'; @@ -837,7 +842,7 @@ class SessionTest extends \Test\TestCase { $this->tokenProvider->expects($this->once()) ->method('getToken') ->with($password) - ->will($this->throwException(new \OC\Authentication\Exceptions\InvalidTokenException())); + ->will($this->throwException(new InvalidTokenException())); $this->tokenProvider->expects($this->once()) ->method('generateToken') @@ -847,24 +852,24 @@ class SessionTest extends \Test\TestCase { } public function testCreateSessionTokenWithTokenPassword() { - $manager = $this->getMockBuilder('\OC\User\Manager') + $manager = $this->getMockBuilder(Manager::class) ->disableOriginalConstructor() ->getMock(); $session = $this->createMock(ISession::class); $token = $this->createMock(IToken::class); $user = $this->createMock(IUser::class); - $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher); + $userSession = new Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher); - $random = $this->createMock(ISecureRandom::class); + $requestId = $this->createMock(IRequestId::class); $config = $this->createMock(IConfig::class); - $csrf = $this->getMockBuilder('\OC\Security\CSRF\CsrfTokenManager') + $csrf = $this->getMockBuilder(CsrfTokenManager::class) ->disableOriginalConstructor() ->getMock(); - $request = new \OC\AppFramework\Http\Request([ + $request = new Request([ 'server' => [ 'HTTP_USER_AGENT' => 'Firefox', ] - ], $random, $config, $csrf); + ], $requestId, $config, $csrf); $uid = 'user123'; $loginName = 'User123'; @@ -896,11 +901,11 @@ class SessionTest extends \Test\TestCase { } public function testCreateSessionTokenWithNonExistentUser() { - $manager = $this->getMockBuilder('\OC\User\Manager') + $manager = $this->getMockBuilder(Manager::class) ->disableOriginalConstructor() ->getMock(); $session = $this->createMock(ISession::class); - $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher); + $userSession = new Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher); $request = $this->createMock(IRequest::class); $uid = 'user123'; |