ldc-cocotte/tests/Feature/App/Providers/AuthServiceProviderTest.php

67 lines
2.6 KiB
PHP
Raw Permalink Normal View History

<?php
declare(strict_types=1);
namespace Tests\Feature\App\Providers;
use App\Models\Customer;
use Domain\Models\ValueObjects\Email;
use Illuminate\Auth\Notifications\ResetPassword;
use Illuminate\Auth\Notifications\VerifyEmail;
use Illuminate\Http\Request;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Str;
use Webmozart\Assert\Assert;
describe('AuthServiceProvider auth URL callbacks', function (): void {
it('registers VerifyEmail URL callback during boot without instantiating AuthController', function (): void {
// GIVEN
/** @var Customer $customer */
$customer = Customer::query()->forceCreate([
'email' => new Email('verify-callback@example.com'),
'hashcode' => (string) Str::uuid(),
]);
$notification = new VerifyEmail();
// WHEN
$mail = $notification->toMail($customer);
Assert::isInstanceOf($mail, MailMessage::class);
/** @var string $actionUrl */
$actionUrl = $mail->actionUrl;
$relativeUrl = (string) parse_url($actionUrl, \PHP_URL_PATH) . '?' . (string) parse_url($actionUrl, \PHP_URL_QUERY);
$request = Request::create($relativeUrl, 'GET');
// THEN
$expectedHash = hash_hmac('sha256', 'verify-callback@example.com', Config::string('app.key'));
expect($actionUrl)->toContain('/auth/email/verify/' . $customer->id . '/' . $expectedHash)
->and(URL::hasValidSignature($request, absolute: false))->toBeTrue();
});
it('registers ResetPassword URL callback during boot without instantiating AuthController', function (): void {
// GIVEN
/** @var Customer $customer */
$customer = Customer::query()->forceCreate([
'email' => new Email('reset-callback@example.com'),
'hashcode' => (string) Str::uuid(),
]);
$notification = new ResetPassword('secret-reset-token');
// WHEN
$mail = $notification->toMail($customer);
Assert::isInstanceOf($mail, MailMessage::class);
/** @var string $actionUrl */
$actionUrl = $mail->actionUrl;
$relativeUrl = (string) parse_url($actionUrl, \PHP_URL_PATH) . '?' . (string) parse_url($actionUrl, \PHP_URL_QUERY);
$request = Request::create($relativeUrl, 'GET');
// THEN
expect($actionUrl)->toContain('/auth/reinitialiser-mdp/secret-reset-token')
->and($actionUrl)->toContain('email=reset-callback%40example.com')
->and(URL::hasValidSignature($request, absolute: false))->toBeTrue();
});
});