You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UserDeletedTokenCleanupListenerTest.php 3.3KB

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