ResetTokenTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OCA\UpdateNotification\Tests;
  27. use OCA\UpdateNotification\BackgroundJob\ResetToken as BackgroundJobResetToken;
  28. use OCP\AppFramework\Utility\ITimeFactory;
  29. use OCP\IAppConfig;
  30. use OCP\IConfig;
  31. use PHPUnit\Framework\MockObject\MockObject;
  32. use Test\TestCase;
  33. class ResetTokenTest extends TestCase {
  34. private IConfig|MockObject $config;
  35. private IAppConfig|MockObject $appConfig;
  36. private ITimeFactory|MockObject $timeFactory;
  37. private BackgroundJobResetToken $resetTokenBackgroundJob;
  38. protected function setUp(): void {
  39. parent::setUp();
  40. $this->appConfig = $this->createMock(IAppConfig::class);
  41. $this->config = $this->createMock(IConfig::class);
  42. $this->timeFactory = $this->createMock(ITimeFactory::class);
  43. $this->resetTokenBackgroundJob = new BackgroundJobResetToken(
  44. $this->timeFactory,
  45. $this->config,
  46. $this->appConfig,
  47. );
  48. }
  49. public function testRunWithNotExpiredToken() {
  50. $this->timeFactory
  51. ->expects($this->atLeastOnce())
  52. ->method('getTime')
  53. ->willReturn(123);
  54. $this->appConfig
  55. ->expects($this->once())
  56. ->method('getValueInt')
  57. ->with('core', 'updater.secret.created', 123);
  58. $this->config
  59. ->expects($this->once())
  60. ->method('getSystemValueBool')
  61. ->with('config_is_read_only')
  62. ->willReturn(false);
  63. $this->config
  64. ->expects($this->never())
  65. ->method('deleteSystemValue');
  66. static::invokePrivate($this->resetTokenBackgroundJob, 'run', [null]);
  67. }
  68. public function testRunWithExpiredToken() {
  69. $this->timeFactory
  70. ->expects($this->once())
  71. ->method('getTime')
  72. ->willReturn(1455045234);
  73. $this->appConfig
  74. ->expects($this->once())
  75. ->method('getValueInt')
  76. ->with('core', 'updater.secret.created', 1455045234)
  77. ->willReturn(2 * 24 * 60 * 60 + 1); // over 2 days
  78. $this->config
  79. ->expects($this->once())
  80. ->method('deleteSystemValue')
  81. ->with('updater.secret');
  82. static::invokePrivate($this->resetTokenBackgroundJob, 'run', [null]);
  83. }
  84. public function testRunWithExpiredTokenAndReadOnlyConfigFile() {
  85. $this->timeFactory
  86. ->expects($this->never())
  87. ->method('getTime');
  88. $this->appConfig
  89. ->expects($this->never())
  90. ->method('getValueInt');
  91. $this->config
  92. ->expects($this->once())
  93. ->method('getSystemValueBool')
  94. ->with('config_is_read_only')
  95. ->willReturn(true);
  96. $this->config
  97. ->expects($this->never())
  98. ->method('deleteSystemValue');
  99. static::invokePrivate($this->resetTokenBackgroundJob, 'run', [null]);
  100. }
  101. }