-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add 403UserCentricPreparator to handle random parameters that generat…
…es a 403 error because the user is not allowed to browse them
- Loading branch information
Showing
4 changed files
with
110 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace APITester\Preparator; | ||
|
||
use APITester\Definition\Collection\Operations; | ||
use APITester\Definition\Example\OperationExample; | ||
use APITester\Definition\Example\ResponseExample; | ||
use APITester\Definition\Operation; | ||
use APITester\Definition\Response as DefinitionResponse; | ||
use APITester\Test\TestCase; | ||
|
||
final class Error403UserCentricPreparator extends TestCasesPreparator | ||
{ | ||
public function getStatusCode(): string | ||
{ | ||
return '403'; | ||
} | ||
/** | ||
* @inheritDoc | ||
*/ | ||
protected function prepare(Operations $operations): iterable | ||
{ | ||
/** @var iterable<array-key, TestCase> */ | ||
return $operations | ||
->filter(fn ($operation) => $operation->isUserCentric()) | ||
->values() | ||
->map(function ($operation) { | ||
return $this->prepareTestCase($operation); | ||
}) | ||
->flatten() | ||
; | ||
} | ||
|
||
/** | ||
* @return array<TestCase> | ||
*/ | ||
private function prepareTestCase(Operation $operation): array | ||
{ | ||
$testcases = []; | ||
|
||
if ($operation->getRequestBodies()->count() === 0) { | ||
$testcases[] = $this->buildTestCase( | ||
OperationExample::create('RandomPath', $operation) | ||
->setForceRandom() | ||
->setResponse( | ||
ResponseExample::create() | ||
->setStatusCode($this->config->response->getStatusCode() ?? '403') | ||
->setHeaders($this->config->response->headers ?? []) | ||
->setContent($this->config->response->body ?? null) | ||
) | ||
); | ||
} | ||
|
||
foreach ($operation->getRequestBodies() as $ignored) { | ||
$testcases[] = $this->buildTestCase( | ||
OperationExample::create('RandomPath', $operation) | ||
->setForceRandom() | ||
->setResponse( | ||
ResponseExample::create() | ||
->setStatusCode($this->config->response->getStatusCode() ?? '403') | ||
->setHeaders($this->config->response->headers ?? []) | ||
->setContent($this->config->response->body ?? null) | ||
) | ||
); | ||
} | ||
|
||
return $testcases; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters