From bb12538fab250b049c79752156623afb81dff327 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Tue, 15 Mar 2022 10:18:28 +0100 Subject: [PATCH 1/4] Added new endpoint for API: config --- .../Controller/ConfigRestController.php | 23 +++++++++ .../Resources/config/routing_rest.yml | 5 ++ .../Controller/ConfigRestControllerTest.php | 49 +++++++++++++++++++ 3 files changed, 77 insertions(+) create mode 100644 src/Wallabag/ApiBundle/Controller/ConfigRestController.php create mode 100644 tests/Wallabag/ApiBundle/Controller/ConfigRestControllerTest.php diff --git a/src/Wallabag/ApiBundle/Controller/ConfigRestController.php b/src/Wallabag/ApiBundle/Controller/ConfigRestController.php new file mode 100644 index 000000000..65046ff5f --- /dev/null +++ b/src/Wallabag/ApiBundle/Controller/ConfigRestController.php @@ -0,0 +1,23 @@ +validateAuthentication(); + + return $this->sendResponse($this->getUser()->getConfig()); + } +} diff --git a/src/Wallabag/ApiBundle/Resources/config/routing_rest.yml b/src/Wallabag/ApiBundle/Resources/config/routing_rest.yml index 98efeeb12..7785d25f2 100644 --- a/src/Wallabag/ApiBundle/Resources/config/routing_rest.yml +++ b/src/Wallabag/ApiBundle/Resources/config/routing_rest.yml @@ -32,3 +32,8 @@ user: type: rest resource: "WallabagApiBundle:UserRest" name_prefix: api_ + +config: + type: rest + resource: "WallabagApiBundle:ConfigRest" + name_prefix: api_ diff --git a/tests/Wallabag/ApiBundle/Controller/ConfigRestControllerTest.php b/tests/Wallabag/ApiBundle/Controller/ConfigRestControllerTest.php new file mode 100644 index 000000000..f4ca14ccf --- /dev/null +++ b/tests/Wallabag/ApiBundle/Controller/ConfigRestControllerTest.php @@ -0,0 +1,49 @@ +client->request('GET', '/api/config.json'); + $this->assertSame(200, $this->client->getResponse()->getStatusCode()); + + $content = json_decode($this->client->getResponse()->getContent(), true); + + $this->assertArrayHasKey('id', $content); + $this->assertArrayHasKey('theme', $content); + $this->assertArrayHasKey('items_per_page', $content); + $this->assertArrayHasKey('language', $content); + $this->assertArrayHasKey('feed_token', $content); + $this->assertArrayHasKey('feed_limit', $content); + $this->assertArrayHasKey('reading_speed', $content); + $this->assertArrayHasKey('pocket_consumer_key', $content); + $this->assertArrayHasKey('action_mark_as_read', $content); + $this->assertArrayHasKey('list_mode', $content); + + $this->assertSame('material', $content['theme']); + $this->assertSame(200.0, $content['reading_speed']); + $this->assertSame('xxxxx', $content['pocket_consumer_key']); + + $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type')); + } + + public function testGetConfigWithoutAuthentication() + { + $client = static::createClient(); + $client->request('GET', '/api/config.json'); + $this->assertSame(401, $client->getResponse()->getStatusCode()); + + $content = json_decode($client->getResponse()->getContent(), true); + + $this->assertArrayHasKey('error', $content); + $this->assertArrayHasKey('error_description', $content); + + $this->assertSame('access_denied', $content['error']); + + $this->assertSame('application/json', $client->getResponse()->headers->get('Content-Type')); + } +} From aaa03cc395cdd177d9992e070b8f8c94df7f4cea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Tue, 15 Mar 2022 10:44:32 +0100 Subject: [PATCH 2/4] Added serialization group --- .../Controller/ConfigRestController.php | 11 +++++++++- .../DataFixtures/ConfigFixtures.php | 1 + src/Wallabag/CoreBundle/Entity/Config.php | 21 +++++++++++++++++++ .../Controller/ConfigRestControllerTest.php | 2 -- 4 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/Wallabag/ApiBundle/Controller/ConfigRestController.php b/src/Wallabag/ApiBundle/Controller/ConfigRestController.php index 65046ff5f..134efd19d 100644 --- a/src/Wallabag/ApiBundle/Controller/ConfigRestController.php +++ b/src/Wallabag/ApiBundle/Controller/ConfigRestController.php @@ -2,6 +2,7 @@ namespace Wallabag\ApiBundle\Controller; +use JMS\Serializer\SerializationContext; use Nelmio\ApiDocBundle\Annotation\ApiDoc; use Symfony\Component\HttpFoundation\JsonResponse; @@ -18,6 +19,14 @@ class ConfigRestController extends WallabagRestController { $this->validateAuthentication(); - return $this->sendResponse($this->getUser()->getConfig()); + $json = $this->get('jms_serializer')->serialize( + $this->getUser()->getConfig(), + 'json', + SerializationContext::create()->setGroups(['config_api']) + ); + + return (new JsonResponse()) + ->setJson($json) + ->setStatusCode(JsonResponse::HTTP_OK); } } diff --git a/src/Wallabag/CoreBundle/DataFixtures/ConfigFixtures.php b/src/Wallabag/CoreBundle/DataFixtures/ConfigFixtures.php index 6dde08e81..64fe99b45 100644 --- a/src/Wallabag/CoreBundle/DataFixtures/ConfigFixtures.php +++ b/src/Wallabag/CoreBundle/DataFixtures/ConfigFixtures.php @@ -24,6 +24,7 @@ class ConfigFixtures extends Fixture implements DependentFixtureInterface $adminConfig->setPocketConsumerKey('xxxxx'); $adminConfig->setActionMarkAsRead(0); $adminConfig->setListMode(0); + $adminConfig->setListMode(0); $manager->persist($adminConfig); diff --git a/src/Wallabag/CoreBundle/Entity/Config.php b/src/Wallabag/CoreBundle/Entity/Config.php index 1bed45138..1c53de1f1 100644 --- a/src/Wallabag/CoreBundle/Entity/Config.php +++ b/src/Wallabag/CoreBundle/Entity/Config.php @@ -4,6 +4,7 @@ namespace Wallabag\CoreBundle\Entity; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\ORM\Mapping as ORM; +use JMS\Serializer\Annotation\Groups; use Symfony\Component\Validator\Constraints as Assert; use Wallabag\UserBundle\Entity\User; @@ -29,6 +30,8 @@ class Config * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") + * + * @Groups({"config_api"}) */ private $id; @@ -37,6 +40,8 @@ class Config * * @Assert\NotBlank() * @ORM\Column(name="theme", type="string", nullable=false) + * + * @Groups({"config_api"}) */ private $theme; @@ -50,6 +55,8 @@ class Config * maxMessage = "validator.item_per_page_too_high" * ) * @ORM\Column(name="items_per_page", type="integer", nullable=false) + * + * @Groups({"config_api"}) */ private $itemsPerPage; @@ -58,6 +65,8 @@ class Config * * @Assert\NotBlank() * @ORM\Column(name="language", type="string", nullable=false) + * + * @Groups({"config_api"}) */ private $language; @@ -65,6 +74,8 @@ class Config * @var string * * @ORM\Column(name="feed_token", type="string", nullable=true) + * + * @Groups({"config_api"}) */ private $feedToken; @@ -77,6 +88,8 @@ class Config * max = 100000, * maxMessage = "validator.feed_limit_too_high" * ) + * + * @Groups({"config_api"}) */ private $feedLimit; @@ -84,6 +97,8 @@ class Config * @var float * * @ORM\Column(name="reading_speed", type="float", nullable=true) + * + * @Groups({"config_api"}) */ private $readingSpeed; @@ -91,6 +106,8 @@ class Config * @var string * * @ORM\Column(name="pocket_consumer_key", type="string", nullable=true) + * + * @Groups({"config_api"}) */ private $pocketConsumerKey; @@ -98,6 +115,8 @@ class Config * @var int * * @ORM\Column(name="action_mark_as_read", type="integer", nullable=true, options={"default" = 0}) + * + * @Groups({"config_api"}) */ private $actionMarkAsRead; @@ -105,6 +124,8 @@ class Config * @var int * * @ORM\Column(name="list_mode", type="integer", nullable=true) + * + * @Groups({"config_api"}) */ private $listMode; diff --git a/tests/Wallabag/ApiBundle/Controller/ConfigRestControllerTest.php b/tests/Wallabag/ApiBundle/Controller/ConfigRestControllerTest.php index f4ca14ccf..c7c2fffcb 100644 --- a/tests/Wallabag/ApiBundle/Controller/ConfigRestControllerTest.php +++ b/tests/Wallabag/ApiBundle/Controller/ConfigRestControllerTest.php @@ -17,8 +17,6 @@ class ConfigRestControllerTest extends WallabagApiTestCase $this->assertArrayHasKey('theme', $content); $this->assertArrayHasKey('items_per_page', $content); $this->assertArrayHasKey('language', $content); - $this->assertArrayHasKey('feed_token', $content); - $this->assertArrayHasKey('feed_limit', $content); $this->assertArrayHasKey('reading_speed', $content); $this->assertArrayHasKey('pocket_consumer_key', $content); $this->assertArrayHasKey('action_mark_as_read', $content); From 33b1c252a75c98589a654ef250ca48833293dac5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Tue, 15 Mar 2022 11:07:57 +0100 Subject: [PATCH 3/4] fixed review --- src/Wallabag/CoreBundle/DataFixtures/ConfigFixtures.php | 1 - src/Wallabag/CoreBundle/Entity/Config.php | 4 ---- .../ApiBundle/Controller/ConfigRestControllerTest.php | 5 +---- 3 files changed, 1 insertion(+), 9 deletions(-) diff --git a/src/Wallabag/CoreBundle/DataFixtures/ConfigFixtures.php b/src/Wallabag/CoreBundle/DataFixtures/ConfigFixtures.php index 64fe99b45..6dde08e81 100644 --- a/src/Wallabag/CoreBundle/DataFixtures/ConfigFixtures.php +++ b/src/Wallabag/CoreBundle/DataFixtures/ConfigFixtures.php @@ -24,7 +24,6 @@ class ConfigFixtures extends Fixture implements DependentFixtureInterface $adminConfig->setPocketConsumerKey('xxxxx'); $adminConfig->setActionMarkAsRead(0); $adminConfig->setListMode(0); - $adminConfig->setListMode(0); $manager->persist($adminConfig); diff --git a/src/Wallabag/CoreBundle/Entity/Config.php b/src/Wallabag/CoreBundle/Entity/Config.php index 1c53de1f1..f00077110 100644 --- a/src/Wallabag/CoreBundle/Entity/Config.php +++ b/src/Wallabag/CoreBundle/Entity/Config.php @@ -40,8 +40,6 @@ class Config * * @Assert\NotBlank() * @ORM\Column(name="theme", type="string", nullable=false) - * - * @Groups({"config_api"}) */ private $theme; @@ -106,8 +104,6 @@ class Config * @var string * * @ORM\Column(name="pocket_consumer_key", type="string", nullable=true) - * - * @Groups({"config_api"}) */ private $pocketConsumerKey; diff --git a/tests/Wallabag/ApiBundle/Controller/ConfigRestControllerTest.php b/tests/Wallabag/ApiBundle/Controller/ConfigRestControllerTest.php index c7c2fffcb..129c41f16 100644 --- a/tests/Wallabag/ApiBundle/Controller/ConfigRestControllerTest.php +++ b/tests/Wallabag/ApiBundle/Controller/ConfigRestControllerTest.php @@ -14,17 +14,14 @@ class ConfigRestControllerTest extends WallabagApiTestCase $content = json_decode($this->client->getResponse()->getContent(), true); $this->assertArrayHasKey('id', $content); - $this->assertArrayHasKey('theme', $content); $this->assertArrayHasKey('items_per_page', $content); $this->assertArrayHasKey('language', $content); $this->assertArrayHasKey('reading_speed', $content); - $this->assertArrayHasKey('pocket_consumer_key', $content); $this->assertArrayHasKey('action_mark_as_read', $content); $this->assertArrayHasKey('list_mode', $content); - $this->assertSame('material', $content['theme']); $this->assertSame(200.0, $content['reading_speed']); - $this->assertSame('xxxxx', $content['pocket_consumer_key']); + $this->assertSame('en', $content['language']); $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type')); } From a885f5043afd04911e5d5df15ce0a422e1ea1b5b Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Wed, 20 Apr 2022 22:14:56 +0200 Subject: [PATCH 4/4] Update tests --- .../Controller/ConfigRestControllerTest.php | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/tests/Wallabag/ApiBundle/Controller/ConfigRestControllerTest.php b/tests/Wallabag/ApiBundle/Controller/ConfigRestControllerTest.php index 129c41f16..e6cd6eb5c 100644 --- a/tests/Wallabag/ApiBundle/Controller/ConfigRestControllerTest.php +++ b/tests/Wallabag/ApiBundle/Controller/ConfigRestControllerTest.php @@ -11,17 +11,19 @@ class ConfigRestControllerTest extends WallabagApiTestCase $this->client->request('GET', '/api/config.json'); $this->assertSame(200, $this->client->getResponse()->getStatusCode()); - $content = json_decode($this->client->getResponse()->getContent(), true); + $config = json_decode($this->client->getResponse()->getContent(), true); - $this->assertArrayHasKey('id', $content); - $this->assertArrayHasKey('items_per_page', $content); - $this->assertArrayHasKey('language', $content); - $this->assertArrayHasKey('reading_speed', $content); - $this->assertArrayHasKey('action_mark_as_read', $content); - $this->assertArrayHasKey('list_mode', $content); + $this->assertArrayHasKey('id', $config); + $this->assertArrayHasKey('items_per_page', $config); + $this->assertArrayHasKey('language', $config); + $this->assertArrayHasKey('reading_speed', $config); + $this->assertArrayHasKey('action_mark_as_read', $config); + $this->assertArrayHasKey('list_mode', $config); - $this->assertSame(200.0, $content['reading_speed']); - $this->assertSame('en', $content['language']); + $this->assertSame(200.0, $config['reading_speed']); + $this->assertSame('en', $config['language']); + + $this->assertCount(6, $config); $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type')); } @@ -32,12 +34,12 @@ class ConfigRestControllerTest extends WallabagApiTestCase $client->request('GET', '/api/config.json'); $this->assertSame(401, $client->getResponse()->getStatusCode()); - $content = json_decode($client->getResponse()->getContent(), true); + $config = json_decode($client->getResponse()->getContent(), true); - $this->assertArrayHasKey('error', $content); - $this->assertArrayHasKey('error_description', $content); + $this->assertArrayHasKey('error', $config); + $this->assertArrayHasKey('error_description', $config); - $this->assertSame('access_denied', $content['error']); + $this->assertSame('access_denied', $config['error']); $this->assertSame('application/json', $client->getResponse()->headers->get('Content-Type')); }