diff --git a/app/Http/Controllers/Fo/Auth/ForgotPasswordController.php b/app/Http/Controllers/Fo/Auth/ForgotPasswordController.php index 7d892bdf..32ac693c 100644 --- a/app/Http/Controllers/Fo/Auth/ForgotPasswordController.php +++ b/app/Http/Controllers/Fo/Auth/ForgotPasswordController.php @@ -7,7 +7,6 @@ namespace App\Http\Controllers\Fo\Auth; use App\Contracts\Executor; use App\Cqs\Queries\Customers\GetCustomerByEmailQuery; use App\Cqs\Queries\Customers\GetCustomerByEmailQueryInput; -use App\Exceptions\CustomerNotEmployedException; use Domain\Models\ValueObjects\Email; use Domain\Models\ValueObjects\Entities\ShopperId; use Domain\Services\ShopperService; @@ -49,7 +48,7 @@ class ForgotPasswordController extends AuthController if ($shopperId instanceof ShopperId) { $shopper = $this->shopperService->getShopperFor($shopperId); if (! $shopper || ! $shopper->isActive()) { - throw new CustomerNotEmployedException(); + return $this->sendResponse($request->wantsJson()); } } diff --git a/tests/Feature/App/Http/Controllers/Fo/Auth/ForgotPasswordControllerTest.php b/tests/Feature/App/Http/Controllers/Fo/Auth/ForgotPasswordControllerTest.php index 49785b95..0f944161 100644 --- a/tests/Feature/App/Http/Controllers/Fo/Auth/ForgotPasswordControllerTest.php +++ b/tests/Feature/App/Http/Controllers/Fo/Auth/ForgotPasswordControllerTest.php @@ -2,21 +2,42 @@ declare(strict_types=1); +use App\Models\Customer; use App\Models\Page; +use App\Services\CrmService; +use Carbon\CarbonImmutable; use Database\Seeders\ContentSeeder; +use Domain\Enums\Country; +use Domain\Models\Entities\Shopper; +use Domain\Models\ValueObjects\CrmCustomer; +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\Auth\Notifications\ResetPassword; use Illuminate\Support\Facades\Cache; +use Illuminate\Support\Facades\Notification; use Illuminate\Support\Facades\View; +use Illuminate\Support\Str; + +function sharePagesForForgotPasswordTest(): void +{ + View::share('PageList', Page::getCached() + ->mapWithKeys(fn ($page) => [ + $page->slug->value() => $page, + ])); + Cache::flush(); +} describe('ForgotPasswordController', function () { it('can access get /auth/mot-de-passe-oublie', function () { /** @var \Tests\TestCase $this */ // GIVEN $this->seed(ContentSeeder::class); - View::share('PageList', Page::getCached() - ->mapWithKeys(fn ($page) => [ - $page->slug->value() => $page, - ])); - Cache::flush(); + sharePagesForForgotPasswordTest(); // WHEN $response = $this->get('/auth/mot-de-passe-oublie'); @@ -29,11 +50,7 @@ describe('ForgotPasswordController', function () { /** @var \Tests\TestCase $this */ // GIVEN $this->seed(ContentSeeder::class); - View::share('PageList', Page::getCached() - ->mapWithKeys(fn ($page) => [ - $page->slug->value() => $page, - ])); - Cache::flush(); + sharePagesForForgotPasswordTest(); // WHEN $response = $this->post('/auth/mot-de-passe-oublie', []); @@ -41,4 +58,167 @@ describe('ForgotPasswordController', function () { // THEN $response->assertStatus(302); }); + + it('returns the generic status response for an unknown email without sending any notification', function () { + /** @var \Tests\TestCase $this */ + // GIVEN + Notification::fake(); + $this->seed(ContentSeeder::class); + sharePagesForForgotPasswordTest(); + + $crmService = $this->mock(CrmService::class); + $crmService->shouldReceive('getCustomerFromEmail') + ->once() + ->with('unknown@example.com') + ->andReturnNull(); + + // WHEN + $response = $this->from('/auth/mot-de-passe-oublie') + ->post('/auth/mot-de-passe-oublie', ['email' => 'unknown@example.com']); + + // THEN + $response->assertRedirect('/auth/mot-de-passe-oublie') + ->assertSessionHas('status', trans('Please check your email, if we know you you should receive a reset link')); + Notification::assertNothingSent(); + }); + + it('returns the identical generic status response without leaking account state for an inactive employee', function () { + /** @var \Tests\TestCase $this */ + // GIVEN + Notification::fake(); + $this->seed(ContentSeeder::class); + sharePagesForForgotPasswordTest(); + + /** @var Customer $customer */ + $customer = Customer::query()->forceCreate([ + 'email' => new Email('inactive-employee@example.com'), + 'hashcode' => (string) Str::uuid(), + ]); + + $inactiveShopper = new Shopper( + id: $customer->getId(), + email: new Email('inactive-employee@example.com'), + matricule: new Matricule('1234567'), + nom: 'Doe', + prenom: 'Jane', + startDate: CarbonImmutable::now()->subMonth(), + endDate: CarbonImmutable::now()->subDay(), + etablissement: new ShopperEstablishmentCode('01'), + site: new ShopperSiteCode('SITE'), + service: new ShopperServiceCode('01'), + ); + + $shopperService = \Mockery::mock(ShopperService::class); + $shopperService->shouldReceive('getShopperFor') + ->once() + ->with(\Mockery::on(fn (ShopperId $id): bool => $id->equals($customer->getId()))) + ->andReturn($inactiveShopper); + $this->app->instance(ShopperService::class, $shopperService); + + // WHEN + $response = $this->from('/auth/mot-de-passe-oublie') + ->post('/auth/mot-de-passe-oublie', ['email' => 'inactive-employee@example.com']); + + // THEN + $response->assertRedirect('/auth/mot-de-passe-oublie') + ->assertSessionHas('status', trans('Please check your email, if we know you you should receive a reset link')) + ->assertSessionHasNoErrors(); + Notification::assertNothingSent(); + }); + + it('returns the identical JSON 202 response for an inactive employee without sending any notification', function () { + /** @var \Tests\TestCase $this */ + // GIVEN + Notification::fake(); + $this->seed(ContentSeeder::class); + sharePagesForForgotPasswordTest(); + + /** @var Customer $customer */ + $customer = Customer::query()->forceCreate([ + 'email' => new Email('inactive-json@example.com'), + 'hashcode' => (string) Str::uuid(), + ]); + + $shopperService = \Mockery::mock(ShopperService::class); + $shopperService->shouldReceive('getShopperFor') + ->once() + ->with(\Mockery::on(fn (ShopperId $id): bool => $id->equals($customer->getId()))) + ->andReturn(null); + $this->app->instance(ShopperService::class, $shopperService); + + // WHEN + $response = $this->postJson('/auth/mot-de-passe-oublie', ['email' => 'inactive-json@example.com']); + + // THEN + $response->assertStatus(202) + ->assertJson(['message' => trans('Please check your email, if we know you you should receive a reset link')]); + Notification::assertNothingSent(); + }); + + it('sends the reset password notification and returns the generic response for an active employee', function () { + /** @var \Tests\TestCase $this */ + // GIVEN + Notification::fake(); + $this->seed(ContentSeeder::class); + sharePagesForForgotPasswordTest(); + + /** @var Customer $customer */ + $customer = Customer::query()->forceCreate([ + 'email' => new Email('active-employee@example.com'), + 'hashcode' => (string) Str::uuid(), + ]); + + $crmCustomer = new CrmCustomer( + hashcode: $customer->hashcode->value, + email: 'active-employee@example.com', + password: 'old-password', + firstname: 'John', + lastname: 'Doe', + birthDate: null, + phone: null, + address: null, + address2: null, + city: null, + postcode: null, + country: Country::france, + optIn: false, + status: true, + lastConnection: null, + ); + + $crmService = $this->mock(CrmService::class); + $crmService->shouldReceive('getCustomerFromEmail') + ->once() + ->with('active-employee@example.com') + ->andReturn($crmCustomer); + + $activeShopper = new Shopper( + id: $customer->getId(), + email: new Email('active-employee@example.com'), + matricule: new Matricule('1234568'), + nom: 'Doe', + prenom: 'John', + startDate: CarbonImmutable::now()->subMonth(), + endDate: CarbonImmutable::now()->addMonth(), + etablissement: new ShopperEstablishmentCode('01'), + site: new ShopperSiteCode('SITE'), + service: new ShopperServiceCode('01'), + ); + + $shopperService = \Mockery::mock(ShopperService::class); + $shopperService->shouldReceive('getShopperFor') + ->once() + ->with(\Mockery::on(fn (ShopperId $id): bool => $id->equals($customer->getId()))) + ->andReturn($activeShopper); + $this->app->instance(ShopperService::class, $shopperService); + + // WHEN + $response = $this->from('/auth/mot-de-passe-oublie') + ->post('/auth/mot-de-passe-oublie', ['email' => 'active-employee@example.com']); + + // THEN + $response->assertRedirect('/auth/mot-de-passe-oublie') + ->assertSessionHas('status', trans('Please check your email, if we know you you should receive a reset link')); + Notification::assertSentTo($customer, ResetPassword::class); + }); });