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

Commit ebe6d77

Browse files
committed
feat(PasswordResetToken): support ttlMinutes for token
1 parent 7ba30dd commit ebe6d77

File tree

3 files changed

+69
-2
lines changed

3 files changed

+69
-2
lines changed

src/Core/Request/Customers/CustomerPasswordTokenRequest.php

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
class CustomerPasswordTokenRequest extends AbstractApiRequest
2828
{
2929
const EMAIL = 'email';
30+
const TTL_MINUTES = 'ttlMinutes';
3031

3132
protected $resultClass = CustomerToken::class;
3233

@@ -35,14 +36,24 @@ class CustomerPasswordTokenRequest extends AbstractApiRequest
3536
*/
3637
protected $email;
3738

39+
/**
40+
* @var int
41+
*/
42+
protected $ttlMinutes;
43+
3844
/**
3945
* @param string $email
40-
* @param Context $context
46+
* @param Context|null $context
47+
* @param int|null $ttlMinutes
4148
*/
42-
public function __construct($email, Context $context = null)
49+
public function __construct($email, Context $context = null, $ttlMinutes = null)
4350
{
4451
parent::__construct(CustomersEndpoint::endpoint(), $context);
4552
$this->email = $email;
53+
54+
if (!is_null($ttlMinutes)) {
55+
$this->ttlMinutes = $ttlMinutes;
56+
}
4657
}
4758

4859
/**
@@ -57,6 +68,20 @@ public static function ofEmail(
5768
return new static($email, $context);
5869
}
5970

71+
/**
72+
* @param string $email
73+
* @param int $ttlMinutes
74+
* @param Context $context
75+
* @return static
76+
*/
77+
public static function ofEmailAndTtlMinutes(
78+
$email,
79+
$ttlMinutes,
80+
Context $context = null
81+
) {
82+
return new static($email, $context, $ttlMinutes);
83+
}
84+
6085
/**
6186
* @return string
6287
* @internal
@@ -75,6 +100,11 @@ public function httpRequest()
75100
$payload = [
76101
static::EMAIL => $this->email
77102
];
103+
104+
if (!is_null($this->ttlMinutes)) {
105+
$payload[static::TTL_MINUTES] = $this->ttlMinutes;
106+
}
107+
78108
return new JsonRequest(HttpMethod::POST, $this->getPath(), $payload);
79109
}
80110

tests/integration/Customer/CustomerLoginRequestTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Commercetools\Core\Error\DuplicateFieldError;
1414
use Commercetools\Core\Model\Cart\CartState;
1515
use Commercetools\Core\Model\Customer\CustomerDraft;
16+
use Commercetools\Core\Model\Customer\CustomerToken;
1617
use Commercetools\Core\Request\Carts\CartByIdGetRequest;
1718
use Commercetools\Core\Request\Carts\CartCreateRequest;
1819
use Commercetools\Core\Request\Carts\CartDeleteRequest;
@@ -186,6 +187,26 @@ public function testPasswordReset()
186187
$this->assertTrue($response->isError());
187188
}
188189

190+
public function testPasswordResetWithTtlMinutes()
191+
{
192+
$draft = $this->getDraft('email');
193+
$customer = $this->createCustomer($draft);
194+
195+
$request = CustomerPasswordTokenRequest::ofEmailAndTtlMinutes(
196+
$customer->getEmail(),
197+
60
198+
);
199+
$response = $request->executeWithClient($this->getClient());
200+
$result = $request->mapResponse($response);
201+
202+
$this->assertInstanceOf(CustomerToken::class, $result);
203+
204+
$dateCreated = $result->getCreatedAt()->getDateTime();
205+
$dateExpires = $result->getExpiresAt()->getDateTime();
206+
$interval = $dateExpires->diff($dateCreated);
207+
$this->assertSame(1, (int)$interval->format('%h'));
208+
}
209+
189210
public function testPasswordResetLowerCased()
190211
{
191212
$draft = $this->getDraft('email');

tests/unit/Request/Customers/CustomerPasswordTokenRequestTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,22 @@ public function testHttpRequestObject()
4242
);
4343
}
4444

45+
public function testHttpRequestObjectWithTtlMinutes()
46+
{
47+
$request = CustomerPasswordTokenRequest::ofEmailAndTtlMinutes('john.doe@company.com', 50);
48+
$httpRequest = $request->httpRequest();
49+
50+
$this->assertJsonStringEqualsJsonString(
51+
json_encode(
52+
[
53+
'email' => 'john.doe@company.com',
54+
'ttlMinutes' => 50
55+
]
56+
),
57+
(string)$httpRequest->getBody()
58+
);
59+
}
60+
4561
public function testBuildResponse()
4662
{
4763
$mockBuilder = $this->getMockBuilder('\GuzzleHttp\Psr7\Response');

0 commit comments

Comments
 (0)