From aaa9a1ec7be825277fb2e9dc169062969547a9fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Sat, 26 Jun 2021 10:41:22 +0200 Subject: [PATCH 1/3] create user _index and _entity endpoint --- .../Controller/UserApiController.php | 10 ++++ .../ChillMainExtension.php | 23 ++++++++- .../ChillMainBundle/chill.api.specs.yaml | 48 +++++++++++++++++++ 3 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 src/Bundle/ChillMainBundle/Controller/UserApiController.php diff --git a/src/Bundle/ChillMainBundle/Controller/UserApiController.php b/src/Bundle/ChillMainBundle/Controller/UserApiController.php new file mode 100644 index 000000000..6b583f546 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Controller/UserApiController.php @@ -0,0 +1,10 @@ + \Chill\MainBundle\Entity\User::class, + 'controller' => \Chill\MainBundle\Controller\UserApiController::class, + 'name' => 'user', + 'base_path' => '/api/1.0/main/user', + 'base_role' => 'ROLE_USER', + 'actions' => [ + '_index' => [ + 'methods' => [ + Request::METHOD_GET => true, + Request::METHOD_HEAD => true + ], + ], + '_entity' => [ + 'methods' => [ + Request::METHOD_GET => true, + Request::METHOD_HEAD => true, + ] + ], + ] + ], ] ]); } diff --git a/src/Bundle/ChillMainBundle/chill.api.specs.yaml b/src/Bundle/ChillMainBundle/chill.api.specs.yaml index b81cc97aa..e53900643 100644 --- a/src/Bundle/ChillMainBundle/chill.api.specs.yaml +++ b/src/Bundle/ChillMainBundle/chill.api.specs.yaml @@ -10,6 +10,19 @@ servers: components: schemas: + User: + type: object + properties: + id: + type: integer + type: + type: string + enum: + - user + username: + type: string + text: + type: string Center: type: object properties: @@ -425,3 +438,38 @@ paths: description: "not found" 401: description: "Unauthorized" + + + /1.0/main/user.json: + get: + tags: + - user + summary: Return a list of all user + responses: + 200: + description: "ok" + /1.0/main/user/{id}.json: + get: + tags: + - user + summary: Return a user by id + parameters: + - name: id + in: path + required: true + description: The user id + schema: + type: integer + format: integer + minimum: 1 + responses: + 200: + description: "ok" + content: + application/json: + schema: + $ref: '#/components/schemas/User' + 404: + description: "not found" + 401: + description: "Unauthorized" From abe012de60e9fccbdf10e13274dc0d6f81e751ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Sat, 26 Jun 2021 11:08:10 +0200 Subject: [PATCH 2/3] add endpoint whoami --- .../Controller/UserApiController.php | 17 +++++++++++++++++ src/Bundle/ChillMainBundle/chill.api.specs.yaml | 8 ++++++++ 2 files changed, 25 insertions(+) diff --git a/src/Bundle/ChillMainBundle/Controller/UserApiController.php b/src/Bundle/ChillMainBundle/Controller/UserApiController.php index 6b583f546..3742a7ded 100644 --- a/src/Bundle/ChillMainBundle/Controller/UserApiController.php +++ b/src/Bundle/ChillMainBundle/Controller/UserApiController.php @@ -3,8 +3,25 @@ namespace Chill\MainBundle\Controller; use Chill\MainBundle\CRUD\Controller\ApiController; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\JsonResponse; +use Symfony\Component\Routing\Annotation\Route; class UserApiController extends ApiController { + /** + * @Route( + * "/api/1.0/main/whoami.{_format}", + * name="chill_main_user_whoami", + * requirements={ + * "_format": "json" + * } + * ) + */ + public function whoami($_format): JsonResponse + { + return $this->json($this->getUser(), JsonResponse::HTTP_OK, [], + [ "groups" => [ "read" ] ]); + } } diff --git a/src/Bundle/ChillMainBundle/chill.api.specs.yaml b/src/Bundle/ChillMainBundle/chill.api.specs.yaml index e53900643..775407847 100644 --- a/src/Bundle/ChillMainBundle/chill.api.specs.yaml +++ b/src/Bundle/ChillMainBundle/chill.api.specs.yaml @@ -448,6 +448,14 @@ paths: responses: 200: description: "ok" + /1.0/main/whoami.json: + get: + tags: + - user + summary: Return the currently authenticated user + responses: + 200: + description: "ok" /1.0/main/user/{id}.json: get: tags: From a43e6c12a0c200ac6534aa190323466d64d485e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Sat, 26 Jun 2021 11:08:26 +0200 Subject: [PATCH 3/3] add tests for UserApiController --- .../Controller/UserApiControllerTest.php | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/Bundle/ChillMainBundle/Tests/Controller/UserApiControllerTest.php diff --git a/src/Bundle/ChillMainBundle/Tests/Controller/UserApiControllerTest.php b/src/Bundle/ChillMainBundle/Tests/Controller/UserApiControllerTest.php new file mode 100644 index 000000000..85d9895fc --- /dev/null +++ b/src/Bundle/ChillMainBundle/Tests/Controller/UserApiControllerTest.php @@ -0,0 +1,49 @@ +getClientAuthenticated(); + + $client->request(Request::METHOD_GET, '/api/1.0/main/user.json'); + + $this->assertResponseIsSuccessful(); + + $data = \json_decode($client->getResponse()->getContent(), true); + $this->assertTrue(\array_key_exists('count', $data)); + $this->assertGreaterThan(0, $data['count']); + $this->assertTrue(\array_key_exists('results', $data)); + + return $data['results'][0]; + } + + /** + * @depends testIndex + */ + public function testEntity($existingUser) + { + $client = $this->getClientAuthenticated(); + + $client->request(Request::METHOD_GET, '/api/1.0/main/user/'.$existingUser['id'].'.json'); + + $this->assertResponseIsSuccessful(); + } + + public function testWhoami() + { + $client = $this->getClientAuthenticated(); + + $client->request(Request::METHOD_GET, '/api/1.0/main/whoami.json'); + + $this->assertResponseIsSuccessful(); + } +}