diff --git a/src/Wallabag/ApiBundle/Controller/ConfigRestController.php b/src/Wallabag/ApiBundle/Controller/ConfigRestController.php new file mode 100644 index 000000000..134efd19d --- /dev/null +++ b/src/Wallabag/ApiBundle/Controller/ConfigRestController.php @@ -0,0 +1,32 @@ +validateAuthentication(); + + $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/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/src/Wallabag/CoreBundle/Entity/Config.php b/src/Wallabag/CoreBundle/Entity/Config.php index 1bed45138..f00077110 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; @@ -50,6 +53,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 +63,8 @@ class Config * * @Assert\NotBlank() * @ORM\Column(name="language", type="string", nullable=false) + * + * @Groups({"config_api"}) */ private $language; @@ -65,6 +72,8 @@ class Config * @var string * * @ORM\Column(name="feed_token", type="string", nullable=true) + * + * @Groups({"config_api"}) */ private $feedToken; @@ -77,6 +86,8 @@ class Config * max = 100000, * maxMessage = "validator.feed_limit_too_high" * ) + * + * @Groups({"config_api"}) */ private $feedLimit; @@ -84,6 +95,8 @@ class Config * @var float * * @ORM\Column(name="reading_speed", type="float", nullable=true) + * + * @Groups({"config_api"}) */ private $readingSpeed; @@ -98,6 +111,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 +120,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 new file mode 100644 index 000000000..e6cd6eb5c --- /dev/null +++ b/tests/Wallabag/ApiBundle/Controller/ConfigRestControllerTest.php @@ -0,0 +1,46 @@ +client->request('GET', '/api/config.json'); + $this->assertSame(200, $this->client->getResponse()->getStatusCode()); + + $config = json_decode($this->client->getResponse()->getContent(), true); + + $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, $config['reading_speed']); + $this->assertSame('en', $config['language']); + + $this->assertCount(6, $config); + + $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()); + + $config = json_decode($client->getResponse()->getContent(), true); + + $this->assertArrayHasKey('error', $config); + $this->assertArrayHasKey('error_description', $config); + + $this->assertSame('access_denied', $config['error']); + + $this->assertSame('application/json', $client->getResponse()->headers->get('Content-Type')); + } +}