ldc-cocotte/tests/Feature/App/Http/Controllers/Fo/Auth/LoginControllerTest.php

312 lines
11 KiB
PHP

<?php
declare(strict_types=1);
use App\Exceptions\Crm\CrmCustomerNotActivatedException;
use App\Exceptions\CustomerNotEmployedException;
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\VerifyEmail;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Str;
use Shared\ValueObjects\CrmHashcode;
describe('LoginController', function () {
it('can access get /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->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);
});
});