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/twofactor_backupcodes/tests | |
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/twofactor_backupcodes/tests')
-rw-r--r-- | apps/twofactor_backupcodes/tests/Service/BackupCodeStorageTest.php | 19 | ||||
-rw-r--r-- | apps/twofactor_backupcodes/tests/Unit/Listener/ClearNotificationsTest.php | 77 |
2 files changed, 96 insertions, 0 deletions
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); + } +} |