From 219f7de4555b143bf5adaa7078ca204ea31d74b3 Mon Sep 17 00:00:00 2001 From: "amelie.haladjian" Date: Mon, 18 Nov 2024 17:35:50 +0100 Subject: [PATCH] feat(api tester): add test on filtered tokens --- tests/Preparator/ExamplesPreparatorTest.php | 98 +++++++++++++++++++++ 1 file changed, 98 insertions(+) diff --git a/tests/Preparator/ExamplesPreparatorTest.php b/tests/Preparator/ExamplesPreparatorTest.php index b5b6697..6ffab34 100644 --- a/tests/Preparator/ExamplesPreparatorTest.php +++ b/tests/Preparator/ExamplesPreparatorTest.php @@ -4,14 +4,18 @@ namespace APITester\Tests\Preparator; +use APITester\Config\Filters; use APITester\Definition\Api; use APITester\Definition\Body; +use APITester\Definition\Collection\Scopes; use APITester\Definition\Example\BodyExample; use APITester\Definition\Example\OperationExample; use APITester\Definition\Example\ResponseExample; use APITester\Definition\Operation; use APITester\Definition\Parameter; use APITester\Definition\Response as DefinitionResponse; +use APITester\Definition\Security\HttpSecurity; +use APITester\Definition\Token; use APITester\Preparator\Config\ExamplesPreparatorConfig; use APITester\Preparator\ExamplesPreparator; use APITester\Test\TestCase; @@ -43,6 +47,7 @@ public function testPrepare(Api $api, array $expected): void { $preparator = new ExamplesPreparator(); + $this->addTokens($preparator); $preparator->configure([]); Assert::objectsEqual( $expected, @@ -376,5 +381,98 @@ public function getExpectedTestSuites(): iterable ), ], ]; + + yield 'with filtered tokens' => [ + Api::create()->addOperation( + Operation::create('filtered_token_test', '/tokens') + ->addSecurity( + HttpSecurity::create( + 'bearer_test', + 'bearer', + scopes: Scopes::fromNames(['scope5']) + ) + ) + ->addResponse(DefinitionResponse::create(200)) + ->addExample( + OperationExample::create('200.default') + ->setResponse(new ResponseExample()) + ) + ), + [ + new TestCase( + ExamplesPreparator::getName() . ' - filtered_token_test - 200.default', + OperationExample::create('filtered_token_test') + ->setPath('/tokens') + ->setHeaders(['Authorization'=> ['Bearer 3333']]) + ->setResponse(ResponseExample::create('200')), + ), + ], + ]; + + yield 'without filtered tokens but multiple options' => [ + Api::create()->addOperation( + Operation::create('unfiltered_token_test', '/tokens') + ->addSecurity( + HttpSecurity::create( + 'bearer_test', + 'bearer', + scopes: Scopes::fromNames(['scope5']) + ) + ) + ->addResponse(DefinitionResponse::create(200)) + ->addExample( + OperationExample::create('200.default') + ->setResponse(new ResponseExample()) + ) + ), + [ + new TestCase( + ExamplesPreparator::getName() . ' - unfiltered_token_test - 200.default', + OperationExample::create('unfiltered_token_test') + ->setPath('/tokens') + ->setHeaders(['Authorization'=> ['Bearer 1111']]) + ->setResponse(ResponseExample::create('200')), + ), + ], + ]; + } + + private function addTokens(ExamplesPreparator $preparator): void + { + $preparator->addToken( + new Token( + 'token1', + 'oauth2_implicit', + '1111', + [ + 'scope1', + 'scope2', + 'scope5', + ], + ) + ) + ->addToken( + new Token( + 'token2', + 'oauth2_implicit', + '2222', + [ + 'scope3', + 'scope4', + ], + ) + ) + ->addToken( + new Token( + 'token3', + 'oauth2_implicit', + '3333', + [ + 'scope5', + ], + filters: new Filters(include: [['id' => 'filtered_token_test']]) + ) + ) + ; } }