Repository navigation
test: cover remote pings and restore full coverage #1162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
xHeaven
merged 1 commit into
dev-null-sh:production
from
xHeaven:fix/production-ping-coverage
Oct 26, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
105 changes: 105 additions & 0 deletions
105
tests/Feature/Controllers/Api/ConversationPingControllerTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use App\Enums\OrgEvent; | ||
| use App\Enums\RemoteStatus; | ||
| use App\Models\ConversationParticipant; | ||
| use App\Models\Message; | ||
| use App\Models\User; | ||
|
|
||
| beforeEach(function (): void { | ||
| config(['remotes.requests' => true, 'remotes.everyone' => true]); | ||
| $this->user = User::factory()->withRemotes()->create(); | ||
| $this->keyPair = apiKeyPairFor($this->user); | ||
| }); | ||
|
|
||
| it('records a ping, brings both participants out of the stash and marks it read only for its sender', function (): void { | ||
| $partner = User::factory()->withRemotes()->create(); | ||
| $previous = sendChatMessage($partner, $this->user); | ||
| $conversation = $previous->conversation; | ||
| ConversationParticipant::query()->where('conversation_id', $conversation->id)->update(['archived_at' => now()]); | ||
| $this->travel(10)->seconds(); | ||
|
|
||
| $response = signedApiRequest($this->keyPair, 'POST', route('api-conversation-ping.store', $partner))->assertCreated(); | ||
|
|
||
| $ping = Message::query()->latest('id')->firstOrFail(); | ||
| $response->assertJsonPath('data.id', $ping->id) | ||
| ->assertJsonPath('data.user_id', $this->user->id) | ||
| ->assertJsonPath('data.body', '') | ||
| ->assertJsonPath('data.event', 'ping'); | ||
| expect($ping->event)->toBe(OrgEvent::Ping) | ||
| ->and($ping->conversation_id)->toBe($conversation->id) | ||
| ->and($ping->client_id)->toBeUuid() | ||
| ->and($conversation->fresh()->last_message_id)->toBe($ping->id); | ||
| $this->assertDatabaseCount('messages', 2); | ||
| $this->assertDatabaseHas('conversation_participants', ['conversation_id' => $conversation->id, 'user_id' => $this->user->id, 'read_through_id' => $ping->id, 'read_at' => now(), 'archived_at' => null, 'last_message_at' => now()]); | ||
| $this->assertDatabaseHas('conversation_participants', ['conversation_id' => $conversation->id, 'user_id' => $partner->id, 'read_through_id' => $previous->id, 'archived_at' => null, 'last_message_at' => now()]); | ||
| }); | ||
|
|
||
| it('requires a conversation before pinging', function (): void { | ||
| $partner = User::factory()->withRemotes()->create(); | ||
|
|
||
| signedApiRequest($this->keyPair, 'POST', route('api-conversation-ping.store', $partner))->assertForbidden() | ||
| ->assertJsonPath('message', "Say hi to {$partner->mention()} before pinging them."); | ||
|
|
||
| $this->assertDatabaseCount('conversations', 0); | ||
| $this->assertDatabaseCount('messages', 0); | ||
| }); | ||
|
|
||
| it('hides a ping target when either person blocks the other', function (bool $senderBlocks): void { | ||
| $partner = User::factory()->withRemotes()->create(); | ||
| sendChatMessage($partner, $this->user); | ||
| $blocker = $senderBlocks ? $this->user : $partner; | ||
| $blocked = $senderBlocks ? $partner : $this->user; | ||
| $blocker->blocking()->attach($blocked); | ||
|
|
||
| signedApiRequest($this->keyPair, 'POST', route('api-conversation-ping.store', $partner))->assertNotFound(); | ||
|
|
||
| $this->assertDatabaseCount('messages', 1); | ||
| })->with([true, false]); | ||
|
|
||
| it('rejects pings while the sender has Remotes disabled', function (): void { | ||
| $partner = User::factory()->withRemotes()->create(); | ||
| sendChatMessage($partner, $this->user); | ||
| $this->user->update(['remotes_enabled' => false]); | ||
|
|
||
| signedApiRequest($this->keyPair, 'POST', route('api-conversation-ping.store', $partner))->assertNotFound(); | ||
|
|
||
| $this->assertDatabaseCount('messages', 1); | ||
| }); | ||
|
|
||
| it('rejects pings while the partner has Remotes disabled', function (): void { | ||
| $partner = User::factory()->withRemotes()->create(); | ||
| sendChatMessage($partner, $this->user); | ||
| $partner->update(['remotes_enabled' => false]); | ||
|
|
||
| signedApiRequest($this->keyPair, 'POST', route('api-conversation-ping.store', $partner))->assertForbidden() | ||
| ->assertJsonPath('message', "You can't ping {$partner->mention()} right now."); | ||
|
|
||
| $this->assertDatabaseCount('messages', 1); | ||
| }); | ||
|
|
||
| it('does not ping a request before it is merged or after it is dropped', function (string $side, RemoteStatus $status): void { | ||
| $partner = User::factory()->withRemotes()->create(); | ||
| $message = sendChatMessage($partner, $this->user); | ||
| ConversationParticipant::query()->where('conversation_id', $message->conversation_id) | ||
| ->where('user_id', $side === 'sender' ? $this->user->id : $partner->id)->update(['status' => $status]); | ||
|
|
||
| signedApiRequest($this->keyPair, 'POST', route('api-conversation-ping.store', $partner))->assertForbidden() | ||
| ->assertJsonPath('message', "You can't ping {$partner->mention()} right now."); | ||
|
|
||
| $this->assertDatabaseCount('messages', 1); | ||
| })->with([ | ||
| 'incoming request' => ['sender', RemoteStatus::Fetched], | ||
| 'outgoing request' => ['partner', RemoteStatus::Fetched], | ||
| 'dropped request' => ['partner', RemoteStatus::Dropped], | ||
| ]); | ||
|
|
||
| it('returns 401 for an unsigned ping', function (): void { | ||
| $partner = User::factory()->withRemotes()->create(); | ||
|
|
||
| $this->postJson(route('api-conversation-ping.store', $partner))->assertUnauthorized(); | ||
|
|
||
| $this->assertDatabaseCount('messages', 0); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use App\Enums\OrgEvent; | ||
| use App\Models\Conversation; | ||
| use App\Models\Message; | ||
| use App\Models\User; | ||
|
|
||
| beforeEach(function (): void { | ||
| $this->user = User::factory()->withRemotes()->create(); | ||
| $this->keyPair = apiKeyPairFor($this->user); | ||
| }); | ||
|
|
||
| it('records a ping from a maintainer or contributor with its sender', function (bool $contributor): void { | ||
| [$member] = followersOf($this->user, 1); | ||
| $org = createOrg($this->user, [$member]); | ||
| if ($contributor) { | ||
| $this->user = $member; | ||
| $this->keyPair = apiKeyPairFor($this->user); | ||
| } | ||
|
|
||
| $response = signedApiRequest($this->keyPair, 'POST', route('api-org-ping.store', $org))->assertCreated(); | ||
|
|
||
| $ping = Message::query()->latest('id')->firstOrFail(); | ||
| $response->assertJsonPath('data.id', $ping->id) | ||
| ->assertJsonPath('data.event', 'ping') | ||
| ->assertJsonPath('data.body', '') | ||
| ->assertJsonPath('data.user.username', $this->user->username); | ||
| expect($ping->event)->toBe(OrgEvent::Ping) | ||
| ->and($ping->conversation_id)->toBe($org) | ||
| ->and(Conversation::query()->findOrFail($org)->last_message_id)->toBe($ping->id); | ||
| $this->assertDatabaseCount('messages', 2); | ||
| })->with([false, true]); | ||
|
|
||
| it('hides an org from a nonmember trying to ping it', function (): void { | ||
| $maintainer = User::factory()->withRemotes()->create(); | ||
| [$member] = followersOf($maintainer, 1); | ||
| $org = createOrg($maintainer, [$member]); | ||
|
|
||
| signedApiRequest($this->keyPair, 'POST', route('api-org-ping.store', $org))->assertNotFound(); | ||
|
|
||
| $this->assertDatabaseCount('messages', 1); | ||
| }); | ||
|
|
||
| it('rejects pings from a member with Remotes disabled', function (): void { | ||
| [$member] = followersOf($this->user, 1); | ||
| $org = createOrg($this->user, [$member]); | ||
| $this->user->update(['remotes_enabled' => false]); | ||
|
|
||
| signedApiRequest($this->keyPair, 'POST', route('api-org-ping.store', $org))->assertNotFound(); | ||
|
|
||
| $this->assertDatabaseCount('messages', 1); | ||
| }); | ||
|
|
||
| it('returns 401 for an unsigned org ping', function (): void { | ||
| [$member] = followersOf($this->user, 1); | ||
| $org = createOrg($this->user, [$member]); | ||
|
|
||
| $this->postJson(route('api-org-ping.store', $org))->assertUnauthorized(); | ||
|
|
||
| $this->assertDatabaseCount('messages', 1); | ||
| }); |
127 changes: 127 additions & 0 deletions
127
tests/Feature/Controllers/ConversationPingControllerTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use App\Enums\OrgEvent; | ||
| use App\Enums\RemoteStatus; | ||
| use App\Models\ConversationParticipant; | ||
| use App\Models\Message; | ||
| use App\Models\User; | ||
|
|
||
| beforeEach(function (): void { | ||
| config(['remotes.requests' => true, 'remotes.everyone' => true]); | ||
| $this->user = User::factory()->withRemotes()->create(); | ||
| }); | ||
|
|
||
| it('records a ping, brings both participants out of the stash and marks it read only for its sender', function (): void { | ||
| $partner = User::factory()->withRemotes()->create(); | ||
| $previous = sendChatMessage($partner, $this->user); | ||
| $conversation = $previous->conversation; | ||
| ConversationParticipant::query()->where('conversation_id', $conversation->id)->update(['archived_at' => now()]); | ||
| $this->travel(10)->seconds(); | ||
|
|
||
| $response = $this->actingAs($this->user)->postJson(route('conversation-ping.store', $partner))->assertCreated(); | ||
|
|
||
| $ping = Message::query()->latest('id')->firstOrFail(); | ||
| $response->assertJsonPath('message.id', $ping->id) | ||
| ->assertJsonPath('message.user_id', $this->user->id) | ||
| ->assertJsonPath('message.body', '') | ||
| ->assertJsonPath('message.event', 'ping'); | ||
| expect($ping->event)->toBe(OrgEvent::Ping) | ||
| ->and($ping->conversation_id)->toBe($conversation->id) | ||
| ->and($ping->client_id)->toBeUuid() | ||
| ->and($conversation->fresh()->last_message_id)->toBe($ping->id); | ||
| $this->assertDatabaseCount('messages', 2); | ||
| $this->assertDatabaseHas('conversation_participants', ['conversation_id' => $conversation->id, 'user_id' => $this->user->id, 'read_through_id' => $ping->id, 'read_at' => now(), 'archived_at' => null, 'last_message_at' => now()]); | ||
| $this->assertDatabaseHas('conversation_participants', ['conversation_id' => $conversation->id, 'user_id' => $partner->id, 'read_through_id' => $previous->id, 'archived_at' => null, 'last_message_at' => now()]); | ||
| }); | ||
|
|
||
| it('requires a conversation before pinging', function (): void { | ||
| $partner = User::factory()->withRemotes()->create(); | ||
|
|
||
| $this->actingAs($this->user)->postJson(route('conversation-ping.store', $partner))->assertForbidden() | ||
| ->assertJsonPath('message', "Say hi to {$partner->mention()} before pinging them."); | ||
|
|
||
| $this->assertDatabaseCount('conversations', 0); | ||
| $this->assertDatabaseCount('messages', 0); | ||
| }); | ||
|
|
||
| it('hides a ping target when either person blocks the other', function (bool $senderBlocks): void { | ||
| $partner = User::factory()->withRemotes()->create(); | ||
| sendChatMessage($partner, $this->user); | ||
| $blocker = $senderBlocks ? $this->user : $partner; | ||
| $blocked = $senderBlocks ? $partner : $this->user; | ||
| $blocker->blocking()->attach($blocked); | ||
|
|
||
| $this->actingAs($this->user)->postJson(route('conversation-ping.store', $partner))->assertNotFound(); | ||
|
|
||
| $this->assertDatabaseCount('messages', 1); | ||
| })->with([true, false]); | ||
|
|
||
| it('rejects pings while the sender has Remotes disabled', function (): void { | ||
| $partner = User::factory()->withRemotes()->create(); | ||
| sendChatMessage($partner, $this->user); | ||
| $this->user->update(['remotes_enabled' => false]); | ||
|
|
||
| $this->actingAs($this->user)->postJson(route('conversation-ping.store', $partner))->assertNotFound(); | ||
|
|
||
| $this->assertDatabaseCount('messages', 1); | ||
| }); | ||
|
|
||
| it('rejects pings while the partner has Remotes disabled', function (): void { | ||
| $partner = User::factory()->withRemotes()->create(); | ||
| sendChatMessage($partner, $this->user); | ||
| $partner->update(['remotes_enabled' => false]); | ||
|
|
||
| $this->actingAs($this->user)->postJson(route('conversation-ping.store', $partner))->assertForbidden() | ||
| ->assertJsonPath('message', "You can't ping {$partner->mention()} right now."); | ||
|
|
||
| $this->assertDatabaseCount('messages', 1); | ||
| }); | ||
|
|
||
| it('does not ping a request before it is merged or after it is dropped', function (string $side, RemoteStatus $status): void { | ||
| $partner = User::factory()->withRemotes()->create(); | ||
| $message = sendChatMessage($partner, $this->user); | ||
| ConversationParticipant::query()->where('conversation_id', $message->conversation_id) | ||
| ->where('user_id', $side === 'sender' ? $this->user->id : $partner->id)->update(['status' => $status]); | ||
|
|
||
| $this->actingAs($this->user)->postJson(route('conversation-ping.store', $partner))->assertForbidden() | ||
| ->assertJsonPath('message', "You can't ping {$partner->mention()} right now."); | ||
|
|
||
| $this->assertDatabaseCount('messages', 1); | ||
| })->with([ | ||
| 'incoming request' => ['sender', RemoteStatus::Fetched], | ||
| 'outgoing request' => ['partner', RemoteStatus::Fetched], | ||
| 'dropped request' => ['partner', RemoteStatus::Dropped], | ||
| ]); | ||
|
|
||
| it('returns 401 for a guest ping', function (): void { | ||
| $partner = User::factory()->withRemotes()->create(); | ||
|
|
||
| $this->postJson(route('conversation-ping.store', $partner))->assertUnauthorized(); | ||
|
|
||
| $this->assertDatabaseCount('messages', 0); | ||
| }); | ||
|
|
||
| it('limits a sender to three pings a minute across web and API remotes', function (): void { | ||
| $partner = User::factory()->withRemotes()->create(); | ||
| sendChatMessage($partner, $this->user); | ||
| $keyPair = apiKeyPairFor($this->user); | ||
| [$member] = followersOf($this->user, 1); | ||
| $org = createOrg($this->user, [$member]); | ||
|
|
||
| $this->actingAs($this->user)->postJson(route('conversation-ping.store', $partner))->assertCreated(); | ||
| signedApiRequest($keyPair, 'POST', route('api-conversation-ping.store', $partner))->assertCreated(); | ||
| $this->postJson(route('org-ping.store', $org))->assertCreated(); | ||
| signedApiRequest($keyPair, 'POST', route('api-org-ping.store', $org))->assertTooManyRequests() | ||
| ->assertJsonPath('message', 'Three pings a minute, tops. Try again in a bit.') | ||
| ->assertHeader('Retry-After', '60'); | ||
|
|
||
| expect(Message::query()->where('event', OrgEvent::Ping)->count())->toBe(3); | ||
|
|
||
| $this->travel(61)->seconds(); | ||
|
|
||
| signedApiRequest($keyPair, 'POST', route('api-org-ping.store', $org))->assertCreated(); | ||
|
|
||
| expect(Message::query()->where('event', OrgEvent::Ping)->count())->toBe(4); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use App\Enums\OrgEvent; | ||
| use App\Jobs\SendRemotePush; | ||
| use App\Models\Conversation; | ||
| use App\Models\Message; | ||
| use App\Models\PushSubscription; | ||
| use App\Models\User; | ||
| use Illuminate\Support\Facades\Queue; | ||
| use Minishlink\WebPush\VAPID; | ||
|
|
||
| beforeEach(function (): void { | ||
| $this->user = User::factory()->withRemotes()->create(); | ||
| }); | ||
|
|
||
| it('records a ping from a maintainer or contributor with its sender', function (bool $contributor): void { | ||
| [$member] = followersOf($this->user, 1); | ||
| $org = createOrg($this->user, [$member]); | ||
| if ($contributor) { | ||
| $this->user = $member; | ||
| } | ||
|
|
||
| $response = $this->actingAs($this->user)->postJson(route('org-ping.store', $org))->assertCreated(); | ||
|
|
||
| $ping = Message::query()->latest('id')->firstOrFail(); | ||
| $response->assertJsonPath('message.id', $ping->id) | ||
| ->assertJsonPath('message.event', 'ping') | ||
| ->assertJsonPath('message.body', '') | ||
| ->assertJsonPath('message.user.username', $this->user->username); | ||
| expect($ping->event)->toBe(OrgEvent::Ping) | ||
| ->and($ping->conversation_id)->toBe($org) | ||
| ->and(Conversation::query()->findOrFail($org)->last_message_id)->toBe($ping->id); | ||
| $this->assertDatabaseCount('messages', 2); | ||
| })->with([false, true]); | ||
|
|
||
| it('hides an org from a nonmember trying to ping it', function (): void { | ||
| $maintainer = User::factory()->withRemotes()->create(); | ||
| [$member] = followersOf($maintainer, 1); | ||
| $org = createOrg($maintainer, [$member]); | ||
|
|
||
| $this->actingAs($this->user)->postJson(route('org-ping.store', $org))->assertNotFound(); | ||
|
|
||
| $this->assertDatabaseCount('messages', 1); | ||
| }); | ||
|
|
||
| it('rejects pings from a member with Remotes disabled', function (): void { | ||
| [$member] = followersOf($this->user, 1); | ||
| $org = createOrg($this->user, [$member]); | ||
| $this->user->update(['remotes_enabled' => false]); | ||
|
|
||
| $this->actingAs($this->user)->postJson(route('org-ping.store', $org))->assertNotFound(); | ||
|
|
||
| $this->assertDatabaseCount('messages', 1); | ||
| }); | ||
|
|
||
| it('returns 401 for a guest org ping', function (): void { | ||
| [$member] = followersOf($this->user, 1); | ||
| $org = createOrg($this->user, [$member]); | ||
| auth()->logout(); | ||
|
|
||
| $this->postJson(route('org-ping.store', $org))->assertUnauthorized(); | ||
|
|
||
| $this->assertDatabaseCount('messages', 1); | ||
| }); | ||
|
|
||
| it('queues a delayed push for each subscribed member except the sender', function (): void { | ||
| [$first, $second, $unsubscribed] = followersOf($this->user, 3); | ||
| $org = createOrg($this->user, [$first, $second, $unsubscribed]); | ||
| $outsider = User::factory()->withRemotes()->create(); | ||
| foreach ([$this->user, $first, $second, $outsider] as $user) { | ||
| PushSubscription::factory()->create(['user_id' => $user->id]); | ||
| } | ||
|
|
||
| $keys = VAPID::createVapidKeys(); | ||
| config(['services.web_push.public_key' => $keys['publicKey'], 'services.web_push.private_key' => $keys['privateKey']]); | ||
| Queue::fake([SendRemotePush::class]); | ||
|
|
||
| $response = $this->actingAs($this->user)->postJson(route('org-ping.store', $org))->assertCreated(); | ||
|
|
||
| Queue::assertPushed(SendRemotePush::class, 2); | ||
| foreach ([$first, $second] as $recipient) { | ||
| Queue::assertPushed(SendRemotePush::class, fn (SendRemotePush $job): bool => $job->messageId === $response->json('message.id') | ||
| && $job->userId === $recipient->id && $job->afterCommit === true && $job->delay->equalTo(now()->addSeconds(10))); | ||
| } | ||
| }); | ||
|
|
||
| it('does not queue a ping push when either web push key is missing', function (string $missingKey): void { | ||
| [$member] = followersOf($this->user, 1); | ||
| $org = createOrg($this->user, [$member]); | ||
| PushSubscription::factory()->create(['user_id' => $member->id]); | ||
| $keys = VAPID::createVapidKeys(); | ||
| config(['services.web_push.public_key' => $keys['publicKey'], 'services.web_push.private_key' => $keys['privateKey'], 'services.web_push.'.$missingKey => null]); | ||
| Queue::fake([SendRemotePush::class]); | ||
|
|
||
| $this->actingAs($this->user)->postJson(route('org-ping.store', $org))->assertCreated(); | ||
|
|
||
| Queue::assertNothingPushed(); | ||
| })->with(['public_key', 'private_key']); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔎 Supported by static analysis
Script executed:
Repository: dev-null-sh/dev-null.sh
Length of output: 39845
Script executed:
Repository: dev-null-sh/dev-null.sh
Length of output: 5360
Web query:
Laravel framework v13.38.0 ThrottleRequests Retry-After RateLimiter availableIn sourceResult:
Avoid asserting an exact
Retry-Aftervalue.The ping routes use Laravel’s
throttle:pingsmiddleware. Laravel derivesRetry-Afterfrom the remaining limiter time. The fourth request can therefore return a value below60after elapsed time. Freeze the clock or assert a valid remaining-time range.