From 2283d6f66135304a93bfbfc0677c4a34438df50d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my?= Date: Sat, 25 Nov 2023 15:47:21 +0100 Subject: [PATCH] test: add test for api errors --- src/Model/Publication/Request/AddVideo.php | 1 + .../AlreadyExistingVideoValidator.php | 3 + .../Publication/UrlVideoValidator.php | 2 + .../Post/PublicationVideoPostTest.php | 177 ++++++++++++++++++ 4 files changed, 183 insertions(+) create mode 100644 tests/Api/Publication/Post/PublicationVideoPostTest.php diff --git a/src/Model/Publication/Request/AddVideo.php b/src/Model/Publication/Request/AddVideo.php index 909ce1b1..787a233f 100644 --- a/src/Model/Publication/Request/AddVideo.php +++ b/src/Model/Publication/Request/AddVideo.php @@ -11,6 +11,7 @@ #[Post( uriTemplate: '/publications/video/add', + security: "is_granted('IS_AUTHENTICATED_REMEMBERED')", name: 'api_publication_video_add', processor: VideoPostProcessor::class, )] diff --git a/src/Validator/Publication/AlreadyExistingVideoValidator.php b/src/Validator/Publication/AlreadyExistingVideoValidator.php index 1c753dca..f2ea9d46 100644 --- a/src/Validator/Publication/AlreadyExistingVideoValidator.php +++ b/src/Validator/Publication/AlreadyExistingVideoValidator.php @@ -12,6 +12,8 @@ class AlreadyExistingVideoValidator extends ConstraintValidator { + const ERROR_CODE_EXISTING_VIDEO = 'music_all_99153e73-dd44-4557-90aa-3c0e354fce62'; + public function __construct( private readonly YoutubeUrlHelper $youtubeUrlHelper, private readonly PublicationRepository $publicationRepository, @@ -37,6 +39,7 @@ public function validate(mixed $value, Constraint $constraint) } // the argument must be a string or an object implementing __toString() $this->context->buildViolation($constraint->message) + ->setCode(self::ERROR_CODE_EXISTING_VIDEO) ->addViolation(); } } \ No newline at end of file diff --git a/src/Validator/Publication/UrlVideoValidator.php b/src/Validator/Publication/UrlVideoValidator.php index 8c3bc423..a24fc3d0 100644 --- a/src/Validator/Publication/UrlVideoValidator.php +++ b/src/Validator/Publication/UrlVideoValidator.php @@ -10,6 +10,7 @@ class UrlVideoValidator extends ConstraintValidator { + const ERROR_CODE_URL_VIDEO = 'music_all_f03dc5f4-8ba0-11ee-b9d1-0242ac120002'; public function __construct( private readonly YoutubeUrlHelper $youtubeUrlHelper, ) { @@ -33,6 +34,7 @@ public function validate(mixed $value, Constraint $constraint) } // the argument must be a string or an object implementing __toString() $this->context->buildViolation($constraint->message) + ->setCode(self::ERROR_CODE_URL_VIDEO) ->addViolation(); } } \ No newline at end of file diff --git a/tests/Api/Publication/Post/PublicationVideoPostTest.php b/tests/Api/Publication/Post/PublicationVideoPostTest.php new file mode 100644 index 00000000..c40c26c3 --- /dev/null +++ b/tests/Api/Publication/Post/PublicationVideoPostTest.php @@ -0,0 +1,177 @@ +asBaseUser()->create(); + + $this->client->loginUser($user1->object()); + $this->client->jsonRequest('POST', '/api/publications/video/add', + [], + ['CONTENT_TYPE' => 'application/ld+json', 'HTTP_ACCEPT' => 'application/ld+json'] + ); + $this->assertJsonEquals([ + 'status' => 422, + 'violations' => [ + [ + 'propertyPath' => 'url', + 'message' => 'Cette valeur ne doit pas être vide.', + 'code' => 'c1051bb4-d103-4f74-8988-acbcafc7fdc3', + ], + [ + 'propertyPath' => 'title', + 'message' => 'Cette valeur ne doit pas être vide.', + 'code' => 'c1051bb4-d103-4f74-8988-acbcafc7fdc3', + ], + [ + 'propertyPath' => 'description', + 'message' => 'Cette valeur ne doit pas être vide.', + 'code' => 'c1051bb4-d103-4f74-8988-acbcafc7fdc3', + ], + ], + 'detail' => 'url: Cette valeur ne doit pas être vide. +title: Cette valeur ne doit pas être vide. +description: Cette valeur ne doit pas être vide.', + 'hydra:title' => 'An error occurred', + 'hydra:description' => 'url: Cette valeur ne doit pas être vide. +title: Cette valeur ne doit pas être vide. +description: Cette valeur ne doit pas être vide.', + 'type' => '/validation_errors/0=c1051bb4-d103-4f74-8988-acbcafc7fdc3;1=c1051bb4-d103-4f74-8988-acbcafc7fdc3;2=c1051bb4-d103-4f74-8988-acbcafc7fdc3', + 'title' => 'An error occurred', + ]); + } + + public function testWrongUrl() + { + $user1 = UserFactory::new()->asBaseUser()->create(); + + $this->client->loginUser($user1->object()); + $this->client->jsonRequest('POST', '/api/publications/video/add', + [ + 'url' => 'wrong_url', + 'title' => 'title', + 'description' => 'this is a description', + ], + ['CONTENT_TYPE' => 'application/ld+json', 'HTTP_ACCEPT' => 'application/ld+json'] + ); + $this->assertJsonEquals([ + 'status' => 422, + 'violations' => [ + [ + 'propertyPath' => 'url', + 'message' => 'Cette valeur n\'est pas une URL valide.', + 'code' => '57c2f299-1154-4870-89bb-ef3b1f5ad229', + ], + ], + 'detail' => 'url: Cette valeur n\'est pas une URL valide.', + 'hydra:title' => 'An error occurred', + 'hydra:description' => 'url: Cette valeur n\'est pas une URL valide.', + 'type' => '/validation_errors/57c2f299-1154-4870-89bb-ef3b1f5ad229', + 'title' => 'An error occurred', + ]); + } + + public function testNotYoutubeUrl() + { + $user1 = UserFactory::new()->asBaseUser()->create(); + + $this->client->loginUser($user1->object()); + $this->client->jsonRequest('POST', '/api/publications/video/add', + [ + 'url' => 'https://musicall.com', + 'title' => 'title', + 'description' => 'this is a description', + ], + ['CONTENT_TYPE' => 'application/ld+json', 'HTTP_ACCEPT' => 'application/ld+json'] + ); + $this->assertJsonEquals([ + 'status' => 422, + 'violations' => [ + [ + 'propertyPath' => 'url', + 'message' => 'L\'url de cette vidéo n\'est pas supportée', + 'code' => 'music_all_f03dc5f4-8ba0-11ee-b9d1-0242ac120002', + ], + ], + 'detail' => 'url: L\'url de cette vidéo n\'est pas supportée', + 'hydra:title' => 'An error occurred', + 'hydra:description' => 'url: L\'url de cette vidéo n\'est pas supportée', + 'type' => '/validation_errors/music_all_f03dc5f4-8ba0-11ee-b9d1-0242ac120002', + 'title' => 'An error occurred', + ]); + } + + public function testExistingVideo() + { + $user1 = UserFactory::new()->asBaseUser()->create(); + $sub = PublicationSubCategoryFactory::new()->asDecouvertes()->create(); + $author = UserFactory::new()->asAdminUser()->create(); + PublicationFactory::new([ + 'author' => $author, + 'content' => 'kcelgrGY1h8', + 'creationDatetime' => \DateTime::createFromFormat(\DateTimeInterface::ATOM, '2020-01-02T02:03:04+00:00'), + 'editionDatetime' => \DateTime::createFromFormat(\DateTimeInterface::ATOM, '2021-01-02T02:03:04+00:00'), + 'publicationDatetime' => \DateTime::createFromFormat(\DateTimeInterface::ATOM, '2022-01-02T02:03:04+00:00'), + 'shortDescription' => 'Petite description de la publication 1', + 'slug' => 'titre-de-la-publication-1', + 'status' => Publication::STATUS_ONLINE, + 'subCategory' => $sub, + 'title' => 'Titre de la publication 1', + 'type' => Publication::TYPE_VIDEO, + 'viewCache' => ViewCacheFactory::new(['count' => 10])->create(), + ])->create()->object(); + + $this->client->loginUser($user1->object()); + $this->client->jsonRequest('POST', '/api/publications/video/add', + [ + 'url' => 'https://www.youtube.com/watch?v=kcelgrGY1h8', + 'title' => 'title', + 'description' => 'this is a description', + ], + ['CONTENT_TYPE' => 'application/ld+json', 'HTTP_ACCEPT' => 'application/ld+json'] + ); + $this->assertJsonEquals([ + 'status' => 422, + 'violations' => [ + [ + 'propertyPath' => 'url', + 'message' => 'Cette vidéo existe déjà sur MusicAll', + 'code' => 'music_all_99153e73-dd44-4557-90aa-3c0e354fce62', + ], + ], + 'detail' => 'url: Cette vidéo existe déjà sur MusicAll', + 'hydra:title' => 'An error occurred', + 'hydra:description' => 'url: Cette vidéo existe déjà sur MusicAll', + 'type' => '/validation_errors/music_all_99153e73-dd44-4557-90aa-3c0e354fce62', + 'title' => 'An error occurred', + ]); + } + + public function test_post_without_logged_in() + { + $this->client->jsonRequest('POST', '/api/publications/video/add', [], ['CONTENT_TYPE' => 'application/ld+json', 'HTTP_ACCEPT' => 'application/ld+json']); + $this->assertResponseStatusCodeSame(Response::HTTP_UNAUTHORIZED); + $this->assertJsonEquals([ + 'code' => 401, + 'message' => 'JWT Token not found', + ]); + } +} \ No newline at end of file