Skip to content

Commit

Permalink
Merge pull request #2074 from nextcloud/bugfix/noid/make-testing-push…
Browse files Browse the repository at this point in the history
…-server-easier

fix(push): Make testing the push server easier
  • Loading branch information
nickvergessen authored Oct 11, 2024
2 parents 5fcb745 + c61af81 commit cd29ef9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
25 changes: 20 additions & 5 deletions lib/Push.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use OCP\Notification\IManager as INotificationManager;
use OCP\Notification\IncompleteParsedNotificationException;
use OCP\Notification\INotification;
use OCP\Security\ISecureRandom;
use OCP\UserStatus\IManager as IUserStatusManager;
use OCP\UserStatus\IUserStatus;
use OCP\Util;
Expand Down Expand Up @@ -81,6 +82,7 @@ public function __construct(
protected IUserStatusManager $userStatusManager,
protected IFactory $l10nFactory,
protected ITimeFactory $timeFactory,
protected ISecureRandom $random,
protected LoggerInterface $log,
) {
$this->cache = $cacheFactory->createDistributed('pushtokens');
Expand Down Expand Up @@ -422,6 +424,17 @@ protected function sendNotificationsToProxies(): void {
return;
}

$subscriptionAwareServer = rtrim($this->config->getAppValue(Application::APP_ID, 'subscription_aware_server', 'https://push-notifications.nextcloud.com'), '/');
if ($subscriptionAwareServer === 'https://push-notifications.nextcloud.com') {
$subscriptionKey = $this->config->getAppValue('support', 'subscription_key');
} else {
$subscriptionKey = $this->config->getAppValue(Application::APP_ID, 'push_subscription_key');
if ($subscriptionKey === '') {
$subscriptionKey = $this->createPushSubscriptionKey();
$this->config->setAppValue(Application::APP_ID, 'push_subscription_key', $subscriptionKey);
}
}

$client = $this->clientService->newClient();
foreach ($pushNotifications as $proxyServer => $notifications) {
try {
Expand All @@ -431,11 +444,8 @@ protected function sendNotificationsToProxies(): void {
],
];

if ($proxyServer === 'https://push-notifications.nextcloud.com') {
$subscriptionKey = $this->config->getAppValue('support', 'subscription_key');
if ($subscriptionKey) {
$requestData['headers']['X-Nextcloud-Subscription-Key'] = $subscriptionKey;
}
if ($subscriptionKey !== '' && $proxyServer === $subscriptionAwareServer) {
$requestData['headers']['X-Nextcloud-Subscription-Key'] = $subscriptionKey;
}

$response = $client->post($proxyServer . '/notifications', $requestData);
Expand Down Expand Up @@ -729,4 +739,9 @@ protected function deletePushTokenByDeviceIdentifier(string $deviceIdentifier):
protected function createFakeUserObject(string $userId): IUser {
return new FakeUser($userId);
}

protected function createPushSubscriptionKey(): string {
$key = $this->random->generate(25, ISecureRandom::CHAR_ALPHANUMERIC);
return implode('-', str_split($key, 5));
}
}
19 changes: 19 additions & 0 deletions tests/Unit/PushTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use OCP\L10N\IFactory;
use OCP\Notification\IManager as INotificationManager;
use OCP\Notification\INotification;
use OCP\Security\ISecureRandom;
use OCP\UserStatus\IManager as IUserStatusManager;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Http\Message\ResponseInterface;
Expand All @@ -53,6 +54,7 @@ class PushTest extends TestCase {
protected IUserStatusManager&MockObject $userStatusManager;
protected IFactory&MockObject $l10nFactory;
protected ITimeFactory&MockObject $timeFactory;
protected ISecureRandom&MockObject $random;
protected LoggerInterface&MockObject $logger;

protected function setUp(): void {
Expand All @@ -69,6 +71,7 @@ protected function setUp(): void {
$this->userStatusManager = $this->createMock(IUserStatusManager::class);
$this->l10nFactory = $this->createMock(IFactory::class);
$this->timeFactory = $this->createMock(ITimeFactory::class);
$this->random = $this->createMock(ISecureRandom::class);
$this->logger = $this->createMock(LoggerInterface::class);

$this->cacheFactory->method('createDistributed')
Expand All @@ -93,6 +96,7 @@ protected function getPush(array $methods = []): Push|MockObject {
$this->userStatusManager,
$this->l10nFactory,
$this->timeFactory,
$this->random,
$this->logger,
])
->onlyMethods($methods)
Expand All @@ -110,6 +114,7 @@ protected function getPush(array $methods = []): Push|MockObject {
$this->userStatusManager,
$this->l10nFactory,
$this->timeFactory,
$this->random,
$this->logger,
);
}
Expand Down Expand Up @@ -487,6 +492,13 @@ public function testPushToDeviceSending(bool $isDebug): void {
->with('debug', false)
->willReturn($isDebug);

$this->config
->method('getAppValue')
->willReturnMap([
['notifications', 'subscription_aware_server', 'https://push-notifications.nextcloud.com', 'https://push-notifications.nextcloud.com'],
['support', 'subscription_key', '', ''],
]);

$this->l10nFactory
->method('getUserLanguage')
->with($user)
Expand Down Expand Up @@ -759,6 +771,13 @@ public function testPushToDeviceTalkNotification(array $deviceTypes, bool $isTal
->with('has_internet_connection', true)
->willReturn(true);

$this->config
->method('getAppValue')
->willReturnMap([
['notifications', 'subscription_aware_server', 'https://push-notifications.nextcloud.com', 'https://push-notifications.nextcloud.com'],
['support', 'subscription_key', '', ''],
]);

$this->notificationManager->method('isFairUseOfFreePushService')
->willReturn(true);

Expand Down

0 comments on commit cd29ef9

Please sign in to comment.