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.

ProviderDisabledTest.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2019, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  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. */
  24. namespace OCA\TwoFactorBackupCodes\Tests\Unit\Listener;
  25. use OCA\TwoFactorBackupCodes\BackgroundJob\RememberBackupCodesJob;
  26. use OCA\TwoFactorBackupCodes\Listener\ProviderDisabled;
  27. use OCP\Authentication\TwoFactorAuth\IRegistry;
  28. use OCP\Authentication\TwoFactorAuth\RegistryEvent;
  29. use OCP\BackgroundJob\IJobList;
  30. use OCP\EventDispatcher\Event;
  31. use OCP\IUser;
  32. use Test\TestCase;
  33. class ProviderDisabledTest extends TestCase {
  34. /** @var IRegistry|\PHPUnit\Framework\MockObject\MockObject */
  35. private $registy;
  36. /** @var IJobList|\PHPUnit\Framework\MockObject\MockObject */
  37. private $jobList;
  38. /** @var ProviderDisabled */
  39. private $listener;
  40. protected function setUp() {
  41. parent::setUp();
  42. $this->registy = $this->createMock(IRegistry::class);
  43. $this->jobList = $this->createMock(IJobList::class);
  44. $this->listener = new ProviderDisabled($this->registy, $this->jobList);
  45. }
  46. public function testHandleGenericEvent() {
  47. $event = $this->createMock(Event::class);
  48. $this->jobList->expects($this->never())
  49. ->method($this->anything());
  50. $this->listener->handle($event);
  51. }
  52. public function testHandleStillActiveProvider() {
  53. $user = $this->createMock(IUser::class);
  54. $user->method('getUID')
  55. ->willReturn('myUID');
  56. $event = $this->createMock(RegistryEvent::class);
  57. $event->method('getUser')
  58. ->willReturn($user);
  59. $this->registy->method('getProviderStates')
  60. ->with($user)
  61. ->willReturn([
  62. 'backup_codes' => false,
  63. 'foo' => true,
  64. ]);
  65. $this->jobList->expects($this->never())
  66. ->method($this->anything());
  67. $this->listener->handle($event);
  68. }
  69. public function testHandleNoActiveProvider() {
  70. $user = $this->createMock(IUser::class);
  71. $user->method('getUID')
  72. ->willReturn('myUID');
  73. $event = $this->createMock(RegistryEvent::class);
  74. $event->method('getUser')
  75. ->willReturn($user);
  76. $this->registy->method('getProviderStates')
  77. ->with($user)
  78. ->willReturn([
  79. 'backup_codes' => false,
  80. 'foo' => false,
  81. ]);
  82. $this->jobList->expects($this->once())
  83. ->method('remove')
  84. ->with(
  85. $this->equalTo(RememberBackupCodesJob::class),
  86. $this->equalTo(['uid' => 'myUID'])
  87. );
  88. $this->listener->handle($event);
  89. }
  90. }