AdminControllerTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\UpdateNotification\Tests\Controller;
  26. use OCA\UpdateNotification\Controller\AdminController;
  27. use OCA\UpdateNotification\ResetTokenBackgroundJob;
  28. use OCP\AppFramework\Http\DataResponse;
  29. use OCP\AppFramework\Utility\ITimeFactory;
  30. use OCP\BackgroundJob\IJobList;
  31. use OCP\IConfig;
  32. use OCP\IL10N;
  33. use OCP\IRequest;
  34. use OCP\Security\ISecureRandom;
  35. use Test\TestCase;
  36. class AdminControllerTest extends TestCase {
  37. /** @var IRequest|\PHPUnit_Framework_MockObject_MockObject */
  38. private $request;
  39. /** @var IJobList|\PHPUnit_Framework_MockObject_MockObject */
  40. private $jobList;
  41. /** @var ISecureRandom|\PHPUnit_Framework_MockObject_MockObject */
  42. private $secureRandom;
  43. /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
  44. private $config;
  45. /** @var AdminController */
  46. private $adminController;
  47. /** @var ITimeFactory|\PHPUnit_Framework_MockObject_MockObject */
  48. private $timeFactory;
  49. /** @var IL10N|\PHPUnit_Framework_MockObject_MockObject */
  50. private $l10n;
  51. protected function setUp(): void {
  52. parent::setUp();
  53. $this->request = $this->createMock(IRequest::class);
  54. $this->jobList = $this->createMock(IJobList::class);
  55. $this->secureRandom = $this->createMock(ISecureRandom::class);
  56. $this->config = $this->createMock(IConfig::class);
  57. $this->timeFactory = $this->createMock(ITimeFactory::class);
  58. $this->l10n = $this->createMock(IL10N::class);
  59. $this->adminController = new AdminController(
  60. 'updatenotification',
  61. $this->request,
  62. $this->jobList,
  63. $this->secureRandom,
  64. $this->config,
  65. $this->timeFactory,
  66. $this->l10n
  67. );
  68. }
  69. public function testCreateCredentials() {
  70. $this->jobList
  71. ->expects($this->once())
  72. ->method('add')
  73. ->with(ResetTokenBackgroundJob::class);
  74. $this->secureRandom
  75. ->expects($this->once())
  76. ->method('generate')
  77. ->with(64)
  78. ->willReturn('MyGeneratedToken');
  79. $this->config
  80. ->expects($this->once())
  81. ->method('setSystemValue')
  82. ->with('updater.secret');
  83. $this->timeFactory
  84. ->expects($this->once())
  85. ->method('getTime')
  86. ->willReturn(12345);
  87. $this->config
  88. ->expects($this->once())
  89. ->method('setAppValue')
  90. ->with('core', 'updater.secret.created', 12345);
  91. $expected = new DataResponse('MyGeneratedToken');
  92. $this->assertEquals($expected, $this->adminController->createCredentials());
  93. }
  94. }