Skip to content

Commit

Permalink
JPush: Add set_auth_token to JpushReport
Browse files Browse the repository at this point in the history
Issue: HKGO-1825

Reviewed at https://reviews.lunr.nl/r/994/
  • Loading branch information
brianstoop authored and pprkut committed Jul 7, 2023
1 parent 50d3781 commit 75a0a66
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 7 deletions.
33 changes: 29 additions & 4 deletions src/Lunr/Vortex/JPush/JPushReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ class JPushReport
*/
protected Requests_Session $http;

/**
* Push Notification authentication token.
* @var string|null
*/
protected ?string $auth_token;

/**
* The statuses per endpoint.
* @var array
Expand All @@ -57,9 +63,10 @@ class JPushReport
*/
public function __construct(Requests_Session $http, LoggerInterface $logger)
{
$this->statuses = [];
$this->http = $http;
$this->logger = $logger;
$this->statuses = [];
$this->http = $http;
$this->logger = $logger;
$this->auth_token = NULL;
}

/**
Expand All @@ -70,6 +77,7 @@ public function __destruct()
unset($this->http);
unset($this->logger);
unset($this->statuses);
unset($this->auth_token);
}

/**
Expand All @@ -87,9 +95,14 @@ public function get_report($message_id, $endpoints): void
'registration_ids' => $endpoints,
];

$headers = [
'Content-Type' => 'application/json',
'Authorization' => 'Basic ' . $this->auth_token,
];

try
{
$response = $this->http->post(static::JPUSH_REPORT_URL, [], json_encode($payload), []);
$response = $this->http->post(static::JPUSH_REPORT_URL, $headers, json_encode($payload), []);

Check failure on line 105 in src/Lunr/Vortex/JPush/JPushReport.php

View workflow job for this annotation

GitHub Actions / PHPStan

Unsafe access to private constant Lunr\Vortex\JPush\JPushReport::JPUSH_REPORT_URL through static::.
$response->throw_for_status();
}
catch (Requests_Exception_HTTP $e)
Expand Down Expand Up @@ -235,6 +248,18 @@ private function report_endpoint_error(string $endpoint, string $error_code): vo
$this->statuses[$endpoint] = $status;
}

/**
* Set the the auth token for the http headers.
*
* @param string $auth_token The auth token for the JPush push notifications
*
* @return void
*/
public function set_auth_token(string $auth_token): void
{
$this->auth_token = $auth_token;
}

}

?>
20 changes: 20 additions & 0 deletions src/Lunr/Vortex/JPush/Tests/JPushReportBaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,26 @@ public function testStatusesIsEmptyArray(): void
$this->assertPropertyEquals('statuses', []);
}

/**
* Test auth_token is initialized as NULL.
*/
public function testAuthTokenIsInitializedAsNull(): void
{
$this->assertNull($this->get_reflection_property_value('auth_token'));
}

/**
* Test the set_auth_token() sets auth token.
*
* @covers \Lunr\Vortex\JPush\JPushReport::set_auth_token
*/
public function testSetAuthTokenSetsAuthToken(): void
{
$this->class->set_auth_token('auth_token_24412');

$this->assertPropertySame('auth_token', 'auth_token_24412');
}

}

?>
27 changes: 24 additions & 3 deletions src/Lunr/Vortex/JPush/Tests/JPushReportGetReportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,18 @@ public function testGetReportReturnsWhenHttpRequestFails(): void
{
$this->mock_method([ $this->class, 'report_error' ], function ($response) { echo $response->status_code; });

$this->set_reflection_property_value('auth_token', 'auth_token_24412');

$headers = [
'Content-Type' => 'application/json',
'Authorization' => 'Basic auth_token_24412',
];

$this->response->status_code = 400;

$this->http->expects($this->once())
->method('post')
->with('https://report.jpush.cn/v3/status/message', [], '{"msg_id":1453658564165,"registration_ids":["endpoint1"]}', [])
->with('https://report.jpush.cn/v3/status/message', $headers, '{"msg_id":1453658564165,"registration_ids":["endpoint1"]}', [])
->willReturn($this->response);

$this->response->expects($this->once())
Expand All @@ -58,9 +65,16 @@ public function testGetReportReturnsWhenHttpRequestFails(): void
*/
public function testGetReportWithCurlErrors(): void
{
$this->set_reflection_property_value('auth_token', 'auth_token_24412');

$headers = [
'Content-Type' => 'application/json',
'Authorization' => 'Basic auth_token_24412',
];

$this->http->expects($this->once())
->method('post')
->with('https://report.jpush.cn/v3/status/message', [], '{"msg_id":1453658564165,"registration_ids":["endpoint1"]}', [])
->with('https://report.jpush.cn/v3/status/message', $headers, '{"msg_id":1453658564165,"registration_ids":["endpoint1"]}', [])
->willReturn($this->response);

$this->response->expects($this->once())
Expand Down Expand Up @@ -93,9 +107,16 @@ public function testGetReportWillFetchUpstreamMixedErrorSuccess(): void
$this->response->success = TRUE;
$this->response->body = $report_content;

$this->set_reflection_property_value('auth_token', 'auth_token_24412');

$headers = [
'Content-Type' => 'application/json',
'Authorization' => 'Basic auth_token_24412',
];

$this->http->expects($this->once())
->method('post')
->with('https://report.jpush.cn/v3/status/message', [], '{"msg_id":1453658564165,"registration_ids":["endpoint1","endpoint2","endpoint3","endpoint4","endpoint5","endpoint6","endpoint7"]}', [])
->with('https://report.jpush.cn/v3/status/message', $headers, '{"msg_id":1453658564165,"registration_ids":["endpoint1","endpoint2","endpoint3","endpoint4","endpoint5","endpoint6","endpoint7"]}', [])

Check warning on line 119 in src/Lunr/Vortex/JPush/Tests/JPushReportGetReportTest.php

View workflow job for this annotation

GitHub Actions / PHPCS

Line exceeds 150 characters; contains 217 characters
->willReturn($this->response);

$this->response->expects($this->once())
Expand Down

0 comments on commit 75a0a66

Please sign in to comment.