diff options
author | Christoph Wurst <christoph@winzerhof-wurst.at> | 2019-05-28 17:56:01 +0200 |
---|---|---|
committer | Christoph Wurst <christoph@winzerhof-wurst.at> | 2019-06-27 17:16:18 +0200 |
commit | aa6622ccef610b4f3bfb57facb80e3e6613fd22e (patch) | |
tree | 723d8be9abcdea889d931bc0dc3377d20d5dfd95 /tests/lib/Authentication | |
parent | bc6053eb2119b462f78098d72d665aba744826cb (diff) | |
download | nextcloud-server-aa6622ccef610b4f3bfb57facb80e3e6613fd22e.tar.gz nextcloud-server-aa6622ccef610b4f3bfb57facb80e3e6613fd22e.zip |
Decouple remote wipe notifcation channels with events
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
Diffstat (limited to 'tests/lib/Authentication')
5 files changed, 513 insertions, 0 deletions
diff --git a/tests/lib/Authentication/Events/RemoteWipeFinishedTest.php b/tests/lib/Authentication/Events/RemoteWipeFinishedTest.php new file mode 100644 index 00000000000..49e9e79462f --- /dev/null +++ b/tests/lib/Authentication/Events/RemoteWipeFinishedTest.php @@ -0,0 +1,39 @@ +<?php declare(strict_types=1); + +/** + * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @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 Test\Authentication\Events; + +use OC\Authentication\Events\RemoteWipeFinished; +use OC\Authentication\Token\IToken; +use Test\TestCase; + +class RemoteWipeFinishedTest extends TestCase { + + public function testGetToken() { + $token = $this->createMock(IToken::class); + $event = new RemoteWipeFinished($token); + + $this->assertSame($token, $event->getToken()); + } + +} diff --git a/tests/lib/Authentication/Events/RemoteWipeStartedTest.php b/tests/lib/Authentication/Events/RemoteWipeStartedTest.php new file mode 100644 index 00000000000..8fbaa086656 --- /dev/null +++ b/tests/lib/Authentication/Events/RemoteWipeStartedTest.php @@ -0,0 +1,39 @@ +<?php declare(strict_types=1); + +/** + * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @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 Test\Authentication\Events; + +use OC\Authentication\Events\RemoteWipeStarted; +use OC\Authentication\Token\IToken; +use Test\TestCase; + +class RemoteWipeStartedTest extends TestCase { + + public function testGetToken() { + $token = $this->createMock(IToken::class); + $event = new RemoteWipeStarted($token); + + $this->assertSame($token, $event->getToken()); + } + +} diff --git a/tests/lib/Authentication/Listeners/RemoteWipeActivityListenerTest.php b/tests/lib/Authentication/Listeners/RemoteWipeActivityListenerTest.php new file mode 100644 index 00000000000..87a7b030273 --- /dev/null +++ b/tests/lib/Authentication/Listeners/RemoteWipeActivityListenerTest.php @@ -0,0 +1,154 @@ +<?php declare(strict_types=1); + +/** + * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @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 Test\Authentication\Events; + +use OC\Activity\Event as IActivityEvent; +use OC\Authentication\Events\RemoteWipeFinished; +use OC\Authentication\Events\RemoteWipeStarted; +use OC\Authentication\Listeners\RemoteWipeActivityListener; +use OC\Authentication\Token\IToken; +use OCP\Activity\IManager as IActivityManager; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; +use OCP\ILogger; +use OCP\IUser; +use PHPUnit\Framework\MockObject\MockObject; +use Test\TestCase; + +class RemoteWipeActivityListenerTests extends TestCase { + + /** @var IActivityManager|MockObject */ + private $activityManager; + + /** @var ILogger|MockObject */ + private $logger; + + /** @var IEventListener */ + private $listener; + + protected function setUp() { + parent::setUp(); + + $this->activityManager = $this->createMock(IActivityManager::class); + $this->logger = $this->createMock(ILogger::class); + + $this->listener = new RemoteWipeActivityListener( + $this->activityManager, + $this->logger + ); + } + + public function testHandleUnrelated() { + $event = new Event(); + + $this->listener->handle($event); + + $this->addToAssertionCount(1); + } + + public function testHandleRemoteWipeStarted() { + /** @var IToken|MockObject $token */ + $token = $this->createMock(IToken::class); + $event = new RemoteWipeStarted($token); + $activityEvent = $this->createMock(IActivityEvent::class); + $this->activityManager->expects($this->once()) + ->method('generateEvent') + ->willReturn($activityEvent); + $activityEvent->expects($this->once()) + ->method('setApp') + ->with('core') + ->willReturnSelf(); + $activityEvent->expects($this->once()) + ->method('setType') + ->with('security') + ->willReturnSelf(); + $token->method('getUID')->willReturn('user123'); + $activityEvent->expects($this->once()) + ->method('setAuthor') + ->with('user123') + ->willReturnSelf(); + $activityEvent->expects($this->once()) + ->method('setAffectedUser') + ->with('user123') + ->willReturnSelf(); + $token->method('getName')->willReturn('Token 1'); + $activityEvent->expects($this->once()) + ->method('setSubject') + ->with('remote_wipe_start', ['name' => 'Token 1']) + ->willReturnSelf(); + $this->activityManager->expects($this->once()) + ->method('publish'); + + $this->listener->handle($event); + } + + public function testHandleRemoteWipeStartedCanNotPublish() { + $token = $this->createMock(IToken::class); + $event = new RemoteWipeStarted($token); + $this->activityManager->expects($this->once()) + ->method('generateEvent'); + $this->activityManager->expects($this->once()) + ->method('publish') + ->willThrowException(new \BadMethodCallException()); + + $this->listener->handle($event); + } + + public function testHandleRemoteWipeFinished() { + /** @var IToken|MockObject $token */ + $token = $this->createMock(IToken::class); + $event = new RemoteWipeFinished($token); + $activityEvent = $this->createMock(IActivityEvent::class); + $this->activityManager->expects($this->once()) + ->method('generateEvent') + ->willReturn($activityEvent); + $activityEvent->expects($this->once()) + ->method('setApp') + ->with('core') + ->willReturnSelf(); + $activityEvent->expects($this->once()) + ->method('setType') + ->with('security') + ->willReturnSelf(); + $token->method('getUID')->willReturn('user123'); + $activityEvent->expects($this->once()) + ->method('setAuthor') + ->with('user123') + ->willReturnSelf(); + $activityEvent->expects($this->once()) + ->method('setAffectedUser') + ->with('user123') + ->willReturnSelf(); + $token->method('getName')->willReturn('Token 1'); + $activityEvent->expects($this->once()) + ->method('setSubject') + ->with('remote_wipe_finish', ['name' => 'Token 1']) + ->willReturnSelf(); + $this->activityManager->expects($this->once()) + ->method('publish'); + + $this->listener->handle($event); + } + +} diff --git a/tests/lib/Authentication/Listeners/RemoteWipeNotificationsListenerTest.php b/tests/lib/Authentication/Listeners/RemoteWipeNotificationsListenerTest.php new file mode 100644 index 00000000000..27d386ca5b3 --- /dev/null +++ b/tests/lib/Authentication/Listeners/RemoteWipeNotificationsListenerTest.php @@ -0,0 +1,150 @@ +<?php declare(strict_types=1); + +/** + * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @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 Test\Authentication\Events; + +use DateTime; +use OC\Authentication\Events\RemoteWipeFinished; +use OC\Authentication\Events\RemoteWipeStarted; +use OC\Authentication\Listeners\RemoteWipeNotificationsListener; +use OC\Authentication\Token\IToken; +use OCP\AppFramework\Utility\ITimeFactory; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; +use OCP\Notification\IManager as INotificationManager; +use OCP\Notification\INotification; +use PHPUnit\Framework\MockObject\MockObject; +use Test\TestCase; + +class RemoteWipeNotificationListenerTests extends TestCase { + + /** @var INotificationManager|MockObject */ + private $notificationManager; + + /** @var ITimeFactory|MockObject */ + private $timeFactory; + + /** @var IEventListener */ + private $listener; + + protected function setUp() { + parent::setUp(); + + $this->notificationManager = $this->createMock(INotificationManager::class); + $this->timeFactory = $this->createMock(ITimeFactory::class); + + $this->listener = new RemoteWipeNotificationsListener( + $this->notificationManager, + $this->timeFactory + ); + } + + public function testHandleUnrelated() { + $event = new Event(); + + $this->listener->handle($event); + + $this->addToAssertionCount(1); + } + + public function testHandleRemoteWipeStarted() { + $token = $this->createMock(IToken::class); + $event = new RemoteWipeStarted($token); + $notification = $this->createMock(INotification::class); + $this->notificationManager->expects($this->once()) + ->method('createNotification') + ->willReturn($notification); + $notification->expects($this->once()) + ->method('setApp') + ->with('auth') + ->willReturnSelf(); + $token->method('getUID')->willReturn('user123'); + $notification->expects($this->once()) + ->method('setUser') + ->with('user123') + ->willReturnSelf(); + $now = new DateTime(); + $this->timeFactory->method('getDateTime')->willReturn($now); + $notification->expects($this->once()) + ->method('setDateTime') + ->with($now) + ->willReturnSelf(); + $token->method('getId')->willReturn(123); + $notification->expects($this->once()) + ->method('setObject') + ->with('token', 123) + ->willReturnSelf(); + $token->method('getName')->willReturn('Token 1'); + $notification->expects($this->once()) + ->method('setSubject') + ->with('remote_wipe_start', [ + 'name' => 'Token 1' + ]) + ->willReturnSelf(); + $this->notificationManager->expects($this->once()) + ->method('notify'); + + $this->listener->handle($event); + } + + public function testHandleRemoteWipeFinished() { + $token = $this->createMock(IToken::class); + $event = new RemoteWipeFinished($token); + $notification = $this->createMock(INotification::class); + $this->notificationManager->expects($this->once()) + ->method('createNotification') + ->willReturn($notification); + $notification->expects($this->once()) + ->method('setApp') + ->with('auth') + ->willReturnSelf(); + $token->method('getUID')->willReturn('user123'); + $notification->expects($this->once()) + ->method('setUser') + ->with('user123') + ->willReturnSelf(); + $now = new DateTime(); + $this->timeFactory->method('getDateTime')->willReturn($now); + $notification->expects($this->once()) + ->method('setDateTime') + ->with($now) + ->willReturnSelf(); + $token->method('getId')->willReturn(123); + $notification->expects($this->once()) + ->method('setObject') + ->with('token', 123) + ->willReturnSelf(); + $token->method('getName')->willReturn('Token 1'); + $notification->expects($this->once()) + ->method('setSubject') + ->with('remote_wipe_finish', [ + 'name' => 'Token 1' + ]) + ->willReturnSelf(); + $this->notificationManager->expects($this->once()) + ->method('notify'); + + $this->listener->handle($event); + } + +} diff --git a/tests/lib/Authentication/Token/RemoteWipeTest.php b/tests/lib/Authentication/Token/RemoteWipeTest.php new file mode 100644 index 00000000000..e0b3e9fcae9 --- /dev/null +++ b/tests/lib/Authentication/Token/RemoteWipeTest.php @@ -0,0 +1,131 @@ +<?php declare(strict_types=1); + +/** + * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @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 Test\Authentication\Token; + +use OC\Authentication\Events\RemoteWipeFinished; +use OC\Authentication\Events\RemoteWipeStarted; +use OC\Authentication\Exceptions\WipeTokenException; +use OC\Authentication\Token\IProvider as ITokenProvider; +use OC\Authentication\Token\IProvider; +use OC\Authentication\Token\IToken; +use OC\Authentication\Token\RemoteWipe; +use OCP\EventDispatcher\IEventDispatcher; +use OCP\ILogger; +use PHPUnit\Framework\MockObject\MockObject; +use Test\TestCase; + +class RemoteWipeTest extends TestCase { + + /** @var ITokenProvider|MockObject */ + private $tokenProvider; + + /** @var IEventDispatcher|MockObject */ + private $eventDispatcher; + + /** @var ILogger|MockObject */ + private $logger; + + /** @var RemoteWipe */ + private $remoteWipe; + + protected function setUp() { + parent::setUp(); + + $this->tokenProvider = $this->createMock(IProvider::class); + $this->eventDispatcher = $this->createMock(IEventDispatcher::class); + $this->logger = $this->createMock(ILogger::class); + + $this->remoteWipe = new RemoteWipe( + $this->tokenProvider, + $this->eventDispatcher, + $this->logger + ); + } + + public function testStartWipingNotAWipeToken() { + $token = $this->createMock(IToken::class); + $this->tokenProvider->expects($this->once()) + ->method('getToken') + ->with('tk1') + ->willReturn($token); + $this->eventDispatcher->expects($this->never()) + ->method('dispatch'); + + $result = $this->remoteWipe->start('tk1'); + + $this->assertFalse($result); + } + + public function testStartWiping() { + $token = $this->createMock(IToken::class); + $this->tokenProvider->expects($this->once()) + ->method('getToken') + ->with('tk1') + ->willThrowException(new WipeTokenException($token)); + $this->eventDispatcher->expects($this->once()) + ->method('dispatch'); + $this->eventDispatcher->expects($this->once()) + ->method('dispatch') + ->with(RemoteWipeStarted::class, $this->equalTo(new RemoteWipeStarted($token))); + + $result = $this->remoteWipe->start('tk1'); + + $this->assertTrue($result); + } + + public function testFinishWipingNotAWipeToken() { + $token = $this->createMock(IToken::class); + $this->tokenProvider->expects($this->once()) + ->method('getToken') + ->with('tk1') + ->willReturn($token); + $this->eventDispatcher->expects($this->never()) + ->method('dispatch'); + + $result = $this->remoteWipe->finish('tk1'); + + $this->assertFalse($result); + } + + public function startFinishWiping() { + $token = $this->createMock(IToken::class); + $this->tokenProvider->expects($this->once()) + ->method('getToken') + ->with('tk1') + ->willThrowException(new WipeTokenException($token)); + $this->eventDispatcher->expects($this->once()) + ->method('dispatch'); + $this->tokenProvider->expects($this->once()) + ->method('invalidateToken') + ->with($token); + $this->eventDispatcher->expects($this->once()) + ->method('dispatch') + ->with(RemoteWipeFinished::class, $this->equalTo(new RemoteWipeFinished($token))); + + $result = $this->remoteWipe->finish('tk1'); + + $this->assertTrue($result); + } + +} |