trypost/tests/Feature/ApiKeyControllerTest.php
Paulo Castellano 74c6442728 refactor: code review fixes — policies, enums, data_get, tests
- Refactor WorkspacePolicy to use pivot role instead of workspace.user_id
- Add manageBilling policy (owner only) to BillingController
- Fix ApiKeyController authorization (view → manageTeam for store/destroy)
- Fix WorkspaceInviteController using workspace.user_id for owner checks
- Fix WorkspaceController settings is_owner using workspace.user_id
- Create PostAction enum for UpdatePost/PostController action strings
- Create ApiToken\Status enum
- Add User::SUBSCRIPTION_NAME constant, replace all hardcoded 'default'
- Convert wantsEmailFor to accept NotificationType enum
- Convert all $data[] to data_get() across publishers, controllers, jobs
- Fix SocialLoginController callback missing try/catch
- Fix SocialController::toggleActive missing workspace null check
- Fix UpdatePost NPE on meta merge when postPlatform not found
- Remove HTML5 required attributes from form inputs
- Convert function declarations to arrow functions in Vue components
- Replace hardcoded URLs with Wayfinder route helpers
- Replace new Date() with dayjs
- Add 16 new test files covering policies, authorization, publishing
2026-03-31 00:40:18 -03:00

102 lines
3.4 KiB
PHP

<?php
declare(strict_types=1);
use App\Enums\User\Setup;
use App\Enums\UserWorkspace\Role;
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' => Role::Owner->value]);
$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();
});
it('member cannot create api key', function () {
$member = User::factory()->create(['setup' => Setup::Completed]);
$this->workspace->members()->attach($member->id, ['role' => Role::Member->value]);
$member->update(['current_workspace_id' => $this->workspace->id]);
$this->actingAs($member)
->post(route('app.api-keys.store'), ['name' => 'Test Key'])
->assertForbidden();
});
it('member cannot delete api key', function () {
$member = User::factory()->create(['setup' => Setup::Completed]);
$this->workspace->members()->attach($member->id, ['role' => Role::Member->value]);
$member->update(['current_workspace_id' => $this->workspace->id]);
$token = ApiToken::factory()->create(['workspace_id' => $this->workspace->id]);
$this->actingAs($member)
->delete(route('app.api-keys.destroy', $token))
->assertForbidden();
});