BackupCodeStorageTest.php 2.8 KB

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