From 75a0a6692807f4e425e070770e399c2dbdd296a3 Mon Sep 17 00:00:00 2001 From: Brian Stoop Date: Fri, 7 Jul 2023 11:16:11 +0200 Subject: [PATCH] JPush: Add set_auth_token to JpushReport Issue: HKGO-1825 Reviewed at https://reviews.lunr.nl/r/994/ --- src/Lunr/Vortex/JPush/JPushReport.php | 33 ++++++++++++++++--- .../JPush/Tests/JPushReportBaseTest.php | 20 +++++++++++ .../JPush/Tests/JPushReportGetReportTest.php | 27 +++++++++++++-- 3 files changed, 73 insertions(+), 7 deletions(-) diff --git a/src/Lunr/Vortex/JPush/JPushReport.php b/src/Lunr/Vortex/JPush/JPushReport.php index ef8af1c..0271790 100644 --- a/src/Lunr/Vortex/JPush/JPushReport.php +++ b/src/Lunr/Vortex/JPush/JPushReport.php @@ -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 @@ -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; } /** @@ -70,6 +77,7 @@ public function __destruct() unset($this->http); unset($this->logger); unset($this->statuses); + unset($this->auth_token); } /** @@ -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), []); $response->throw_for_status(); } catch (Requests_Exception_HTTP $e) @@ -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; + } + } ?> diff --git a/src/Lunr/Vortex/JPush/Tests/JPushReportBaseTest.php b/src/Lunr/Vortex/JPush/Tests/JPushReportBaseTest.php index 72f429d..53b586d 100644 --- a/src/Lunr/Vortex/JPush/Tests/JPushReportBaseTest.php +++ b/src/Lunr/Vortex/JPush/Tests/JPushReportBaseTest.php @@ -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'); + } + } ?> diff --git a/src/Lunr/Vortex/JPush/Tests/JPushReportGetReportTest.php b/src/Lunr/Vortex/JPush/Tests/JPushReportGetReportTest.php index 6cc50a1..3e5d6db 100644 --- a/src/Lunr/Vortex/JPush/Tests/JPushReportGetReportTest.php +++ b/src/Lunr/Vortex/JPush/Tests/JPushReportGetReportTest.php @@ -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()) @@ -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()) @@ -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"]}', []) ->willReturn($this->response); $this->response->expects($this->once())