ldc-cocotte/tests/Integration/WithApp/App/Actions/Auth/ResetCustomerPasswordTest.php
Jérémy Munsch 47b0c16857
feat(auth): implement ResetCustomerPassword action and related classes
Adds functionality for resetting customer passwords, including input and output handling.
2026-11-15 19:57:38 +01:00

150 lines
5.8 KiB
PHP

<?php
declare(strict_types=1);
use App\Cqs\Actions\Auth\ResetCustomerPassword;
use App\Cqs\Actions\Auth\ResetCustomerPasswordInput;
use App\Cqs\Actions\Auth\ResetCustomerPasswordOutput;
use App\Enums\ResetCustomerPasswordStatus;
use App\Models\Customer;
use Carbon\CarbonImmutable;
use Domain\Models\Entities\Shopper;
use Domain\Models\ValueObjects\Email;
use Domain\Models\ValueObjects\Entities\ShopperId;
use Domain\Models\ValueObjects\Matricule;
use Domain\Models\ValueObjects\ShopperEstablishmentCode;
use Domain\Models\ValueObjects\ShopperServiceCode;
use Domain\Models\ValueObjects\ShopperSiteCode;
use Domain\Services\ShopperService;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
use Infrastructure\Adapters\Services\AppCustomerAuthService;
describe('ResetCustomerPassword action', function (): void {
it('resets password, rotates remember_token and returns success output for active employee', function (): void {
// GIVEN
/** @var Customer $customer */
$customer = Customer::query()->forceCreate([
'email' => new Email('action-reset@example.com'),
'hashcode' => (string) Str::uuid(),
]);
$shopper = new Shopper(
id: $customer->getId(),
email: new Email('action-reset@example.com'),
matricule: new Matricule('1234567'),
nom: 'Doe',
prenom: 'John',
startDate: CarbonImmutable::now()->subMonth(),
endDate: null,
etablissement: new ShopperEstablishmentCode('FAC01'),
site: new ShopperSiteCode('SITE'),
service: new ShopperServiceCode('101'),
);
$shopperService = \Mockery::mock(ShopperService::class);
$shopperService->shouldReceive('getShopperFor')
->once()
->with(\Mockery::on(fn (ShopperId $id): bool => $id->equals($customer->getId())))
->andReturn($shopper);
$authService = \Mockery::mock(AppCustomerAuthService::class);
$authService->shouldReceive('resetPasswordFor')
->once()
->with($customer, 'NewPassword123!')
->andReturnTrue();
$authService->shouldReceive('hasVerifiedEmail')
->once()
->with($customer)
->andReturnTrue();
$action = new ResetCustomerPassword($shopperService, $authService);
$input = new ResetCustomerPasswordInput($customer, 'NewPassword123!');
// WHEN
$output = $action($input);
// THEN
expect($output)->toBeInstanceOf(ResetCustomerPasswordOutput::class)
->and($output->status)->toBe(ResetCustomerPasswordStatus::SUCCESS)
->and($output->emailVerified)->toBeTrue()
->and($customer->fresh()?->getRememberToken())->not->toBeNull();
});
it('returns notEmployed output when shopper is missing or inactive', function (): void {
// GIVEN
/** @var Customer $customer */
$customer = Customer::query()->forceCreate([
'email' => new Email('action-inactive@example.com'),
'hashcode' => (string) Str::uuid(),
]);
$inactiveShopper = new Shopper(
id: $customer->getId(),
email: new Email('action-inactive@example.com'),
matricule: new Matricule('1234568'),
nom: 'Doe',
prenom: 'Jane',
startDate: CarbonImmutable::now()->subMonth(),
endDate: CarbonImmutable::now()->subDay(),
etablissement: new ShopperEstablishmentCode('FAC01'),
site: new ShopperSiteCode('SITE'),
service: new ShopperServiceCode('101'),
);
$shopperService = \Mockery::mock(ShopperService::class);
$shopperService->shouldReceive('getShopperFor')->once()->andReturn($inactiveShopper);
$authService = \Mockery::mock(AppCustomerAuthService::class);
$authService->shouldNotReceive('resetPasswordFor');
$action = new ResetCustomerPassword($shopperService, $authService);
// WHEN
$output = $action(new ResetCustomerPasswordInput($customer, 'NewPassword123!'));
// THEN
expect($output->status)->toBe(ResetCustomerPasswordStatus::NOT_EMPLOYED)
->and($customer->fresh()?->getRememberToken())->toBeNull();
});
it('returns invalidPassword output when CRM rejects the new password', function (): void {
// GIVEN
/** @var Customer $customer */
$customer = Customer::query()->forceCreate([
'email' => new Email('action-invalid@example.com'),
'hashcode' => (string) Str::uuid(),
]);
$shopper = new Shopper(
id: $customer->getId(),
email: new Email('action-invalid@example.com'),
matricule: new Matricule('1234569'),
nom: 'Doe',
prenom: 'Alice',
startDate: CarbonImmutable::now()->subMonth(),
endDate: null,
etablissement: new ShopperEstablishmentCode('FAC01'),
site: new ShopperSiteCode('SITE'),
service: new ShopperServiceCode('101'),
);
$shopperService = \Mockery::mock(ShopperService::class);
$shopperService->shouldReceive('getShopperFor')->once()->andReturn($shopper);
$authService = \Mockery::mock(AppCustomerAuthService::class);
$authService->shouldReceive('resetPasswordFor')
->once()
->andReturn(ValidationException::withMessages(['password' => 'Too weak']));
$action = new ResetCustomerPassword($shopperService, $authService);
// WHEN
$output = $action(new ResetCustomerPasswordInput($customer, 'weak'));
// THEN
expect($output->status)->toBe(ResetCustomerPasswordStatus::INVALID_PASSWORD)
->and($output->errorMessage)->toContain('Too weak')
->and($customer->fresh()?->getRememberToken())->toBeNull();
});
});