diff options
author | Roeland Jago Douma <roeland@famdouma.nl> | 2018-09-29 21:03:28 +0200 |
---|---|---|
committer | Roeland Jago Douma <roeland@famdouma.nl> | 2018-10-01 15:35:25 +0200 |
commit | c55731426240c0debd3a9bad3cb5e32b6f7e76a8 (patch) | |
tree | 895addc0bdda121221d5b1d90ebe894a0622a497 /apps | |
parent | 956fe1b86769c1a8a380a61ba72441f0e334e36a (diff) | |
download | nextcloud-server-c55731426240c0debd3a9bad3cb5e32b6f7e76a8.tar.gz nextcloud-server-c55731426240c0debd3a9bad3cb5e32b6f7e76a8.zip |
Clear notification to generate backup code once codes are generated
Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
Diffstat (limited to 'apps')
6 files changed, 151 insertions, 0 deletions
diff --git a/apps/twofactor_backupcodes/composer/composer/autoload_classmap.php b/apps/twofactor_backupcodes/composer/composer/autoload_classmap.php index 12da21df05f..da57cbb94c2 100644 --- a/apps/twofactor_backupcodes/composer/composer/autoload_classmap.php +++ b/apps/twofactor_backupcodes/composer/composer/autoload_classmap.php @@ -14,6 +14,7 @@ return array( 'OCA\\TwoFactorBackupCodes\\Db\\BackupCodeMapper' => $baseDir . '/../lib/Db/BackupCodeMapper.php', 'OCA\\TwoFactorBackupCodes\\Event\\CodesGenerated' => $baseDir . '/../lib/Event/CodesGenerated.php', 'OCA\\TwoFactorBackupCodes\\Listener\\ActivityPublisher' => $baseDir . '/../lib/Listener/ActivityPublisher.php', + 'OCA\\TwoFactorBackupCodes\\Listener\\ClearNotifications' => $baseDir . '/../lib/Listener/ClearNotifications.php', 'OCA\\TwoFactorBackupCodes\\Listener\\IListener' => $baseDir . '/../lib/Listener/IListener.php', 'OCA\\TwoFactorBackupCodes\\Listener\\ProviderEnabled' => $baseDir . '/../lib/Listener/ProviderEnabled.php', 'OCA\\TwoFactorBackupCodes\\Listener\\RegistryUpdater' => $baseDir . '/../lib/Listener/RegistryUpdater.php', diff --git a/apps/twofactor_backupcodes/composer/composer/autoload_static.php b/apps/twofactor_backupcodes/composer/composer/autoload_static.php index 350ef5d0b90..164ba30e830 100644 --- a/apps/twofactor_backupcodes/composer/composer/autoload_static.php +++ b/apps/twofactor_backupcodes/composer/composer/autoload_static.php @@ -29,6 +29,7 @@ class ComposerStaticInitTwoFactorBackupCodes 'OCA\\TwoFactorBackupCodes\\Db\\BackupCodeMapper' => __DIR__ . '/..' . '/../lib/Db/BackupCodeMapper.php', 'OCA\\TwoFactorBackupCodes\\Event\\CodesGenerated' => __DIR__ . '/..' . '/../lib/Event/CodesGenerated.php', 'OCA\\TwoFactorBackupCodes\\Listener\\ActivityPublisher' => __DIR__ . '/..' . '/../lib/Listener/ActivityPublisher.php', + 'OCA\\TwoFactorBackupCodes\\Listener\\ClearNotifications' => __DIR__ . '/..' . '/../lib/Listener/ClearNotifications.php', 'OCA\\TwoFactorBackupCodes\\Listener\\IListener' => __DIR__ . '/..' . '/../lib/Listener/IListener.php', 'OCA\\TwoFactorBackupCodes\\Listener\\ProviderEnabled' => __DIR__ . '/..' . '/../lib/Listener/ProviderEnabled.php', 'OCA\\TwoFactorBackupCodes\\Listener\\RegistryUpdater' => __DIR__ . '/..' . '/../lib/Listener/RegistryUpdater.php', diff --git a/apps/twofactor_backupcodes/lib/AppInfo/Application.php b/apps/twofactor_backupcodes/lib/AppInfo/Application.php index 1af114a2791..f5d0139dbd9 100644 --- a/apps/twofactor_backupcodes/lib/AppInfo/Application.php +++ b/apps/twofactor_backupcodes/lib/AppInfo/Application.php @@ -28,6 +28,7 @@ namespace OCA\TwoFactorBackupCodes\AppInfo; use OCA\TwoFactorBackupCodes\Db\BackupCodeMapper; use OCA\TwoFactorBackupCodes\Event\CodesGenerated; use OCA\TwoFactorBackupCodes\Listener\ActivityPublisher; +use OCA\TwoFactorBackupCodes\Listener\ClearNotifications; use OCA\TwoFactorBackupCodes\Listener\IListener; use OCA\TwoFactorBackupCodes\Listener\ProviderEnabled; use OCA\TwoFactorBackupCodes\Listener\RegistryUpdater; @@ -67,6 +68,7 @@ class Application extends App { $listeners = [ $container->query(ActivityPublisher::class), $container->query(RegistryUpdater::class), + $container->query(ClearNotifications::class), ]; foreach ($listeners as $listener) { diff --git a/apps/twofactor_backupcodes/lib/Listener/ClearNotifications.php b/apps/twofactor_backupcodes/lib/Listener/ClearNotifications.php new file mode 100644 index 00000000000..ad7fd188ebc --- /dev/null +++ b/apps/twofactor_backupcodes/lib/Listener/ClearNotifications.php @@ -0,0 +1,51 @@ +<?php +declare(strict_types=1); +/** + * @copyright Copyright (c) 2018, Roeland Jago Douma <roeland@famdouma.nl> + * + * @author Roeland Jago Douma <roeland@famdouma.nl> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OCA\TwoFactorBackupCodes\Listener; + +use OCA\TwoFactorBackupCodes\Event\CodesGenerated; +use OCP\Notification\IManager; +use Symfony\Component\EventDispatcher\Event; + +class ClearNotifications implements IListener { + + /** @var IManager */ + private $manager; + + public function __construct(IManager $manager) { + $this->manager = $manager; + } + + public function handle(Event $event) { + if (!($event instanceof CodesGenerated)) { + return; + } + + $notification = $this->manager->createNotification(); + $notification->setApp('twofactor_backupcodes') + ->setUser($event->getUser()->getUID()) + ->setObject('create', 'codes'); + $this->manager->markProcessed($notification); + } +} diff --git a/apps/twofactor_backupcodes/tests/Service/BackupCodeStorageTest.php b/apps/twofactor_backupcodes/tests/Service/BackupCodeStorageTest.php index 7d47b4ea721..679f111a75b 100644 --- a/apps/twofactor_backupcodes/tests/Service/BackupCodeStorageTest.php +++ b/apps/twofactor_backupcodes/tests/Service/BackupCodeStorageTest.php @@ -23,6 +23,8 @@ namespace OCA\TwoFactorBackupCodes\Tests\Service; use OCA\TwoFactorBackupCodes\Service\BackupCodeStorage; +use OCP\Notification\IManager; +use OCP\Notification\INotification; use Test\TestCase; /** @@ -36,10 +38,18 @@ class BackupCodeStorageTest extends TestCase { /** @var string */ private $testUID = 'test123456789'; + /** @var IManager|\PHPUnit_Framework_MockObject_MockObject */ + private $notificationManager; + protected function setUp() { parent::setUp(); $this->storage = \OC::$server->query(BackupCodeStorage::class); + + $this->notificationManager = $this->createMock(IManager::class); + $this->notificationManager->method('createNotification') + ->willReturn(\OC::$server->query(IManager::class)->createNotification()); + $this->overwriteService(IManager::class, $this->notificationManager); } public function testSimpleWorkFlow() { @@ -48,6 +58,15 @@ class BackupCodeStorageTest extends TestCase { ->method('getUID') ->will($this->returnValue($this->testUID)); + $this->notificationManager->expects($this->once()) + ->method('markProcessed') + ->with($this->callback(function (INotification $notification) { + return $notification->getUser() === $this->testUID && + $notification->getObjectType() === 'create' && + $notification->getObjectId() === 'codes' && + $notification->getApp() === 'twofactor_backupcodes'; + })); + // Create codes $codes = $this->storage->createCodes($user, 5); $this->assertCount(5, $codes); diff --git a/apps/twofactor_backupcodes/tests/Unit/Listener/ClearNotificationsTest.php b/apps/twofactor_backupcodes/tests/Unit/Listener/ClearNotificationsTest.php new file mode 100644 index 00000000000..123c008cbbb --- /dev/null +++ b/apps/twofactor_backupcodes/tests/Unit/Listener/ClearNotificationsTest.php @@ -0,0 +1,77 @@ +<?php +declare(strict_types=1); +/** + * @copyright Copyright (c) 2018, Roeland Jago Douma <roeland@famdouma.nl> + * + * @author Roeland Jago Douma <roeland@famdouma.nl> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OCA\TwoFactorBackupCodes\Tests\Unit\Listener; + +use OCA\TwoFactorBackupCodes\Event\CodesGenerated; +use OCA\TwoFactorBackupCodes\Listener\ClearNotifications; +use OCP\IUser; +use OCP\Notification\IManager; +use OCP\Notification\INotification; +use Symfony\Component\EventDispatcher\Event; +use Test\TestCase; + +class ClearNotificationsTest extends TestCase { + + /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */ + private $notificationManager; + + /** @var ClearNotifications */ + private $listener; + + protected function setUp() { + parent::setUp(); + + $this->notificationManager = $this->createMock(IManager::class); + $this->notificationManager->method('createNotification') + ->willReturn(\OC::$server->query(IManager::class)->createNotification()); + + $this->listener = new ClearNotifications($this->notificationManager); + } + + public function testHandleGenericEvent() { + $event = $this->createMock(Event::class); + $this->notificationManager->expects($this->never()) + ->method($this->anything()); + + $this->listener->handle($event); + } + + public function testHandleCodesGeneratedEvent() { + $user = $this->createMock(IUser::class); + $user->method('getUID')->willReturn('fritz'); + $event = new CodesGenerated($user); + + $this->notificationManager->expects($this->once()) + ->method('markProcessed') + ->with($this->callback(function(INotification $n) { + return $n->getUser() === 'fritz' && + $n->getApp() === 'twofactor_backupcodes' && + $n->getObjectType() === 'create' && + $n->getObjectId() === 'codes'; + })); + + $this->listener->handle($event); + } +} |