Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>pull/15775/head
parent
bc6053eb21
commit
aa6622ccef
@ -0,0 +1,46 @@ |
||||
<?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 OC\Authentication\Events; |
||||
|
||||
use OC\Authentication\Token\IToken; |
||||
use OCP\EventDispatcher\Event; |
||||
use Symfony\Component\EventDispatcher\GenericEvent; |
||||
|
||||
abstract class ARemoteWipeEvent extends Event { |
||||
|
||||
/** @var IToken */ |
||||
private $token; |
||||
|
||||
public function __construct(IToken $token) { |
||||
parent::__construct(); |
||||
$this->token = $token; |
||||
} |
||||
|
||||
public function getToken(): IToken { |
||||
return $this->token; |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,30 @@ |
||||
<?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 OC\Authentication\Events; |
||||
|
||||
class RemoteWipeFinished extends ARemoteWipeEvent { |
||||
|
||||
} |
||||
@ -0,0 +1,30 @@ |
||||
<?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 OC\Authentication\Events; |
||||
|
||||
class RemoteWipeStarted extends ARemoteWipeEvent { |
||||
|
||||
} |
||||
@ -0,0 +1,80 @@ |
||||
<?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 OC\Authentication\Listeners; |
||||
|
||||
use BadMethodCallException; |
||||
use OC\Authentication\Events\ARemoteWipeEvent; |
||||
use OC\Authentication\Events\RemoteWipeFinished; |
||||
use OC\Authentication\Events\RemoteWipeStarted; |
||||
use OC\Authentication\Token\IToken; |
||||
use OCP\Activity\IManager as IActvityManager; |
||||
use OCP\EventDispatcher\Event; |
||||
use OCP\EventDispatcher\IEventListener; |
||||
use OCP\ILogger; |
||||
|
||||
class RemoteWipeActivityListener implements IEventListener { |
||||
|
||||
/** @var IActvityManager */ |
||||
private $activityManager; |
||||
|
||||
/** @var ILogger */ |
||||
private $logger; |
||||
|
||||
public function __construct(IActvityManager $activityManager, |
||||
ILogger $logger) { |
||||
$this->activityManager = $activityManager; |
||||
$this->logger = $logger; |
||||
} |
||||
|
||||
public function handle(Event $event): void { |
||||
if ($event instanceof RemoteWipeStarted) { |
||||
$this->publishActivity('remote_wipe_start', $event->getToken()); |
||||
} else if ($event instanceof RemoteWipeFinished) { |
||||
$this->publishActivity('remote_wipe_finish', $event->getToken()); |
||||
} |
||||
} |
||||
|
||||
private function publishActivity(string $event, IToken $token): void { |
||||
$activity = $this->activityManager->generateEvent(); |
||||
$activity->setApp('core') |
||||
->setType('security') |
||||
->setAuthor($token->getUID()) |
||||
->setAffectedUser($token->getUID()) |
||||
->setSubject($event, [ |
||||
'name' => $token->getName(), |
||||
]); |
||||
try { |
||||
$this->activityManager->publish($activity); |
||||
} catch (BadMethodCallException $e) { |
||||
$this->logger->logException($e, [ |
||||
'app' => 'core', |
||||
'level' => ILogger::WARN, |
||||
'message' => 'could not publish activity', |
||||
]); |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,71 @@ |
||||
<?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 OC\Authentication\Listeners; |
||||
|
||||
use OC\Authentication\Events\ARemoteWipeEvent; |
||||
use OC\Authentication\Events\RemoteWipeFinished; |
||||
use OC\Authentication\Events\RemoteWipeStarted; |
||||
use OC\Authentication\Token\IToken; |
||||
use OCP\AppFramework\Utility\ITimeFactory; |
||||
use OCP\EventDispatcher\Event; |
||||
use OCP\EventDispatcher\IEventListener; |
||||
use OCP\Notification\IManager as INotificationManager; |
||||
|
||||
class RemoteWipeNotificationsListener implements IEventListener { |
||||
|
||||
/** @var INotificationManager */ |
||||
private $notificationManager; |
||||
|
||||
/** @var ITimeFactory */ |
||||
private $timeFactory; |
||||
|
||||
public function __construct(INotificationManager $notificationManager, |
||||
ITimeFactory $timeFactory) { |
||||
$this->notificationManager = $notificationManager; |
||||
$this->timeFactory = $timeFactory; |
||||
} |
||||
|
||||
public function handle(Event $event): void { |
||||
if ($event instanceof RemoteWipeStarted) { |
||||
$this->sendNotification('remote_wipe_start', $event->getToken()); |
||||
} else if ($event instanceof RemoteWipeFinished) { |
||||
$this->sendNotification('remote_wipe_finish', $event->getToken()); |
||||
} |
||||
} |
||||
|
||||
private function sendNotification(string $event, IToken $token): void { |
||||
$notification = $this->notificationManager->createNotification(); |
||||
$notification->setApp('auth') |
||||
->setUser($token->getUID()) |
||||
->setDateTime($this->timeFactory->getDateTime()) |
||||
->setObject('token', $token->getId()) |
||||
->setSubject($event, [ |
||||
'name' => $token->getName(), |
||||
]); |
||||
$this->notificationManager->notify($notification); |
||||
} |
||||
|
||||
} |
||||
@ -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()); |
||||
} |
||||
|
||||
} |
||||
@ -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()); |
||||
} |
||||
|
||||
} |
||||
@ -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); |
||||
} |
||||
|
||||
} |
||||
@ -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); |
||||
} |
||||
|
||||
} |
||||
@ -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); |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue