2026-03-29 23:14:23 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2026-03-31 03:40:18 +00:00
|
|
|
use App\Enums\UserWorkspace\Role;
|
2026-05-03 21:38:17 +00:00
|
|
|
use App\Models\AccessToken;
|
2026-03-29 23:14:23 +00:00
|
|
|
use App\Models\User;
|
|
|
|
|
use App\Models\Workspace;
|
|
|
|
|
|
|
|
|
|
beforeEach(function () {
|
2026-05-03 21:38:17 +00:00
|
|
|
$this->user = User::factory()->create();
|
|
|
|
|
$this->workspace = Workspace::factory()->create([
|
|
|
|
|
'account_id' => $this->user->account_id,
|
|
|
|
|
'user_id' => $this->user->id,
|
|
|
|
|
]);
|
|
|
|
|
$this->workspace->members()->attach($this->user->id, ['role' => Role::Admin->value]);
|
2026-03-29 23:14:23 +00:00
|
|
|
$this->user->update(['current_workspace_id' => $this->workspace->id]);
|
|
|
|
|
$this->user->refresh();
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-03 21:38:17 +00:00
|
|
|
function makeWorkspaceToken(User $user, Workspace $workspace): AccessToken
|
|
|
|
|
{
|
|
|
|
|
$result = $user->createToken('Existing');
|
|
|
|
|
$token = AccessToken::find($result->token->id);
|
|
|
|
|
$token->forceFill(['workspace_id' => $workspace->id])->saveQuietly();
|
|
|
|
|
|
|
|
|
|
return $token->refresh();
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-29 23:14:23 +00:00
|
|
|
it('shows api keys page', function () {
|
2026-05-03 21:38:17 +00:00
|
|
|
makeWorkspaceToken($this->user, $this->workspace);
|
2026-03-29 23:14:23 +00:00
|
|
|
|
|
|
|
|
$this->actingAs($this->user)
|
|
|
|
|
->get(route('app.api-keys.index'))
|
|
|
|
|
->assertOk()
|
|
|
|
|
->assertInertia(fn ($page) => $page
|
2026-05-03 16:44:13 +00:00
|
|
|
->component('settings/workspace/ApiKeys')
|
2026-03-29 23:14:23 +00:00
|
|
|
->has('apiTokens', 1)
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('creates an api key', function () {
|
|
|
|
|
$this->actingAs($this->user)
|
2026-05-03 21:38:17 +00:00
|
|
|
->post(route('app.api-keys.store'), ['name' => 'My API Key'])
|
2026-03-29 23:14:23 +00:00
|
|
|
->assertRedirect();
|
|
|
|
|
|
2026-05-03 21:38:17 +00:00
|
|
|
$tokens = AccessToken::where('user_id', $this->user->id)
|
|
|
|
|
->where('workspace_id', $this->workspace->id)
|
|
|
|
|
->get();
|
2026-03-29 23:14:23 +00:00
|
|
|
|
2026-05-03 21:38:17 +00:00
|
|
|
expect($tokens)->toHaveCount(1);
|
|
|
|
|
expect($tokens->first()->name)->toBe('My API Key');
|
|
|
|
|
expect($tokens->first()->revoked)->toBeFalse();
|
2026-03-29 23:14:23 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
2026-05-03 21:38:17 +00:00
|
|
|
$token = AccessToken::where('user_id', $this->user->id)
|
|
|
|
|
->where('workspace_id', $this->workspace->id)
|
|
|
|
|
->first();
|
|
|
|
|
|
2026-03-29 23:14:23 +00:00
|
|
|
expect($token->expires_at)->not->toBeNull();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('validates name is required', function () {
|
|
|
|
|
$this->actingAs($this->user)
|
|
|
|
|
->post(route('app.api-keys.store'), [])
|
|
|
|
|
->assertSessionHasErrors('name');
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-03 21:38:17 +00:00
|
|
|
it('revokes an api key', function () {
|
|
|
|
|
$token = makeWorkspaceToken($this->user, $this->workspace);
|
2026-03-29 23:14:23 +00:00
|
|
|
|
|
|
|
|
$this->actingAs($this->user)
|
2026-05-03 21:38:17 +00:00
|
|
|
->delete(route('app.api-keys.destroy', $token->id))
|
2026-03-29 23:14:23 +00:00
|
|
|
->assertRedirect();
|
|
|
|
|
|
2026-05-03 21:38:17 +00:00
|
|
|
expect($token->refresh()->revoked)->toBeTrue();
|
2026-03-29 23:14:23 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('cannot delete api key from another workspace', function () {
|
2026-05-03 21:38:17 +00:00
|
|
|
$otherUser = User::factory()->create();
|
|
|
|
|
$otherWorkspace = Workspace::factory()->create([
|
|
|
|
|
'account_id' => $otherUser->account_id,
|
|
|
|
|
'user_id' => $otherUser->id,
|
|
|
|
|
]);
|
|
|
|
|
$token = makeWorkspaceToken($otherUser, $otherWorkspace);
|
2026-03-29 23:14:23 +00:00
|
|
|
|
|
|
|
|
$this->actingAs($this->user)
|
2026-05-03 21:38:17 +00:00
|
|
|
->delete(route('app.api-keys.destroy', $token->id))
|
2026-03-29 23:14:23 +00:00
|
|
|
->assertNotFound();
|
|
|
|
|
});
|
2026-03-31 03:40:18 +00:00
|
|
|
|
|
|
|
|
it('member cannot create api key', function () {
|
2026-05-03 21:38:17 +00:00
|
|
|
$member = User::factory()->create();
|
2026-03-31 03:40:18 +00:00
|
|
|
$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 () {
|
2026-05-03 21:38:17 +00:00
|
|
|
$member = User::factory()->create();
|
2026-03-31 03:40:18 +00:00
|
|
|
$this->workspace->members()->attach($member->id, ['role' => Role::Member->value]);
|
|
|
|
|
$member->update(['current_workspace_id' => $this->workspace->id]);
|
|
|
|
|
|
2026-05-03 21:38:17 +00:00
|
|
|
$token = makeWorkspaceToken($this->user, $this->workspace);
|
2026-03-31 03:40:18 +00:00
|
|
|
|
|
|
|
|
$this->actingAs($member)
|
2026-05-03 21:38:17 +00:00
|
|
|
->delete(route('app.api-keys.destroy', $token->id))
|
2026-03-31 03:40:18 +00:00
|
|
|
->assertForbidden();
|
|
|
|
|
});
|
feat: social account toggle action, API, MCP + full test coverage
- Extract ToggleSocialAccount action from SocialController
- Add API endpoints: GET /social-accounts, PUT /social-accounts/{id}/toggle
- Add MCP tools: ListSocialAccountsTool, ToggleSocialAccountTool
- Fix all MCP tools: findOrFail → find + Response::error for graceful errors
- Fix MCP tools using $request->validated() without validate() call
- Fix return types to Response|ResponseFactory for error paths
- Add SocialAccountResource is_active/status fields (no tokens exposed)
- Add 43 MCP tests covering all 18 tools (CRUD, validation, cross-workspace)
- Add API response structure tests for posts, hashtags, labels, workspace
- Add API validation tests for post create/update, api-key expiry, label color
- Add API cross-workspace delete tests for hashtags and labels
- Add app validation tests for hashtag/label update, invite fields, password
- Add auth required tests for notifications, profile delete, api-keys index
- Add media reorder validation tests
2026-03-31 04:42:39 +00:00
|
|
|
|
|
|
|
|
it('api keys page requires authentication', function () {
|
|
|
|
|
$this->get(route('app.api-keys.index'))->assertRedirect(route('login'));
|
|
|
|
|
});
|