From a83c2659881a91e10179b677ab24b5e6c3df8249 Mon Sep 17 00:00:00 2001 From: Mateusz Zalewski Date: Wed, 2 Aug 2017 13:38:04 +0200 Subject: [PATCH 1/3] "/me" endpoint test --- .../LoggedInCustomerDetailsActionTest.php | 40 +++++++++++++++++++ .../logged_in_customer_details_response.json | 5 +++ 2 files changed, 45 insertions(+) create mode 100644 tests/Controller/LoggedInCustomerDetailsActionTest.php create mode 100644 tests/Responses/Expected/customer/logged_in_customer_details_response.json diff --git a/tests/Controller/LoggedInCustomerDetailsActionTest.php b/tests/Controller/LoggedInCustomerDetailsActionTest.php new file mode 100644 index 000000000..39f095140 --- /dev/null +++ b/tests/Controller/LoggedInCustomerDetailsActionTest.php @@ -0,0 +1,40 @@ +loadFixturesFromFile('customer.yml'); + + $data = +<<client->request('POST', '/shop-api/login_check', [], [], ['CONTENT_TYPE' => 'application/json', 'ACCEPT' => 'application/json'], $data); + + $response = json_decode($this->client->getResponse()->getContent(), true); + $this->client->setServerParameter('HTTP_Authorization', sprintf('Bearer %s', $response['token'])); + + $this->client->request('GET', '/me', [], [], [ + 'CONTENT_TYPE' => 'application/json', + 'ACCEPT' => 'application/json', + ]); + + $response = $this->client->getResponse(); + $this->assertResponse($response, 'customer/logged_in_customer_detail_response', Response::HTTP_OK); + } +} diff --git a/tests/Responses/Expected/customer/logged_in_customer_details_response.json b/tests/Responses/Expected/customer/logged_in_customer_details_response.json new file mode 100644 index 000000000..9431fe6fc --- /dev/null +++ b/tests/Responses/Expected/customer/logged_in_customer_details_response.json @@ -0,0 +1,5 @@ +{ + "firstName": "Olivier", + "lastName": "Queen", + "email": "oliver@queen.com" +} From b61907a20dda12800e4dcb95e877ce9ec8d537b5 Mon Sep 17 00:00:00 2001 From: Mateusz Zalewski Date: Wed, 2 Aug 2017 14:14:29 +0200 Subject: [PATCH 2/3] Implement logged in customer details endpoint --- .../LoggedInCustomerDetailsAction.php | 52 +++++++++++++++++++ src/Resources/config/routing.yml | 4 ++ src/Resources/config/routing/customer.yml | 5 ++ .../config/services/actions/customer.xml | 7 +++ .../LoggedInCustomerDetailsActionTest.php | 4 +- .../logged_in_customer_details_response.json | 2 +- 6 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 src/Controller/Customer/LoggedInCustomerDetailsAction.php create mode 100644 src/Resources/config/routing/customer.yml diff --git a/src/Controller/Customer/LoggedInCustomerDetailsAction.php b/src/Controller/Customer/LoggedInCustomerDetailsAction.php new file mode 100644 index 000000000..ee72b45da --- /dev/null +++ b/src/Controller/Customer/LoggedInCustomerDetailsAction.php @@ -0,0 +1,52 @@ +viewHandler = $viewHandler; + $this->tokenStorage = $tokenStorage; + } + + public function __invoke(Request $request): Response + { + /** @var ShopUserInterface $user */ + $user = $this->tokenStorage->getToken()->getUser(); + + Assert::isInstanceOf($user, ShopUserInterface::class); + + $customer = $user->getCustomer(); + + return $this->viewHandler->handle(View::create([ + 'firstName' => $customer->getFirstName(), + 'lastName' => $customer->getLastName(), + 'email' => $customer->getEmail(), + ], Response::HTTP_OK)); + } +} diff --git a/src/Resources/config/routing.yml b/src/Resources/config/routing.yml index ebe41ba2d..9a4875bbc 100644 --- a/src/Resources/config/routing.yml +++ b/src/Resources/config/routing.yml @@ -21,3 +21,7 @@ shop_api_register: shop_api_checkout: resource: "@ShopApiPlugin/Resources/config/routing/checkout.yml" prefix: /shop-api/checkout + +shop_api_customer: + resource: "@ShopApiPlugin/Resources/config/routing/customer.yml" + prefix: /shop-api diff --git a/src/Resources/config/routing/customer.yml b/src/Resources/config/routing/customer.yml new file mode 100644 index 000000000..1c05c5b7c --- /dev/null +++ b/src/Resources/config/routing/customer.yml @@ -0,0 +1,5 @@ +shop_api_me: + path: /me + methods: [GET] + defaults: + _controller: sylius.shop_api_plugin.controller.customer.logged_in_customer_details_action diff --git a/src/Resources/config/services/actions/customer.xml b/src/Resources/config/services/actions/customer.xml index e0105a6c0..c248dbe66 100644 --- a/src/Resources/config/services/actions/customer.xml +++ b/src/Resources/config/services/actions/customer.xml @@ -26,5 +26,12 @@ + + + + + diff --git a/tests/Controller/LoggedInCustomerDetailsActionTest.php b/tests/Controller/LoggedInCustomerDetailsActionTest.php index 39f095140..ff006439b 100644 --- a/tests/Controller/LoggedInCustomerDetailsActionTest.php +++ b/tests/Controller/LoggedInCustomerDetailsActionTest.php @@ -29,12 +29,12 @@ public function it_shows_currently_logged_in_customer_details() $response = json_decode($this->client->getResponse()->getContent(), true); $this->client->setServerParameter('HTTP_Authorization', sprintf('Bearer %s', $response['token'])); - $this->client->request('GET', '/me', [], [], [ + $this->client->request('GET', '/shop-api/me', [], [], [ 'CONTENT_TYPE' => 'application/json', 'ACCEPT' => 'application/json', ]); $response = $this->client->getResponse(); - $this->assertResponse($response, 'customer/logged_in_customer_detail_response', Response::HTTP_OK); + $this->assertResponse($response, 'customer/logged_in_customer_details_response', Response::HTTP_OK); } } diff --git a/tests/Responses/Expected/customer/logged_in_customer_details_response.json b/tests/Responses/Expected/customer/logged_in_customer_details_response.json index 9431fe6fc..9e3e74acf 100644 --- a/tests/Responses/Expected/customer/logged_in_customer_details_response.json +++ b/tests/Responses/Expected/customer/logged_in_customer_details_response.json @@ -1,5 +1,5 @@ { - "firstName": "Olivier", + "firstName": "Oliver", "lastName": "Queen", "email": "oliver@queen.com" } From 448000ece064df88476eb0d5056e4718fa0c44a1 Mon Sep 17 00:00:00 2001 From: Mateusz Zalewski Date: Mon, 7 Aug 2017 09:11:22 +0200 Subject: [PATCH 3/3] "/me" endpoint docs --- doc/swagger.yml | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/doc/swagger.yml b/doc/swagger.yml index a09b00c27..588eae177 100644 --- a/doc/swagger.yml +++ b/doc/swagger.yml @@ -756,7 +756,7 @@ paths: - "users" summary: "Request resetting password of user with passed email." description: "Email with reset password path will be sent to user. Default path for password resetting is `/password-reset/{token}`. To change it, you need to override template `@SyliusShopApi\\Email\\passwordReset.html.twig`." - opearationId: "requestPasswordReset" + operationId: "requestPasswordReset" consumes: - "application/json" parameters: @@ -764,13 +764,26 @@ paths: in: "body" description: "Email of user which want to reset password." required: true - schama: + schema: $ref: "#/definitions/RequestPasswordResetting" responses: 204: description: "Reset password request has been sent." 500: description: "User with provided email has not been found." + /me: + get: + tags: + - "users" + summary: "Provides currently logged in user details." + operationId: "me" + responses: + 200: + schema: + type: "object" + $ref: "#/definitions/LoggedInCustomerDetails" + 500: + description: "There is no currently logged in user." definitions: PickupCartRequest: @@ -1330,6 +1343,18 @@ definitions: email: type: "string" example: "test@example.com" + LoggedInCustomerDetails: + type: "object" + properties: + firstName: + type: "string" + example: "Sherlock" + lastName: + type: "string" + example: "Holmes" + email: + type: "string" + example: "sherlock@holmes.com" GeneralError: type: "object" properties: