trypost/tests/Feature/BillingControllerTest.php
Paulo Castellano ded1c998ec feat: redesign billing, onboarding, sidebar, and settings architecture
- Sidebar reorganized: Workspace group (connections, hashtags, labels,
  API keys, settings) and Account group (settings, usage, billing)
- Account group only visible to owner and hidden in self-hosted mode
- Onboarding simplified: role -> account (connect socials) -> completed
  -> redirect to /subscribe. Removed Subscription setup step.
- Subscribe page redesigned with 4 plan cards, monthly/yearly toggle,
  trial info, and per-plan features list
- Billing page redesigned following Sendkit layout (sections with
  sidebar labels)
- Processing page uses usePoll with immediate watch for subscription
  activation
- Cancel URL redirects directly to /subscribe
- Account settings page with name and billing_email (syncs with Stripe)
- Usage page with ring meters for all plan limits
- Settings layout tabs only for user pages (profile, password,
  notifications). Workspace/API keys/billing are standalone pages.
- GoogleAuthButton extracted as reusable component
- WorkspaceRole TypeScript enum for type-safe role checks in frontend
- Trial period changed to 7 days
- Fixed onboarding loop when user confirms email
- All 1101 tests passing
2026-04-15 00:33:38 -03:00

154 lines
4.7 KiB
PHP

<?php
declare(strict_types=1);
use App\Enums\User\Setup;
use App\Enums\UserWorkspace\Role;
use App\Models\Account;
use App\Models\Plan;
use App\Models\User;
use App\Models\Workspace;
beforeEach(function () {
$this->account = Account::factory()->create();
$this->user = User::factory()->create([
'setup' => Setup::Completed,
'account_id' => $this->account->id,
]);
$this->account->update(['owner_id' => $this->user->id]);
$this->workspace = Workspace::factory()->create([
'account_id' => $this->account->id,
'user_id' => $this->user->id,
]);
$this->workspace->members()->attach($this->user->id, ['role' => Role::Member->value]);
$this->user->update(['current_workspace_id' => $this->workspace->id]);
});
// Subscribe tests
test('subscribe requires authentication', function () {
$response = $this->get(route('app.subscribe'));
$response->assertRedirect(route('login'));
});
test('subscribe shows subscription page', function () {
config(['trypost.self_hosted' => false]);
$response = $this->actingAs($this->user)->get(route('app.subscribe'));
$response->assertOk();
$response->assertInertia(fn ($page) => $page
->component('billing/Subscribe', false)
->has('plans')
->has('trialDays')
);
});
test('subscribe redirects to billing index when account has active subscription', function () {
$this->account->subscriptions()->create([
'type' => Account::SUBSCRIPTION_NAME,
'stripe_id' => 'sub_test_'.fake()->uuid(),
'stripe_status' => 'active',
'stripe_price' => 'price_123',
]);
$response = $this->actingAs($this->user)->get(route('app.subscribe'));
$response->assertRedirect(route('app.billing.index'));
});
// Index tests
test('billing index requires authentication', function () {
$response = $this->get(route('app.billing.index'));
$response->assertRedirect(route('login'));
});
test('billing index shows billing dashboard', function () {
$this->account->subscriptions()->create([
'type' => Account::SUBSCRIPTION_NAME,
'stripe_id' => 'sub_test_'.fake()->uuid(),
'stripe_status' => 'active',
'stripe_price' => 'price_123',
]);
$response = $this->actingAs($this->user)->get(route('app.billing.index'));
$response->assertOk();
$response->assertInertia(fn ($page) => $page
->component('billing/Index', false)
->has('hasSubscription')
->has('plan')
->has('plans')
);
});
// Processing tests
test('billing processing requires authentication', function () {
$response = $this->get(route('app.billing.processing'));
$response->assertRedirect(route('login'));
});
test('billing processing shows processing page', function () {
$response = $this->actingAs($this->user)->get(route('app.billing.processing'));
$response->assertOk();
$response->assertInertia(fn ($page) => $page
->component('billing/Processing', false)
->has('subscriptionActive')
);
});
// Checkout tests
test('checkout requires authentication', function () {
$plan = Plan::first();
$response = $this->post(route('app.billing.checkout', $plan));
$response->assertRedirect(route('login'));
});
// Portal tests
test('portal requires authentication', function () {
$response = $this->get(route('app.billing.portal'));
$response->assertRedirect(route('login'));
});
// Authorization tests
test('non-owner admin cannot access billing index', function () {
$admin = User::factory()->create([
'setup' => Setup::Completed,
'account_id' => $this->account->id,
]);
$this->workspace->members()->attach($admin->id, ['role' => Role::Admin->value]);
$admin->update(['current_workspace_id' => $this->workspace->id]);
$this->account->subscriptions()->create([
'type' => Account::SUBSCRIPTION_NAME,
'stripe_id' => 'sub_test_'.fake()->uuid(),
'stripe_status' => 'active',
'stripe_price' => 'price_123',
]);
$this->actingAs($admin)->get(route('app.billing.index'))->assertForbidden();
});
test('member cannot access billing index', function () {
$member = User::factory()->create([
'setup' => Setup::Completed,
'account_id' => $this->account->id,
]);
$this->workspace->members()->attach($member->id, ['role' => Role::Member->value]);
$member->update(['current_workspace_id' => $this->workspace->id]);
$this->account->subscriptions()->create([
'type' => Account::SUBSCRIPTION_NAME,
'stripe_id' => 'sub_test_'.fake()->uuid(),
'stripe_status' => 'active',
'stripe_price' => 'price_123',
]);
$this->actingAs($member)->get(route('app.billing.index'))->assertForbidden();
});