TwoFactorMiddlewareTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. /**
  3. * @author Christoph Wurst <christoph@owncloud.com>
  4. *
  5. * @copyright Copyright (c) 2016, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace Test\Core\Middleware;
  22. use OC\Core\Middleware\TwoFactorMiddleware;
  23. use OC\AppFramework\Http\Request;
  24. use Test\TestCase;
  25. class TwoFactorMiddlewareTest extends TestCase {
  26. private $twoFactorManager;
  27. private $userSession;
  28. private $session;
  29. private $urlGenerator;
  30. private $reflector;
  31. private $request;
  32. /** @var TwoFactorMiddleware */
  33. private $middleware;
  34. protected function setUp() {
  35. parent::setUp();
  36. $this->twoFactorManager = $this->getMockBuilder('\OC\Authentication\TwoFactorAuth\Manager')
  37. ->disableOriginalConstructor()
  38. ->getMock();
  39. $this->userSession = $this->getMockBuilder('\OC\User\Session')
  40. ->disableOriginalConstructor()
  41. ->getMock();
  42. $this->session = $this->getMock('\OCP\ISession');
  43. $this->urlGenerator = $this->getMock('\OCP\IURLGenerator');
  44. $this->reflector = $this->getMock('\OCP\AppFramework\Utility\IControllerMethodReflector');
  45. $this->request = new Request(
  46. [
  47. 'server' => [
  48. 'REQUEST_URI' => 'test/url'
  49. ]
  50. ],
  51. $this->getMock('\OCP\Security\ISecureRandom'),
  52. $this->getMock('\OCP\IConfig')
  53. );
  54. $this->middleware = new TwoFactorMiddleware($this->twoFactorManager, $this->userSession, $this->session, $this->urlGenerator, $this->reflector, $this->request);
  55. }
  56. public function testBeforeControllerNotLoggedIn() {
  57. $this->reflector->expects($this->once())
  58. ->method('hasAnnotation')
  59. ->with('PublicPage')
  60. ->will($this->returnValue(false));
  61. $this->userSession->expects($this->once())
  62. ->method('isLoggedIn')
  63. ->will($this->returnValue(false));
  64. $this->userSession->expects($this->never())
  65. ->method('getUser');
  66. $this->middleware->beforeController(null, 'index');
  67. }
  68. public function testBeforeControllerPublicPage() {
  69. $this->reflector->expects($this->once())
  70. ->method('hasAnnotation')
  71. ->with('PublicPage')
  72. ->will($this->returnValue(true));
  73. $this->userSession->expects($this->never())
  74. ->method('isLoggedIn');
  75. $this->middleware->beforeController(null, 'create');
  76. }
  77. public function testBeforeControllerNoTwoFactorCheckNeeded() {
  78. $user = $this->getMock('\OCP\IUser');
  79. $this->reflector->expects($this->once())
  80. ->method('hasAnnotation')
  81. ->with('PublicPage')
  82. ->will($this->returnValue(false));
  83. $this->userSession->expects($this->once())
  84. ->method('isLoggedIn')
  85. ->will($this->returnValue(true));
  86. $this->userSession->expects($this->once())
  87. ->method('getUser')
  88. ->will($this->returnValue($user));
  89. $this->twoFactorManager->expects($this->once())
  90. ->method('isTwoFactorAuthenticated')
  91. ->with($user)
  92. ->will($this->returnValue(false));
  93. $this->middleware->beforeController(null, 'index');
  94. }
  95. /**
  96. * @expectedException \OC\Authentication\Exceptions\TwoFactorAuthRequiredException
  97. */
  98. public function testBeforeControllerTwoFactorAuthRequired() {
  99. $user = $this->getMock('\OCP\IUser');
  100. $this->reflector->expects($this->once())
  101. ->method('hasAnnotation')
  102. ->with('PublicPage')
  103. ->will($this->returnValue(false));
  104. $this->userSession->expects($this->once())
  105. ->method('isLoggedIn')
  106. ->will($this->returnValue(true));
  107. $this->userSession->expects($this->once())
  108. ->method('getUser')
  109. ->will($this->returnValue($user));
  110. $this->twoFactorManager->expects($this->once())
  111. ->method('isTwoFactorAuthenticated')
  112. ->with($user)
  113. ->will($this->returnValue(true));
  114. $this->twoFactorManager->expects($this->once())
  115. ->method('needsSecondFactor')
  116. ->will($this->returnValue(true));
  117. $this->middleware->beforeController(null, 'index');
  118. }
  119. /**
  120. * @expectedException \OC\Authentication\Exceptions\UserAlreadyLoggedInException
  121. */
  122. public function testBeforeControllerUserAlreadyLoggedIn() {
  123. $user = $this->getMock('\OCP\IUser');
  124. $this->reflector->expects($this->once())
  125. ->method('hasAnnotation')
  126. ->with('PublicPage')
  127. ->will($this->returnValue(false));
  128. $this->userSession->expects($this->once())
  129. ->method('isLoggedIn')
  130. ->will($this->returnValue(true));
  131. $this->userSession->expects($this->once())
  132. ->method('getUser')
  133. ->will($this->returnValue($user));
  134. $this->twoFactorManager->expects($this->once())
  135. ->method('isTwoFactorAuthenticated')
  136. ->with($user)
  137. ->will($this->returnValue(true));
  138. $this->twoFactorManager->expects($this->once())
  139. ->method('needsSecondFactor')
  140. ->will($this->returnValue(false));
  141. $twoFactorChallengeController = $this->getMockBuilder('\OC\Core\Controller\TwoFactorChallengeController')
  142. ->disableOriginalConstructor()
  143. ->getMock();
  144. $this->middleware->beforeController($twoFactorChallengeController, 'index');
  145. }
  146. public function testAfterExceptionTwoFactorAuthRequired() {
  147. $ex = new \OC\Authentication\Exceptions\TwoFactorAuthRequiredException();
  148. $this->urlGenerator->expects($this->once())
  149. ->method('linkToRoute')
  150. ->with('core.TwoFactorChallenge.selectChallenge')
  151. ->will($this->returnValue('test/url'));
  152. $expected = new \OCP\AppFramework\Http\RedirectResponse('test/url');
  153. $this->assertEquals($expected, $this->middleware->afterException(null, 'index', $ex));
  154. }
  155. public function testAfterException() {
  156. $ex = new \OC\Authentication\Exceptions\UserAlreadyLoggedInException();
  157. $this->urlGenerator->expects($this->once())
  158. ->method('linkToRoute')
  159. ->with('files.view.index')
  160. ->will($this->returnValue('redirect/url'));
  161. $expected = new \OCP\AppFramework\Http\RedirectResponse('redirect/url');
  162. $this->assertEquals($expected, $this->middleware->afterException(null, 'index', $ex));
  163. }
  164. }