ResetTokenBackgroundJobTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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\ResetTokenBackgroundJob;
  28. use OCP\AppFramework\Utility\ITimeFactory;
  29. use OCP\IConfig;
  30. use Test\TestCase;
  31. class ResetTokenBackgroundJobTest extends TestCase {
  32. /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
  33. private $config;
  34. /** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */
  35. private $timeFactory;
  36. /** @var ResetTokenBackgroundJob */
  37. private $resetTokenBackgroundJob;
  38. protected function setUp(): void {
  39. parent::setUp();
  40. $this->config = $this->createMock(IConfig::class);
  41. $this->timeFactory = $this->createMock(ITimeFactory::class);
  42. $this->resetTokenBackgroundJob = new ResetTokenBackgroundJob($this->config, $this->timeFactory);
  43. }
  44. public function testRunWithNotExpiredToken() {
  45. $this->timeFactory
  46. ->expects($this->atLeastOnce())
  47. ->method('getTime')
  48. ->willReturn(123);
  49. $this->config
  50. ->expects($this->once())
  51. ->method('getAppValue')
  52. ->with('core', 'updater.secret.created', 123);
  53. $this->config
  54. ->expects($this->once())
  55. ->method('getSystemValueBool')
  56. ->with('config_is_read_only')
  57. ->willReturn(false);
  58. $this->config
  59. ->expects($this->never())
  60. ->method('deleteSystemValue');
  61. static::invokePrivate($this->resetTokenBackgroundJob, 'run', [null]);
  62. }
  63. public function testRunWithExpiredToken() {
  64. $this->timeFactory
  65. ->expects($this->exactly(2))
  66. ->method('getTime')
  67. ->willReturnOnConsecutiveCalls(1455131633, 1455045234);
  68. $this->config
  69. ->expects($this->once())
  70. ->method('getAppValue')
  71. ->with('core', 'updater.secret.created', 1455045234);
  72. $this->config
  73. ->expects($this->once())
  74. ->method('deleteSystemValue')
  75. ->with('updater.secret');
  76. static::invokePrivate($this->resetTokenBackgroundJob, 'run', [null]);
  77. }
  78. public function testRunWithExpiredTokenAndReadOnlyConfigFile() {
  79. $this->timeFactory
  80. ->expects($this->never())
  81. ->method('getTime');
  82. $this->config
  83. ->expects($this->never())
  84. ->method('getAppValue');
  85. $this->config
  86. ->expects($this->once())
  87. ->method('getSystemValueBool')
  88. ->with('config_is_read_only')
  89. ->willReturn(true);
  90. $this->config
  91. ->expects($this->never())
  92. ->method('deleteSystemValue');
  93. static::invokePrivate($this->resetTokenBackgroundJob, 'run', [null]);
  94. }
  95. }