BackupCodeStorageTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. *
  11. * @license GNU AGPL version 3 or any later version
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License as
  15. * published by the Free Software Foundation, either version 3 of the
  16. * License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. */
  27. namespace OCA\TwoFactorBackupCodes\Tests\Service;
  28. use OCA\TwoFactorBackupCodes\Service\BackupCodeStorage;
  29. use OCP\Notification\IManager;
  30. use OCP\Notification\INotification;
  31. use Test\TestCase;
  32. /**
  33. * @group DB
  34. */
  35. class BackupCodeStorageTest extends TestCase {
  36. /** @var BackupCodeStorage */
  37. private $storage;
  38. /** @var string */
  39. private $testUID = 'test123456789';
  40. /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
  41. private $notificationManager;
  42. protected function setUp(): void {
  43. parent::setUp();
  44. $this->storage = \OC::$server->query(BackupCodeStorage::class);
  45. $this->notificationManager = $this->createMock(IManager::class);
  46. $this->notificationManager->method('createNotification')
  47. ->willReturn(\OC::$server->query(IManager::class)->createNotification());
  48. $this->overwriteService(IManager::class, $this->notificationManager);
  49. }
  50. public function testSimpleWorkFlow() {
  51. $user = $this->getMockBuilder(\OCP\IUser::class)->getMock();
  52. $user->expects($this->any())
  53. ->method('getUID')
  54. ->willReturn($this->testUID);
  55. $this->notificationManager->expects($this->once())
  56. ->method('markProcessed')
  57. ->with($this->callback(function (INotification $notification) {
  58. return $notification->getUser() === $this->testUID &&
  59. $notification->getObjectType() === 'create' &&
  60. $notification->getObjectId() === 'codes' &&
  61. $notification->getApp() === 'twofactor_backupcodes';
  62. }));
  63. // Create codes
  64. $codes = $this->storage->createCodes($user, 5);
  65. $this->assertCount(5, $codes);
  66. $this->assertTrue($this->storage->hasBackupCodes($user));
  67. $initialState = [
  68. 'enabled' => true,
  69. 'total' => 5,
  70. 'used' => 0,
  71. ];
  72. $this->assertEquals($initialState, $this->storage->getBackupCodesState($user));
  73. // Use codes
  74. $code = $codes[2];
  75. $this->assertTrue($this->storage->validateCode($user, $code));
  76. // Code must not be used twice
  77. $this->assertFalse($this->storage->validateCode($user, $code));
  78. // Invalid codes are invalid
  79. $this->assertFalse($this->storage->validateCode($user, 'I DO NOT EXIST'));
  80. $stateAfter = [
  81. 'enabled' => true,
  82. 'total' => 5,
  83. 'used' => 1,
  84. ];
  85. $this->assertEquals($stateAfter, $this->storage->getBackupCodesState($user));
  86. // Deplete codes
  87. $this->assertTrue($this->storage->validateCode($user, $codes[0]));
  88. $this->assertTrue($this->storage->validateCode($user, $codes[1]));
  89. $this->assertTrue($this->storage->validateCode($user, $codes[3]));
  90. $this->assertTrue($this->storage->validateCode($user, $codes[4]));
  91. $stateAllUsed = [
  92. 'enabled' => true,
  93. 'total' => 5,
  94. 'used' => 5,
  95. ];
  96. $this->assertEquals($stateAllUsed, $this->storage->getBackupCodesState($user));
  97. }
  98. }