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

View file

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