[Unit] Add contract tests regarding ShopUser password reset

This commit is contained in:
Rafikooo 2023-10-24 22:04:44 +02:00
parent c3b956e9bc
commit f6978e18ad
No known key found for this signature in database
GPG key ID: 4A26D8327BC2442B
5 changed files with 145 additions and 1 deletions

View file

@ -0,0 +1,18 @@
Sylius\Component\Core\Model\ShopUser:
shop_user_{oliver, dave}:
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"
Sylius\Component\Core\Model\Customer:
customer_{oliver, dave}:
firstName: "<current()>"
lastName: "Doe"
email: "<current()>\\@doe.com"
emailCanonical: "<current()>\\@doe.com"
birthday: "<(new \\DateTime())>"

View file

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

View file

@ -47,7 +47,7 @@ final class SendContactRequestTest extends JsonApiTestCase
/** @test */
public function it_sends_contact_request_as_logged_in_user(): void
{
$fixtures = $this->loadFixturesFromFiles(['channel.yaml', 'authentication/customer.yaml']);
$fixtures = $this->loadFixturesFromFiles(['channel.yaml', 'authentication/shop_user.yaml']);
/** @var CustomerInterface $customer */
$customer = $fixtures['customer_oliver'];

View file

@ -0,0 +1,108 @@
<?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\ContentType;
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.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.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: ContentType::APPLICATION_JSON_MERGE_PATCH,
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.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: ContentType::APPLICATION_JSON_MERGE_PATCH,
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.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: ContentType::APPLICATION_JSON_MERGE_PATCH,
content: json_encode([
'newPassword' => 'newPassword',
'confirmNewPassword' => 'newPassword',
], JSON_THROW_ON_ERROR),
);
$response = $this->client->getResponse();
$this->assertResponseCode($response, Response::HTTP_UNPROCESSABLE_ENTITY);
}
}