From 4d2766c97e193823e4398d01860f7e9ef86b1d1e 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 edc995f9b..eadaf3c28 100644 --- a/lib/Push.php +++ b/lib/Push.php @@ -115,6 +115,10 @@ public function flushPayloads(): void { } public function pushToDevice(int $id, INotification $notification, ?OutputInterface $output = null): 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 93100cad9..186c71955 100644 --- a/tests/Unit/PushTest.php +++ b/tests/Unit/PushTest.php @@ -131,6 +131,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()) @@ -138,6 +158,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|MockObject$notification */ $notification = $this->createMock(INotification::class); $notification @@ -159,6 +184,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|MockObject $notification */ $notification = $this->createMock(INotification::class); $notification @@ -187,6 +217,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|MockObject $notification */ $notification = $this->createMock(INotification::class); $notification @@ -226,6 +261,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|MockObject $notification */ $notification = $this->createMock(INotification::class); $notification @@ -286,6 +326,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|MockObject $notification */ $notification = $this->createMock(INotification::class); $notification @@ -440,6 +485,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') @@ -645,6 +695,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); }