41 lines
1.2 KiB
PHP
41 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Models;
|
|
|
|
use App\Models\User;
|
|
use App\Models\UserApplication;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Tests\TestCase;
|
|
|
|
class UserApplicationTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
public function test_application_keys_are_hashed_and_hidden(): void
|
|
{
|
|
$plainTextKey = UserApplication::generatePlainTextKey();
|
|
$keyHash = UserApplication::hashKey($plainTextKey);
|
|
|
|
$this->assertStringStartsWith('kg_app_', $plainTextKey);
|
|
$this->assertNotSame($plainTextKey, $keyHash);
|
|
$this->assertSame(64, strlen($keyHash));
|
|
$this->assertSame($keyHash, UserApplication::hashKey($plainTextKey));
|
|
|
|
$application = UserApplication::query()->create([
|
|
'key_hash' => $keyHash,
|
|
'name' => 'Claude Desktop',
|
|
'user_id' => User::factory()->create()->id,
|
|
]);
|
|
|
|
$this->assertArrayNotHasKey('key_hash', $application->toArray());
|
|
$this->assertDatabaseHas('user_applications', [
|
|
'id' => $application->id,
|
|
'key_hash' => $keyHash,
|
|
]);
|
|
$this->assertDatabaseMissing('user_applications', [
|
|
'id' => $application->id,
|
|
'key_hash' => $plainTextKey,
|
|
]);
|
|
}
|
|
}
|