Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add test for the new code
Signed-off-by: Joas Schilling <[email protected]>
  • Loading branch information
nickvergessen authored and backportbot[bot] committed Oct 1, 2020
commit 0e66c054cb851bb198e51dafd780d5f70941f566
12 changes: 7 additions & 5 deletions lib/Push.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ protected function sendNotificationsToProxies(): void {
$body = $response->getBody();
$error = \is_string($body) ? $body : ('no reason given (' . $response->getStatusCode() . ')');

$this->log->debug('Could not send notification to push server [{url}]: {error}',[
$this->log->debug('Could not send notification to push server [{url}]: {error}', [
'error' => $error,
'url' => $proxyServer,
'app' => 'notifications',
Expand All @@ -295,10 +295,12 @@ protected function sendNotificationsToProxies(): void {
$body = $response->getBody();
$bodyData = json_decode($body, true);

if (is_array($bodyData) && isset($bodyData['unknown'], $bodyData['failed']) && is_array($bodyData['unknown'])) {
foreach ($bodyData['unknown'] as $unknownDevice) {
$this->printInfo('Deleting device because it is unknown by the push server: ' . $unknownDevice);
$this->deletePushTokenByDeviceIdentifier($unknownDevice);
if (is_array($bodyData) && isset($bodyData['unknown'], $bodyData['failed'])) {
if (is_array($bodyData['unknown'])) {
foreach ($bodyData['unknown'] as $unknownDevice) {
$this->printInfo('Deleting device because it is unknown by the push server: ' . $unknownDevice);
$this->deletePushTokenByDeviceIdentifier($unknownDevice);
}
}

if ($bodyData['failed'] !== 0) {
Expand Down
39 changes: 36 additions & 3 deletions tests/Unit/PushTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ public function dataPushToDeviceSending() {
* @param bool $isDebug
*/
public function testPushToDeviceSending($isDebug) {
$push = $this->getPush(['getDevicesForUser', 'encryptAndSign', 'deletePushToken', 'validateToken']);
$push = $this->getPush(['getDevicesForUser', 'encryptAndSign', 'deletePushToken', 'validateToken', 'deletePushTokenByDeviceIdentifier']);

/** @var INotification|MockObject $notification */
$notification = $this->createMock(INotification::class);
Expand Down Expand Up @@ -447,6 +447,11 @@ public function testPushToDeviceSending($isDebug) {
'token' => 64,
'apptype' => 'other',
],
[
'proxyserver' => 'badrequest-with-devices',
'token' => 128,
'apptype' => 'other',
],
]);

$this->config->expects($this->exactly(2))
Expand Down Expand Up @@ -474,11 +479,11 @@ public function testPushToDeviceSending($isDebug) {
->with($user)
->willReturn($key);

$push->expects($this->exactly(5))
$push->expects($this->exactly(6))
->method('validateToken')
->willReturn(true);

$push->expects($this->exactly(5))
$push->expects($this->exactly(6))
->method('encryptAndSign')
->willReturn(['Payload']);

Expand Down Expand Up @@ -581,6 +586,34 @@ public function testPushToDeviceSending($isDebug) {
])
->willReturn($response3);

/** @var IResponse|MockObject $response1 */
$response4 = $this->createMock(IResponse::class);
$response4->expects($this->once())
->method('getStatusCode')
->willReturn(Http::STATUS_BAD_REQUEST);
$response4->expects($this->once())
->method('getBody')
->willReturn(json_encode([
'failed' => 1,
'unknown' => [
'123456'
]
]));
$e = $this->createMock(ClientException::class);
$e->method('getResponse')
->willReturn($response4);
$client->expects($this->at(4))
->method('post')
->with('badrequest-with-devices/notifications', [
'body' => [
'notifications' => ['["Payload"]'],
],
])
->willThrowException($e);

$push->method('deletePushTokenByDeviceIdentifier')
->with('123456');

$push->pushToDevice(207787, $notification);
}

Expand Down