|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace App\Tests\Functional\ApiKey\Transport\Controller\Api\v1; |
| 6 | + |
| 7 | +use App\ApiKey\Application\Resource\ApiKeyResource; |
| 8 | +use App\ApiKey\Domain\Entity\ApiKey; |
| 9 | +use App\ApiKey\Infrastructure\DataFixtures\ORM\LoadApiKeyData; |
| 10 | +use App\General\Domain\Utils\JSON; |
| 11 | +use App\General\Transport\Utils\Tests\WebTestCase; |
| 12 | +use App\User\Domain\Entity\UserGroup; |
| 13 | +use App\User\Infrastructure\DataFixtures\ORM\LoadUserGroupData; |
| 14 | +use Generator; |
| 15 | +use Symfony\Component\HttpFoundation\Response; |
| 16 | +use Throwable; |
| 17 | + |
| 18 | +/** |
| 19 | + * Class ApiKeyControllerTest |
| 20 | + * |
| 21 | + * @package App\Tests |
| 22 | + */ |
| 23 | +class ApiKeyControllerTest extends WebTestCase |
| 24 | +{ |
| 25 | + private string $baseUrl = self::API_URL_PREFIX . '/v1/api_key'; |
| 26 | + |
| 27 | + /** |
| 28 | + * @testdox Test that `GET /v1/api_key` request returns `401` for non-logged user. |
| 29 | + * |
| 30 | + * @throws Throwable |
| 31 | + */ |
| 32 | + public function testThatGetBaseRouteReturn401(): void |
| 33 | + { |
| 34 | + $client = $this->getTestClient(); |
| 35 | + |
| 36 | + $client->request('GET', $this->baseUrl); |
| 37 | + $response = $client->getResponse(); |
| 38 | + $content = $response->getContent(); |
| 39 | + self::assertNotFalse($content); |
| 40 | + self::assertSame(Response::HTTP_UNAUTHORIZED, $response->getStatusCode(), "Response:\n" . $response); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * @testdox Test that `$method $action` returns forbidden error for non-root user. |
| 45 | + * |
| 46 | + * @dataProvider dataProviderActions |
| 47 | + * |
| 48 | + * @throws Throwable |
| 49 | + */ |
| 50 | + public function testThatAllActionsForbiddenForNonRootUser(string $method, string $action): void |
| 51 | + { |
| 52 | + $client = $this->getTestClient('john-admin', 'password-admin'); |
| 53 | + |
| 54 | + $client->request($method, $action); |
| 55 | + $response = $client->getResponse(); |
| 56 | + $content = $response->getContent(); |
| 57 | + self::assertNotFalse($content); |
| 58 | + self::assertSame(Response::HTTP_FORBIDDEN, $response->getStatusCode(), "Response:\n" . $response); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @testdox Test that `GET /v1/api_key` returns `$responseCode` with login: `$login`, password: `$password`. |
| 63 | + * |
| 64 | + * @dataProvider dataProviderTestThatFindActionWorksAsExpected |
| 65 | + * |
| 66 | + * @throws Throwable |
| 67 | + */ |
| 68 | + public function testThatFindActionWorksAsExpected(string $login, string $password, int $responseCode): void |
| 69 | + { |
| 70 | + $client = $this->getTestClient($login, $password); |
| 71 | + |
| 72 | + $client->request('GET', $this->baseUrl); |
| 73 | + $response = $client->getResponse(); |
| 74 | + $content = $response->getContent(); |
| 75 | + self::assertNotFalse($content); |
| 76 | + self::assertSame($responseCode, $response->getStatusCode(), "Response:\n" . $response); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * @testdox Test that `POST /v1/api_key` (create api-key) for the Root user returns success response. |
| 81 | + * |
| 82 | + * @throws Throwable |
| 83 | + */ |
| 84 | + public function testThatCreateActionForRootUserReturnsSuccessResponse(): void |
| 85 | + { |
| 86 | + $client = $this->getTestClient('john-root', 'password-root'); |
| 87 | + |
| 88 | + $requestData = [ |
| 89 | + 'description' => 'test api key', |
| 90 | + 'userGroups' => [ |
| 91 | + LoadUserGroupData::$uuids['Role-api'], |
| 92 | + ], |
| 93 | + ]; |
| 94 | + $client->request(method: 'POST', uri: $this->baseUrl, content: JSON::encode($requestData)); |
| 95 | + $response = $client->getResponse(); |
| 96 | + $content = $response->getContent(); |
| 97 | + self::assertNotFalse($content); |
| 98 | + self::assertSame(Response::HTTP_CREATED, $response->getStatusCode(), "Response:\n" . $response); |
| 99 | + $responseData = JSON::decode($content, true); |
| 100 | + self::assertArrayHasKey('id', $responseData); |
| 101 | + self::assertArrayHasKey('token', $responseData); |
| 102 | + self::assertArrayHasKey('description', $responseData); |
| 103 | + self::assertEquals($requestData['description'], $responseData['description']); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * @testdox Test that `GET /v1/api_key/{id}` for the Root user returns success response. |
| 108 | + * |
| 109 | + * @depends testThatCreateActionForRootUserReturnsSuccessResponse |
| 110 | + * |
| 111 | + * @throws Throwable |
| 112 | + */ |
| 113 | + public function testThatFindOneActionForRootUserReturnsSuccessResponse(): void |
| 114 | + { |
| 115 | + $client = $this->getTestClient('john-root', 'password-root'); |
| 116 | + $resource = static::getContainer()->get(ApiKeyResource::class); |
| 117 | + static::assertInstanceOf(ApiKeyResource::class, $resource); |
| 118 | + $apiKeyEntity = $resource->findOneBy([ |
| 119 | + 'description' => 'test api key', |
| 120 | + ]); |
| 121 | + self::assertInstanceOf(ApiKey::class, $apiKeyEntity); |
| 122 | + |
| 123 | + $client->request('GET', $this->baseUrl . '/' . $apiKeyEntity->getId()); |
| 124 | + $response = $client->getResponse(); |
| 125 | + $content = $response->getContent(); |
| 126 | + self::assertNotFalse($content); |
| 127 | + self::assertSame(Response::HTTP_OK, $response->getStatusCode(), "Response:\n" . $response); |
| 128 | + $responseData = JSON::decode($content, true); |
| 129 | + self::assertArrayHasKey('id', $responseData); |
| 130 | + self::assertArrayHasKey('token', $responseData); |
| 131 | + self::assertArrayHasKey('description', $responseData); |
| 132 | + self::assertEquals($apiKeyEntity->getToken(), $responseData['token']); |
| 133 | + self::assertEquals($apiKeyEntity->getDescription(), $responseData['description']); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * @testdox Test that `PUT /v1/api_key/{id}` for the Root user returns success response. |
| 138 | + * |
| 139 | + * @depends testThatCreateActionForRootUserReturnsSuccessResponse |
| 140 | + * |
| 141 | + * @throws Throwable |
| 142 | + */ |
| 143 | + public function testThatUpdateActionForRootUserReturnsSuccessResponse(): void |
| 144 | + { |
| 145 | + $client = $this->getTestClient('john-root', 'password-root'); |
| 146 | + $resource = static::getContainer()->get(ApiKeyResource::class); |
| 147 | + static::assertInstanceOf(ApiKeyResource::class, $resource); |
| 148 | + $apiKeyEntity = $resource->findOneBy([ |
| 149 | + 'description' => 'test api key', |
| 150 | + ]); |
| 151 | + self::assertInstanceOf(ApiKey::class, $apiKeyEntity); |
| 152 | + $requestData = [ |
| 153 | + 'description' => 'test api key', |
| 154 | + 'userGroups' => [ |
| 155 | + LoadUserGroupData::$uuids['Role-logged'], |
| 156 | + ], |
| 157 | + ]; |
| 158 | + |
| 159 | + $client->request( |
| 160 | + method: 'PUT', |
| 161 | + uri: $this->baseUrl . '/' . $apiKeyEntity->getId(), |
| 162 | + content: JSON::encode($requestData) |
| 163 | + ); |
| 164 | + $response = $client->getResponse(); |
| 165 | + $content = $response->getContent(); |
| 166 | + self::assertNotFalse($content); |
| 167 | + self::assertSame(Response::HTTP_OK, $response->getStatusCode(), "Response:\n" . $response); |
| 168 | + $responseData = JSON::decode($content, true); |
| 169 | + self::assertArrayHasKey('id', $responseData); |
| 170 | + self::assertArrayHasKey('token', $responseData); |
| 171 | + self::assertArrayHasKey('description', $responseData); |
| 172 | + self::assertEquals($apiKeyEntity->getToken(), $responseData['token']); |
| 173 | + self::assertEquals($apiKeyEntity->getDescription(), $responseData['description']); |
| 174 | + // let's check that after updating the entity we have new userGroup |
| 175 | + $apiKeyUpdatedEntity = $resource->findOne((string)$responseData['id']); |
| 176 | + self::assertInstanceOf(ApiKey::class, $apiKeyUpdatedEntity); |
| 177 | + self::assertCount(1, $apiKeyUpdatedEntity->getUserGroups()); |
| 178 | + $apiKeyUserGroup = $apiKeyUpdatedEntity->getUserGroups()->first(); |
| 179 | + self::assertInstanceOf(UserGroup::class, $apiKeyUserGroup); |
| 180 | + self::assertSame($requestData['userGroups'][0], $apiKeyUserGroup->getId()); |
| 181 | + } |
| 182 | + |
| 183 | + /** |
| 184 | + * @testdox Test that `PATCH /v1/api_key/{id}` for the Root user returns success response. |
| 185 | + * |
| 186 | + * @depends testThatCreateActionForRootUserReturnsSuccessResponse |
| 187 | + * |
| 188 | + * @throws Throwable |
| 189 | + */ |
| 190 | + public function testThatPatchActionForRootUserReturnsSuccessResponse(): void |
| 191 | + { |
| 192 | + $client = $this->getTestClient('john-root', 'password-root'); |
| 193 | + $resource = static::getContainer()->get(ApiKeyResource::class); |
| 194 | + static::assertInstanceOf(ApiKeyResource::class, $resource); |
| 195 | + $apiKeyEntity = $resource->findOneBy([ |
| 196 | + 'description' => 'test api key', |
| 197 | + ]); |
| 198 | + self::assertInstanceOf(ApiKey::class, $apiKeyEntity); |
| 199 | + $apiKeyUserGroup = $apiKeyEntity->getUserGroups()->first(); |
| 200 | + self::assertInstanceOf(UserGroup::class, $apiKeyUserGroup); |
| 201 | + $requestData = [ |
| 202 | + 'description' => 'test api key edited', |
| 203 | + ]; |
| 204 | + |
| 205 | + $client->request( |
| 206 | + method: 'PATCH', |
| 207 | + uri: $this->baseUrl . '/' . $apiKeyEntity->getId(), |
| 208 | + content: JSON::encode($requestData) |
| 209 | + ); |
| 210 | + $response = $client->getResponse(); |
| 211 | + $content = $response->getContent(); |
| 212 | + self::assertNotFalse($content); |
| 213 | + self::assertSame(Response::HTTP_OK, $response->getStatusCode(), "Response:\n" . $response); |
| 214 | + $responseData = JSON::decode($content, true); |
| 215 | + self::assertArrayHasKey('id', $responseData); |
| 216 | + self::assertArrayHasKey('token', $responseData); |
| 217 | + self::assertArrayHasKey('description', $responseData); |
| 218 | + self::assertEquals($apiKeyEntity->getToken(), $responseData['token']); |
| 219 | + self::assertEquals('test api key edited', $responseData['description']); |
| 220 | + // tet's check that after patch the entity we have the same userGroup as before |
| 221 | + $apiKeyUpdatedEntity = $resource->findOne((string)$responseData['id']); |
| 222 | + self::assertInstanceOf(ApiKey::class, $apiKeyUpdatedEntity); |
| 223 | + self::assertCount(1, $apiKeyUpdatedEntity->getUserGroups()); |
| 224 | + $apiKeyUpdatedUserGroup = $apiKeyUpdatedEntity->getUserGroups()->first(); |
| 225 | + self::assertInstanceOf(UserGroup::class, $apiKeyUpdatedUserGroup); |
| 226 | + self::assertSame($apiKeyUserGroup->getId(), $apiKeyUpdatedUserGroup->getId()); |
| 227 | + } |
| 228 | + |
| 229 | + /** |
| 230 | + * @testdox Test that `DELETE /v1/api_key/{id}` for the Root user returns success response. |
| 231 | + * |
| 232 | + * @depends testThatPatchActionForRootUserReturnsSuccessResponse |
| 233 | + * |
| 234 | + * @throws Throwable |
| 235 | + */ |
| 236 | + public function testThatDeleteActionForRootUserReturnsSuccessResponse(): void |
| 237 | + { |
| 238 | + $client = $this->getTestClient('john-root', 'password-root'); |
| 239 | + |
| 240 | + $resource = static::getContainer()->get(ApiKeyResource::class); |
| 241 | + static::assertInstanceOf(ApiKeyResource::class, $resource); |
| 242 | + $apiKeyEntity = $resource->findOneBy([ |
| 243 | + 'description' => 'test api key edited', |
| 244 | + ]); |
| 245 | + self::assertInstanceOf(ApiKey::class, $apiKeyEntity); |
| 246 | + |
| 247 | + $client->request('DELETE', $this->baseUrl . '/' . $apiKeyEntity->getId()); |
| 248 | + $response = $client->getResponse(); |
| 249 | + $content = $response->getContent(); |
| 250 | + self::assertNotFalse($content); |
| 251 | + self::assertSame(Response::HTTP_OK, $response->getStatusCode(), "Response:\n" . $response); |
| 252 | + $responseData = JSON::decode($content, true); |
| 253 | + self::assertArrayHasKey('id', $responseData); |
| 254 | + self::assertArrayHasKey('token', $responseData); |
| 255 | + self::assertArrayHasKey('description', $responseData); |
| 256 | + } |
| 257 | + |
| 258 | + /** |
| 259 | + * @testdox Test that `GET /v1/api_key/ids` for the Root user returns success response. |
| 260 | + * |
| 261 | + * @throws Throwable |
| 262 | + */ |
| 263 | + public function testThatIdsActionForRootUserReturnsSuccessResponse(): void |
| 264 | + { |
| 265 | + $client = $this->getTestClient('john-root', 'password-root'); |
| 266 | + |
| 267 | + $client->request('GET', $this->baseUrl . '/ids'); |
| 268 | + $response = $client->getResponse(); |
| 269 | + $content = $response->getContent(); |
| 270 | + self::assertNotFalse($content); |
| 271 | + self::assertSame(Response::HTTP_OK, $response->getStatusCode(), "Response:\n" . $response); |
| 272 | + } |
| 273 | + |
| 274 | + /** |
| 275 | + * @return Generator<array{0: string, 1: string, 2: int}> |
| 276 | + */ |
| 277 | + public function dataProviderTestThatFindActionWorksAsExpected(): Generator |
| 278 | + { |
| 279 | + // username === login |
| 280 | + yield ['john', 'password', Response::HTTP_FORBIDDEN]; |
| 281 | + yield ['john-logged', 'password-logged', Response::HTTP_FORBIDDEN]; |
| 282 | + yield ['john-api', 'password-api', Response::HTTP_FORBIDDEN]; |
| 283 | + yield ['john-user', 'password-user', Response::HTTP_FORBIDDEN]; |
| 284 | + yield ['john-admin', 'password-admin', Response::HTTP_FORBIDDEN]; |
| 285 | + yield ['john-root', 'password-root', Response::HTTP_OK]; |
| 286 | + |
| 287 | + // email === login |
| 288 | + yield [ '[email protected]', 'password', Response:: HTTP_FORBIDDEN]; |
| 289 | + yield [ '[email protected]', 'password-logged', Response:: HTTP_FORBIDDEN]; |
| 290 | + yield [ '[email protected]', 'password-api', Response:: HTTP_FORBIDDEN]; |
| 291 | + yield [ '[email protected]', 'password-user', Response:: HTTP_FORBIDDEN]; |
| 292 | + yield [ '[email protected]', 'password-admin', Response:: HTTP_FORBIDDEN]; |
| 293 | + yield [ '[email protected]', 'password-root', Response:: HTTP_OK]; |
| 294 | + } |
| 295 | + |
| 296 | + /** |
| 297 | + * @return Generator<array{0: string, 1: string}> |
| 298 | + */ |
| 299 | + public function dataProviderActions(): Generator |
| 300 | + { |
| 301 | + yield ['GET', $this->baseUrl . '/count']; |
| 302 | + yield ['GET', $this->baseUrl]; |
| 303 | + yield ['POST', $this->baseUrl]; |
| 304 | + yield ['GET', $this->baseUrl . '/' . LoadApiKeyData::$uuids['-root']]; |
| 305 | + yield ['PUT', $this->baseUrl . '/' . LoadApiKeyData::$uuids['-root']]; |
| 306 | + yield ['DELETE', $this->baseUrl . '/' . LoadApiKeyData::$uuids['-root']]; |
| 307 | + yield ['PATCH', $this->baseUrl . '/' . LoadApiKeyData::$uuids['-root']]; |
| 308 | + yield ['GET', $this->baseUrl . '/ids']; |
| 309 | + } |
| 310 | +} |
0 commit comments