Files
KanbanGit/tests/Feature/Settings/UserApplicationTest.php

128 lines
4.4 KiB
PHP

<?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)
->missing('applications.0.key_hash')
->missing('applications.0.plain_text_key'),
);
}
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,
]);
}
public function test_application_list_only_contains_the_current_users_keys(): void
{
$user = User::factory()->create();
UserApplication::factory()->for($user)->create(['name' => 'Own tool']);
UserApplication::factory()->create(['name' => 'Foreign tool']);
$this->actingAs($user)->get(route('applications.edit'))
->assertInertia(fn (Assert $page) => $page
->has('applications', 1)
->where('applications.0.name', 'Own tool')
->where('applicationKey', null));
}
public function test_invalid_application_name_does_not_issue_a_key(): void
{
$user = User::factory()->create();
$this->actingAs($user)->from(route('applications.edit'))
->post(route('applications.store'), ['name' => ''])
->assertSessionHasErrors('name')
->assertSessionMissing('applicationKey');
$this->assertDatabaseCount('user_applications', 0);
}
}