Three orthogonal fixes that together close the gap where social tokens
were silently aging out without ever being refreshed, then dying at the
provider when the refresh_token also got revoked.
The original failure mode: a user's X token expired because the hourly
proactive-refresh cron's smart `verify()` skip-logic kept saying 'token
still works, no need to refresh', and once the token actually expired,
the cron's WHERE clause excluded it from future runs. By the time anyone
noticed, the refresh_token at X was also gone.
(C) ConnectionVerifier: rename private `refreshTokenIfNeeded` →
public `refreshToken`. Callers that want the smart 'try
access_token first' behavior keep using `verify()`. Callers that
want a proactive refresh (the cron) call `refreshToken` directly.
(B) RefreshExpiringTokens command: drop the
`where('token_expires_at', '>', now())` filter. Already-expired
tokens now get a last-chance refresh attempt before the
refresh_token also dies at the provider. Status filter
(`Connected`) still excludes accounts already marked TokenExpired.
(D) RefreshSocialToken job: switch from `verify()` to
`refreshToken()`, and on `TokenExpiredException` call
`markAsTokenExpired` so the user is notified immediately. The lock
+ transition detection in markAsTokenExpired prevents notification
spam if subsequent cron passes also fail.
Tests:
- 3 new tests for RefreshSocialToken (calls refreshToken not verify,
marks TokenExpired on TokenExpiredException, logs warning on other
errors)
- Updated RefreshExpiringTokens test to assert already-expired tokens
are now dispatched (was previously asserted as 'should NOT')
73 lines
2.4 KiB
PHP
73 lines
2.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use App\Enums\SocialAccount\Platform;
|
|
use App\Enums\SocialAccount\Status;
|
|
use App\Jobs\RefreshSocialToken;
|
|
use App\Models\SocialAccount;
|
|
use App\Models\Workspace;
|
|
use Illuminate\Support\Facades\Queue;
|
|
|
|
test('it dispatches refresh jobs for tokens expiring within 2 hours or already expired', function () {
|
|
Queue::fake();
|
|
|
|
$workspace = Workspace::factory()->create();
|
|
|
|
// Should be refreshed (expires in 1 hour)
|
|
$expiringSoon = SocialAccount::factory()->create([
|
|
'workspace_id' => $workspace->id,
|
|
'platform' => Platform::LinkedIn,
|
|
'status' => Status::Connected,
|
|
'token_expires_at' => now()->addHour(),
|
|
]);
|
|
|
|
// Should NOT be refreshed (expires in 5 hours — outside the proactive window)
|
|
SocialAccount::factory()->create([
|
|
'workspace_id' => $workspace->id,
|
|
'platform' => Platform::Instagram,
|
|
'status' => Status::Connected,
|
|
'token_expires_at' => now()->addHours(5),
|
|
]);
|
|
|
|
// SHOULD be refreshed (already expired — last-chance attempt before the
|
|
// refresh_token also dies at the provider).
|
|
$justExpired = SocialAccount::factory()->create([
|
|
'workspace_id' => $workspace->id,
|
|
'platform' => Platform::TikTok,
|
|
'status' => Status::Connected,
|
|
'token_expires_at' => now()->subHour(),
|
|
]);
|
|
|
|
// Should NOT be refreshed (disconnected)
|
|
SocialAccount::factory()->create([
|
|
'workspace_id' => $workspace->id,
|
|
'platform' => Platform::X,
|
|
'status' => Status::Disconnected,
|
|
'token_expires_at' => now()->addHour(),
|
|
]);
|
|
|
|
// Should NOT be refreshed (already token expired — daily verify handles these)
|
|
SocialAccount::factory()->create([
|
|
'workspace_id' => $workspace->id,
|
|
'platform' => Platform::Pinterest,
|
|
'status' => Status::TokenExpired,
|
|
'token_expires_at' => now()->subHour(),
|
|
]);
|
|
|
|
$this->artisan('social:refresh-expiring-tokens')
|
|
->assertSuccessful();
|
|
|
|
Queue::assertPushed(RefreshSocialToken::class, 2);
|
|
Queue::assertPushed(RefreshSocialToken::class, fn ($job) => $job->account->id === $expiringSoon->id);
|
|
Queue::assertPushed(RefreshSocialToken::class, fn ($job) => $job->account->id === $justExpired->id);
|
|
});
|
|
|
|
test('it dispatches nothing when no tokens are expiring', function () {
|
|
Queue::fake();
|
|
|
|
$this->artisan('social:refresh-expiring-tokens')
|
|
->assertSuccessful();
|
|
|
|
Queue::assertNothingPushed();
|
|
});
|