Add application API keys
linter / quality (pull_request) Successful in 1m19s
tests / ci (8.4) (pull_request) Successful in 1m32s

This commit is contained in:
2026-04-20 19:58:24 +02:00
parent a6a4615ad5
commit bf0e50464a
31 changed files with 1124 additions and 46 deletions
@@ -0,0 +1,103 @@
<?php
namespace Tests\Feature\Settings;
use App\Models\User;
use App\Models\UserApplication;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Inertia\Testing\AssertableInertia as Assert;
use Tests\TestCase;
class UserApplicationTest extends TestCase
{
use RefreshDatabase;
public function test_applications_page_is_displayed(): void
{
$user = User::factory()->create();
UserApplication::factory()->for($user)->create([
'name' => 'Claude Desktop',
]);
$this->actingAs($user)
->get(route('applications.edit'))
->assertOk()
->assertInertia(fn (Assert $page) => $page
->component('settings/Applications')
->where('applications.0.name', 'Claude Desktop')
->where('applicationKey', null),
);
}
public function test_users_can_create_applications_and_see_plaintext_key_once(): void
{
$user = User::factory()->create();
$response = $this->actingAs($user)
->post(route('applications.store'), [
'name' => 'Claude Desktop',
])
->assertRedirect(route('applications.edit'))
->assertSessionHas('applicationKey')
->assertSessionHas('status', 'Claude Desktop was created. Save this key now; it will not be shown again.');
$plainTextKey = (string) $response->baseResponse->getSession()->get('applicationKey');
$this->assertStringStartsWith('kg_app_', $plainTextKey);
$this->assertDatabaseHas('user_applications', [
'name' => 'Claude Desktop',
'user_id' => $user->id,
'key_hash' => UserApplication::hashKey($plainTextKey),
]);
$this->assertDatabaseMissing('user_applications', [
'name' => 'Claude Desktop',
'key_hash' => $plainTextKey,
]);
$this->actingAs($user)
->get(route('applications.edit'))
->assertOk()
->assertInertia(fn (Assert $page) => $page
->where('applicationKey', $plainTextKey),
);
$this->actingAs($user)
->get(route('applications.edit'))
->assertOk()
->assertInertia(fn (Assert $page) => $page
->where('applicationKey', null),
);
}
public function test_users_can_delete_their_own_applications(): void
{
$user = User::factory()->create();
$application = UserApplication::factory()->for($user)->create([
'name' => 'Claude Desktop',
]);
$this->actingAs($user)
->delete(route('applications.destroy', $application))
->assertRedirect(route('applications.edit'))
->assertSessionHas('status', 'Claude Desktop was removed.');
$this->assertDatabaseMissing('user_applications', [
'id' => $application->id,
]);
}
public function test_users_cannot_delete_foreign_applications(): void
{
$owner = User::factory()->create();
$otherUser = User::factory()->create();
$application = UserApplication::factory()->for($owner)->create();
$this->actingAs($otherUser)
->delete(route('applications.destroy', $application))
->assertForbidden();
$this->assertDatabaseHas('user_applications', [
'id' => $application->id,
]);
}
}