|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the API Platform project. |
| 5 | + * |
| 6 | + * (c) Kévin Dunglas <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace ApiPlatform\Tests\Functional\Parameters; |
| 15 | + |
| 16 | +use ApiPlatform\Symfony\Bundle\Test\ApiTestCase; |
| 17 | +use Symfony\Component\HttpFoundation\Response; |
| 18 | +use Symfony\Component\Security\Core\User\InMemoryUser; |
| 19 | + |
| 20 | +class SecurityTests extends ApiTestCase |
| 21 | +{ |
| 22 | + public function dataUserAuthorization(): iterable |
| 23 | + { |
| 24 | + yield [['ROLE_ADMIN'], Response::HTTP_OK]; |
| 25 | + yield [['ROLE_USER'], Response::HTTP_FORBIDDEN]; |
| 26 | + } |
| 27 | + |
| 28 | + /** @dataProvider dataUserAuthorization */ |
| 29 | + public function testUserAuthorization(array $roles, int $expectedStatusCode): void |
| 30 | + { |
| 31 | + $client = self::createClient(); |
| 32 | + $client->loginUser(new InMemoryUser('emmanuel', 'password', $roles)); |
| 33 | + |
| 34 | + $client->request('GET', 'with_security_parameters_collection?name=foo'); |
| 35 | + $this->assertResponseStatusCodeSame($expectedStatusCode); |
| 36 | + } |
| 37 | + |
| 38 | + public function testNoValueParameter(): void |
| 39 | + { |
| 40 | + $client = self::createClient(); |
| 41 | + $client->loginUser(new InMemoryUser('emmanuel', 'password', ['ROLE_ADMIN'])); |
| 42 | + |
| 43 | + $client->request('GET', 'with_security_parameters_collection?name'); |
| 44 | + $this->assertResponseIsSuccessful(); |
| 45 | + } |
| 46 | + |
| 47 | + public function dataSecurityValues(): iterable |
| 48 | + { |
| 49 | + yield ['secured', Response::HTTP_OK]; |
| 50 | + yield ['not_the_expected_parameter_value', Response::HTTP_UNAUTHORIZED]; |
| 51 | + } |
| 52 | + |
| 53 | + /** @dataProvider dataSecurityValues */ |
| 54 | + public function testSecurityHeaderValues(string $parameterValue, int $expectedStatusCode): void |
| 55 | + { |
| 56 | + self::createClient()->request('GET', 'with_security_parameters_collection', [ |
| 57 | + 'headers' => [ |
| 58 | + 'auth' => $parameterValue, |
| 59 | + ], |
| 60 | + ]); |
| 61 | + $this->assertResponseStatusCodeSame($expectedStatusCode); |
| 62 | + } |
| 63 | + |
| 64 | + /** @dataProvider dataSecurityValues */ |
| 65 | + public function testSecurityQueryValues(string $parameterValue, int $expectedStatusCode): void |
| 66 | + { |
| 67 | + self::createClient()->request('GET', sprintf('with_security_parameters_collection?secret=%s', $parameterValue)); |
| 68 | + $this->assertResponseStatusCodeSame($expectedStatusCode); |
| 69 | + } |
| 70 | +} |
0 commit comments