|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace PhpList\RestBundle\Tests\System\Controller; |
| 5 | + |
| 6 | +use GuzzleHttp\Client; |
| 7 | +use PhpList\PhpList4\TestingSupport\Traits\SymfonyServerTrait; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | +use Symfony\Component\HttpFoundation\Response; |
| 10 | + |
| 11 | +/** |
| 12 | + * Testcase. |
| 13 | + * |
| 14 | + * @author Oliver Klee <[email protected]> |
| 15 | + */ |
| 16 | +class SessionControllerTest extends TestCase |
| 17 | +{ |
| 18 | + use SymfonyServerTrait; |
| 19 | + |
| 20 | + /** |
| 21 | + * @var Client |
| 22 | + */ |
| 23 | + private $httpClient = null; |
| 24 | + |
| 25 | + protected function setUp() |
| 26 | + { |
| 27 | + $this->httpClient = new Client(['http_errors' => false]); |
| 28 | + } |
| 29 | + |
| 30 | + protected function tearDown() |
| 31 | + { |
| 32 | + $this->stopSymfonyServer(); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * @return string[][] |
| 37 | + */ |
| 38 | + public function environmentDataProvider(): array |
| 39 | + { |
| 40 | + return [ |
| 41 | + 'test' => ['test'], |
| 42 | + 'dev' => ['dev'], |
| 43 | + ]; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * @test |
| 48 | + * @param string $environment |
| 49 | + * @dataProvider environmentDataProvider |
| 50 | + */ |
| 51 | + public function postSessionsWithInvalidCredentialsReturnsNotAuthorized(string $environment) |
| 52 | + { |
| 53 | + $this->startSymfonyServer($environment); |
| 54 | + |
| 55 | + $loginName = 'john.doe'; |
| 56 | + $password = 'a sandwich and a cup of coffee'; |
| 57 | + $jsonData = ['login_name' => $loginName, 'password' => $password]; |
| 58 | + |
| 59 | + $response = $this->httpClient->post( |
| 60 | + '/api/v2/sessions', |
| 61 | + ['base_uri' => $this->getBaseUrl(), 'body' => \json_encode($jsonData)] |
| 62 | + ); |
| 63 | + self::assertSame(Response::HTTP_UNAUTHORIZED, $response->getStatusCode()); |
| 64 | + self::assertSame( |
| 65 | + [ |
| 66 | + 'code' => Response::HTTP_UNAUTHORIZED, |
| 67 | + 'message' => 'Not authorized', |
| 68 | + ], |
| 69 | + \json_decode($response->getBody()->getContents(), true) |
| 70 | + ); |
| 71 | + } |
| 72 | +} |
0 commit comments