|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the FOSRestBundle package. |
| 5 | + * |
| 6 | + * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
| 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 | +namespace FOS\RestBundle\Tests\Functional; |
| 13 | + |
| 14 | +use Symfony\Component\ErrorHandler\ErrorRenderer\ErrorRendererInterface; |
| 15 | + |
| 16 | +abstract class AbstractAuthenticatorTestCase extends WebTestCase |
| 17 | +{ |
| 18 | + protected static $client; |
| 19 | + |
| 20 | + public static function setUpBeforeClass() |
| 21 | + { |
| 22 | + if (!interface_exists(ErrorRendererInterface::class)) { |
| 23 | + self::markTestSkipped(); |
| 24 | + } |
| 25 | + |
| 26 | + parent::setUpBeforeClass(); |
| 27 | + |
| 28 | + self::$client = self::createClient(['test_case' => static::getTestCase()]); |
| 29 | + } |
| 30 | + |
| 31 | + public static function tearDownAfterClass() |
| 32 | + { |
| 33 | + self::deleteTmpDir(static::getTestCase()); |
| 34 | + |
| 35 | + parent::tearDownAfterClass(); |
| 36 | + } |
| 37 | + |
| 38 | + public function testNoCredentialsGives401() |
| 39 | + { |
| 40 | + self::$client->request('POST', '/api/login', [], [], ['CONTENT_TYPE' => 'application/json']); |
| 41 | + $response = self::$client->getResponse(); |
| 42 | + |
| 43 | + $this->assertEquals(401, $response->getStatusCode()); |
| 44 | + $this->assertEquals('application/json', $response->headers->get('Content-Type')); |
| 45 | + } |
| 46 | + |
| 47 | + public function testWrongCredentialsGives401() |
| 48 | + { |
| 49 | + $this->sendRequestContainingInvalidCredentials('/api/login'); |
| 50 | + |
| 51 | + $response = self::$client->getResponse(); |
| 52 | + |
| 53 | + $this->assertEquals(401, $response->getStatusCode()); |
| 54 | + $this->assertEquals('application/json', $response->headers->get('Content-Type')); |
| 55 | + } |
| 56 | + |
| 57 | + public function testSuccessfulLogin() |
| 58 | + { |
| 59 | + $this->sendRequestContainingValidCredentials('/api/login'); |
| 60 | + |
| 61 | + $response = self::$client->getResponse(); |
| 62 | + |
| 63 | + $this->assertEquals(200, $response->getStatusCode()); |
| 64 | + $this->assertEquals('application/json', $response->headers->get('Content-Type')); |
| 65 | + } |
| 66 | + |
| 67 | + public function testAccessDeniedExceptionGives403() |
| 68 | + { |
| 69 | + $this->sendRequestContainingValidCredentials('/api/comments'); |
| 70 | + |
| 71 | + $response = self::$client->getResponse(); |
| 72 | + |
| 73 | + $this->assertEquals(403, $response->getStatusCode()); |
| 74 | + $this->assertEquals('application/json', $response->headers->get('Content-Type')); |
| 75 | + } |
| 76 | + |
| 77 | + abstract protected static function getTestCase(): string; |
| 78 | + |
| 79 | + abstract protected function sendRequestContainingInvalidCredentials(string $path): void; |
| 80 | + |
| 81 | + abstract protected function sendRequestContainingValidCredentials(string $path): void; |
| 82 | +} |
0 commit comments