|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * @copyright 2020 Christoph Wurst <christoph@winzerhof-wurst.at> |
| 7 | + * |
| 8 | + * @author 2020 Christoph Wurst <christoph@winzerhof-wurst.at> |
| 9 | + * |
| 10 | + * @license GNU AGPL version 3 or any later version |
| 11 | + * |
| 12 | + * This program is free software: you can redistribute it and/or modify |
| 13 | + * it under the terms of the GNU Affero General Public License as |
| 14 | + * published by the Free Software Foundation, either version 3 of the |
| 15 | + * License, or (at your option) any later version. |
| 16 | + * |
| 17 | + * This program is distributed in the hope that it will be useful, |
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | + * GNU Affero General Public License for more details. |
| 21 | + * |
| 22 | + * You should have received a copy of the GNU Affero General Public License |
| 23 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 24 | + */ |
| 25 | + |
| 26 | +namespace Test\Authentication\Listeners; |
| 27 | + |
| 28 | +use Exception; |
| 29 | +use OC\Authentication\Listeners\UserDeletedTokenCleanupListener; |
| 30 | +use OC\Authentication\Token\IToken; |
| 31 | +use OC\Authentication\Token\Manager; |
| 32 | +use OCP\EventDispatcher\Event; |
| 33 | +use OCP\ILogger; |
| 34 | +use OCP\IUser; |
| 35 | +use OCP\User\Events\UserDeletedEvent; |
| 36 | +use PHPUnit\Framework\MockObject\MockObject; |
| 37 | +use Test\TestCase; |
| 38 | + |
| 39 | +class UserDeletedTokenCleanupListenerTest extends TestCase { |
| 40 | + |
| 41 | + |
| 42 | + /** @var Manager|MockObject */ |
| 43 | + private $manager; |
| 44 | + |
| 45 | + /** @var ILogger|MockObject */ |
| 46 | + private $logger; |
| 47 | + |
| 48 | + /** @var UserDeletedTokenCleanupListener */ |
| 49 | + private $listener; |
| 50 | + |
| 51 | + protected function setUp(): void { |
| 52 | + parent::setUp(); |
| 53 | + |
| 54 | + $this->manager = $this->createMock(Manager::class); |
| 55 | + $this->logger = $this->createMock(ILogger::class); |
| 56 | + |
| 57 | + $this->listener = new UserDeletedTokenCleanupListener( |
| 58 | + $this->manager, |
| 59 | + $this->logger |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + public function testHandleUnrelated(): void { |
| 64 | + $event = new Event(); |
| 65 | + $this->manager->expects($this->never())->method('getTokenByUser'); |
| 66 | + $this->logger->expects($this->never())->method('logException'); |
| 67 | + |
| 68 | + $this->listener->handle($event); |
| 69 | + } |
| 70 | + |
| 71 | + public function testHandleWithErrors(): void { |
| 72 | + $user = $this->createMock(IUser::class); |
| 73 | + $user->method('getUID')->willReturn('user123'); |
| 74 | + $event = new UserDeletedEvent($user); |
| 75 | + $exception = new Exception('nope'); |
| 76 | + $this->manager->expects($this->once()) |
| 77 | + ->method('getTokenByUser') |
| 78 | + ->with('user123') |
| 79 | + ->willThrowException($exception); |
| 80 | + $this->logger->expects($this->once()) |
| 81 | + ->method('logException') |
| 82 | + ->with($exception, $this->anything()); |
| 83 | + |
| 84 | + $this->listener->handle($event); |
| 85 | + } |
| 86 | + |
| 87 | + public function testHandle(): void { |
| 88 | + $user = $this->createMock(IUser::class); |
| 89 | + $user->method('getUID')->willReturn('user123'); |
| 90 | + $event = new UserDeletedEvent($user); |
| 91 | + $token1 = $this->createMock(IToken::class); |
| 92 | + $token1->method('getId')->willReturn(1); |
| 93 | + $token2 = $this->createMock(IToken::class); |
| 94 | + $token2->method('getId')->willReturn(2); |
| 95 | + $token3 = $this->createMock(IToken::class); |
| 96 | + $token3->method('getId')->willReturn(3); |
| 97 | + $this->manager->expects($this->once()) |
| 98 | + ->method('getTokenByUser') |
| 99 | + ->with('user123') |
| 100 | + ->willReturn([ |
| 101 | + $token1, |
| 102 | + $token2, |
| 103 | + $token3, |
| 104 | + ]); |
| 105 | + $this->manager->expects($this->exactly(3)) |
| 106 | + ->method('invalidateTokenById') |
| 107 | + ->withConsecutive( |
| 108 | + ['user123', 1], |
| 109 | + ['user123', 2], |
| 110 | + ['user123', 3] |
| 111 | + ); |
| 112 | + $this->logger->expects($this->never()) |
| 113 | + ->method('logException'); |
| 114 | + |
| 115 | + $this->listener->handle($event); |
| 116 | + } |
| 117 | +} |
0 commit comments