Skip to content

Commit

Permalink
Fixing tests after upgrades.
Browse files Browse the repository at this point in the history
  • Loading branch information
krulis-martin committed Jun 8, 2024
1 parent a12aec7 commit ebd0bf9
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 44 deletions.
9 changes: 4 additions & 5 deletions app/V1Module/presenters/SecurityPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
);
Expand Down
29 changes: 17 additions & 12 deletions app/V1Module/presenters/base/ApiErrorPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -32,7 +35,7 @@ class ApiErrorPresenter extends BasePresenter
/**
* @param Exception $exception
* @return void
* @throws \Nette\Application\AbortException
* @throws AbortException
*/
public function renderDefault($exception)
{
Expand All @@ -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();
Expand All @@ -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);
}
}
}
Expand All @@ -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,
Expand All @@ -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();
Expand Down
6 changes: 2 additions & 4 deletions app/V1Module/router/DeleteRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
6 changes: 2 additions & 4 deletions app/V1Module/router/GetRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
6 changes: 2 additions & 4 deletions app/V1Module/router/MethodRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
*/
class MethodRoute implements Router
{

/** @var string */
private $method;

Expand All @@ -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);
}

/**
Expand Down
6 changes: 2 additions & 4 deletions app/V1Module/router/PostRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
6 changes: 2 additions & 4 deletions app/V1Module/router/PutRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
5 changes: 3 additions & 2 deletions app/helpers/FileStorage/LocalStorage/LocalHashFileStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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;
}

Expand Down
8 changes: 4 additions & 4 deletions tests/AccessToken/AccessManager.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -243,31 +243,31 @@ 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));
}

public function testExtractFromHeaderWrongType()
{
$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));
}

public function testExtractFromHeaderEmpty()
{
$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));
}

public function testExtractFromHeaderWithSpace()
{
$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));
}
}
Expand Down
5 changes: 4 additions & 1 deletion tests/Security/UserStorage.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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))]
);

Expand Down

0 comments on commit ebd0bf9

Please sign in to comment.