[API] Enable remaining contract tests for Customer resource + minor refactor

This commit is contained in:
Grzegorz Sadowski 2024-09-02 14:01:58 +02:00
parent 08df41295a
commit b0cd88be22
No known key found for this signature in database
GPG key ID: EF87FF4E6E3BD364
4 changed files with 95 additions and 190 deletions

View file

@ -1,107 +0,0 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Sylius\Tests\Api\Shop;
use Sylius\Tests\Api\JsonApiTestCase;
use Sylius\Tests\Api\Utils\ShopUserLoginTrait;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
final class ShopUsersTest extends JsonApiTestCase
{
use ShopUserLoginTrait;
/** @test */
public function it_sends_shop_user_password_reset_email(): void
{
$this->loadFixturesFromFiles(['channel/channel.yaml', 'authentication/shop_user.yaml']);
$this->client->request(
method: Request::METHOD_POST,
uri: '/api/v2/shop/reset-password',
server: self::CONTENT_TYPE_HEADER,
content: json_encode([
'email' => 'api@example.com',
], JSON_THROW_ON_ERROR),
);
$response = $this->client->getResponse();
$this->assertResponseCode($response, Response::HTTP_ACCEPTED);
}
/** @test */
public function it_resets_shop_user_password_with_valid_token(): void
{
$this->loadFixturesFromFiles(['channel/channel.yaml', 'authentication/shop_user_with_reset_password_token.yaml']);
$validToken = 'valid_token';
$this->client->request(
method: Request::METHOD_PATCH,
uri: sprintf('/api/v2/shop/reset-password/%s', $validToken),
server: self::CONTENT_TYPE_HEADER,
content: json_encode([
'newPassword' => 'newPassword',
'confirmNewPassword' => 'newPassword',
], JSON_THROW_ON_ERROR),
);
$response = $this->client->getResponse();
$this->assertResponseCode($response, Response::HTTP_ACCEPTED);
}
/** @test */
public function it_prevents_shop_user_from_resetting_password_with_invalid_token(): void
{
$this->loadFixturesFromFiles(['channel/channel.yaml', 'authentication/shop_user_with_reset_password_token.yaml']);
$validToken = 'invalid_token';
$this->client->request(
method: Request::METHOD_PATCH,
uri: sprintf('/api/v2/shop/reset-password/%s', $validToken),
server: self::CONTENT_TYPE_HEADER,
content: json_encode([
'newPassword' => 'newPassword',
'confirmNewPassword' => 'newPassword',
], JSON_THROW_ON_ERROR),
);
$response = $this->client->getResponse();
$this->assertResponseCode($response, Response::HTTP_UNPROCESSABLE_ENTITY);
}
/** @test */
public function it_prevents_shop_user_from_resetting_password_with_expired_token(): void
{
$this->loadFixturesFromFiles(['channel/channel.yaml', 'authentication/shop_user_with_expired_reset_password_token.yaml']);
$validToken = 'valid_token';
$this->client->request(
method: Request::METHOD_PATCH,
uri: sprintf('/api/v2/shop/reset-password/%s', $validToken),
server: self::CONTENT_TYPE_HEADER,
content: json_encode([
'newPassword' => 'newPassword',
'confirmNewPassword' => 'newPassword',
], JSON_THROW_ON_ERROR),
);
$response = $this->client->getResponse();
$this->assertResponseCode($response, Response::HTTP_UNPROCESSABLE_ENTITY);
}
}

View file

@ -1,18 +1,18 @@
Sylius\Component\Core\Model\ShopUser: Sylius\Component\Core\Model\ShopUser:
shop_user_{oliver, dave}: shop_user_john:
plainPassword: "sylius" plainPassword: "sylius"
roles: [ROLE_USER] roles: [ROLE_USER]
enabled: true enabled: true
passwordResetToken: "valid_token" passwordResetToken: "valid_token"
passwordRequestedAt: "<(new \\DateTime('last week'))>" passwordRequestedAt: "<(new \\DateTime('last week'))>"
customer: "@customer_<current()>" customer: "@customer_john"
username: "<current()>\\@doe.com" username: "john@doe.com"
usernameCanonical: "<current()>\\@doe.com" usernameCanonical: "john@doe.com"
Sylius\Component\Core\Model\Customer: Sylius\Component\Core\Model\Customer:
customer_{oliver, dave}: customer_john:
firstName: "<current()>" firstName: "John"
lastName: "Doe" lastName: "Doe"
email: "<current()>\\@doe.com" email: "john@doe.com"
emailCanonical: "<current()>\\@doe.com" emailCanonical: "john@doe.com"
birthday: "<(new \\DateTime())>" birthday: "<(new \\DateTime())>"

View file

@ -1,18 +1,18 @@
Sylius\Component\Core\Model\ShopUser: Sylius\Component\Core\Model\ShopUser:
shop_user_{oliver, dave}: shop_user_john:
plainPassword: "sylius" plainPassword: "sylius"
roles: [ROLE_USER] roles: [ROLE_USER]
enabled: true enabled: true
passwordResetToken: "valid_token" passwordResetToken: "valid_token"
passwordRequestedAt: "<(new \\DateTime())>" passwordRequestedAt: "<(new \\DateTime())>"
customer: "@customer_<current()>" customer: "@customer_john"
username: "<current()>\\@doe.com" username: "john@doe.com"
usernameCanonical: "<current()>\\@doe.com" usernameCanonical: "john@doe.com"
Sylius\Component\Core\Model\Customer: Sylius\Component\Core\Model\Customer:
customer_{oliver, dave}: customer_john:
firstName: "<current()>" firstName: "John"
lastName: "Doe" lastName: "Doe"
email: "<current()>\\@doe.com" email: "john@doe.com"
emailCanonical: "<current()>\\@doe.com" emailCanonical: "john@doe.com"
birthday: "<(new \\DateTime())>" birthday: "<(new \\DateTime())>"

View file

@ -16,12 +16,19 @@ namespace Sylius\Tests\Api\Shop;
use Sylius\Component\Core\Model\CustomerInterface; use Sylius\Component\Core\Model\CustomerInterface;
use Sylius\Component\Core\Model\ShopUserInterface; use Sylius\Component\Core\Model\ShopUserInterface;
use Sylius\Tests\Api\JsonApiTestCase; use Sylius\Tests\Api\JsonApiTestCase;
use Sylius\Tests\Api\Utils\ShopUserLoginTrait;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
final class CustomersTest extends JsonApiTestCase final class CustomersTest extends JsonApiTestCase
{ {
use ShopUserLoginTrait; protected function setUp(): void
{
parent::setUp();
$this->setUpDefaultGetHeaders();
$this->setUpDefaultPostHeaders();
$this->setUpDefaultPutHeaders();
$this->setUpDefaultPatchHeaders();
}
/** @test */ /** @test */
public function it_gets_customer(): void public function it_gets_customer(): void
@ -30,17 +37,9 @@ final class CustomersTest extends JsonApiTestCase
/** @var CustomerInterface $customer */ /** @var CustomerInterface $customer */
$customer = $fixtures['customer_oliver']; $customer = $fixtures['customer_oliver'];
$header = array_merge($this->logInShopUser($customer->getEmailCanonical()), self::CONTENT_TYPE_HEADER); $this->requestGet('/api/v2/shop/customers/' . $customer->getId());
$this->client->request( $this->assertResponse($this->client->getResponse(), 'shop/customer/get_customer_response');
method: 'GET',
uri: '/api/v2/shop/customers/' . $customer->getId(),
server: $header,
);
$response = $this->client->getResponse();
$this->assertResponse($response, 'shop/customer/get_customer_response', Response::HTTP_OK);
} }
/** @test */ /** @test */
@ -48,19 +47,15 @@ final class CustomersTest extends JsonApiTestCase
{ {
$this->loadFixturesFromFiles(['authentication/shop_user.yaml']); $this->loadFixturesFromFiles(['authentication/shop_user.yaml']);
$this->client->request( $this->requestPost(
method: 'POST', '/api/v2/shop/customers/token',
uri: '/api/v2/shop/customers/token', [
server: self::CONTENT_TYPE_HEADER,
content: json_encode([
'email' => 'oliver@doe.com', 'email' => 'oliver@doe.com',
'password' => 'sylius', 'password' => 'sylius',
], \JSON_THROW_ON_ERROR), ],
); );
$response = $this->client->getResponse(); $this->assertResponse($this->client->getResponse(), 'shop/customer/log_in_customer_response');
$this->assertResponse($response, 'shop/customer/log_in_customer_response', Response::HTTP_OK);
} }
/** @test */ /** @test */
@ -68,22 +63,18 @@ final class CustomersTest extends JsonApiTestCase
{ {
$this->loadFixturesFromFiles(['channel/channel.yaml']); $this->loadFixturesFromFiles(['channel/channel.yaml']);
$this->client->request( $this->requestPost(
method: 'POST', '/api/v2/shop/customers',
uri: '/api/v2/shop/customers', [
server: self::CONTENT_TYPE_HEADER,
content: json_encode([
'firstName' => 'John', 'firstName' => 'John',
'lastName' => 'Doe', 'lastName' => 'Doe',
'email' => 'shop@example.com', 'email' => 'shop@example.com',
'password' => 'sylius', 'password' => 'sylius',
'subscribedToNewsletter' => true, 'subscribedToNewsletter' => true,
], \JSON_THROW_ON_ERROR), ],
); );
$response = $this->client->getResponse(); $this->assertResponseCode($this->client->getResponse(), Response::HTTP_NO_CONTENT);
$this->assertResponseCode($response, Response::HTTP_NO_CONTENT);
} }
/** @test */ /** @test */
@ -93,13 +84,9 @@ final class CustomersTest extends JsonApiTestCase
/** @var CustomerInterface $customer */ /** @var CustomerInterface $customer */
$customer = $loadedData['customer_oliver']; $customer = $loadedData['customer_oliver'];
$header = array_merge($this->logInShopUser($customer->getEmailCanonical()), self::CONTENT_TYPE_HEADER); $this->requestPut(
'/api/v2/shop/customers/' . $customer->getId(),
$this->client->request( [
method: 'PUT',
uri: '/api/v2/shop/customers/' . $customer->getId(),
server: $header,
content: json_encode([
'email' => 'john.wick@tarasov.mob', 'email' => 'john.wick@tarasov.mob',
'firstName' => 'John', 'firstName' => 'John',
'lastName' => 'Wick', 'lastName' => 'Wick',
@ -107,12 +94,10 @@ final class CustomersTest extends JsonApiTestCase
'gender' => 'm', 'gender' => 'm',
'birthday' => '2023-10-24T11:00:00.000Z', 'birthday' => '2023-10-24T11:00:00.000Z',
'subscribedToNewsletter' => true, 'subscribedToNewsletter' => true,
], \JSON_THROW_ON_ERROR), ],
); );
$response = $this->client->getResponse(); $this->assertResponse($this->client->getResponse(), 'shop/customer/put_customer_response');
$this->assertResponse($response, 'shop/customer/put_customer_response', Response::HTTP_OK);
} }
/** @test */ /** @test */
@ -122,19 +107,15 @@ final class CustomersTest extends JsonApiTestCase
/** @var CustomerInterface $customer */ /** @var CustomerInterface $customer */
$customer = $loadedData['customer_oliver']; $customer = $loadedData['customer_oliver'];
$this->client->request( $this->requestPost(
method: 'POST', '/api/v2/shop/customers/reset-password',
uri: '/api/v2/shop/customers/reset-password', [
server: self::CONTENT_TYPE_HEADER,
content: json_encode([
'email' => $customer->getEmailCanonical(), 'email' => $customer->getEmailCanonical(),
'localeCode' => 'en_US', 'localeCode' => 'en_US',
], \JSON_THROW_ON_ERROR), ],
); );
$response = $this->client->getResponse(); $this->assertResponseCode($this->client->getResponse(), Response::HTTP_ACCEPTED);
$this->assertResponseCode($response, Response::HTTP_ACCEPTED);
$this->assertEmailCount(1); $this->assertEmailCount(1);
} }
@ -143,19 +124,19 @@ final class CustomersTest extends JsonApiTestCase
{ {
$this->loadFixturesFromFiles(['authentication/shop_user.yaml', 'channel/channel.yaml']); $this->loadFixturesFromFiles(['authentication/shop_user.yaml', 'channel/channel.yaml']);
$this->client->request( $this->requestPost(
method: 'POST', '/api/v2/shop/customers/reset-password',
uri: '/api/v2/shop/customers/reset-password', [
server: self::CONTENT_TYPE_HEADER,
content: json_encode([
'email' => 'wrong_email', 'email' => 'wrong_email',
'localeCode' => 'te_ST', 'localeCode' => 'te_ST',
], \JSON_THROW_ON_ERROR), ],
); );
$response = $this->client->getResponse(); $this->assertResponse(
$this->client->getResponse(),
$this->assertResponse($response, 'shop/customer/reset_password_validation_response', Response::HTTP_UNPROCESSABLE_ENTITY); 'shop/customer/reset_password_validation_response',
Response::HTTP_UNPROCESSABLE_ENTITY,
);
} }
/** @test */ /** @test */
@ -169,18 +150,49 @@ final class CustomersTest extends JsonApiTestCase
$shopUser->setPasswordRequestedAt(new \DateTime('now')); $shopUser->setPasswordRequestedAt(new \DateTime('now'));
$this->getEntityManager()->flush(); $this->getEntityManager()->flush();
$this->client->request( $this->requestPatch(
method: 'PATCH', '/api/v2/shop/customers/reset-password/token',
uri: '/api/v2/shop/customers/reset-password/token', [
server: self::PATCH_CONTENT_TYPE_HEADER,
content: json_encode([
'newPassword' => 'newPassword', 'newPassword' => 'newPassword',
'confirmNewPassword' => 'newPassword', 'confirmNewPassword' => 'newPassword',
], \JSON_THROW_ON_ERROR), ],
); );
$response = $this->client->getResponse(); $this->assertResponseCode($this->client->getResponse(), Response::HTTP_ACCEPTED);
}
$this->assertResponseCode($response, Response::HTTP_ACCEPTED); /** @test */
public function it_prevents_from_resetting_password_with_invalid_token(): void
{
$this->loadFixturesFromFiles(['authentication/shop_user_with_reset_password_token.yaml', 'channel/channel.yaml']);
$this->requestPatch(
'/api/v2/shop/customers/reset-password/invalid_token',
[
'newPassword' => 'newPassword',
'confirmNewPassword' => 'newPassword',
],
);
$this->assertResponseCode($this->client->getResponse(), Response::HTTP_UNPROCESSABLE_ENTITY);
}
/** @test */
public function it_prevents_from_resetting_password_with_expired_token(): void
{
$this->loadFixturesFromFiles([
'authentication/shop_user_with_expired_reset_password_token.yaml',
'channel/channel.yaml',
]);
$this->requestPatch(
'/api/v2/shop/customers/reset-password/valid_token',
[
'newPassword' => 'newPassword',
'confirmNewPassword' => 'newPassword',
],
);
$this->assertResponseCode($this->client->getResponse(), Response::HTTP_UNPROCESSABLE_ENTITY);
} }
} }