trypost/tests/Feature/LimitEnforcementTest.php
Paulo Castellano 2da15df96c feat: introduce Account entity as billing owner and refactor architecture
- Create Account model as Cashier Billable entity (stripe, plan, subscription)
- Account owns workspaces and has an owner_id (User)
- User belongs to one Account via account_id
- Workspace belongs to Account via account_id, no longer has billing fields
- Remove Brand model entirely (workspaces serve as grouping)
- Rename brand_limit to workspace_limit in plans
- Workspace roles simplified: admin/member/viewer (owner via Account)
- Invites now belong to Account with workspaces JSON array
- Pennant features scope changed from Workspace to Account
- EnsureSubscribed middleware checks Account subscription
- All controllers updated: BillingController, OnboardingController,
  WorkspaceInviteController, SocialController, StripeEventListener
- Frontend: extract GoogleAuthButton component, create WorkspaceRole
  enum for type-safe role checks, fix all views for new architecture
- All 1101 tests passing
2026-04-14 22:22:04 -03:00

75 lines
2.2 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 () {
config(['trypost.self_hosted' => false]);
$this->plan = Plan::first();
$this->plan->update([
'workspace_limit' => 5,
'member_limit' => 5,
]);
$this->account = Account::factory()->create(['plan_id' => $this->plan->id]);
$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]);
});
test('can invite member within limit', function () {
expect($this->user->can('inviteMember', $this->workspace))->toBeTrue();
});
test('cannot invite member beyond limit', function () {
$members = User::factory()->count(4)->create([
'account_id' => $this->account->id,
]);
foreach ($members as $member) {
$this->workspace->members()->attach($member->id, ['role' => Role::Member->value]);
}
// 1 owner + 4 members = 5, which is the limit
expect($this->user->can('inviteMember', $this->workspace))->toBeFalse();
});
test('self hosted mode bypasses workspace limit', function () {
config(['trypost.self_hosted' => true]);
Workspace::factory()->count(10)->create([
'account_id' => $this->account->id,
'user_id' => $this->user->id,
]);
expect($this->user->can('create', Workspace::class))->toBeTrue();
});
test('self hosted mode bypasses member limit', function () {
config(['trypost.self_hosted' => true]);
$members = User::factory()->count(10)->create([
'account_id' => $this->account->id,
]);
foreach ($members as $member) {
$this->workspace->members()->attach($member->id, ['role' => Role::Member->value]);
}
expect($this->user->can('inviteMember', $this->workspace))->toBeTrue();
});