seed(ContentSeeder::class); View::share('PageList', Page::getCached() ->mapWithKeys(fn ($page) => [ $page->slug->value() => $page, ])); Cache::flush(); // WHEN $response = $this->get('/auth/connexion'); // THEN $response->assertStatus(200); }); it('renders a flashed warning message on the login page', function () { /** @var \Tests\TestCase $this */ // GIVEN $this->seed(ContentSeeder::class); View::share('PageList', Page::getCached() ->mapWithKeys(fn ($page) => [ $page->slug->value() => $page, ])); Cache::flush(); // WHEN $response = $this->withSession(['warning' => [__('Please log in to continue.')]]) ->get('/auth/connexion'); // THEN $response->assertOk(); $response->assertSeeText(__('Please log in to continue.')); }); it('flashes a warning when a guest hits a protected route', function () { /** @var \Tests\TestCase $this */ // WHEN $response = $this->get(route('fo.account.dashboard')); // THEN $response->assertRedirect(route('fo.auth.connexion')); $response->assertSessionHas('warning', [__('Please log in to continue.')]); }); it('can access post /auth/connexion', function () { /** @var \Tests\TestCase $this */ // GIVEN $this->seed(ContentSeeder::class); View::share('PageList', Page::getCached() ->mapWithKeys(fn ($page) => [ $page->slug->value() => $page, ])); Cache::flush(); // WHEN $response = $this->post('/auth/connexion', []); // THEN $response->assertStatus(302); }); it('can access post /auth/deconnexion', function () { /** @var \Tests\TestCase $this */ // GIVEN $this->seed(ContentSeeder::class); View::share('PageList', Page::getCached() ->mapWithKeys(fn ($page) => [ $page->slug->value() => $page, ])); Cache::flush(); $user = \Mockery::mock(Customer::class)->makePartial(); $user->shouldReceive('getAuthIdentifier')->andReturn(1); $user->hashcode = 'dummy_hashcode'; $user->shouldReceive('getAuthPassword')->andReturn('dummy'); $this->actingAs($user, 'frontend'); // WHEN $response = $this->post('/auth/deconnexion'); // THEN $response->assertStatus(302); }); it('throws CustomerNotEmployedException when shopper is not active during login', function () { /** @var \Tests\TestCase $this */ // GIVEN $this->seed(ContentSeeder::class); View::share('PageList', Page::getCached() ->mapWithKeys(fn ($page) => [ $page->slug->value() => $page, ])); Cache::flush(); $customer = Customer::forceCreate([ 'email' => new Email('test@example.com'), 'hashcode' => (string) Str::uuid(), ]); $crmCustomer = new CrmCustomer( hashcode: $customer->hashcode->value, email: 'test@example.com', password: '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')->with('test@example.com')->andReturn($crmCustomer); $crmService->shouldReceive('login')->with('test@example.com', 'password')->andReturnTrue(); $shopperService = \Mockery::mock(ShopperService::class); $shopperService->shouldReceive('getShopperFor') ->withArgs(function ($arg) use ($customer) { return $arg instanceof ShopperId && (string) $arg->value === $customer->id; }) ->andReturn(null); $this->app->instance(ShopperService::class, $shopperService); // WHEN $this->withoutExceptionHandling(); $this->post('/auth/connexion', [ 'email' => 'test@example.com', 'password' => 'password', ]); })->throws(CustomerNotEmployedException::class); it('flashes success message when shopper is active during login', function () { // GIVEN /** @var \Tests\TestCase $this */ $this->seed(ContentSeeder::class); View::share('PageList', Page::getCached() ->mapWithKeys(fn ($page) => [ $page->slug->value() => $page, ])); Cache::flush(); $customer = Customer::forceCreate([ 'email' => new Email('test2@example.com'), 'hashcode' => (string) Str::uuid(), ]); $crmCustomer = new CrmCustomer( hashcode: $customer->hashcode->value, email: 'test2@example.com', password: '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')->with('test2@example.com')->andReturn($crmCustomer); $crmService->shouldReceive('login')->with('test2@example.com', 'password')->andReturnTrue(); $crmService->shouldReceive('getCustomerFromHashCode') ->withArgs(fn (CrmHashcode $hashcode): bool => $hashcode->value === $customer->hashcode->value) ->andReturn($crmCustomer); $shopper = new Shopper( id: clone $customer->getId(), email: new Email('test2@example.com'), matricule: new Matricule('1234567'), nom: 'Doe', prenom: 'John', startDate: CarbonImmutable::now()->subDays(1), endDate: null, etablissement: new ShopperEstablishmentCode('FAC01'), site: new ShopperSiteCode('SITE'), service: new ShopperServiceCode('101'), ); $shopperService = \Mockery::mock(ShopperService::class); $shopperService->shouldReceive('getShopperFor') ->withArgs(function ($arg) use ($customer) { return $arg instanceof ShopperId && (string) $arg->value === $customer->id; }) ->andReturn($shopper); $this->app->instance(ShopperService::class, $shopperService); // WHEN $response = $this->post('/auth/connexion', [ 'email' => 'test2@example.com', 'password' => 'password', ]); // THEN $response->assertRedirect(route('fo.pages.home')); $response->assertSessionHas('success', \trans('You are logged in!')); }); it('does not flash success message when email is not verified during login', function () { /** @var \Tests\TestCase $this */ // GIVEN Notification::fake(); $this->seed(ContentSeeder::class); View::share('PageList', Page::getCached() ->mapWithKeys(fn ($page) => [ $page->slug->value() => $page, ])); Cache::flush(); $customer = Customer::forceCreate([ 'email' => new Email('unverified@example.com'), 'hashcode' => (string) Str::uuid(), ]); $crmCustomer = new CrmCustomer( hashcode: $customer->hashcode->value, email: 'unverified@example.com', password: 'password', firstname: 'Jane', lastname: 'Doe', birthDate: null, phone: null, address: null, address2: null, city: null, postcode: null, country: Country::france, optIn: false, status: false, lastConnection: null, ); $crmService = $this->mock(CrmService::class); $crmService->shouldReceive('getCustomerFromEmail')->with('unverified@example.com')->andReturn($crmCustomer); $crmService->shouldReceive('login') ->with('unverified@example.com', 'password') ->andThrow(new CrmCustomerNotActivatedException()); $crmService->shouldReceive('getCustomerFromHashCode') ->withArgs(fn (CrmHashcode $hashcode): bool => $hashcode->value === $customer->hashcode->value) ->andReturn($crmCustomer); $shopper = new Shopper( id: clone $customer->getId(), email: new Email('unverified@example.com'), matricule: new Matricule('1234568'), nom: 'Doe', prenom: 'Jane', startDate: CarbonImmutable::now()->subDays(1), endDate: null, etablissement: new ShopperEstablishmentCode('FAC01'), site: new ShopperSiteCode('SITE'), service: new ShopperServiceCode('101'), ); $shopperService = \Mockery::mock(ShopperService::class); $shopperService->shouldReceive('getShopperFor') ->withArgs(function ($arg) use ($customer) { return $arg instanceof ShopperId && (string) $arg->value === $customer->id; }) ->andReturn($shopper); $this->app->instance(ShopperService::class, $shopperService); // WHEN $response = $this->post('/auth/connexion', [ 'email' => 'unverified@example.com', 'password' => 'password', ]); // THEN $response->assertRedirect(route('fo.pages.home')); $response->assertSessionMissing('success'); Notification::assertSentTo($customer, VerifyEmail::class); }); });