Files
portal/app/tests/Feature/Account/ChangePasswordTest.php
T
Дмитрий 2225a8487e
Accessibility (Pa11y live) / a11y (push) Has been cancelled
SAST — Semgrep / Semgrep SAST scan (push) Has been cancelled
feat(security): рабочая «Завершить сессию» — реальный отзыв активных сессий
UI-аудит: вкладка Безопасность показывала фейк-сессии с мёртвой кнопкой.
Теперь — реальные активные сессии + рабочий отзыв.

- UserSessionTracker (новый): запись сессии при входе (login + 2FA verify +
  recovery-use) в существующую таблицу user_sessions; отзыв = удаление строки
  + удаление сессии из Redis по session_id (реальный выход с устройства);
  logout снимает текущую сессию из списка. Best-effort (не ломает вход/выход).
- AccountController: GET /api/account/security отдаёт реальные сессии;
  DELETE /api/account/sessions/{id} — отзыв (только свои; чужая → 404).
- Фронт SessionsTable: список + кнопка «Завершить» (кроме текущей).
- phpstan-baseline обновлён (Pest-$this нового теста).

Pest: 10/10. Верификация: Playwright (2 сессии → «Завершить» → исчезла).

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

88 lines
3.3 KiB
PHP

<?php
declare(strict_types=1);
use App\Models\Tenant;
use App\Models\User;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
uses(DatabaseTransactions::class);
beforeEach(function () {
$this->tenant = Tenant::factory()->create();
$this->user = User::factory()->create([
'tenant_id' => $this->tenant->id,
'password_hash' => Hash::make('current-pass-123'),
]);
$this->actingAs($this->user);
});
test('POST /api/account/change-password меняет пароль при верном текущем', function () {
$response = $this->postJson('/api/account/change-password', [
'current_password' => 'current-pass-123',
'password' => 'brand-new-pass-456',
'password_confirmation' => 'brand-new-pass-456',
]);
$response->assertOk();
expect($response->json('last_password_change_at'))->not->toBeNull();
$this->user->refresh();
expect(Hash::check('brand-new-pass-456', $this->user->password_hash))->toBeTrue();
expect(Hash::check('current-pass-123', $this->user->password_hash))->toBeFalse();
// Событие записано в auth_log.
$logged = DB::table('auth_log')
->where('user_id', $this->user->id)
->where('event', 'password_changed')
->exists();
expect($logged)->toBeTrue();
});
test('POST /api/account/change-password: 422 при неверном текущем пароле', function () {
$this->postJson('/api/account/change-password', [
'current_password' => 'wrong-current-pass',
'password' => 'brand-new-pass-456',
'password_confirmation' => 'brand-new-pass-456',
])->assertStatus(422)->assertJsonValidationErrorFor('current_password');
// Пароль НЕ изменён.
$this->user->refresh();
expect(Hash::check('current-pass-123', $this->user->password_hash))->toBeTrue();
});
test('POST /api/account/change-password: 422 при коротком новом пароле', function () {
$this->postJson('/api/account/change-password', [
'current_password' => 'current-pass-123',
'password' => 'short',
'password_confirmation' => 'short',
])->assertStatus(422)->assertJsonValidationErrorFor('password');
});
test('POST /api/account/change-password: 422 при несовпадении подтверждения', function () {
$this->postJson('/api/account/change-password', [
'current_password' => 'current-pass-123',
'password' => 'brand-new-pass-456',
'password_confirmation' => 'different-pass-789',
])->assertStatus(422)->assertJsonValidationErrorFor('password');
});
test('POST /api/account/change-password без auth: 401', function () {
auth()->logout();
$this->postJson('/api/account/change-password', [
'current_password' => 'current-pass-123',
'password' => 'brand-new-pass-456',
'password_confirmation' => 'brand-new-pass-456',
])->assertStatus(401);
});
test('GET /api/account/security возвращает дату смены и список сессий', function () {
$response = $this->getJson('/api/account/security');
$response->assertOk();
expect($response->json('sessions'))->toBeArray();
expect($response->json())->toHaveKey('last_password_change_at');
});