Skip to content
This repository has been archived by the owner on Oct 24, 2023. It is now read-only.

Commit

Permalink
refactor(Customer): adjust customer password change request to API ch…
Browse files Browse the repository at this point in the history
…anges

BREAKING CHANGE:

Before:
```
CustomerPasswordResetRequest::ofIdVersionTokenAndPassword($id, $version, $token, $newPassword)
```

After:
```
CustomerPasswordResetRequest::ofTokenAndPassword($token, $newPassword)
```
  • Loading branch information
Jens Schulze committed Mar 23, 2016
1 parent 1b0b930 commit 318e93f
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 29 deletions.
7 changes: 2 additions & 5 deletions features/bootstrap/ApiContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]
);
}

Expand Down
5 changes: 1 addition & 4 deletions features/request/Customer/CustomerPasswordChange.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
1 change: 1 addition & 0 deletions src/Model/Order/ShipmentState.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ class ShipmentState
const PENDING = 'Pending';
const PARTIAL = 'Partial';
const BACKORDER = 'Backorder';
const DELAYED = 'Delayed';
}
29 changes: 13 additions & 16 deletions src/Request/Customers/CustomerPasswordResetRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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);
}

/**
Expand All @@ -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());
}
}
146 changes: 146 additions & 0 deletions tests/integration/Customer/CustomerLoginRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
<?php
/**
* @author @jayS-de <jens.schulze@commercetools.de>
*/


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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,28 @@ 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());
}

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());
}

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()
);
Expand Down

0 comments on commit 318e93f

Please # to comment.