Fills gaps surfaced in the code review: - 3 new `markAsDisconnected` tests (mirroring the existing TokenExpired trio). Previously the method had zero coverage despite this PR changing its lock key. - 1 test confirming `markAsTokenExpired($msg, notify: false)` skips notification dispatch (used by VerifyWorkspaceConnections batch path). - 1 test confirming `disconnected_at` is preserved when already set (the `?? now()` branch). - 2 end-to-end tests (one per method) that DO NOT fake the queue, so `SendNotification::handle()` runs synchronously. Asserts the Notification row is actually created in the DB with the correct i18n-substituted title/body, that NotificationCreated event is dispatched, and that AccountDisconnected mail is queued. Catches bugs where the i18n placeholder keys (`:platform`, `:account`) silently fail to substitute.
188 lines
6.3 KiB
PHP
188 lines
6.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use App\Enums\Notification\Type;
|
|
use App\Enums\SocialAccount\Status;
|
|
use App\Events\NotificationCreated;
|
|
use App\Jobs\SendNotification;
|
|
use App\Mail\AccountDisconnected;
|
|
use App\Models\Notification;
|
|
use App\Models\SocialAccount;
|
|
use App\Models\User;
|
|
use App\Models\Workspace;
|
|
use Illuminate\Support\Facades\Event;
|
|
use Illuminate\Support\Facades\Mail;
|
|
use Illuminate\Support\Facades\Queue;
|
|
|
|
beforeEach(function () {
|
|
$this->owner = User::factory()->create();
|
|
$this->workspace = Workspace::factory()->create(['user_id' => $this->owner->id]);
|
|
});
|
|
|
|
// ---- markAsTokenExpired ----
|
|
|
|
test('markAsTokenExpired updates status and dispatches notification when transitioning from connected', function () {
|
|
Queue::fake();
|
|
|
|
$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 () {
|
|
Queue::fake();
|
|
|
|
$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 () {
|
|
Queue::fake();
|
|
|
|
$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);
|
|
});
|
|
|
|
test('markAsTokenExpired respects notify=false flag', function () {
|
|
Queue::fake();
|
|
|
|
$account = SocialAccount::factory()->x()->create([
|
|
'workspace_id' => $this->workspace->id,
|
|
'status' => Status::Connected,
|
|
'username' => 'testuser',
|
|
]);
|
|
|
|
$account->markAsTokenExpired('refresh_token rejected', notify: false);
|
|
|
|
expect($account->fresh()->status)->toBe(Status::TokenExpired);
|
|
Queue::assertNotPushed(SendNotification::class);
|
|
});
|
|
|
|
test('markAsTokenExpired preserves existing disconnected_at value', function () {
|
|
Queue::fake();
|
|
|
|
$earlier = now()->subDays(3);
|
|
$account = SocialAccount::factory()->x()->create([
|
|
'workspace_id' => $this->workspace->id,
|
|
'status' => Status::Connected,
|
|
'disconnected_at' => $earlier,
|
|
]);
|
|
|
|
$account->markAsTokenExpired('refresh_token rejected');
|
|
|
|
expect($account->fresh()->disconnected_at->toIso8601String())
|
|
->toBe($earlier->toIso8601String());
|
|
});
|
|
|
|
test('markAsTokenExpired creates notification row with i18n placeholders substituted', function () {
|
|
Event::fake([NotificationCreated::class]);
|
|
Mail::fake();
|
|
|
|
$account = SocialAccount::factory()->x()->create([
|
|
'workspace_id' => $this->workspace->id,
|
|
'status' => Status::Connected,
|
|
'username' => 'testuser',
|
|
]);
|
|
|
|
$account->markAsTokenExpired('refresh_token rejected');
|
|
|
|
$notification = Notification::where('user_id', $this->owner->id)->first();
|
|
|
|
expect($notification)->not->toBeNull();
|
|
expect($notification->title)->toBe('X account needs to be reconnected');
|
|
expect($notification->body)->toBe('@testuser session expired — please reconnect to keep posting');
|
|
expect($notification->type)->toBe(Type::AccountDisconnected);
|
|
expect($notification->data)->toBe(['social_account_id' => $account->id]);
|
|
|
|
Event::assertDispatched(NotificationCreated::class);
|
|
Mail::assertQueued(AccountDisconnected::class);
|
|
});
|
|
|
|
// ---- markAsDisconnected ----
|
|
|
|
test('markAsDisconnected updates status and dispatches notification when transitioning from connected', function () {
|
|
Queue::fake();
|
|
|
|
$account = SocialAccount::factory()->x()->create([
|
|
'workspace_id' => $this->workspace->id,
|
|
'status' => Status::Connected,
|
|
'username' => 'testuser',
|
|
]);
|
|
|
|
$account->markAsDisconnected('manual disconnect');
|
|
|
|
expect($account->fresh()->status)->toBe(Status::Disconnected);
|
|
expect($account->fresh()->error_message)->toBe('manual disconnect');
|
|
expect($account->fresh()->disconnected_at)->not->toBeNull();
|
|
|
|
Queue::assertPushed(SendNotification::class, function ($job) {
|
|
return $job->user->id === $this->owner->id
|
|
&& $job->type === Type::AccountDisconnected;
|
|
});
|
|
});
|
|
|
|
test('markAsDisconnected does not dispatch notification when already disconnected', function () {
|
|
Queue::fake();
|
|
|
|
$account = SocialAccount::factory()->x()->create([
|
|
'workspace_id' => $this->workspace->id,
|
|
'status' => Status::Disconnected,
|
|
'disconnected_at' => now()->subDay(),
|
|
]);
|
|
|
|
$account->markAsDisconnected('another disconnect');
|
|
|
|
Queue::assertNotPushed(SendNotification::class);
|
|
});
|
|
|
|
test('markAsDisconnected creates notification row with i18n placeholders substituted', function () {
|
|
Event::fake([NotificationCreated::class]);
|
|
Mail::fake();
|
|
|
|
$account = SocialAccount::factory()->x()->create([
|
|
'workspace_id' => $this->workspace->id,
|
|
'status' => Status::Connected,
|
|
'username' => 'testuser',
|
|
]);
|
|
|
|
$account->markAsDisconnected('manual disconnect');
|
|
|
|
$notification = Notification::where('user_id', $this->owner->id)->first();
|
|
|
|
expect($notification)->not->toBeNull();
|
|
expect($notification->title)->toBe('X account disconnected');
|
|
expect($notification->body)->toBe('@testuser needs to be reconnected');
|
|
expect($notification->type)->toBe(Type::AccountDisconnected);
|
|
expect($notification->data)->toBe(['social_account_id' => $account->id]);
|
|
|
|
Event::assertDispatched(NotificationCreated::class);
|
|
Mail::assertQueued(AccountDisconnected::class);
|
|
});
|