trypost/tests/Feature/McpSettingsControllerTest.php
Paulo Castellano b4f61be6ef
Welcome: pre-subscription funnel and member subscription-required screen (#243)
* Rename pre-subscription onboarding funnel to Welcome.

Move the ICP steps to /welcome, drop the social-connect checkout gate, hold unpaid members on a subscription-required screen, and keep legacy /onboarding URLs working until the post-subscription checklist lands.

Closes #237

Co-authored-by: Cursor <cursoragent@cursor.com>

* Drop legacy /onboarding ICP URL aliases.

Unfinished users re-enter Welcome via EnsureAccountReady on next login; /onboarding stays free for the post-subscription checklist.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Simplify Welcome PostHog event names.

Use welcome.persona/goals/referral and drop the unused checkout case — begin checkout stays on the frontend as checkout.started / begin_checkout.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Slim welcome goal options to match the #204 set.

Drop team_collaboration, automate_api, and track_performance so the goals step stays at nine choices.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Split Welcome AI goals into TryPost AI and MCP assistants.

Rewrite ai_content for in-app generation and add use_mcp so Claude/ChatGPT/Cursor intent is captured separately across locales.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Harden Welcome goals gate and drop dead checkout UI.

Treat removed goal values as incomplete so mid-funnel users re-select, remove the unused canCheckout branch, and fix the pt-BR welcome progress label.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Add password visibility toggle to the login form.

Match the register eye control so users can reveal their password while signing in.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Point the sidebar community link to Discord.

Replace the X stay-updated entry with Join Discord and the trypost.it/discord invite.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Rename the sidebar Discord link to Discord community.

Softer label that matches the other support nav items.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Fix broken Turkish Discord community translation.

An unescaped apostrophe left a parse error in lang/tr/sidebar.php.

Co-authored-by: Cursor <cursoragent@cursor.com>

---------

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-06 11:34:50 -03:00

289 lines
11 KiB
PHP

<?php
declare(strict_types=1);
use App\Enums\UserWorkspace\Role;
use App\Models\AccessToken;
use App\Models\User;
use App\Models\Workspace;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
beforeEach(function (): void {
config(['trypost.self_hosted' => false]);
$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]);
$this->user->update(['current_workspace_id' => $this->workspace->id]);
$this->user->refresh();
// SaaS mode: the MCP settings live behind EnsureAccountReady.
subscribeAccount($this->user->account);
});
it('shows the mcp settings page', function (): void {
$this->actingAs($this->user)
->get(route('app.mcp.index'))
->assertOk()
->assertInertia(fn ($page) => $page
->component('settings/workspace/Mcp')
->where('mcpUrl', route('mcp.trypost'))
->missing('docsUrl')
->missing('mcpClients')
->has('connectedClients'));
});
it('shows the mcp settings page without a subscription in self-hosted mode', function (): void {
config(['trypost.self_hosted' => true]);
$this->user->account->subscriptions()->delete();
$this->actingAs($this->user->fresh())
->get(route('app.mcp.index'))
->assertOk()
->assertInertia(fn ($page) => $page
->component('settings/workspace/Mcp')
->where('mcpUrl', route('mcp.trypost')));
});
it('lists only the current users oauth clients as connected, excluding personal access tokens', function (): void {
$member = User::factory()->create(['account_id' => $this->user->account_id]);
$this->workspace->members()->attach($member->id, ['role' => Role::Member->value]);
$member->update(['current_workspace_id' => $this->workspace->id]);
$ownerClientId = mcpOauthClient('Owner Agent');
mcpAccessToken($this->user, $ownerClientId);
$memberClientId = mcpOauthClient('Member Agent');
mcpAccessToken($member, $memberClientId);
$pat = $this->user->createToken('API Key');
AccessToken::query()->findOrFail($pat->token->id)
->forceFill(['workspace_id' => $this->workspace->id])
->saveQuietly();
$this->actingAs($this->user)
->get(route('app.mcp.index'))
->assertInertia(fn ($page) => $page
->has('connectedClients', 1)
->where('connectedClients.0.name', 'Owner Agent')
->where('connectedClients.0.can_disconnect', true)
->where('connectedClients', fn ($clients): bool => collect($clients)->every(
fn (array $client): bool => $client['name'] !== 'Member Agent',
)));
});
it('excludes unscoped oauth grants from connected clients', function (): void {
mcpAccessToken($this->user, mcpOauthClient('Unscoped Agent'), scopes: []);
$this->actingAs($this->user)
->get(route('app.mcp.index'))
->assertInertia(fn ($page) => $page->where('connectedClients', []));
});
it('lists viewer own oauth grants as connected clients', function (): void {
$viewer = User::factory()->create(['account_id' => $this->user->account_id]);
$this->workspace->members()->attach($viewer->id, ['role' => Role::Viewer->value]);
$viewer->update(['current_workspace_id' => $this->workspace->id]);
mcpAccessToken($viewer, mcpOauthClient('Viewer Agent'));
$this->actingAs($viewer->fresh())
->get(route('app.mcp.index'))
->assertInertia(fn ($page) => $page
->has('connectedClients', 1)
->where('connectedClients.0.name', 'Viewer Agent')
->where('connectedClients.0.can_disconnect', true));
});
it('disconnects a client by revoking its tokens', function (): void {
$clientId = mcpOauthClient();
$token = mcpAccessToken($this->user, $clientId);
$this->actingAs($this->user)
->delete(route('app.mcp.disconnect', ['client' => $clientId]))
->assertRedirect()
->assertSessionHas('flash.success', __('mcp.disconnected'));
expect($token->fresh()->revoked)->toBeTrue();
});
it('disconnects a client when its access token expired but its refresh token is live', function (): void {
$clientId = mcpOauthClient();
$token = mcpAccessToken($this->user, $clientId);
$token->forceFill(['expires_at' => now()->subMinute()])->saveQuietly();
$refreshTokenId = Str::random(80);
DB::table('oauth_refresh_tokens')->insert([
'id' => $refreshTokenId,
'access_token_id' => $token->id,
'revoked' => false,
'expires_at' => now()->addMonth(),
]);
$this->actingAs($this->user)
->delete(route('app.mcp.disconnect', ['client' => $clientId]))
->assertRedirect()
->assertSessionHas('flash.success');
expect($token->fresh()->revoked)->toBeTrue()
->and(DB::table('oauth_refresh_tokens')->where('id', $refreshTokenId)->value('revoked'))->toBeTrue();
});
it('lists a client when its access token expired but its refresh token is live', function (): void {
$clientId = mcpOauthClient('Recoverable Agent');
$token = mcpAccessToken($this->user, $clientId);
$token->forceFill(['expires_at' => now()->subMinute()])->saveQuietly();
DB::table('oauth_refresh_tokens')->insert([
'id' => Str::random(80),
'access_token_id' => $token->id,
'revoked' => false,
'expires_at' => now()->addMonth(),
]);
$this->actingAs($this->user)
->get(route('app.mcp.index'))
->assertOk()
->assertInertia(fn ($page) => $page
->has('connectedClients', 1)
->where('connectedClients.0.name', 'Recoverable Agent')
->where('connectedClients.0.can_disconnect', true));
});
it('hides a client when both access and refresh tokens are expired', function (): void {
$clientId = mcpOauthClient('Dead Agent');
$token = mcpAccessToken($this->user, $clientId);
$token->forceFill(['expires_at' => now()->subMinute()])->saveQuietly();
DB::table('oauth_refresh_tokens')->insert([
'id' => Str::random(80),
'access_token_id' => $token->id,
'revoked' => false,
'expires_at' => now()->subMinute(),
]);
$this->actingAs($this->user)
->get(route('app.mcp.index'))
->assertOk()
->assertInertia(fn ($page) => $page->where('connectedClients', []));
});
it('does not flash success when disconnecting an unknown client', function (): void {
$this->actingAs($this->user)
->delete(route('app.mcp.disconnect', ['client' => (string) Str::uuid()]))
->assertRedirect()
->assertSessionMissing('flash.success');
});
it('does not list a teammates mcp connection', function (): void {
$member = User::factory()->create(['account_id' => $this->user->account_id]);
$this->workspace->members()->attach($member->id, ['role' => Role::Member->value]);
$member->update(['current_workspace_id' => $this->workspace->id]);
mcpAccessToken($this->user, mcpOauthClient('Owner Agent'));
$this->actingAs($member->fresh())
->get(route('app.mcp.index'))
->assertOk()
->assertInertia(fn ($page) => $page->where('connectedClients', []));
});
it('allows workspace members to view and disconnect their own mcp clients', function (): void {
$member = User::factory()->create(['account_id' => $this->user->account_id]);
$this->workspace->members()->attach($member->id, ['role' => Role::Member->value]);
$member->update(['current_workspace_id' => $this->workspace->id]);
$clientId = mcpOauthClient('Member Agent');
$token = mcpAccessToken($member, $clientId);
$this->actingAs($member->fresh())
->get(route('app.mcp.index'))
->assertOk()
->assertInertia(fn ($page) => $page
->has('connectedClients', 1)
->where('connectedClients.0.name', 'Member Agent'));
$this->actingAs($member->fresh())
->delete(route('app.mcp.disconnect', ['client' => $clientId]))
->assertRedirect()
->assertSessionHas('flash.success');
expect($token->fresh()->revoked)->toBeTrue();
});
it('allows workspace viewers to view and disconnect their own mcp clients', function (): void {
$viewer = User::factory()->create(['account_id' => $this->user->account_id]);
$this->workspace->members()->attach($viewer->id, ['role' => Role::Viewer->value]);
$viewer->update(['current_workspace_id' => $this->workspace->id]);
$clientId = mcpOauthClient('Viewer Agent');
$token = mcpAccessToken($viewer, $clientId);
$this->actingAs($viewer->fresh())
->get(route('app.mcp.index'))
->assertOk()
->assertInertia(fn ($page) => $page
->component('settings/workspace/Mcp')
->has('connectedClients', 1)
->where('connectedClients.0.name', 'Viewer Agent')
->where('connectedClients.0.can_disconnect', true));
$this->actingAs($viewer->fresh())
->delete(route('app.mcp.disconnect', ['client' => $clientId]))
->assertRedirect()
->assertSessionHas('flash.success');
expect($token->fresh()->revoked)->toBeTrue();
});
it('does not revoke another users mcp client tokens', function (): void {
$member = User::factory()->create(['account_id' => $this->user->account_id]);
$this->workspace->members()->attach($member->id, ['role' => Role::Member->value]);
$member->update(['current_workspace_id' => $this->workspace->id]);
$clientId = mcpOauthClient('Owner Agent');
$token = mcpAccessToken($this->user, $clientId);
$this->actingAs($member->fresh())
->delete(route('app.mcp.disconnect', ['client' => $clientId]))
->assertRedirect()
->assertSessionMissing('flash.success');
expect($token->fresh()->revoked)->toBeFalse();
});
it('does not revoke personal access tokens via disconnect', function (): void {
$pat = $this->user->createToken('API Key');
$token = AccessToken::query()->findOrFail($pat->token->id);
$token->forceFill(['workspace_id' => $this->workspace->id])->saveQuietly();
$this->actingAs($this->user)
->delete(route('app.mcp.disconnect', ['client' => $token->client_id]))
->assertRedirect()
->assertSessionMissing('flash.success');
expect($token->fresh()->revoked)->toBeFalse();
});
it('requires authentication', function (): void {
$this->get(route('app.mcp.index'))->assertRedirect();
});
it('forbids users without workspace access', function (): void {
$outsider = User::factory()->create();
subscribeAccount($outsider->account);
$outsider->update(['current_workspace_id' => $this->workspace->id]);
$this->actingAs($outsider->fresh())
->get(route('app.mcp.index'))
->assertForbidden();
});
it('redirects to welcome when the account has no app access', function (): void {
$this->user->account->subscriptions()->delete();
$this->actingAs($this->user->fresh())
->get(route('app.mcp.index'))
->assertRedirect(route('app.welcome.persona'));
});