TwoFactorChallengeControllerTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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\Controller;
  22. use OC\Core\Controller\TwoFactorChallengeController;
  23. use Test\TestCase;
  24. class TwoFactorChallengeControllerTest extends TestCase {
  25. private $request;
  26. private $twoFactorManager;
  27. private $userSession;
  28. private $session;
  29. private $urlGenerator;
  30. /** @var TwoFactorChallengeController|\PHPUnit_Framework_MockObject_MockObject */
  31. private $controller;
  32. protected function setUp() {
  33. parent::setUp();
  34. $this->request = $this->getMockBuilder('\OCP\IRequest')->getMock();
  35. $this->twoFactorManager = $this->getMockBuilder('\OC\Authentication\TwoFactorAuth\Manager')
  36. ->disableOriginalConstructor()
  37. ->getMock();
  38. $this->userSession = $this->getMockBuilder('\OCP\IUserSession')->getMock();
  39. $this->session = $this->getMockBuilder('\OCP\ISession')->getMock();
  40. $this->urlGenerator = $this->getMockBuilder('\OCP\IURLGenerator')->getMock();
  41. $this->controller = $this->getMockBuilder('OC\Core\Controller\TwoFactorChallengeController')
  42. ->setConstructorArgs([
  43. 'core',
  44. $this->request,
  45. $this->twoFactorManager,
  46. $this->userSession,
  47. $this->session,
  48. $this->urlGenerator,
  49. ])
  50. ->setMethods(['getLogoutAttribute'])
  51. ->getMock();
  52. $this->controller->expects($this->any())
  53. ->method('getLogoutAttribute')
  54. ->willReturn('logoutAttribute');
  55. }
  56. public function testSelectChallenge() {
  57. $user = $this->getMockBuilder('\OCP\IUser')->getMock();
  58. $providers = [
  59. 'prov1',
  60. 'prov2',
  61. ];
  62. $this->userSession->expects($this->once())
  63. ->method('getUser')
  64. ->will($this->returnValue($user));
  65. $this->twoFactorManager->expects($this->once())
  66. ->method('getProviders')
  67. ->with($user)
  68. ->will($this->returnValue($providers));
  69. $expected = new \OCP\AppFramework\Http\TemplateResponse('core', 'twofactorselectchallenge', [
  70. 'providers' => $providers,
  71. 'redirect_url' => '/some/url',
  72. 'logout_attribute' => 'logoutAttribute',
  73. ], 'guest');
  74. $this->assertEquals($expected, $this->controller->selectChallenge('/some/url'));
  75. }
  76. public function testShowChallenge() {
  77. $user = $this->getMockBuilder('\OCP\IUser')->getMock();
  78. $provider = $this->getMockBuilder('\OCP\Authentication\TwoFactorAuth\IProvider')
  79. ->disableOriginalConstructor()
  80. ->getMock();
  81. $tmpl = $this->getMockBuilder('\OCP\Template')
  82. ->disableOriginalConstructor()
  83. ->getMock();
  84. $this->userSession->expects($this->once())
  85. ->method('getUser')
  86. ->will($this->returnValue($user));
  87. $this->twoFactorManager->expects($this->once())
  88. ->method('getProvider')
  89. ->with($user, 'myprovider')
  90. ->will($this->returnValue($provider));
  91. $this->session->expects($this->once())
  92. ->method('exists')
  93. ->with('two_factor_auth_error')
  94. ->will($this->returnValue(true));
  95. $this->session->expects($this->once())
  96. ->method('remove')
  97. ->with('two_factor_auth_error');
  98. $provider->expects($this->once())
  99. ->method('getTemplate')
  100. ->with($user)
  101. ->will($this->returnValue($tmpl));
  102. $tmpl->expects($this->once())
  103. ->method('fetchPage')
  104. ->will($this->returnValue('<html/>'));
  105. $expected = new \OCP\AppFramework\Http\TemplateResponse('core', 'twofactorshowchallenge', [
  106. 'error' => true,
  107. 'provider' => $provider,
  108. 'logout_attribute' => 'logoutAttribute',
  109. 'template' => '<html/>',
  110. ], 'guest');
  111. $this->assertEquals($expected, $this->controller->showChallenge('myprovider', '/re/dir/ect/url'));
  112. }
  113. public function testShowInvalidChallenge() {
  114. $user = $this->getMockBuilder('\OCP\IUser')->getMock();
  115. $this->userSession->expects($this->once())
  116. ->method('getUser')
  117. ->will($this->returnValue($user));
  118. $this->twoFactorManager->expects($this->once())
  119. ->method('getProvider')
  120. ->with($user, 'myprovider')
  121. ->will($this->returnValue(null));
  122. $this->urlGenerator->expects($this->once())
  123. ->method('linkToRoute')
  124. ->with('core.TwoFactorChallenge.selectChallenge')
  125. ->will($this->returnValue('select/challenge/url'));
  126. $expected = new \OCP\AppFramework\Http\RedirectResponse('select/challenge/url');
  127. $this->assertEquals($expected, $this->controller->showChallenge('myprovider', 'redirect/url'));
  128. }
  129. public function testSolveChallenge() {
  130. $user = $this->getMockBuilder('\OCP\IUser')->getMock();
  131. $provider = $this->getMockBuilder('\OCP\Authentication\TwoFactorAuth\IProvider')
  132. ->disableOriginalConstructor()
  133. ->getMock();
  134. $this->userSession->expects($this->once())
  135. ->method('getUser')
  136. ->will($this->returnValue($user));
  137. $this->twoFactorManager->expects($this->once())
  138. ->method('getProvider')
  139. ->with($user, 'myprovider')
  140. ->will($this->returnValue($provider));
  141. $this->twoFactorManager->expects($this->once())
  142. ->method('verifyChallenge')
  143. ->with('myprovider', $user, 'token')
  144. ->will($this->returnValue(true));
  145. $expected = new \OCP\AppFramework\Http\RedirectResponse(\OC_Util::getDefaultPageUrl());
  146. $this->assertEquals($expected, $this->controller->solveChallenge('myprovider', 'token'));
  147. }
  148. public function testSolveChallengeInvalidProvider() {
  149. $user = $this->getMockBuilder('\OCP\IUser')->getMock();
  150. $this->userSession->expects($this->once())
  151. ->method('getUser')
  152. ->will($this->returnValue($user));
  153. $this->twoFactorManager->expects($this->once())
  154. ->method('getProvider')
  155. ->with($user, 'myprovider')
  156. ->will($this->returnValue(null));
  157. $this->urlGenerator->expects($this->once())
  158. ->method('linkToRoute')
  159. ->with('core.TwoFactorChallenge.selectChallenge')
  160. ->will($this->returnValue('select/challenge/url'));
  161. $expected = new \OCP\AppFramework\Http\RedirectResponse('select/challenge/url');
  162. $this->assertEquals($expected, $this->controller->solveChallenge('myprovider', 'token'));
  163. }
  164. public function testSolveInvalidChallenge() {
  165. $user = $this->getMockBuilder('\OCP\IUser')->getMock();
  166. $provider = $this->getMockBuilder('\OCP\Authentication\TwoFactorAuth\IProvider')
  167. ->disableOriginalConstructor()
  168. ->getMock();
  169. $this->userSession->expects($this->once())
  170. ->method('getUser')
  171. ->will($this->returnValue($user));
  172. $this->twoFactorManager->expects($this->once())
  173. ->method('getProvider')
  174. ->with($user, 'myprovider')
  175. ->will($this->returnValue($provider));
  176. $this->twoFactorManager->expects($this->once())
  177. ->method('verifyChallenge')
  178. ->with('myprovider', $user, 'token')
  179. ->will($this->returnValue(false));
  180. $this->session->expects($this->once())
  181. ->method('set')
  182. ->with('two_factor_auth_error', true);
  183. $this->urlGenerator->expects($this->once())
  184. ->method('linkToRoute')
  185. ->with('core.TwoFactorChallenge.showChallenge', [
  186. 'challengeProviderId' => 'myprovider',
  187. 'redirect_url' => '/url',
  188. ])
  189. ->will($this->returnValue('files/index/url'));
  190. $provider->expects($this->once())
  191. ->method('getId')
  192. ->will($this->returnValue('myprovider'));
  193. $expected = new \OCP\AppFramework\Http\RedirectResponse('files/index/url');
  194. $this->assertEquals($expected, $this->controller->solveChallenge('myprovider', 'token', '/url'));
  195. }
  196. }