Return 201 on user creation

This commit is contained in:
Jeremy Benoist 2017-06-02 20:03:25 +02:00
parent 044079967b
commit a1e6187406
No known key found for this signature in database
GPG key ID: BCA73962457ACC3C
2 changed files with 14 additions and 7 deletions

View file

@ -46,7 +46,9 @@ class UserRestController extends WallabagRestController
if (!$this->getParameter('fosuser_registration') || !$this->get('craue_config')->get('api_user_registration')) { if (!$this->getParameter('fosuser_registration') || !$this->get('craue_config')->get('api_user_registration')) {
$json = $this->get('serializer')->serialize(['error' => "Server doesn't allow registrations"], 'json'); $json = $this->get('serializer')->serialize(['error' => "Server doesn't allow registrations"], 'json');
return (new JsonResponse())->setJson($json)->setStatusCode(403); return (new JsonResponse())
->setJson($json)
->setStatusCode(JsonResponse::HTTP_FORBIDDEN);
} }
$userManager = $this->get('fos_user.user_manager'); $userManager = $this->get('fos_user.user_manager');
@ -90,7 +92,9 @@ class UserRestController extends WallabagRestController
$json = $this->get('serializer')->serialize(['error' => $errors], 'json'); $json = $this->get('serializer')->serialize(['error' => $errors], 'json');
return (new JsonResponse())->setJson($json)->setStatusCode(400); return (new JsonResponse())
->setJson($json)
->setStatusCode(JsonResponse::HTTP_BAD_REQUEST);
} }
$userManager->updateUser($user); $userManager->updateUser($user);
@ -99,17 +103,18 @@ class UserRestController extends WallabagRestController
$event = new UserEvent($user, $request); $event = new UserEvent($user, $request);
$this->get('event_dispatcher')->dispatch(FOSUserEvents::USER_CREATED, $event); $this->get('event_dispatcher')->dispatch(FOSUserEvents::USER_CREATED, $event);
return $this->sendUser($user); return $this->sendUser($user, JsonResponse::HTTP_CREATED);
} }
/** /**
* Send user response. * Send user response.
* *
* @param User $user * @param User $user
* @param int $status HTTP Status code to send
* *
* @return JsonResponse * @return JsonResponse
*/ */
private function sendUser(User $user) private function sendUser(User $user, $status = JsonResponse::HTTP_OK)
{ {
$json = $this->get('serializer')->serialize( $json = $this->get('serializer')->serialize(
$user, $user,
@ -117,7 +122,9 @@ class UserRestController extends WallabagRestController
SerializationContext::create()->setGroups(['user_api']) SerializationContext::create()->setGroups(['user_api'])
); );
return (new JsonResponse())->setJson($json); return (new JsonResponse())
->setJson($json)
->setStatusCode($status);
} }
/** /**

View file

@ -52,7 +52,7 @@ class UserRestControllerTest extends WallabagApiTestCase
'email' => 'wallabag@google.com', 'email' => 'wallabag@google.com',
]); ]);
$this->assertEquals(200, $this->client->getResponse()->getStatusCode()); $this->assertEquals(201, $this->client->getResponse()->getStatusCode());
$content = json_decode($this->client->getResponse()->getContent(), true); $content = json_decode($this->client->getResponse()->getContent(), true);
@ -81,7 +81,7 @@ class UserRestControllerTest extends WallabagApiTestCase
'email' => 'wallabag@google.com', 'email' => 'wallabag@google.com',
]); ]);
$this->assertEquals(200, $client->getResponse()->getStatusCode()); $this->assertEquals(201, $client->getResponse()->getStatusCode());
$content = json_decode($client->getResponse()->getContent(), true); $content = json_decode($client->getResponse()->getContent(), true);