SecurityTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Lukas Reschke <lukas@statuscode.ch>
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Julius Härtl <jus@bitgrid.net>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. *
  12. * @license GNU AGPL version 3 or any later version
  13. *
  14. * This program is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License as
  16. * published by the Free Software Foundation, either version 3 of the
  17. * License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  26. *
  27. */
  28. namespace OCA\Settings\Tests\Settings\Admin;
  29. use OC\Authentication\TwoFactorAuth\MandatoryTwoFactor;
  30. use OC\Encryption\Manager;
  31. use OCA\Settings\Settings\Admin\Security;
  32. use OCP\AppFramework\Http\TemplateResponse;
  33. use OCP\AppFramework\Services\IInitialState;
  34. use OCP\IURLGenerator;
  35. use OCP\IUserManager;
  36. use PHPUnit\Framework\MockObject\MockObject;
  37. use Test\TestCase;
  38. class SecurityTest extends TestCase {
  39. /** @var Security */
  40. private $admin;
  41. /** @var Manager */
  42. private $manager;
  43. /** @var IUserManager */
  44. private $userManager;
  45. /** @var MandatoryTwoFactor|MockObject */
  46. private $mandatoryTwoFactor;
  47. /** @var IInitialState|MockObject */
  48. private $initialState;
  49. protected function setUp(): void {
  50. parent::setUp();
  51. $this->manager = $this->getMockBuilder(Manager::class)->disableOriginalConstructor()->getMock();
  52. $this->userManager = $this->getMockBuilder(IUserManager::class)->getMock();
  53. $this->mandatoryTwoFactor = $this->createMock(MandatoryTwoFactor::class);
  54. $this->initialState = $this->createMock(IInitialState::class);
  55. $this->admin = new Security(
  56. $this->manager,
  57. $this->userManager,
  58. $this->mandatoryTwoFactor,
  59. $this->initialState,
  60. $this->createMock(IURLGenerator::class)
  61. );
  62. }
  63. /**
  64. * @return array
  65. */
  66. public function encryptionSettingsProvider() {
  67. return [
  68. [true],
  69. [false],
  70. ];
  71. }
  72. /**
  73. * @dataProvider encryptionSettingsProvider
  74. * @param bool $enabled
  75. */
  76. public function testGetFormWithOnlyOneBackend($enabled) {
  77. $this->manager
  78. ->expects($this->once())
  79. ->method('isEnabled')
  80. ->willReturn($enabled);
  81. $this->manager
  82. ->expects($this->once())
  83. ->method('isReady')
  84. ->willReturn($enabled);
  85. $this->manager
  86. ->expects($this->once())
  87. ->method('getEncryptionModules')
  88. ->willReturn([]);
  89. $this->userManager
  90. ->expects($this->once())
  91. ->method('getBackends')
  92. ->willReturn(['entry']);
  93. $expected = new TemplateResponse(
  94. 'settings',
  95. 'settings/admin/security',
  96. [],
  97. ''
  98. );
  99. $this->assertEquals($expected, $this->admin->getForm());
  100. }
  101. /**
  102. * @dataProvider encryptionSettingsProvider
  103. * @param bool $enabled
  104. */
  105. public function testGetFormWithMultipleBackends($enabled) {
  106. $this->manager
  107. ->expects($this->once())
  108. ->method('isEnabled')
  109. ->willReturn($enabled);
  110. $this->manager
  111. ->expects($this->once())
  112. ->method('isReady')
  113. ->willReturn($enabled);
  114. $this->manager
  115. ->expects($this->once())
  116. ->method('getEncryptionModules')
  117. ->willReturn([]);
  118. $this->userManager
  119. ->expects($this->once())
  120. ->method('getBackends')
  121. ->willReturn(['entry', 'entry']);
  122. $expected = new TemplateResponse(
  123. 'settings',
  124. 'settings/admin/security',
  125. [ ],
  126. ''
  127. );
  128. $this->assertEquals($expected, $this->admin->getForm());
  129. }
  130. public function testGetSection() {
  131. $this->assertSame('security', $this->admin->getSection());
  132. }
  133. public function testGetPriority() {
  134. $this->assertSame(10, $this->admin->getPriority());
  135. }
  136. }