From e99b5d81f7af73d6977e171063849072b97cf95c Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 9 Jul 2020 15:27:06 +0200 Subject: [PATCH] Don't push without internet connection Signed-off-by: Joas Schilling --- lib/Push.php | 4 +++ tests/Unit/PushTest.php | 55 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/lib/Push.php b/lib/Push.php index 75066370e..c804b6d70 100644 --- a/lib/Push.php +++ b/lib/Push.php @@ -67,6 +67,10 @@ public function __construct(IDBConnection $connection, INotificationManager $not } public function pushToDevice(int $id, INotification $notification): void { + if (!$this->config->getSystemValueBool('has_internet_connection', true)) { + return; + } + $user = $this->userManager->get($notification->getUser()); if (!($user instanceof IUser)) { return; diff --git a/tests/Unit/PushTest.php b/tests/Unit/PushTest.php index 00bb68a47..fd4342294 100644 --- a/tests/Unit/PushTest.php +++ b/tests/Unit/PushTest.php @@ -110,6 +110,26 @@ protected function getPush(array $methods = []) { ); } + public function testPushToDeviceNoInternet() { + $push = $this->getPush(); + + $this->config->expects($this->once()) + ->method('getSystemValueBool') + ->with('has_internet_connection', true) + ->willReturn(false); + $this->keyManager->expects($this->never()) + ->method('getKey'); + $this->clientService->expects($this->never()) + ->method('newClient'); + $this->userManager->expects($this->never()) + ->method('get'); + + /** @var INotification|MockObject$notification */ + $notification = $this->createMock(INotification::class); + + $push->pushToDevice(23, $notification); + } + public function testPushToDeviceInvalidUser() { $push = $this->getPush(); $this->keyManager->expects($this->never()) @@ -117,6 +137,11 @@ public function testPushToDeviceInvalidUser() { $this->clientService->expects($this->never()) ->method('newClient'); + $this->config->expects($this->once()) + ->method('getSystemValueBool') + ->with('has_internet_connection', true) + ->willReturn(true); + /** @var INotification|\PHPUnit_Framework_MockObject_MockObject $notification */ $notification = $this->createMock(INotification::class); $notification->expects($this->once()) @@ -138,6 +163,11 @@ public function testPushToDeviceNoDevices() { $this->clientService->expects($this->never()) ->method('newClient'); + $this->config->expects($this->once()) + ->method('getSystemValueBool') + ->with('has_internet_connection', true) + ->willReturn(true); + /** @var INotification|\PHPUnit_Framework_MockObject_MockObject $notification */ $notification = $this->createMock(INotification::class); $notification->expects($this->exactly(2)) @@ -166,6 +196,11 @@ public function testPushToDeviceNotPrepared() { $this->clientService->expects($this->never()) ->method('newClient'); + $this->config->expects($this->once()) + ->method('getSystemValueBool') + ->with('has_internet_connection', true) + ->willReturn(true); + /** @var INotification|\PHPUnit_Framework_MockObject_MockObject $notification */ $notification = $this->createMock(INotification::class); $notification->expects($this->exactly(3)) @@ -209,6 +244,11 @@ public function testPushToDeviceInvalidToken() { $this->clientService->expects($this->never()) ->method('newClient'); + $this->config->expects($this->once()) + ->method('getSystemValueBool') + ->with('has_internet_connection', true) + ->willReturn(true); + /** @var INotification|\PHPUnit_Framework_MockObject_MockObject $notification */ $notification = $this->createMock(INotification::class); $notification->expects($this->exactly(3)) @@ -270,6 +310,11 @@ public function testPushToDeviceEncryptionError() { $this->clientService->expects($this->never()) ->method('newClient'); + $this->config->expects($this->once()) + ->method('getSystemValueBool') + ->with('has_internet_connection', true) + ->willReturn(true); + /** @var INotification|\PHPUnit_Framework_MockObject_MockObject $notification */ $notification = $this->createMock(INotification::class); $notification->expects($this->exactly(2)) @@ -421,6 +466,11 @@ public function testPushToDeviceSending($isDebug) { ->method('newClient') ->willReturn($client); + $this->config->expects($this->once()) + ->method('getSystemValueBool') + ->with('has_internet_connection', true) + ->willReturn(true); + $e = new \Exception(); $client->expects($this->at(0)) ->method('post') @@ -622,6 +672,11 @@ public function testPushToDeviceTalkNotification(array $deviceTypes, $isTalkNoti ->willReturn($response); } + $this->config->expects($this->once()) + ->method('getSystemValueBool') + ->with('has_internet_connection', true) + ->willReturn(true); + $push->pushToDevice(200718, $notification); }