69 lines
2.2 KiB
PHP
69 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Models\OperationLog;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Tests\TestCase;
|
|
|
|
class IpUserMappingTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
public function test_store_syncs_missing_operation_log_user_labels(): void
|
|
{
|
|
config(['toolbox.admin_ips' => ['127.0.0.1']]);
|
|
|
|
$missingLabelLog = OperationLog::query()->create($this->operationLogData([
|
|
'ip_address' => '192.168.1.10',
|
|
'user_label' => null,
|
|
]));
|
|
$emptyLabelLog = OperationLog::query()->create($this->operationLogData([
|
|
'ip_address' => '192.168.1.10',
|
|
'user_label' => '',
|
|
]));
|
|
$existingLabelLog = OperationLog::query()->create($this->operationLogData([
|
|
'ip_address' => '192.168.1.10',
|
|
'user_label' => 'lisi',
|
|
]));
|
|
$otherIpLog = OperationLog::query()->create($this->operationLogData([
|
|
'ip_address' => '192.168.1.11',
|
|
'user_label' => null,
|
|
]));
|
|
|
|
$response = $this->postJson('/api/admin/ip-user-mappings', [
|
|
'ip_address' => '192.168.1.10',
|
|
'user_name' => 'zhangsan',
|
|
'remark' => 'dev',
|
|
]);
|
|
|
|
$response->assertOk()
|
|
->assertJsonPath('success', true)
|
|
->assertJsonPath('data.synced_operation_logs_count', 2);
|
|
|
|
$this->assertSame('zhangsan', $missingLabelLog->refresh()->user_label);
|
|
$this->assertSame('zhangsan', $emptyLabelLog->refresh()->user_label);
|
|
$this->assertSame('lisi', $existingLabelLog->refresh()->user_label);
|
|
$this->assertNull($otherIpLog->refresh()->user_label);
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $overrides
|
|
* @return array<string, mixed>
|
|
*/
|
|
private function operationLogData(array $overrides = []): array
|
|
{
|
|
return array_merge([
|
|
'ip_address' => '127.0.0.1',
|
|
'user_label' => null,
|
|
'method' => 'POST',
|
|
'path' => '/api/test',
|
|
'route_name' => null,
|
|
'status_code' => 200,
|
|
'duration_ms' => 12,
|
|
'request_payload' => [],
|
|
'user_agent' => 'PHPUnit',
|
|
], $overrides);
|
|
}
|
|
}
|