193 lines
6.4 KiB
PHP
193 lines
6.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
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\Services\ShopperService;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Facades\View;
|
|
use Illuminate\Support\Str;
|
|
|
|
describe('LoginController', function () {
|
|
it('can access get /auth/connexion', function () {
|
|
// 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('can access post /auth/connexion', function () {
|
|
// 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 () {
|
|
// 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 () {
|
|
// 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,
|
|
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();
|
|
$response = $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
|
|
$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,
|
|
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();
|
|
|
|
$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: 'Etab',
|
|
site: 'Site',
|
|
service: 'Service'
|
|
);
|
|
|
|
$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!'));
|
|
});
|
|
});
|