From b0cd88be2275245cdb6ba86d732f678b42d9aeb9 Mon Sep 17 00:00:00 2001 From: Grzegorz Sadowski Date: Mon, 2 Sep 2024 14:01:58 +0200 Subject: [PATCH] [API] Enable remaining contract tests for Customer resource + minor refactor --- disabledTests/Api/Shop/ShopUsersTest.php | 107 ------------- ...ser_with_expired_reset_password_token.yaml | 16 +- .../shop_user_with_reset_password_token.yaml | 16 +- tests/Api/Shop/CustomersTest.php | 146 ++++++++++-------- 4 files changed, 95 insertions(+), 190 deletions(-) delete mode 100644 disabledTests/Api/Shop/ShopUsersTest.php diff --git a/disabledTests/Api/Shop/ShopUsersTest.php b/disabledTests/Api/Shop/ShopUsersTest.php deleted file mode 100644 index e83114ba90..0000000000 --- a/disabledTests/Api/Shop/ShopUsersTest.php +++ /dev/null @@ -1,107 +0,0 @@ -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); - } -} diff --git a/tests/Api/DataFixtures/ORM/authentication/shop_user_with_expired_reset_password_token.yaml b/tests/Api/DataFixtures/ORM/authentication/shop_user_with_expired_reset_password_token.yaml index 9a33447aa4..2aefaad3b5 100644 --- a/tests/Api/DataFixtures/ORM/authentication/shop_user_with_expired_reset_password_token.yaml +++ b/tests/Api/DataFixtures/ORM/authentication/shop_user_with_expired_reset_password_token.yaml @@ -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_" - username: "\\@doe.com" - usernameCanonical: "\\@doe.com" + customer: "@customer_john" + username: "john@doe.com" + usernameCanonical: "john@doe.com" Sylius\Component\Core\Model\Customer: - customer_{oliver, dave}: - firstName: "" + customer_john: + firstName: "John" lastName: "Doe" - email: "\\@doe.com" - emailCanonical: "\\@doe.com" + email: "john@doe.com" + emailCanonical: "john@doe.com" birthday: "<(new \\DateTime())>" diff --git a/tests/Api/DataFixtures/ORM/authentication/shop_user_with_reset_password_token.yaml b/tests/Api/DataFixtures/ORM/authentication/shop_user_with_reset_password_token.yaml index b254963ce5..fc532881f2 100644 --- a/tests/Api/DataFixtures/ORM/authentication/shop_user_with_reset_password_token.yaml +++ b/tests/Api/DataFixtures/ORM/authentication/shop_user_with_reset_password_token.yaml @@ -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_" - username: "\\@doe.com" - usernameCanonical: "\\@doe.com" + customer: "@customer_john" + username: "john@doe.com" + usernameCanonical: "john@doe.com" Sylius\Component\Core\Model\Customer: - customer_{oliver, dave}: - firstName: "" + customer_john: + firstName: "John" lastName: "Doe" - email: "\\@doe.com" - emailCanonical: "\\@doe.com" + email: "john@doe.com" + emailCanonical: "john@doe.com" birthday: "<(new \\DateTime())>" diff --git a/tests/Api/Shop/CustomersTest.php b/tests/Api/Shop/CustomersTest.php index 0b1b59ac7f..35eacb24f4 100644 --- a/tests/Api/Shop/CustomersTest.php +++ b/tests/Api/Shop/CustomersTest.php @@ -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); } }