BackupCodeStorageTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\TwoFactorBackupCodes\Tests\Service;
  8. use OCA\TwoFactorBackupCodes\Service\BackupCodeStorage;
  9. use OCP\Notification\IManager;
  10. use OCP\Notification\INotification;
  11. use Test\TestCase;
  12. /**
  13. * @group DB
  14. */
  15. class BackupCodeStorageTest extends TestCase {
  16. /** @var BackupCodeStorage */
  17. private $storage;
  18. /** @var string */
  19. private $testUID = 'test123456789';
  20. /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
  21. private $notificationManager;
  22. protected function setUp(): void {
  23. parent::setUp();
  24. $this->storage = \OC::$server->query(BackupCodeStorage::class);
  25. $this->notificationManager = $this->createMock(IManager::class);
  26. $this->notificationManager->method('createNotification')
  27. ->willReturn(\OC::$server->query(IManager::class)->createNotification());
  28. $this->overwriteService(IManager::class, $this->notificationManager);
  29. }
  30. public function testSimpleWorkFlow() {
  31. $user = $this->getMockBuilder(\OCP\IUser::class)->getMock();
  32. $user->expects($this->any())
  33. ->method('getUID')
  34. ->willReturn($this->testUID);
  35. $this->notificationManager->expects($this->once())
  36. ->method('markProcessed')
  37. ->with($this->callback(function (INotification $notification) {
  38. return $notification->getUser() === $this->testUID &&
  39. $notification->getObjectType() === 'create' &&
  40. $notification->getObjectId() === 'codes' &&
  41. $notification->getApp() === 'twofactor_backupcodes';
  42. }));
  43. // Create codes
  44. $codes = $this->storage->createCodes($user, 5);
  45. $this->assertCount(5, $codes);
  46. $this->assertTrue($this->storage->hasBackupCodes($user));
  47. $initialState = [
  48. 'enabled' => true,
  49. 'total' => 5,
  50. 'used' => 0,
  51. ];
  52. $this->assertEquals($initialState, $this->storage->getBackupCodesState($user));
  53. // Use codes
  54. $code = $codes[2];
  55. $this->assertTrue($this->storage->validateCode($user, $code));
  56. // Code must not be used twice
  57. $this->assertFalse($this->storage->validateCode($user, $code));
  58. // Invalid codes are invalid
  59. $this->assertFalse($this->storage->validateCode($user, 'I DO NOT EXIST'));
  60. $stateAfter = [
  61. 'enabled' => true,
  62. 'total' => 5,
  63. 'used' => 1,
  64. ];
  65. $this->assertEquals($stateAfter, $this->storage->getBackupCodesState($user));
  66. // Deplete codes
  67. $this->assertTrue($this->storage->validateCode($user, $codes[0]));
  68. $this->assertTrue($this->storage->validateCode($user, $codes[1]));
  69. $this->assertTrue($this->storage->validateCode($user, $codes[3]));
  70. $this->assertTrue($this->storage->validateCode($user, $codes[4]));
  71. $stateAllUsed = [
  72. 'enabled' => true,
  73. 'total' => 5,
  74. 'used' => 5,
  75. ];
  76. $this->assertEquals($stateAllUsed, $this->storage->getBackupCodesState($user));
  77. }
  78. }