TwoFactorChallengeControllerTest.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. $this->twoFactorManager->expects($this->once())
  70. ->method('getBackupProvider')
  71. ->with($user)
  72. ->will($this->returnValue('backup'));
  73. $expected = new \OCP\AppFramework\Http\TemplateResponse('core', 'twofactorselectchallenge', [
  74. 'providers' => $providers,
  75. 'backupProvider' => 'backup',
  76. 'redirect_url' => '/some/url',
  77. 'logout_attribute' => 'logoutAttribute',
  78. ], 'guest');
  79. $this->assertEquals($expected, $this->controller->selectChallenge('/some/url'));
  80. }
  81. public function testShowChallenge() {
  82. $user = $this->getMockBuilder('\OCP\IUser')->getMock();
  83. $provider = $this->getMockBuilder('\OCP\Authentication\TwoFactorAuth\IProvider')
  84. ->disableOriginalConstructor()
  85. ->getMock();
  86. $backupProvider = $this->getMockBuilder('\OCP\Authentication\TwoFactorAuth\IProvider')
  87. ->disableOriginalConstructor()
  88. ->getMock();
  89. $tmpl = $this->getMockBuilder('\OCP\Template')
  90. ->disableOriginalConstructor()
  91. ->getMock();
  92. $this->userSession->expects($this->once())
  93. ->method('getUser')
  94. ->will($this->returnValue($user));
  95. $this->twoFactorManager->expects($this->once())
  96. ->method('getProvider')
  97. ->with($user, 'myprovider')
  98. ->will($this->returnValue($provider));
  99. $this->twoFactorManager->expects($this->once())
  100. ->method('getBackupProvider')
  101. ->with($user)
  102. ->will($this->returnValue($backupProvider));
  103. $provider->expects($this->once())
  104. ->method('getId')
  105. ->will($this->returnValue('u2f'));
  106. $backupProvider->expects($this->once())
  107. ->method('getId')
  108. ->will($this->returnValue('backup_codes'));
  109. $this->session->expects($this->once())
  110. ->method('exists')
  111. ->with('two_factor_auth_error')
  112. ->will($this->returnValue(true));
  113. $this->session->expects($this->once())
  114. ->method('remove')
  115. ->with('two_factor_auth_error');
  116. $provider->expects($this->once())
  117. ->method('getTemplate')
  118. ->with($user)
  119. ->will($this->returnValue($tmpl));
  120. $tmpl->expects($this->once())
  121. ->method('fetchPage')
  122. ->will($this->returnValue('<html/>'));
  123. $expected = new \OCP\AppFramework\Http\TemplateResponse('core', 'twofactorshowchallenge', [
  124. 'error' => true,
  125. 'provider' => $provider,
  126. 'backupProvider' => $backupProvider,
  127. 'logout_attribute' => 'logoutAttribute',
  128. 'template' => '<html/>',
  129. 'redirect_url' => '/re/dir/ect/url',
  130. ], 'guest');
  131. $this->assertEquals($expected, $this->controller->showChallenge('myprovider', '/re/dir/ect/url'));
  132. }
  133. public function testShowInvalidChallenge() {
  134. $user = $this->getMockBuilder('\OCP\IUser')->getMock();
  135. $this->userSession->expects($this->once())
  136. ->method('getUser')
  137. ->will($this->returnValue($user));
  138. $this->twoFactorManager->expects($this->once())
  139. ->method('getProvider')
  140. ->with($user, 'myprovider')
  141. ->will($this->returnValue(null));
  142. $this->urlGenerator->expects($this->once())
  143. ->method('linkToRoute')
  144. ->with('core.TwoFactorChallenge.selectChallenge')
  145. ->will($this->returnValue('select/challenge/url'));
  146. $expected = new \OCP\AppFramework\Http\RedirectResponse('select/challenge/url');
  147. $this->assertEquals($expected, $this->controller->showChallenge('myprovider', 'redirect/url'));
  148. }
  149. public function testSolveChallenge() {
  150. $user = $this->getMockBuilder('\OCP\IUser')->getMock();
  151. $provider = $this->getMockBuilder('\OCP\Authentication\TwoFactorAuth\IProvider')
  152. ->disableOriginalConstructor()
  153. ->getMock();
  154. $this->userSession->expects($this->once())
  155. ->method('getUser')
  156. ->will($this->returnValue($user));
  157. $this->twoFactorManager->expects($this->once())
  158. ->method('getProvider')
  159. ->with($user, 'myprovider')
  160. ->will($this->returnValue($provider));
  161. $this->twoFactorManager->expects($this->once())
  162. ->method('verifyChallenge')
  163. ->with('myprovider', $user, 'token')
  164. ->will($this->returnValue(true));
  165. $expected = new \OCP\AppFramework\Http\RedirectResponse(\OC_Util::getDefaultPageUrl());
  166. $this->assertEquals($expected, $this->controller->solveChallenge('myprovider', 'token'));
  167. }
  168. public function testSolveChallengeInvalidProvider() {
  169. $user = $this->getMockBuilder('\OCP\IUser')->getMock();
  170. $this->userSession->expects($this->once())
  171. ->method('getUser')
  172. ->will($this->returnValue($user));
  173. $this->twoFactorManager->expects($this->once())
  174. ->method('getProvider')
  175. ->with($user, 'myprovider')
  176. ->will($this->returnValue(null));
  177. $this->urlGenerator->expects($this->once())
  178. ->method('linkToRoute')
  179. ->with('core.TwoFactorChallenge.selectChallenge')
  180. ->will($this->returnValue('select/challenge/url'));
  181. $expected = new \OCP\AppFramework\Http\RedirectResponse('select/challenge/url');
  182. $this->assertEquals($expected, $this->controller->solveChallenge('myprovider', 'token'));
  183. }
  184. public function testSolveInvalidChallenge() {
  185. $user = $this->getMockBuilder('\OCP\IUser')->getMock();
  186. $provider = $this->getMockBuilder('\OCP\Authentication\TwoFactorAuth\IProvider')
  187. ->disableOriginalConstructor()
  188. ->getMock();
  189. $this->userSession->expects($this->once())
  190. ->method('getUser')
  191. ->will($this->returnValue($user));
  192. $this->twoFactorManager->expects($this->once())
  193. ->method('getProvider')
  194. ->with($user, 'myprovider')
  195. ->will($this->returnValue($provider));
  196. $this->twoFactorManager->expects($this->once())
  197. ->method('verifyChallenge')
  198. ->with('myprovider', $user, 'token')
  199. ->will($this->returnValue(false));
  200. $this->session->expects($this->once())
  201. ->method('set')
  202. ->with('two_factor_auth_error', true);
  203. $this->urlGenerator->expects($this->once())
  204. ->method('linkToRoute')
  205. ->with('core.TwoFactorChallenge.showChallenge', [
  206. 'challengeProviderId' => 'myprovider',
  207. 'redirect_url' => '/url',
  208. ])
  209. ->will($this->returnValue('files/index/url'));
  210. $provider->expects($this->once())
  211. ->method('getId')
  212. ->will($this->returnValue('myprovider'));
  213. $expected = new \OCP\AppFramework\Http\RedirectResponse('files/index/url');
  214. $this->assertEquals($expected, $this->controller->solveChallenge('myprovider', 'token', '/url'));
  215. }
  216. }