2026-11-15 19:42:03 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2026-11-16 14:26:35 +01:00
|
|
|
namespace Tests\Feature\App\Providers;
|
|
|
|
|
|
2026-11-15 19:42:03 +01:00
|
|
|
use App\Models\Customer;
|
|
|
|
|
use Domain\Models\ValueObjects\Email;
|
|
|
|
|
use Illuminate\Auth\Notifications\ResetPassword;
|
|
|
|
|
use Illuminate\Auth\Notifications\VerifyEmail;
|
|
|
|
|
use Illuminate\Http\Request;
|
2026-11-16 16:26:51 +01:00
|
|
|
use Illuminate\Notifications\Messages\MailMessage;
|
2026-11-15 19:42:03 +01:00
|
|
|
use Illuminate\Support\Facades\Config;
|
|
|
|
|
use Illuminate\Support\Facades\URL;
|
|
|
|
|
use Illuminate\Support\Str;
|
2026-11-16 16:26:51 +01:00
|
|
|
use Webmozart\Assert\Assert;
|
2026-11-15 19:42:03 +01:00
|
|
|
|
|
|
|
|
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);
|
2026-11-16 16:26:51 +01:00
|
|
|
Assert::isInstanceOf($mail, MailMessage::class);
|
2026-11-15 19:42:03 +01:00
|
|
|
/** @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);
|
2026-11-16 16:26:51 +01:00
|
|
|
Assert::isInstanceOf($mail, MailMessage::class);
|
2026-11-15 19:42:03 +01:00
|
|
|
/** @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();
|
|
|
|
|
});
|
|
|
|
|
});
|