Skip to content

Commit

Permalink
FCM: Mark invalid FCM tokens as invalid endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
pprkut committed Jun 10, 2024
1 parent 1acf4ae commit 74ec724
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 9 deletions.
11 changes: 9 additions & 2 deletions src/Lunr/Vortex/FCM/FCMBatchResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,15 @@ private function report_endpoint_error(string $endpoint, Response $response): vo
switch ($response->status_code)
{
case 400:
$status = PushNotificationStatus::Error;
$error_message ??= 'Invalid parameter';
if ($error_message == 'The registration token is not a valid FCM registration token')
{
$status = PushNotificationStatus::InvalidEndpoint;
}
else
{
$status = PushNotificationStatus::Error;
$error_message ??= 'Invalid argument';
}
break;
case 401:
$status = PushNotificationStatus::Error;
Expand Down
39 changes: 36 additions & 3 deletions src/Lunr/Vortex/FCM/Tests/FCMBatchResponseBasePushTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function testPushSuccessWithPreconditionFailWithSingle(): void

$context = [
'endpoint' => 'endpoint1',
'error' => 'Invalid parameter',
'error' => 'Invalid argument',
];

$this->logger->expects($this->once())
Expand Down Expand Up @@ -107,7 +107,7 @@ public function testPushSuccessWithPreconditionFailWithMultiple(): void

$context = [
'endpoint' => 'endpoint1',
'error' => 'Invalid parameter',
'error' => 'Invalid argument',
];

$context1 = [ 'endpoint' => 'endpoint2' ] + $context;
Expand Down Expand Up @@ -583,6 +583,39 @@ public function testPushErrorBadRequestError(): void

parent::baseSetUp($this->class);

$this->assertPropertySame('logger', $this->logger);
$this->assertPropertySame('endpoints', $endpoints);
$this->assertPropertyEquals('statuses', [ 'endpoint1' => PushNotificationStatus::InvalidEndpoint ]);
$this->assertPropertySame('responses', $responses);
}

/**
* Test constructor behavior for error of push notification in case of bad request error.
*
* @covers Lunr\Vortex\FCM\FCMBatchResponse::__construct
*/
public function testPushErrorGenericBadRequestError(): void
{
$http_code = 400;
$content = file_get_contents(TEST_STATICS . '/Vortex/fcm/response_generic_error.json');
$endpoints = [ 'endpoint1' ];

$this->response->status_code = $http_code;
$this->response->body = $content;

$responses = [ 'endpoint1' => $this->response ];

$this->logger->expects($this->once())
->method('warning')
->with(
'Dispatching FCM notification failed for endpoint {endpoint}: {error}',
[ 'endpoint' => 'endpoint1', 'error' => 'Invalid Argument' ]
);

$this->class = new FCMBatchResponse($responses, $this->logger, $endpoints);

parent::baseSetUp($this->class);

$this->assertPropertySame('logger', $this->logger);
$this->assertPropertySame('endpoints', $endpoints);
$this->assertPropertyEquals('statuses', [ 'endpoint1' => PushNotificationStatus::Error ]);
Expand All @@ -598,7 +631,7 @@ public function errorDataProvider(): array
{
$data = [];

$data[] = [ 'Invalid parameter', 400, PushNotificationStatus::Error ];
$data[] = [ 'Invalid argument', 400, PushNotificationStatus::Error ];
$data[] = [ 'Error with authentication', 401, PushNotificationStatus::Error ];
$data[] = [ 'Mismatched sender', 403, PushNotificationStatus::InvalidEndpoint ];
$data[] = [ 'Unregistered or missing token', 404, PushNotificationStatus::InvalidEndpoint ];
Expand Down
8 changes: 4 additions & 4 deletions src/Lunr/Vortex/FCM/Tests/FCMDispatcherPushTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public function testPushWhenProjectIdIsNullSingleEndpoint(): void
[ 'Tried to push FCM notification but project id is not provided.' ],
[
'Dispatching FCM notification failed for endpoint {endpoint}: {error}',
[ 'endpoint' => 'endpoint', 'error' => 'Invalid parameter' ]
[ 'endpoint' => 'endpoint', 'error' => 'Invalid argument' ]
]
);

Expand All @@ -179,15 +179,15 @@ public function testPushWhenProjectIdIsNullMultipleEndpoints(): void
[ 'Tried to push FCM notification but project id is not provided.' ],
[
'Dispatching FCM notification failed for endpoint {endpoint}: {error}',
[ 'endpoint' => 'endpoint', 'error' => 'Invalid parameter' ]
[ 'endpoint' => 'endpoint', 'error' => 'Invalid argument' ]
],
[
'Dispatching FCM notification failed for endpoint {endpoint}: {error}',
[ 'endpoint' => 'endpoint1', 'error' => 'Invalid parameter' ]
[ 'endpoint' => 'endpoint1', 'error' => 'Invalid argument' ]
],
[
'Dispatching FCM notification failed for endpoint {endpoint}: {error}',
[ 'endpoint' => 'endpoint2', 'error' => 'Invalid parameter' ]
[ 'endpoint' => 'endpoint2', 'error' => 'Invalid argument' ]
],
);

Expand Down
14 changes: 14 additions & 0 deletions tests/statics/Vortex/fcm/response_generic_error.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"error": {
"code": 400,
"message": "Invalid Argument",
"status": "INVALID_ARGUMENT",
"details": [
{
"@type": "type.googleapis.com/google.firebase.fcm.v1.FcmError",
"errorCode": "INVALID_ARGUMENT"
}
]
}
}

0 comments on commit 74ec724

Please sign in to comment.