AdminControllerTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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\Controller;
  27. use OCA\UpdateNotification\BackgroundJob\ResetToken;
  28. use OCA\UpdateNotification\Controller\AdminController;
  29. use OCP\AppFramework\Http\DataResponse;
  30. use OCP\AppFramework\Utility\ITimeFactory;
  31. use OCP\BackgroundJob\IJobList;
  32. use OCP\IAppConfig;
  33. use OCP\IConfig;
  34. use OCP\IL10N;
  35. use OCP\IRequest;
  36. use OCP\Security\ISecureRandom;
  37. use PHPUnit\Framework\MockObject\MockObject;
  38. use Test\TestCase;
  39. class AdminControllerTest extends TestCase {
  40. private IRequest|MockObject $request;
  41. private IJobList|MockObject $jobList;
  42. private ISecureRandom|MockObject $secureRandom;
  43. private IConfig|MockObject $config;
  44. private ITimeFactory|MockObject $timeFactory;
  45. private IL10N|MockObject $l10n;
  46. private IAppConfig|MockObject $appConfig;
  47. private AdminController $adminController;
  48. protected function setUp(): void {
  49. parent::setUp();
  50. $this->request = $this->createMock(IRequest::class);
  51. $this->jobList = $this->createMock(IJobList::class);
  52. $this->secureRandom = $this->createMock(ISecureRandom::class);
  53. $this->config = $this->createMock(IConfig::class);
  54. $this->appConfig = $this->createMock(IAppConfig::class);
  55. $this->timeFactory = $this->createMock(ITimeFactory::class);
  56. $this->l10n = $this->createMock(IL10N::class);
  57. $this->adminController = new AdminController(
  58. 'updatenotification',
  59. $this->request,
  60. $this->jobList,
  61. $this->secureRandom,
  62. $this->config,
  63. $this->appConfig,
  64. $this->timeFactory,
  65. $this->l10n
  66. );
  67. }
  68. public function testCreateCredentials() {
  69. $this->jobList
  70. ->expects($this->once())
  71. ->method('add')
  72. ->with(ResetToken::class);
  73. $this->secureRandom
  74. ->expects($this->once())
  75. ->method('generate')
  76. ->with(64)
  77. ->willReturn('MyGeneratedToken');
  78. $this->config
  79. ->expects($this->once())
  80. ->method('setSystemValue')
  81. ->with('updater.secret');
  82. $this->timeFactory
  83. ->expects($this->once())
  84. ->method('getTime')
  85. ->willReturn(12345);
  86. $this->appConfig
  87. ->expects($this->once())
  88. ->method('setValueInt')
  89. ->with('core', 'updater.secret.created', 12345);
  90. $expected = new DataResponse('MyGeneratedToken');
  91. $this->assertEquals($expected, $this->adminController->createCredentials());
  92. }
  93. }