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

Backend (schema-free, без смены SESSION_DRIVER):
- AccountController + ChangePasswordRequest: POST /api/account/change-password —
  проверка текущего пароля (Hash::check против password_hash), новый >=10 симв +
  confirmed, лог password_changed/password_change_failed в auth_log (hash-chain).
- GET /api/account/security: last_password_change_at (max по password-событиям
  auth_log) + recent_logins (реальные login_success: устройство/IP/время).
- Роуты под auth:sanctum + throttle:auth-password.
- Pest: 6 тестов. Регрессия Account+Auth — 23/23 GREEN. phpstan-baseline обновлён
  (Pest-$this false-positives нового теста, как у прочих тестов).

Frontend:
- api/account.ts.
- ChangePasswordCard: реальная дата + диалог (текущий/новый/подтверждение,
  show/hide, обработка 422 неверного текущего пароля).
- SessionsTable -> «Недавние входы»: реальный список из API, убраны 3
  захардкоженных фейк-сессии + мёртвая кнопка «Завершить».

NB: индивидуальный отзыв cookie-сессий требует database-драйвера сессий
(инфра-решение владельца) — отдельный follow-up. Сейчас входы — честный read-only.

Верификация: type-check, build, Playwright (диалог: неверный->ошибка,
смена->дата 21.06, восстановление password123; недавние входы — реальные).

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

102 lines
3.9 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 () {
// Лог: один вход + одна смена пароля.
DB::table('auth_log')->insert([
'actor_type' => 'tenant_user',
'tenant_id' => $this->tenant->id,
'user_id' => $this->user->id,
'email' => $this->user->email,
'event' => 'login_success',
'ip_address' => '10.0.0.1',
'user_agent' => 'Mozilla/5.0 (Windows NT 10.0) Chrome/120.0',
'created_at' => now(),
]);
$response = $this->getJson('/api/account/security');
$response->assertOk();
expect($response->json('recent_logins'))->toBeArray();
expect($response->json('recent_logins.0.device'))->toContain('Chrome');
expect($response->json('recent_logins.0.current'))->toBeTrue();
expect($response->json())->toHaveKey('last_password_change_at');
});