MandatoryTwoFactorTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2018 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author 2018 Christoph Wurst <christoph@winzerhof-wurst.at>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace Tests\Authentication\TwoFactorAuth;
  25. use OC\Authentication\TwoFactorAuth\EnforcementState;
  26. use OC\Authentication\TwoFactorAuth\MandatoryTwoFactor;
  27. use OCP\IConfig;
  28. use OCP\IGroupManager;
  29. use OCP\IUser;
  30. use PHPUnit\Framework\MockObject\MockObject;
  31. use Test\TestCase;
  32. class MandatoryTwoFactorTest extends TestCase {
  33. /** @var IConfig|MockObject */
  34. private $config;
  35. /** @var IGroupManager|MockObject */
  36. private $groupManager;
  37. /** @var MandatoryTwoFactor */
  38. private $mandatoryTwoFactor;
  39. protected function setUp(): void {
  40. parent::setUp();
  41. $this->config = $this->createMock(IConfig::class);
  42. $this->groupManager = $this->createMock(IGroupManager::class);
  43. $this->mandatoryTwoFactor = new MandatoryTwoFactor($this->config, $this->groupManager);
  44. }
  45. public function testIsNotEnforced() {
  46. $this->config
  47. ->method('getSystemValue')
  48. ->willReturnMap([
  49. ['twofactor_enforced', 'false', 'false'],
  50. ['twofactor_enforced_groups', [], []],
  51. ['twofactor_enforced_excluded_groups', [], []],
  52. ]);
  53. $state = $this->mandatoryTwoFactor->getState();
  54. $this->assertFalse($state->isEnforced());
  55. }
  56. public function testIsEnforced() {
  57. $this->config
  58. ->method('getSystemValue')
  59. ->willReturnMap([
  60. ['twofactor_enforced', 'false', 'true'],
  61. ['twofactor_enforced_groups', [], []],
  62. ['twofactor_enforced_excluded_groups', [], []],
  63. ]);
  64. $state = $this->mandatoryTwoFactor->getState();
  65. $this->assertTrue($state->isEnforced());
  66. }
  67. public function testIsNotEnforcedForAnybody() {
  68. $user = $this->createMock(IUser::class);
  69. $user->method('getUID')->willReturn('user123');
  70. $this->config
  71. ->method('getSystemValue')
  72. ->willReturnMap([
  73. ['twofactor_enforced', 'false', 'false'],
  74. ['twofactor_enforced_groups', [], []],
  75. ['twofactor_enforced_excluded_groups', [], []],
  76. ]);
  77. $isEnforced = $this->mandatoryTwoFactor->isEnforcedFor($user);
  78. $this->assertFalse($isEnforced);
  79. }
  80. public function testIsEnforcedForAGroupMember() {
  81. $user = $this->createMock(IUser::class);
  82. $user->method('getUID')->willReturn('user123');
  83. $this->config
  84. ->method('getSystemValue')
  85. ->willReturnMap([
  86. ['twofactor_enforced', 'false', 'true'],
  87. ['twofactor_enforced_groups', [], ['twofactorers']],
  88. ['twofactor_enforced_excluded_groups', [], []],
  89. ]);
  90. $this->groupManager->method('isInGroup')
  91. ->willReturnCallback(function ($user, $group) {
  92. return $user === 'user123' && $group === 'twofactorers';
  93. });
  94. $isEnforced = $this->mandatoryTwoFactor->isEnforcedFor($user);
  95. $this->assertTrue($isEnforced);
  96. }
  97. public function testIsEnforcedForOtherGroups() {
  98. $user = $this->createMock(IUser::class);
  99. $user->method('getUID')->willReturn('user123');
  100. $this->config
  101. ->method('getSystemValue')
  102. ->willReturnMap([
  103. ['twofactor_enforced', 'false', 'true'],
  104. ['twofactor_enforced_groups', [], ['twofactorers']],
  105. ['twofactor_enforced_excluded_groups', [], []],
  106. ]);
  107. $this->groupManager->method('isInGroup')
  108. ->willReturn(false);
  109. $isEnforced = $this->mandatoryTwoFactor->isEnforcedFor($user);
  110. $this->assertFalse($isEnforced);
  111. }
  112. public function testIsEnforcedButMemberOfExcludedGroup() {
  113. $user = $this->createMock(IUser::class);
  114. $user->method('getUID')->willReturn('user123');
  115. $this->config
  116. ->method('getSystemValue')
  117. ->willReturnMap([
  118. ['twofactor_enforced', 'false', 'true'],
  119. ['twofactor_enforced_groups', [], []],
  120. ['twofactor_enforced_excluded_groups', [], ['yoloers']],
  121. ]);
  122. $this->groupManager->method('isInGroup')
  123. ->willReturnCallback(function ($user, $group) {
  124. return $user === 'user123' && $group === 'yoloers';
  125. });
  126. $isEnforced = $this->mandatoryTwoFactor->isEnforcedFor($user);
  127. $this->assertFalse($isEnforced);
  128. }
  129. public function testSetEnforced() {
  130. $this->config
  131. ->expects($this->exactly(3))
  132. ->method('setSystemValue')
  133. ->willReturnMap([
  134. ['twofactor_enforced', 'true'],
  135. ['twofactor_enforced_groups', []],
  136. ['twofactor_enforced_excluded_groups', []],
  137. ]);
  138. $this->mandatoryTwoFactor->setState(new EnforcementState(true));
  139. }
  140. public function testSetEnforcedForGroups() {
  141. $this->config
  142. ->expects($this->exactly(3))
  143. ->method('setSystemValue')
  144. ->willReturnMap([
  145. ['twofactor_enforced', 'true'],
  146. ['twofactor_enforced_groups', ['twofactorers']],
  147. ['twofactor_enforced_excluded_groups', ['yoloers']],
  148. ]);
  149. $this->mandatoryTwoFactor->setState(new EnforcementState(true, ['twofactorers'], ['yoloers']));
  150. }
  151. public function testSetNotEnforced() {
  152. $this->config
  153. ->expects($this->exactly(3))
  154. ->method('setSystemValue')
  155. ->willReturnMap([
  156. ['twofactor_enforced', 'false'],
  157. ['twofactor_enforced_groups', []],
  158. ['twofactor_enforced_excluded_groups', []],
  159. ]);
  160. $this->mandatoryTwoFactor->setState(new EnforcementState(false));
  161. }
  162. }