trypost/tests/Feature/SocialAccount/NetworkUniquenessTest.php
Paulo Castellano 17a091975d fix: merge-readiness — close two billing/network bugs, harden tests
Bugs (both with regression tests):
- OnboardingController::store now guards already-subscribed accounts (mirrors
  index), preventing a second Stripe Checkout / double subscription if a
  subscribed user re-POSTs /onboarding.
- SocialAccountObserver: drop the `platform_user_id != …` clause from the
  creating-time one-per-network check. On create there is no "self" to exclude,
  so it only weakened the rule — the same account connected via two network
  variants (e.g. Instagram standalone + via Facebook, same id) could slip a
  second account into the network. Now any account in the network blocks.

Robustness:
- CreateWorkspace wraps create + member attach + switchWorkspace in a
  transaction (cache-forget / quantity-sync run after), so a partial failure
  can't leave an orphan workspace that inflates the Stripe seat count — covers
  both the signup and the add-workspace paths.

Test honesty & coverage:
- Scope the ten "connect multiple <platform> accounts" tests to self-hosted
  mode (config + name); they only passed because the test env defaults
  SELF_HOSTED=true, and in cloud the one-per-network rule blocks them.
- network_taken popup now has controller-level tests on all six OAuth
  controllers (added Threads, YouTube, LinkedInPage, and a new
  InstagramFacebook test file; LinkedInPage/InstagramFacebook also exercise
  variant collapse).
- Wiring tests that creating/deleting a workspace actually calls
  syncWorkspaceQuantity (guards per-seat billing against silent breakage).
- Strengthen TrialLengthTest to assert the configured length reaches
  trial_ends_at; add a same-id network-variant block test.
2026-06-22 09:26:31 -03:00

125 lines
4 KiB
PHP

<?php
declare(strict_types=1);
use App\Enums\SocialAccount\Platform;
use App\Enums\SocialAccount\Status;
use App\Exceptions\SocialAccount\NetworkAlreadyConnectedException;
use App\Models\SocialAccount;
use App\Models\Workspace;
beforeEach(function () {
config()->set('trypost.self_hosted', false);
$this->workspace = Workspace::factory()->create();
});
test('blocks a second account of the same network', function () {
SocialAccount::factory()->create([
'workspace_id' => $this->workspace->id,
'platform' => Platform::Instagram,
'platform_user_id' => 'ig-a',
]);
expect(fn () => SocialAccount::factory()->create([
'workspace_id' => $this->workspace->id,
'platform' => Platform::Instagram,
'platform_user_id' => 'ig-b',
]))->toThrow(NetworkAlreadyConnectedException::class);
});
test('collapses platform variants into one network', function () {
SocialAccount::factory()->create([
'workspace_id' => $this->workspace->id,
'platform' => Platform::LinkedIn,
'platform_user_id' => 'li-profile',
]);
expect(fn () => SocialAccount::factory()->create([
'workspace_id' => $this->workspace->id,
'platform' => Platform::LinkedInPage,
'platform_user_id' => 'li-page',
]))->toThrow(NetworkAlreadyConnectedException::class);
});
test('allows different networks in the same workspace', function () {
SocialAccount::factory()->create([
'workspace_id' => $this->workspace->id,
'platform' => Platform::Instagram,
'platform_user_id' => 'ig-a',
]);
$x = SocialAccount::factory()->create([
'workspace_id' => $this->workspace->id,
'platform' => Platform::X,
'platform_user_id' => 'x-a',
]);
expect($x->exists)->toBeTrue();
});
test('allows the same network in different workspaces', function () {
$other = Workspace::factory()->create();
SocialAccount::factory()->create([
'workspace_id' => $this->workspace->id,
'platform' => Platform::Instagram,
'platform_user_id' => 'ig-a',
]);
$second = SocialAccount::factory()->create([
'workspace_id' => $other->id,
'platform' => Platform::Instagram,
'platform_user_id' => 'ig-a',
]);
expect($second->exists)->toBeTrue();
});
test('reconnecting the same account via updateOrCreate is allowed', function () {
SocialAccount::factory()->create([
'workspace_id' => $this->workspace->id,
'platform' => Platform::Instagram,
'platform_user_id' => 'ig-a',
'username' => 'old',
]);
$this->workspace->socialAccounts()->updateOrCreate(
['platform' => Platform::Instagram->value, 'platform_user_id' => 'ig-a'],
['username' => 'new', 'status' => Status::Connected],
);
expect($this->workspace->socialAccounts()->count())->toBe(1)
->and($this->workspace->socialAccounts()->first()->username)->toBe('new');
});
test('self-hosted mode bypasses the one-per-network rule', function () {
config()->set('trypost.self_hosted', true);
SocialAccount::factory()->create([
'workspace_id' => $this->workspace->id,
'platform' => Platform::Instagram,
'platform_user_id' => 'ig-a',
]);
$second = SocialAccount::factory()->create([
'workspace_id' => $this->workspace->id,
'platform' => Platform::Instagram,
'platform_user_id' => 'ig-b',
]);
expect($second->exists)->toBeTrue();
});
test('blocks a same-id account connected via a different network variant', function () {
SocialAccount::factory()->create([
'workspace_id' => $this->workspace->id,
'platform' => Platform::Instagram,
'platform_user_id' => 'shared-ig-id',
]);
expect(fn () => SocialAccount::factory()->create([
'workspace_id' => $this->workspace->id,
'platform' => Platform::InstagramFacebook,
'platform_user_id' => 'shared-ig-id',
]))->toThrow(NetworkAlreadyConnectedException::class);
});