Files
portal/app/tests/Feature/Pd/ImpersonationExpiryServiceTest.php
T
Дмитрий c5840ea25d refactor(impersonation): отправку письма о завершении сессии вынести в сервис
F-DEPTRAC: middleware ImpersonationContext зависел от слоя Mail
(ImpersonationEndedMail) — нарушение deptrac ruleset, блокировало ВСЕ
php-коммиты. Отправка письма + завершение сессии вынесены в новый
ImpersonationExpiryService (Service-слой, которому Mail разрешён,
идемпотентно). Middleware теперь зависит только от Service.

deptrac: 0 нарушений. TDD: 2 теста сервиса (письмо в очереди + идемпотентность).
phpstan-baseline.neon перегенерён под Pest $this-false-positives новых тестов
этой серии правок приёмки (level 5, composer stan = 0).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-21 04:43:16 +03:00

77 lines
2.8 KiB
PHP

<?php
declare(strict_types=1);
use App\Mail\ImpersonationEndedMail;
use App\Models\ImpersonationToken;
use App\Models\Tenant;
use App\Services\Pd\ImpersonationExpiryService;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Mail;
use Tests\Concerns\SharesSupplierPdo;
// Сервис завершения impersonation-сессии (F-DEPTRAC рефактор: отправка письма
// вынесена из middleware ImpersonationContext в Service-слой). Токены живут на
// BYPASSRLS-подключении pgsql_supplier — SharesSupplierPdo шарит PDO.
uses(DatabaseTransactions::class, SharesSupplierPdo::class);
beforeEach(function () {
$this->tenant = Tenant::factory()->create([
'contact_email' => 'expiry-svc-tenant@example.ru',
]);
$this->adminId = DB::table('saas_admin_users')->insertGetId([
'email' => 'admin-expiry-svc@liderra.ru',
'full_name' => 'Expiry Svc Admin',
'password_hash' => '$2y$04$dummy-hash-for-test',
'role' => 'support',
'is_active' => true,
'sso_provider' => 'local',
'is_break_glass' => false,
]);
});
it('endSession ставит session_ended_at и шлёт письмо клиенту', function () {
Mail::fake();
$token = ImpersonationToken::create([
'tenant_id' => $this->tenant->id,
'requested_by' => $this->adminId,
'code_hash' => 'active',
'reason' => 'active session '.str_repeat('y', 30),
'sent_to_email' => 'client-notify@example.ru',
'expires_at' => now()->addMinutes(15),
'used_at' => now()->subMinutes(5),
]);
app(ImpersonationExpiryService::class)->endSession($token);
expect($token->fresh()->session_ended_at)->not->toBeNull();
Mail::assertQueued(
ImpersonationEndedMail::class,
fn ($m) => $m->hasTo('client-notify@example.ru'),
);
});
it('endSession идемпотентен — уже завершённая сессия не шлёт повторное письмо', function () {
Mail::fake();
$endedAt = now()->subMinutes(10);
$token = ImpersonationToken::create([
'tenant_id' => $this->tenant->id,
'requested_by' => $this->adminId,
'code_hash' => 'completed',
'reason' => 'completed session '.str_repeat('z', 30),
'sent_to_email' => 'already-ended@example.ru',
'expires_at' => now()->subMinutes(45),
'used_at' => now()->subMinutes(40),
'session_ended_at' => $endedAt,
]);
app(ImpersonationExpiryService::class)->endSession($token);
Mail::assertNothingQueued();
// session_ended_at не перезаписан
expect($token->fresh()->session_ended_at->timestamp)->toBe($endedAt->timestamp);
});