Merge branch 'features/create-user-endpoint' into 'master'

Feature: create user endpoint

See merge request Chill-Projet/chill-bundles!96
This commit is contained in:
Mathieu Jaumotte 2021-06-29 14:02:16 +00:00
commit ea2870eef8
4 changed files with 154 additions and 1 deletions

View File

@ -0,0 +1,27 @@
<?php
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" ] ]);
}
}

View File

@ -347,7 +347,28 @@ class ChillMainExtension extends Extension implements PrependExtensionInterface,
]
],
]
]
],
[
'class' => \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,
]
],
]
],
]
]);
}

View File

@ -0,0 +1,49 @@
<?php
namespace Chill\MainBundle\Tests\Controller;
use Chill\MainBundle\Test\PrepareClientTrait;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class UserApiControllerTest extends WebTestCase
{
use PrepareClientTrait;
public function testIndex()
{
$client = $this->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();
}
}

View File

@ -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,46 @@ 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/whoami.json:
get:
tags:
- user
summary: Return the currently authenticated 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"