From ebd0bf9e29130da5c177184ed5bcc2b696848fe0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kruli=C5=A1?= Date: Fri, 7 Jun 2024 23:54:39 +0200 Subject: [PATCH] Fixing tests after upgrades. --- app/V1Module/presenters/SecurityPresenter.php | 9 +++--- .../presenters/base/ApiErrorPresenter.php | 29 +++++++++++-------- app/V1Module/router/DeleteRoute.php | 6 ++-- app/V1Module/router/GetRoute.php | 6 ++-- app/V1Module/router/MethodRoute.php | 6 ++-- app/V1Module/router/PostRoute.php | 6 ++-- app/V1Module/router/PutRoute.php | 6 ++-- .../LocalStorage/LocalHashFileStorage.php | 5 ++-- tests/AccessToken/AccessManager.phpt | 8 ++--- tests/Security/UserStorage.phpt | 5 +++- 10 files changed, 42 insertions(+), 44 deletions(-) diff --git a/app/V1Module/presenters/SecurityPresenter.php b/app/V1Module/presenters/SecurityPresenter.php index ecb724851..476e41c33 100644 --- a/app/V1Module/presenters/SecurityPresenter.php +++ b/app/V1Module/presenters/SecurityPresenter.php @@ -33,11 +33,10 @@ public function actionCheck() $requestParams = $this->router->match( new Http\Request( new Http\UrlScript("https://foo.tld/" . ltrim($this->getRequest()->getPost("url"), "/"), "/"), - null, - null, - null, - null, - null, + [], + [], + [], + [], $this->getRequest()->getPost("method") ) ); diff --git a/app/V1Module/presenters/base/ApiErrorPresenter.php b/app/V1Module/presenters/base/ApiErrorPresenter.php index f4a753f0b..e295c430c 100644 --- a/app/V1Module/presenters/base/ApiErrorPresenter.php +++ b/app/V1Module/presenters/base/ApiErrorPresenter.php @@ -6,11 +6,14 @@ use App\Exceptions\FrontendErrorMappings; use App\Helpers\UserActions; use App\Presenters\BasePresenter; -use Exception; +use App\Security\UserStorage; use Nette\Http\IResponse; use Nette\Application\BadRequestException; +use Nette\Application\AbortException; use Doctrine\DBAL\Exception\ConnectionException; use Tracy\ILogger; +use Exception; +use Throwable; /** * The error presenter for the API module - all responses are served as JSONs with a fixed format. @@ -32,7 +35,7 @@ class ApiErrorPresenter extends BasePresenter /** * @param Exception $exception * @return void - * @throws \Nette\Application\AbortException + * @throws AbortException */ public function renderDefault($exception) { @@ -58,9 +61,9 @@ public function renderDefault($exception) /** * Send an error response based on a known type of exceptions - derived from ApiException * @param ApiException $exception The exception which caused the error - * @throws \Nette\Application\AbortException + * @throws AbortException */ - protected function handleAPIException(ApiException $exception) + public function handleAPIException(ApiException $exception) { $res = $this->getHttpResponse(); $additionalHeaders = $exception->getAdditionalHttpHeaders(); @@ -78,20 +81,20 @@ protected function handleAPIException(ApiException $exception) /** * Simply logs given exception into standard logger. Some filtering or * further modifications can be engaged. - * @param \Throwable $exception Exception which should be logged + * @param Throwable $ex Exception which should be logged */ - protected function handleLogging(\Throwable $exception) + public function handleLogging(Throwable $ex) { - if ($exception instanceof BadRequestException) { + if ($ex instanceof BadRequestException) { // nothing to log here } else { - if ($exception instanceof ApiException && $exception->getCode() < 500) { + if ($ex instanceof ApiException && $ex->getCode() < 500) { $this->logger->log( - "HTTP code {$exception->getCode()}: {$exception->getMessage()} in {$exception->getFile()}:{$exception->getLine()}", + "HTTP code {$ex->getCode()}: {$ex->getMessage()} in {$ex->getFile()}:{$ex->getLine()}", 'access' ); } else { - $this->logger->log($exception, ILogger::EXCEPTION); + $this->logger->log($ex, ILogger::EXCEPTION); } } } @@ -103,7 +106,7 @@ protected function handleLogging(\Throwable $exception) * @param string $frontendErrorCode custom defined, far more fine-grained exception code * @param mixed $frontendErrorParams parameters belonging to error * @return void - * @throws \Nette\Application\AbortException + * @throws AbortException */ protected function sendErrorResponse( int $code, @@ -114,7 +117,9 @@ protected function sendErrorResponse( // calling user->isLoggedIn results in throwing exception in case of // invalid token (after update to nette/security:v3.1), therefore we // need to call our UserStorage directly - if ($this->getUser()->getStorage()->isAuthenticated()) { + /** @var UserStorage */ + $storage = $this->getUser()->getStorage(); + if ($storage->isAuthenticated()) { // log the action done by the current user // determine the action name from the application request $req = $this->getRequest(); diff --git a/app/V1Module/router/DeleteRoute.php b/app/V1Module/router/DeleteRoute.php index 3851594d4..4bcbca84f 100644 --- a/app/V1Module/router/DeleteRoute.php +++ b/app/V1Module/router/DeleteRoute.php @@ -7,14 +7,12 @@ */ class DeleteRoute extends MethodRoute { - /** * @param string $mask Mask for the Nette\Application\Routers\Route * @param string|array $metadata Metadata for the Nette\Application\Routers\Route - * @param int $flags Flags for the Nette\Application\Routers\Route */ - public function __construct(string $mask, $metadata = [], int $flags = 0) + public function __construct(string $mask, $metadata = []) { - parent::__construct("DELETE", $mask, $metadata, $flags); + parent::__construct("DELETE", $mask, $metadata); } } diff --git a/app/V1Module/router/GetRoute.php b/app/V1Module/router/GetRoute.php index 9b9993d34..edbf7af47 100644 --- a/app/V1Module/router/GetRoute.php +++ b/app/V1Module/router/GetRoute.php @@ -7,14 +7,12 @@ */ class GetRoute extends MethodRoute { - /** * @param string $mask Mask for the Nette\Application\Routers\Route * @param string|array $metadata Metadata for the Nette\Application\Routers\Route - * @param int $flags Flags for the Nette\Application\Routers\Route */ - public function __construct(string $mask, $metadata = [], int $flags = 0) + public function __construct(string $mask, $metadata = []) { - parent::__construct("GET", $mask, $metadata, $flags); + parent::__construct("GET", $mask, $metadata); } } diff --git a/app/V1Module/router/MethodRoute.php b/app/V1Module/router/MethodRoute.php index 54b03478f..03ffe4776 100644 --- a/app/V1Module/router/MethodRoute.php +++ b/app/V1Module/router/MethodRoute.php @@ -14,7 +14,6 @@ */ class MethodRoute implements Router { - /** @var string */ private $method; @@ -25,12 +24,11 @@ class MethodRoute implements Router * @param string $method The HTTP method which is accepted by this route * @param string $mask Mask for the Nette\Application\Routers\Route * @param string|array $metadata Metadata for the Nette\Application\Routers\Route - * @param int $flags Flags for the Nette\Application\Routers\Route */ - public function __construct(string $method, string $mask, $metadata = [], int $flags = 0) + public function __construct(string $method, string $mask, $metadata = []) { $this->method = $method; - $this->route = new Route($mask, $metadata, $flags); + $this->route = new Route($mask, $metadata); } /** diff --git a/app/V1Module/router/PostRoute.php b/app/V1Module/router/PostRoute.php index 05f07a19f..547b15edc 100644 --- a/app/V1Module/router/PostRoute.php +++ b/app/V1Module/router/PostRoute.php @@ -7,14 +7,12 @@ */ class PostRoute extends MethodRoute { - /** * @param string $mask Mask for the Nette\Application\Routers\Route * @param string|array $metadata Metadata for the Nette\Application\Routers\Route - * @param int $flags Flags for the Nette\Application\Routers\Route */ - public function __construct(string $mask, $metadata = [], int $flags = 0) + public function __construct(string $mask, $metadata = []) { - parent::__construct("POST", $mask, $metadata, $flags); + parent::__construct("POST", $mask, $metadata); } } diff --git a/app/V1Module/router/PutRoute.php b/app/V1Module/router/PutRoute.php index a83197086..188aea0ab 100644 --- a/app/V1Module/router/PutRoute.php +++ b/app/V1Module/router/PutRoute.php @@ -7,14 +7,12 @@ */ class PutRoute extends MethodRoute { - /** * @param string $mask Mask for the Nette\Application\Routers\Route * @param string|array $metadata Metadata for the Nette\Application\Routers\Route - * @param int $flags Flags for the Nette\Application\Routers\Route */ - public function __construct(string $mask, $metadata = [], int $flags = 0) + public function __construct(string $mask, $metadata = []) { - parent::__construct("PUT", $mask, $metadata, $flags); + parent::__construct("PUT", $mask, $metadata); } } diff --git a/app/helpers/FileStorage/LocalStorage/LocalHashFileStorage.php b/app/helpers/FileStorage/LocalStorage/LocalHashFileStorage.php index d194128be..c54b9e3ec 100644 --- a/app/helpers/FileStorage/LocalStorage/LocalHashFileStorage.php +++ b/app/helpers/FileStorage/LocalStorage/LocalHashFileStorage.php @@ -105,7 +105,8 @@ public function storeFile(string $path, bool $move = true): string } } } - + + // @phpstan-ignore booleanAnd.rightAlwaysTrue if ($move && file_exists($path)) { @unlink($path); // the file was copied or already exists, lets simulate move } @@ -123,7 +124,7 @@ public function storeContents($contents): string if (file_put_contents($newPath, $contents) === false) { throw new FileStorageException("Saving contents into hash store failed.", $newPath); } - + return $hash; } diff --git a/tests/AccessToken/AccessManager.phpt b/tests/AccessToken/AccessManager.phpt index ade3fcb2d..be6ecdec5 100644 --- a/tests/AccessToken/AccessManager.phpt +++ b/tests/AccessToken/AccessManager.phpt @@ -243,7 +243,7 @@ class TestAccessManager extends Tester\TestCase { $token = "abcdefg"; $url = new UrlScript("https://www.whatever.com/bla/bla/bla?x=y"); - $request = new Request($url, null, null, null, ["Authorization" => "Bearer $token"]); + $request = new Request($url, [], [], [], ["Authorization" => "Bearer $token"]); Assert::equal($token, AccessManager::getGivenAccessToken($request)); } @@ -251,7 +251,7 @@ class TestAccessManager extends Tester\TestCase { $token = "abcdefg"; $url = new UrlScript("https://www.whatever.com/bla/bla/bla?x=y"); - $request = new Request($url, null, null, null, ["Authorization" => "Basic $token"]); + $request = new Request($url, [], [], [], ["Authorization" => "Basic $token"]); Assert::null(AccessManager::getGivenAccessToken($request)); } @@ -259,7 +259,7 @@ class TestAccessManager extends Tester\TestCase { $token = ""; $url = new UrlScript("https://www.whatever.com/bla/bla/bla?x=y"); - $request = new Request($url, null, null, null, ["Authorization" => "Basic $token"]); + $request = new Request($url, [], [], [], ["Authorization" => "Basic $token"]); Assert::null(AccessManager::getGivenAccessToken($request)); } @@ -267,7 +267,7 @@ class TestAccessManager extends Tester\TestCase { $token = ""; $url = new UrlScript("https://www.whatever.com/bla/bla/bla?x=y"); - $request = new Request($url, null, null, null, ["Authorization" => "Bearer $token and more!"]); + $request = new Request($url, [], [], [], ["Authorization" => "Bearer $token and more!"]); Assert::null(AccessManager::getGivenAccessToken($request)); } } diff --git a/tests/Security/UserStorage.phpt b/tests/Security/UserStorage.phpt index 668e2426e..eefbd2f99 100644 --- a/tests/Security/UserStorage.phpt +++ b/tests/Security/UserStorage.phpt @@ -43,7 +43,10 @@ class TestUserStorage extends Tester\TestCase $verificationKey = $this->container->parameters["accessManager"]["verificationKey"]; $usedAlgorithm = $this->container->parameters["accessManager"]["usedAlgorithm"]; $httpRequest = new Http\Request( - new Http\UrlScript("/hello"), null, null, null, + new Http\UrlScript("/hello"), + [], + [], + [], ["Authorization" => sprintf("Bearer %s", $token->encode($verificationKey, $usedAlgorithm))] );