1
0

SecurityTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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\IInitialStateService;
  34. use OCP\IUserManager;
  35. use PHPUnit\Framework\MockObject\MockObject;
  36. use Test\TestCase;
  37. class SecurityTest extends TestCase {
  38. /** @var Security */
  39. private $admin;
  40. /** @var Manager */
  41. private $manager;
  42. /** @var IUserManager */
  43. private $userManager;
  44. /** @var MandatoryTwoFactor|MockObject */
  45. private $mandatoryTwoFactor;
  46. /** @var IInitialStateService|MockObject */
  47. private $initialState;
  48. protected function setUp(): void {
  49. parent::setUp();
  50. $this->manager = $this->getMockBuilder(Manager::class)->disableOriginalConstructor()->getMock();
  51. $this->userManager = $this->getMockBuilder(IUserManager::class)->getMock();
  52. $this->mandatoryTwoFactor = $this->createMock(MandatoryTwoFactor::class);
  53. $this->initialState = $this->createMock(IInitialStateService::class);
  54. $this->admin = new Security(
  55. $this->manager,
  56. $this->userManager,
  57. $this->mandatoryTwoFactor,
  58. $this->initialState
  59. );
  60. }
  61. /**
  62. * @return array
  63. */
  64. public function encryptionSettingsProvider() {
  65. return [
  66. [true],
  67. [false],
  68. ];
  69. }
  70. /**
  71. * @dataProvider encryptionSettingsProvider
  72. * @param bool $enabled
  73. */
  74. public function testGetFormWithOnlyOneBackend($enabled) {
  75. $this->manager
  76. ->expects($this->once())
  77. ->method('isEnabled')
  78. ->willReturn($enabled);
  79. $this->manager
  80. ->expects($this->once())
  81. ->method('isReady')
  82. ->willReturn($enabled);
  83. $this->manager
  84. ->expects($this->once())
  85. ->method('getEncryptionModules')
  86. ->willReturn([]);
  87. $this->userManager
  88. ->expects($this->once())
  89. ->method('getBackends')
  90. ->willReturn(['entry']);
  91. $expected = new TemplateResponse(
  92. 'settings',
  93. 'settings/admin/security',
  94. [
  95. 'encryptionEnabled' => $enabled,
  96. 'encryptionReady' => $enabled,
  97. 'externalBackendsEnabled' => false,
  98. 'encryptionModules' => []
  99. ],
  100. ''
  101. );
  102. $this->assertEquals($expected, $this->admin->getForm());
  103. }
  104. /**
  105. * @dataProvider encryptionSettingsProvider
  106. * @param bool $enabled
  107. */
  108. public function testGetFormWithMultipleBackends($enabled) {
  109. $this->manager
  110. ->expects($this->once())
  111. ->method('isEnabled')
  112. ->willReturn($enabled);
  113. $this->manager
  114. ->expects($this->once())
  115. ->method('isReady')
  116. ->willReturn($enabled);
  117. $this->manager
  118. ->expects($this->once())
  119. ->method('getEncryptionModules')
  120. ->willReturn([]);
  121. $this->userManager
  122. ->expects($this->once())
  123. ->method('getBackends')
  124. ->willReturn(['entry', 'entry']);
  125. $expected = new TemplateResponse(
  126. 'settings',
  127. 'settings/admin/security',
  128. [
  129. 'encryptionEnabled' => $enabled,
  130. 'encryptionReady' => $enabled,
  131. 'externalBackendsEnabled' => true,
  132. 'encryptionModules' => []
  133. ],
  134. ''
  135. );
  136. $this->assertEquals($expected, $this->admin->getForm());
  137. }
  138. public function testGetSection() {
  139. $this->assertSame('security', $this->admin->getSection());
  140. }
  141. public function testGetPriority() {
  142. $this->assertSame(10, $this->admin->getPriority());
  143. }
  144. }