trypost/tests/Feature/SocialAccountModelTest.php
Paulo Castellano 620d23187e fix(social): handle TokenExpired status fail-fast and notify user
Three related fixes for the failure mode where a scheduled post errors out
as 'An unknown X error occurred.' when a social account's refresh_token
was already invalidated by the provider:

1. **PublishToSocialPlatform**: fail-fast when account status is
   `TokenExpired`. Previously the job tried to publish, the publisher
   internally tried to refresh, the provider rejected the rotated
   refresh_token, and the failure surfaced as a generic 'unknown' error
   instead of a clear 'reconnect your account' signal.

2. **XPublisher::refreshToken**: when the OAuth endpoint rejects the
   refresh_token (typically because it was rotated/revoked at X), log the
   raw response and throw `TokenExpiredException` instead of falling
   through to `XPublishException::fromApiResponse` which expects the
   tweet-API response shape (`type`/`title`/`detail`) and treats
   OAuth-style responses (`error`/`error_description`) as 'Unknown'.

3. **SocialAccount::markAsTokenExpired**: dispatch an in-app + email
   notification (`Type::AccountDisconnected`) when an account
   transitions from `Connected` → `TokenExpired`, mirroring the
   existing pattern in `markAsDisconnected`. Wrapped in a lock to
   prevent duplicate notifications on concurrent transitions. Accepts an
   optional `notify: false` so the batch verifier
   (`VerifyWorkspaceConnections`) can suppress per-account
   notifications and rely on its summary email.
2026-05-12 18:46:06 -03:00

61 lines
2 KiB
PHP

<?php
declare(strict_types=1);
use App\Enums\Notification\Type;
use App\Enums\SocialAccount\Status;
use App\Jobs\SendNotification;
use App\Models\SocialAccount;
use App\Models\User;
use App\Models\Workspace;
use Illuminate\Support\Facades\Queue;
beforeEach(function () {
Queue::fake();
$this->owner = User::factory()->create();
$this->workspace = Workspace::factory()->create(['user_id' => $this->owner->id]);
});
test('markAsTokenExpired updates status and dispatches notification when transitioning from connected', function () {
$account = SocialAccount::factory()->x()->create([
'workspace_id' => $this->workspace->id,
'status' => Status::Connected,
'username' => 'testuser',
]);
$account->markAsTokenExpired('refresh_token rejected');
expect($account->fresh()->status)->toBe(Status::TokenExpired);
expect($account->fresh()->error_message)->toBe('refresh_token rejected');
Queue::assertPushed(SendNotification::class, function ($job) {
return $job->user->id === $this->owner->id
&& $job->type === Type::AccountDisconnected
&& str_contains($job->title, 'needs to be reconnected');
});
});
test('markAsTokenExpired does not dispatch notification when already token expired', function () {
$account = SocialAccount::factory()->x()->create([
'workspace_id' => $this->workspace->id,
'status' => Status::TokenExpired,
'disconnected_at' => now()->subDay(),
]);
$account->markAsTokenExpired('another failure');
Queue::assertNotPushed(SendNotification::class);
});
test('markAsTokenExpired does not dispatch notification when account is disconnected', function () {
$account = SocialAccount::factory()->x()->create([
'workspace_id' => $this->workspace->id,
'status' => Status::Disconnected,
'disconnected_at' => now()->subDay(),
]);
$account->markAsTokenExpired('refresh_token rejected after disconnect');
Queue::assertNotPushed(SendNotification::class);
});