- Add API rate limiting (60/min per workspace) in AppServiceProvider - Add throttle:api middleware to all API routes - Add rate limit exception rendering for JSON responses - Add middleware priority list (AuthenticateApiToken before ThrottleRequests) - Add ApiKeyControllerTest (6 tests for App dashboard CRUD) - Remove duplicate test files (Controllers/ dir duplicates) - Remove old controllers and routes/settings.php - Add routes/mcp.php placeholder - Add API Keys nav item to settings layout - Fix LazilyRefreshDatabase conflict in API tests - 698 tests passing
79 lines
2.5 KiB
PHP
79 lines
2.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use App\Enums\User\Setup;
|
|
use App\Models\ApiToken;
|
|
use App\Models\User;
|
|
use App\Models\Workspace;
|
|
|
|
beforeEach(function () {
|
|
$this->user = User::factory()->create(['setup' => Setup::Completed]);
|
|
$this->workspace = Workspace::factory()->create(['user_id' => $this->user->id]);
|
|
$this->workspace->members()->attach($this->user->id, ['role' => 'owner']);
|
|
$this->user->update(['current_workspace_id' => $this->workspace->id]);
|
|
$this->user->refresh();
|
|
});
|
|
|
|
it('shows api keys page', function () {
|
|
$token = ApiToken::factory()->create(['workspace_id' => $this->workspace->id]);
|
|
|
|
$this->actingAs($this->user)
|
|
->get(route('app.api-keys.index'))
|
|
->assertOk()
|
|
->assertInertia(fn ($page) => $page
|
|
->component('settings/ApiKeys')
|
|
->has('apiTokens', 1)
|
|
);
|
|
});
|
|
|
|
it('creates an api key', function () {
|
|
$this->actingAs($this->user)
|
|
->post(route('app.api-keys.store'), [
|
|
'name' => 'My API Key',
|
|
])
|
|
->assertRedirect();
|
|
|
|
expect(ApiToken::where('workspace_id', $this->workspace->id)->count())->toBe(1);
|
|
|
|
$token = ApiToken::where('workspace_id', $this->workspace->id)->first();
|
|
expect($token->name)->toBe('My API Key');
|
|
expect($token->status)->toBe('active');
|
|
});
|
|
|
|
it('creates an api key with expiration', function () {
|
|
$this->actingAs($this->user)
|
|
->post(route('app.api-keys.store'), [
|
|
'name' => 'Expiring Key',
|
|
'expires_at' => now()->addDays(30)->format('Y-m-d'),
|
|
])
|
|
->assertRedirect();
|
|
|
|
$token = ApiToken::where('workspace_id', $this->workspace->id)->first();
|
|
expect($token->expires_at)->not->toBeNull();
|
|
});
|
|
|
|
it('validates name is required', function () {
|
|
$this->actingAs($this->user)
|
|
->post(route('app.api-keys.store'), [])
|
|
->assertSessionHasErrors('name');
|
|
});
|
|
|
|
it('deletes an api key', function () {
|
|
$token = ApiToken::factory()->create(['workspace_id' => $this->workspace->id]);
|
|
|
|
$this->actingAs($this->user)
|
|
->delete(route('app.api-keys.destroy', $token))
|
|
->assertRedirect();
|
|
|
|
expect(ApiToken::find($token->id))->toBeNull();
|
|
});
|
|
|
|
it('cannot delete api key from another workspace', function () {
|
|
$otherWorkspace = Workspace::factory()->create();
|
|
$token = ApiToken::factory()->create(['workspace_id' => $otherWorkspace->id]);
|
|
|
|
$this->actingAs($this->user)
|
|
->delete(route('app.api-keys.destroy', $token))
|
|
->assertNotFound();
|
|
});
|