From 318e93f9822c80c360855d795acb5c722f0f4639 Mon Sep 17 00:00:00 2001 From: Jens Schulze Date: Wed, 23 Mar 2016 15:01:10 +0100 Subject: [PATCH] refactor(Customer): adjust customer password change request to API changes BREAKING CHANGE: Before: ``` CustomerPasswordResetRequest::ofIdVersionTokenAndPassword($id, $version, $token, $newPassword) ``` After: ``` CustomerPasswordResetRequest::ofTokenAndPassword($token, $newPassword) ``` --- features/bootstrap/ApiContext.php | 7 +- .../Customer/CustomerPasswordChange.feature | 5 +- src/Model/Order/ShipmentState.php | 1 + .../CustomerPasswordResetRequest.php | 29 ++-- .../Customer/CustomerLoginRequestTest.php | 146 ++++++++++++++++++ .../CustomerPasswordResetRequestTest.php | 8 +- 6 files changed, 167 insertions(+), 29 deletions(-) create mode 100644 tests/integration/Customer/CustomerLoginRequestTest.php diff --git a/features/bootstrap/ApiContext.php b/features/bootstrap/ApiContext.php index 8d9c014c39..8deb023851 100644 --- a/features/bootstrap/ApiContext.php +++ b/features/bootstrap/ApiContext.php @@ -534,12 +534,9 @@ public function iWantToResetTheContextPasswordToNewPasswordWithToken($context, $ $context = $this->getContext($context); $module = $this->getModuleName($context); $request = '\Commercetools\Core\Request\\' . $module . '\\' . $context . 'PasswordResetRequest'; - $requestContext = $context . 'Request'; - $id = $this->objects[$requestContext]['id']; - $version = $this->objects[$requestContext]['version']; $this->request = call_user_func_array( - $request. '::ofIdVersionTokenAndPassword', - [$id, $version, $token, $newPassword] + $request. '::ofTokenAndPassword', + [$token, $newPassword] ); } diff --git a/features/request/Customer/CustomerPasswordChange.feature b/features/request/Customer/CustomerPasswordChange.feature index 1487382b87..f47fa03899 100644 --- a/features/request/Customer/CustomerPasswordChange.feature +++ b/features/request/Customer/CustomerPasswordChange.feature @@ -18,15 +18,12 @@ Feature: I want to change the customer's password And the method should be "GET" Scenario: Reset customers password - Given a "customer" is identified by "id" and version 1 - And i want to reset the "customer" password to "newPassword" with token "token" + Given i want to reset the "customer" password to "newPassword" with token "token" Then the path should be "customers/password/reset" And the method should be "POST" And the request should be """ { - "id": "id", - "version": 1, "tokenValue": "token", "newPassword": "newPassword" } diff --git a/src/Model/Order/ShipmentState.php b/src/Model/Order/ShipmentState.php index df550dfc90..58f0268262 100644 --- a/src/Model/Order/ShipmentState.php +++ b/src/Model/Order/ShipmentState.php @@ -16,4 +16,5 @@ class ShipmentState const PENDING = 'Pending'; const PARTIAL = 'Partial'; const BACKORDER = 'Backorder'; + const DELAYED = 'Delayed'; } diff --git a/src/Request/Customers/CustomerPasswordResetRequest.php b/src/Request/Customers/CustomerPasswordResetRequest.php index fb71e4f9be..e4c4d7d3bc 100644 --- a/src/Request/Customers/CustomerPasswordResetRequest.php +++ b/src/Request/Customers/CustomerPasswordResetRequest.php @@ -9,19 +9,19 @@ use Commercetools\Core\Client\HttpMethod; use Commercetools\Core\Client\JsonRequest; use Commercetools\Core\Model\Common\Context; -use Commercetools\Core\Request\AbstractUpdateRequest; +use Commercetools\Core\Request\AbstractApiRequest; use Commercetools\Core\Model\Customer\Customer; use Commercetools\Core\Response\ApiResponseInterface; +use Commercetools\Core\Response\ResourceResponse; +use Psr\Http\Message\ResponseInterface; /** * @package Commercetools\Core\Request\Customers * @link https://dev.commercetools.com/http-api-projects-customers.html#reset-customers-password * @method Customer mapResponse(ApiResponseInterface $response) */ -class CustomerPasswordResetRequest extends AbstractUpdateRequest +class CustomerPasswordResetRequest extends AbstractApiRequest { - const ID = 'id'; - const VERSION = 'version'; const TOKEN_VALUE = 'tokenValue'; const NEW_PASSWORD = 'newPassword'; @@ -44,31 +44,25 @@ class CustomerPasswordResetRequest extends AbstractUpdateRequest * @param string $newPassword * @param Context $context */ - public function __construct($id, $version, $tokenValue, $newPassword, Context $context = null) + public function __construct($tokenValue, $newPassword, Context $context = null) { - parent::__construct(CustomersEndpoint::endpoint(), $id, $version, [], $context); - $this->setId($id); - $this->setVersion($version); + parent::__construct(CustomersEndpoint::endpoint(), $context); $this->tokenValue = $tokenValue; $this->newPassword = $newPassword; } /** - * @param string $id - * @param int $version * @param string $tokenValue * @param string $newPassword * @param Context $context * @return static */ - public static function ofIdVersionTokenAndPassword( - $id, - $version, + public static function ofTokenAndPassword( $tokenValue, $newPassword, Context $context = null ) { - return new static($id, $version, $tokenValue, $newPassword, $context); + return new static($tokenValue, $newPassword, $context); } /** @@ -87,11 +81,14 @@ protected function getPath() public function httpRequest() { $payload = [ - static::ID => $this->getId(), - static::VERSION => $this->getVersion(), static::TOKEN_VALUE => $this->tokenValue, static::NEW_PASSWORD => $this->newPassword ]; return new JsonRequest(HttpMethod::POST, $this->getPath(), $payload); } + + public function buildResponse(ResponseInterface $response) + { + return new ResourceResponse($response, $this, $this->getContext()); + } } diff --git a/tests/integration/Customer/CustomerLoginRequestTest.php b/tests/integration/Customer/CustomerLoginRequestTest.php new file mode 100644 index 0000000000..91c0ccebff --- /dev/null +++ b/tests/integration/Customer/CustomerLoginRequestTest.php @@ -0,0 +1,146 @@ + + */ + + +namespace Commercetools\Core\Customer; + +use Commercetools\Core\ApiTestCase; +use Commercetools\Core\Model\Customer\CustomerDraft; +use Commercetools\Core\Request\Customers\CustomerByTokenGetRequest; +use Commercetools\Core\Request\Customers\CustomerCreateRequest; +use Commercetools\Core\Request\Customers\CustomerDeleteRequest; +use Commercetools\Core\Request\Customers\CustomerLoginRequest; +use Commercetools\Core\Request\Customers\CustomerPasswordChangeRequest; +use Commercetools\Core\Request\Customers\CustomerPasswordResetRequest; +use Commercetools\Core\Request\Customers\CustomerPasswordTokenRequest; + +class CustomerLoginRequestTest extends ApiTestCase +{ + /** + * @return CustomerDraft + */ + protected function getDraft($name) + { + $draft = CustomerDraft::ofEmailNameAndPassword( + 'test-' . $this->getTestRun() . '-email', + 'test-' . $this->getTestRun() . '-' . $name, + 'test-' . $this->getTestRun() . '-lastName', + 'test-' . $this->getTestRun() . '-password' + ); + + return $draft; + } + + protected function createCustomer(CustomerDraft $draft) + { + $request = CustomerCreateRequest::ofDraft($draft); + $response = $request->executeWithClient($this->getClient()); + $result = $request->mapResponse($response); + + $this->cleanupRequests[] = $this->deleteRequest = CustomerDeleteRequest::ofIdAndVersion( + $result->getCustomer()->getId(), + $result->getCustomer()->getVersion() + ); + return $result->getCustomer(); + } + + public function testLoginSuccess() + { + $draft = $this->getDraft('email'); + $customer = $this->createCustomer($draft); + + $request = CustomerLoginRequest::ofEmailAndPassword($customer->getEmail(), $draft->getPassword()); + $response = $request->executeWithClient($this->getClient()); + $result = $request->mapResponse($response); + + $this->assertSame($customer->getId(), $result->getCustomer()->getId()); + } + + public function testLoginFailure() + { + $draft = $this->getDraft('email'); + $customer = $this->createCustomer($draft); + + $request = CustomerLoginRequest::ofEmailAndPassword($customer->getEmail(), $this->getTestRun()); + $response = $request->executeWithClient($this->getClient()); + + $this->assertTrue($response->isError()); + } + + public function testChangePasswordSuccess() + { + $draft = $this->getDraft('email'); + $customer = $this->createCustomer($draft); + + $request = CustomerPasswordChangeRequest::ofIdVersionAndPasswords( + $customer->getId(), + $customer->getVersion(), + $draft->getPassword(), + $this->getTestRun() + ); + $response = $request->executeWithClient($this->getClient()); + $result = $request->mapResponse($response); + $this->deleteRequest->setVersion($result->getVersion()); + + $this->assertSame($customer->getId(), $result->getId()); + } + + public function testChangePasswordFailure() + { + $draft = $this->getDraft('email'); + $customer = $this->createCustomer($draft); + + $request = CustomerPasswordChangeRequest::ofIdVersionAndPasswords( + $customer->getId(), + $customer->getVersion(), + $this->getTestRun(), + $draft->getPassword() + ); + $response = $request->executeWithClient($this->getClient()); + $this->assertTrue($response->isError()); + } + + public function testPasswordReset() + { + $draft = $this->getDraft('email'); + $customer = $this->createCustomer($draft); + + $request = CustomerPasswordTokenRequest::ofEmail( + $customer->getEmail() + ); + $response = $request->executeWithClient($this->getClient()); + $result = $request->mapResponse($response); + + $token = $result->getValue(); + $this->assertNotEmpty($token); + + $request = CustomerByTokenGetRequest::ofToken( + $token + ); + $response = $request->executeWithClient($this->getClient()); + $result = $request->mapResponse($response); + $this->assertSame($customer->getId(), $result->getId()); + + $request = CustomerPasswordResetRequest::ofTokenAndPassword( + $token, + $this->getTestRun() + ); + $response = $request->executeWithClient($this->getClient()); + $result = $request->mapResponse($response); + $this->deleteRequest->setVersion($result->getVersion()); + + $this->assertSame($customer->getId(), $result->getId()); + + $request = CustomerLoginRequest::ofEmailAndPassword($customer->getEmail(), $this->getTestRun()); + $response = $request->executeWithClient($this->getClient()); + $result = $request->mapResponse($response); + + $this->assertSame($customer->getId(), $result->getCustomer()->getId()); + + $request = CustomerLoginRequest::ofEmailAndPassword($customer->getEmail(), $draft->getPassword()); + $response = $request->executeWithClient($this->getClient()); + $this->assertTrue($response->isError()); + } +} diff --git a/tests/unit/Request/Customers/CustomerPasswordResetRequestTest.php b/tests/unit/Request/Customers/CustomerPasswordResetRequestTest.php index d13204c22c..42773199fb 100644 --- a/tests/unit/Request/Customers/CustomerPasswordResetRequestTest.php +++ b/tests/unit/Request/Customers/CustomerPasswordResetRequestTest.php @@ -18,7 +18,7 @@ class CustomerPasswordResetRequestTest extends RequestTestCase public function testHttpRequestMethod() { - $request = CustomerPasswordResetRequest::ofIdVersionTokenAndPassword('customerId', 1, 'resetToken', 'newPW'); + $request = CustomerPasswordResetRequest::ofTokenAndPassword('resetToken', 'newPW'); $httpRequest = $request->httpRequest(); $this->assertSame(HttpMethod::POST, $httpRequest->getMethod()); @@ -26,7 +26,7 @@ public function testHttpRequestMethod() public function testHttpRequestPath() { - $request = CustomerPasswordResetRequest::ofIdVersionTokenAndPassword('customerId', 1, 'resetToken', 'newPW'); + $request = CustomerPasswordResetRequest::ofTokenAndPassword('resetToken', 'newPW'); $httpRequest = $request->httpRequest(); $this->assertSame('customers/password/reset', (string)$httpRequest->getUri()); @@ -34,12 +34,12 @@ public function testHttpRequestPath() public function testHttpRequestObject() { - $request = CustomerPasswordResetRequest::ofIdVersionTokenAndPassword('customerId', 1, 'resetToken', 'newPW'); + $request = CustomerPasswordResetRequest::ofTokenAndPassword('resetToken', 'newPW'); $httpRequest = $request->httpRequest(); $this->assertJsonStringEqualsJsonString( json_encode( - ['id' => 'customerId', 'version' => 1, 'tokenValue' => 'resetToken', 'newPassword' => 'newPW'] + ['tokenValue' => 'resetToken', 'newPassword' => 'newPW'] ), (string)$httpRequest->getBody() );