Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable28] fix(push): Make testing the push server easier #2086

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
24 changes: 19 additions & 5 deletions lib/Push.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,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 OCP\UserStatus\IUserStatus;
use OCP\Util;
Expand Down Expand Up @@ -482,6 +483,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 @@ -491,11 +503,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 @@ -781,4 +790,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));
}
}
17 changes: 17 additions & 0 deletions tests/Unit/PushTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,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 Down Expand Up @@ -94,6 +95,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 @@ -119,6 +121,7 @@ protected function getPush(array $methods = []) {
$this->userStatusManager,
$this->l10nFactory,
$this->timeFactory,
$this->random,
$this->logger,
])
->setMethods($methods)
Expand Down Expand Up @@ -513,6 +516,13 @@ public function testPushToDeviceSending($isDebug) {
->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 @@ -821,6 +831,13 @@ public function testPushToDeviceTalkNotification(array $deviceTypes, $isTalkNoti
->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