From 7b5ae70de31a143ce49ab656b53a5c550b2fae7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Sun, 3 Mar 2024 21:02:15 +0100 Subject: [PATCH] Added tests for the Attachments API endpoint --- src/Entity/Attachments/Attachment.php | 1 + tests/API/APIDocsAvailabilityTest.php | 2 +- tests/API/APITokenAuthenticationTest.php | 2 +- tests/API/AuthenticatedApiTestCase.php | 41 +++++++ .../API/Endpoints/AttachmentsEndpointTest.php | 107 ++++++++++++++++++ 5 files changed, 151 insertions(+), 2 deletions(-) create mode 100644 tests/API/AuthenticatedApiTestCase.php create mode 100644 tests/API/Endpoints/AttachmentsEndpointTest.php diff --git a/src/Entity/Attachments/Attachment.php b/src/Entity/Attachments/Attachment.php index b3131eeb..d705d2d2 100644 --- a/src/Entity/Attachments/Attachment.php +++ b/src/Entity/Attachments/Attachment.php @@ -147,6 +147,7 @@ abstract class Attachment extends AbstractNamedDBElement * @var string|null the original filename the file had, when the user uploaded it */ #[ORM\Column(type: Types::STRING, nullable: true)] + #[Groups(['full', 'attachment:read'])] protected ?string $original_filename = null; /** diff --git a/tests/API/APIDocsAvailabilityTest.php b/tests/API/APIDocsAvailabilityTest.php index 18ce714a..bc3b6119 100644 --- a/tests/API/APIDocsAvailabilityTest.php +++ b/tests/API/APIDocsAvailabilityTest.php @@ -21,7 +21,7 @@ declare(strict_types=1); -namespace API; +namespace App\Tests\API; use App\Entity\UserSystem\User; use Doctrine\ORM\EntityManagerInterface; diff --git a/tests/API/APITokenAuthenticationTest.php b/tests/API/APITokenAuthenticationTest.php index dad2645f..fa251839 100644 --- a/tests/API/APITokenAuthenticationTest.php +++ b/tests/API/APITokenAuthenticationTest.php @@ -21,7 +21,7 @@ declare(strict_types=1); -namespace API; +namespace App\Tests\API; use ApiPlatform\Symfony\Bundle\Test\ApiTestCase; use App\DataFixtures\APITokenFixtures; diff --git a/tests/API/AuthenticatedApiTestCase.php b/tests/API/AuthenticatedApiTestCase.php new file mode 100644 index 00000000..601ba595 --- /dev/null +++ b/tests/API/AuthenticatedApiTestCase.php @@ -0,0 +1,41 @@ +. + */ + +declare(strict_types=1); + + +namespace App\Tests\API; + +use ApiPlatform\Symfony\Bundle\Test\ApiTestCase; +use ApiPlatform\Symfony\Bundle\Test\Client; +use App\DataFixtures\APITokenFixtures; + +class AuthenticatedApiTestCase extends ApiTestCase +{ + /** + * Creates an API client with authentication. + * @param string $token + * @return Client + */ + protected static function createAuthenticatedClient(string $token = APITokenFixtures::TOKEN_ADMIN): Client + { + return static::createClient(defaultOptions: ['headers' => ['authorization' => 'Token '.$token]]); + } +} \ No newline at end of file diff --git a/tests/API/Endpoints/AttachmentsEndpointTest.php b/tests/API/Endpoints/AttachmentsEndpointTest.php new file mode 100644 index 00000000..8084db5c --- /dev/null +++ b/tests/API/Endpoints/AttachmentsEndpointTest.php @@ -0,0 +1,107 @@ +. + */ + +declare(strict_types=1); + + +namespace App\Tests\API\Endpoints; + +use App\Tests\API\AuthenticatedApiTestCase; + +class AttachmentsEndpointTest extends AuthenticatedApiTestCase +{ + public function testGetCollection(): void + { + $response = static::createAuthenticatedClient()->request('GET', '/api/attachments'); + self::assertResponseIsSuccessful(); + //There should be 2 attachments in the database yet + self::assertJsonContains([ + '@context' => '/api/contexts/Attachment', + '@id' => '/api/attachments', + '@type' => 'hydra:Collection', + 'hydra:totalItems' => 2, + ]); + } + + public function testCreateAttachmentGuessTypeFromElement(): void + { + $response = static::createAuthenticatedClient()->request('POST', '/api/attachments', ['json' => [ + 'name' => 'test', + 'element' => '/api/parts/1', + 'attachment_type' => '/api/attachment_types/1' + ]]); + + //The attachment should be created successfully + self::assertResponseIsSuccessful(); + self::assertJsonContains([ + 'name' => 'test', + ]); + } + + public function testCreateAttachmentDiscriminatorColumn(): void + { + $response = static::createAuthenticatedClient()->request('POST', '/api/attachments', ['json' => [ + 'name' => 'test', + 'element' => '/api/parts/1', + 'attachment_type' => '/api/attachment_types/1', + '_type' => "Part", + ]]); + + //The attachment should be created successfully + self::assertResponseIsSuccessful(); + self::assertJsonContains([ + 'name' => 'test', + ]); + } + + public function testUploadFile(): void + { + $response = static::createAuthenticatedClient()->request('POST', '/api/attachments', ['json' => [ + 'name' => 'test', + 'element' => '/api/parts/1', + 'attachment_type' => '/api/attachment_types/1', + '_type' => "Part", + "upload" => [ + "data" => "data:@file/octet-stream;base64,LS0gcGhwTXlB", + "filename" => "test.csv", + "private" => true + ], + ]]); + + //The attachment should be created successfully + self::assertResponseIsSuccessful(); + + //Attachment must be set (not null) + $array = json_decode($response->getContent(), true); + + self::assertNotNull($array['media_url']); + + //Attachment must be private + self::assertJsonContains([ + 'private' => true, + ]); + } + + public function testRemoveAttachment(): void + { + $response = static::createAuthenticatedClient()->request('DELETE', '/api/attachments/1'); + self::assertResponseIsSuccessful(); + } +} \ No newline at end of file