Files
portal/app/tests/Unit/Concerns/WritesAuthLogTest.php
T

51 lines
1.6 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
use App\Http\Controllers\Concerns\WritesAuthLog;
use App\Models\Tenant;
use App\Models\User;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;
uses(TestCase::class, DatabaseTransactions::class);
it('writes auth_log row with all fields', function () {
$tenant = Tenant::factory()->create();
$user = User::factory()->create(['tenant_id' => $tenant->id]);
$dummy = new class
{
use WritesAuthLog;
public function fire(?int $userId, ?int $tenantId): void
{
$this->logAuthEvent('login_success', $userId, $tenantId, 'a@b.c', '1.2.3.4', 'UA', null);
}
};
$dummy->fire($user->id, $tenant->id);
$row = DB::table('auth_log')->latest('id')->first();
expect($row->event)->toBe('login_success')
->and($row->actor_type)->toBe('tenant_user')
->and((int) $row->user_id)->toBe($user->id)
->and((int) $row->tenant_id)->toBe($tenant->id)
->and((string) $row->ip_address)->toBe('1.2.3.4')
->and($row->user_agent)->toBe('UA');
});
it('actor_type=tenant_user even if user NULL (anti-enumeration)', function () {
$dummy = new class
{
use WritesAuthLog;
public function fire(?int $userId, ?int $tenantId): void
{
$this->logAuthEvent('login_failed', $userId, $tenantId, 'x@y.z', null, null, 'no_such_user');
}
};
$dummy->fire(null, null);
$row = DB::table('auth_log')->latest('id')->first();
expect($row->actor_type)->toBe('tenant_user')->and($row->user_id)->toBeNull();
});