summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorJoas Schilling <coding@schilljs.com>2021-10-20 10:29:45 +0200
committerJoas Schilling <coding@schilljs.com>2021-10-23 00:54:50 +0200
commitb578a1e8b56f6b3ecf7dee837af6bd8265f9c0b0 (patch)
treee483d50ca038882c3521782aff6574eb0bda0f31 /tests
parent1895a6dc573cdb46cfa1987e25d45781623fdb7d (diff)
downloadnextcloud-server-b578a1e8b56f6b3ecf7dee837af6bd8265f9c0b0.tar.gz
nextcloud-server-b578a1e8b56f6b3ecf7dee837af6bd8265f9c0b0.zip
Fair use of push notifications
We want to keep offering our push notification service for free, but large users overload our infrastructure. For this reason we have to rate-limit the use of push notifications. If you need this feature, consider setting up your own push server or using Nextcloud Enterprise. Signed-off-by: Joas Schilling <coding@schilljs.com>
Diffstat (limited to 'tests')
-rw-r--r--tests/lib/Notification/ManagerTest.php86
-rw-r--r--tests/lib/Support/Subscription/RegistryTest.php9
2 files changed, 86 insertions, 9 deletions
diff --git a/tests/lib/Notification/ManagerTest.php b/tests/lib/Notification/ManagerTest.php
index b1201d31c42..400ae3a53ef 100644
--- a/tests/lib/Notification/ManagerTest.php
+++ b/tests/lib/Notification/ManagerTest.php
@@ -1,4 +1,6 @@
<?php
+
+declare(strict_types=1);
/**
* @author Joas Schilling <nickvergessen@owncloud.com>
*
@@ -25,11 +27,16 @@ use OC\AppFramework\Bootstrap\Coordinator;
use OC\AppFramework\Bootstrap\RegistrationContext;
use OC\AppFramework\Bootstrap\ServiceRegistration;
use OC\Notification\Manager;
-use OCP\ILogger;
+use OCP\AppFramework\Utility\ITimeFactory;
+use OCP\ICache;
+use OCP\ICacheFactory;
+use OCP\IUserManager;
use OCP\Notification\IManager;
use OCP\Notification\INotification;
use OCP\RichObjectStrings\IValidator;
+use OCP\Support\Subscription\IRegistry;
use PHPUnit\Framework\MockObject\MockObject;
+use Psr\Log\LoggerInterface;
use Test\TestCase;
class ManagerTest extends TestCase {
@@ -38,7 +45,17 @@ class ManagerTest extends TestCase {
/** @var IValidator|MockObject */
protected $validator;
- /** @var ILogger|MockObject */
+ /** @var IUserManager|MockObject */
+ protected $userManager;
+ /** @var ICacheFactory|MockObject */
+ protected $cacheFactory;
+ /** @var ICache|MockObject */
+ protected $cache;
+ /** @var ITimeFactory|MockObject */
+ protected $timeFactory;
+ /** @var IRegistry|MockObject */
+ protected $subscriptionRegistry;
+ /** @var LoggerInterface|MockObject */
protected $logger;
/** @var Coordinator|MockObject */
protected $coordinator;
@@ -49,14 +66,23 @@ class ManagerTest extends TestCase {
parent::setUp();
$this->validator = $this->createMock(IValidator::class);
- $this->logger = $this->createMock(ILogger::class);
+ $this->userManager = $this->createMock(IUserManager::class);
+ $this->cache = $this->createMock(ICache::class);
+ $this->timeFactory = $this->createMock(ITimeFactory::class);
+ $this->subscriptionRegistry = $this->createMock(IRegistry::class);
+ $this->logger = $this->createMock(LoggerInterface::class);
+
+ $this->cacheFactory = $this->createMock(ICacheFactory::class);
+ $this->cacheFactory->method('createDistributed')
+ ->with('notifications')
+ ->willReturn($this->cache);
$this->registrationContext = $this->createMock(RegistrationContext::class);
$this->coordinator = $this->createMock(Coordinator::class);
$this->coordinator->method('getRegistrationContext')
->willReturn($this->registrationContext);
- $this->manager = new Manager($this->validator, $this->logger, $this->coordinator);
+ $this->manager = new Manager($this->validator, $this->userManager, $this->cacheFactory, $this->timeFactory, $this->subscriptionRegistry, $this->logger, $this->coordinator);
}
public function testRegisterApp() {
@@ -128,6 +154,10 @@ class ManagerTest extends TestCase {
$manager = $this->getMockBuilder(Manager::class)
->setConstructorArgs([
$this->validator,
+ $this->userManager,
+ $this->cacheFactory,
+ $this->timeFactory,
+ $this->subscriptionRegistry,
$this->logger,
$this->coordinator,
])
@@ -156,6 +186,10 @@ class ManagerTest extends TestCase {
$manager = $this->getMockBuilder(Manager::class)
->setConstructorArgs([
$this->validator,
+ $this->userManager,
+ $this->cacheFactory,
+ $this->timeFactory,
+ $this->subscriptionRegistry,
$this->logger,
$this->coordinator,
])
@@ -177,6 +211,10 @@ class ManagerTest extends TestCase {
$manager = $this->getMockBuilder(Manager::class)
->setConstructorArgs([
$this->validator,
+ $this->userManager,
+ $this->cacheFactory,
+ $this->timeFactory,
+ $this->subscriptionRegistry,
$this->logger,
$this->coordinator,
])
@@ -199,6 +237,10 @@ class ManagerTest extends TestCase {
$manager = $this->getMockBuilder(Manager::class)
->setConstructorArgs([
$this->validator,
+ $this->userManager,
+ $this->cacheFactory,
+ $this->timeFactory,
+ $this->subscriptionRegistry,
$this->logger,
$this->coordinator,
])
@@ -211,4 +253,40 @@ class ManagerTest extends TestCase {
$manager->getCount($notification);
}
+
+ public function dataIsFairUseOfFreePushService() {
+ return [
+ // Before 1st March
+ [1646089199, true, 4999, true],
+ [1646089199, true, 5000, true],
+ [1646089199, false, 4999, true],
+ [1646089199, false, 5000, true],
+
+ // After 1st March
+ [1646089200, true, 4999, true],
+ [1646089200, true, 5000, true],
+ [1646089200, false, 4999, true],
+ [1646089200, false, 5000, false],
+ ];
+ }
+
+ /**
+ * @dataProvider dataIsFairUseOfFreePushService
+ * @param int $time
+ * @param bool $hasValidSubscription
+ * @param int $userCount
+ * @param bool $isFair
+ */
+ public function testIsFairUseOfFreePushService(int $time, bool $hasValidSubscription, int $userCount, bool $isFair): void {
+ $this->timeFactory->method('getTime')
+ ->willReturn($time);
+
+ $this->subscriptionRegistry->method('delegateHasValidSubscription')
+ ->willReturn($hasValidSubscription);
+
+ $this->userManager->method('countSeenUsers')
+ ->willReturn($userCount);
+
+ $this->assertSame($isFair, $this->manager->isFairUseOfFreePushService());
+ }
}
diff --git a/tests/lib/Support/Subscription/RegistryTest.php b/tests/lib/Support/Subscription/RegistryTest.php
index 5349b041d8b..260232ac95d 100644
--- a/tests/lib/Support/Subscription/RegistryTest.php
+++ b/tests/lib/Support/Subscription/RegistryTest.php
@@ -75,8 +75,7 @@ class RegistryTest extends TestCase {
$this->serverContainer,
$this->userManager,
$this->groupManager,
- $this->logger,
- $this->notificationManager
+ $this->logger
);
}
@@ -177,7 +176,7 @@ class RegistryTest extends TestCase {
->method('get')
->willReturn($dummyGroup);
- $this->assertSame(true, $this->registry->delegateIsHardUserLimitReached());
+ $this->assertSame(true, $this->registry->delegateIsHardUserLimitReached($this->notificationManager));
}
public function testDelegateIsHardUserLimitReachedWithoutSupportApp() {
@@ -186,7 +185,7 @@ class RegistryTest extends TestCase {
->with('one-click-instance')
->willReturn(false);
- $this->assertSame(false, $this->registry->delegateIsHardUserLimitReached());
+ $this->assertSame(false, $this->registry->delegateIsHardUserLimitReached($this->notificationManager));
}
public function dataForUserLimitCheck() {
@@ -237,6 +236,6 @@ class RegistryTest extends TestCase {
->willReturn($dummyGroup);
}
- $this->assertSame($expectedResult, $this->registry->delegateIsHardUserLimitReached());
+ $this->assertSame($expectedResult, $this->registry->delegateIsHardUserLimitReached($this->notificationManager));
}
}