ClientFlowLoginV2ControllerTest.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\Core\Controller;
  8. use OC\Core\Controller\ClientFlowLoginV2Controller;
  9. use OC\Core\Data\LoginFlowV2Credentials;
  10. use OC\Core\Db\LoginFlowV2;
  11. use OC\Core\Exception\LoginFlowV2NotFoundException;
  12. use OC\Core\Service\LoginFlowV2Service;
  13. use OCP\AppFramework\Http;
  14. use OCP\Defaults;
  15. use OCP\IL10N;
  16. use OCP\IRequest;
  17. use OCP\ISession;
  18. use OCP\IURLGenerator;
  19. use OCP\IUser;
  20. use OCP\IUserSession;
  21. use OCP\Security\ISecureRandom;
  22. use PHPUnit\Framework\MockObject\MockObject;
  23. use Test\TestCase;
  24. class ClientFlowLoginV2ControllerTest extends TestCase {
  25. /** @var IRequest|MockObject */
  26. private $request;
  27. /** @var LoginFlowV2Service|MockObject */
  28. private $loginFlowV2Service;
  29. /** @var IURLGenerator|MockObject */
  30. private $urlGenerator;
  31. /** @var ISession|MockObject */
  32. private $session;
  33. /** @var IUserSession|MockObject */
  34. private $userSession;
  35. /** @var ISecureRandom|MockObject */
  36. private $random;
  37. /** @var Defaults|MockObject */
  38. private $defaults;
  39. /** @var IL10N|MockObject */
  40. private $l;
  41. /** @var ClientFlowLoginV2Controller */
  42. private $controller;
  43. protected function setUp(): void {
  44. parent::setUp();
  45. $this->request = $this->createMock(IRequest::class);
  46. $this->loginFlowV2Service = $this->createMock(LoginFlowV2Service::class);
  47. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  48. $this->session = $this->createMock(ISession::class);
  49. $this->userSession = $this->createMock(IUserSession::class);
  50. $this->random = $this->createMock(ISecureRandom::class);
  51. $this->defaults = $this->createMock(Defaults::class);
  52. $this->l = $this->createMock(IL10N::class);
  53. $this->controller = new ClientFlowLoginV2Controller(
  54. 'core',
  55. $this->request,
  56. $this->loginFlowV2Service,
  57. $this->urlGenerator,
  58. $this->session,
  59. $this->userSession,
  60. $this->random,
  61. $this->defaults,
  62. 'user',
  63. $this->l
  64. );
  65. }
  66. public function testPollInvalid(): void {
  67. $this->loginFlowV2Service->method('poll')
  68. ->with('token')
  69. ->willThrowException(new LoginFlowV2NotFoundException());
  70. $result = $this->controller->poll('token');
  71. $this->assertSame([], $result->getData());
  72. $this->assertSame(Http::STATUS_NOT_FOUND, $result->getStatus());
  73. }
  74. public function testPollValid(): void {
  75. $creds = new LoginFlowV2Credentials('server', 'login', 'pass');
  76. $this->loginFlowV2Service->method('poll')
  77. ->with('token')
  78. ->willReturn($creds);
  79. $result = $this->controller->poll('token');
  80. $this->assertSame($creds->jsonSerialize(), $result->getData());
  81. $this->assertSame(Http::STATUS_OK, $result->getStatus());
  82. }
  83. public function testLandingInvalid(): void {
  84. $this->session->expects($this->never())
  85. ->method($this->anything());
  86. $this->loginFlowV2Service->method('startLoginFlow')
  87. ->with('token')
  88. ->willReturn(false);
  89. $result = $this->controller->landing('token');
  90. $this->assertSame(Http::STATUS_FORBIDDEN, $result->getStatus());
  91. $this->assertInstanceOf(Http\StandaloneTemplateResponse::class, $result);
  92. }
  93. public function testLandingValid(): void {
  94. $this->session->expects($this->once())
  95. ->method('set')
  96. ->with('client.flow.v2.login.token', 'token');
  97. $this->loginFlowV2Service->method('startLoginFlow')
  98. ->with('token')
  99. ->willReturn(true);
  100. $this->urlGenerator->method('linkToRouteAbsolute')
  101. ->with('core.ClientFlowLoginV2.showAuthPickerPage')
  102. ->willReturn('https://server/path');
  103. $result = $this->controller->landing('token');
  104. $this->assertInstanceOf(Http\RedirectResponse::class, $result);
  105. $this->assertSame(Http::STATUS_SEE_OTHER, $result->getStatus());
  106. $this->assertSame('https://server/path', $result->getRedirectURL());
  107. }
  108. public function testShowAuthPickerNoLoginToken(): void {
  109. $this->session->method('get')
  110. ->willReturn(null);
  111. $result = $this->controller->showAuthPickerPage();
  112. $this->assertSame(Http::STATUS_FORBIDDEN, $result->getStatus());
  113. }
  114. public function testShowAuthPickerInvalidLoginToken(): void {
  115. $this->session->method('get')
  116. ->with('client.flow.v2.login.token')
  117. ->willReturn('loginToken');
  118. $this->loginFlowV2Service->method('getByLoginToken')
  119. ->with('loginToken')
  120. ->willThrowException(new LoginFlowV2NotFoundException());
  121. $result = $this->controller->showAuthPickerPage();
  122. $this->assertSame(Http::STATUS_FORBIDDEN, $result->getStatus());
  123. }
  124. public function testShowAuthPickerValidLoginToken(): void {
  125. $this->session->method('get')
  126. ->with('client.flow.v2.login.token')
  127. ->willReturn('loginToken');
  128. $flow = new LoginFlowV2();
  129. $this->loginFlowV2Service->method('getByLoginToken')
  130. ->with('loginToken')
  131. ->willReturn($flow);
  132. $this->random->method('generate')
  133. ->with(64, ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_DIGITS)
  134. ->willReturn('random');
  135. $this->session->expects($this->once())
  136. ->method('set')
  137. ->with('client.flow.v2.state.token', 'random');
  138. $this->controller->showAuthPickerPage();
  139. }
  140. public function testGrantPageNoStateToken(): void {
  141. $result = $this->controller->grantPage(null);
  142. $this->assertSame(Http::STATUS_FORBIDDEN, $result->getStatus());
  143. }
  144. public function testGrantPageInvalidStateToken(): void {
  145. $this->session->method('get')
  146. ->willReturnCallback(function ($name) {
  147. return null;
  148. });
  149. $result = $this->controller->grantPage('stateToken');
  150. $this->assertSame(Http::STATUS_FORBIDDEN, $result->getStatus());
  151. }
  152. public function testGrantPageInvalidLoginToken(): void {
  153. $this->session->method('get')
  154. ->willReturnCallback(function ($name) {
  155. if ($name === 'client.flow.v2.state.token') {
  156. return 'stateToken';
  157. }
  158. if ($name === 'client.flow.v2.login.token') {
  159. return 'loginToken';
  160. }
  161. return null;
  162. });
  163. $this->loginFlowV2Service->method('getByLoginToken')
  164. ->with('loginToken')
  165. ->willThrowException(new LoginFlowV2NotFoundException());
  166. $result = $this->controller->grantPage('stateToken');
  167. $this->assertSame(Http::STATUS_FORBIDDEN, $result->getStatus());
  168. }
  169. public function testGrantPageValid(): void {
  170. $this->session->method('get')
  171. ->willReturnCallback(function ($name) {
  172. if ($name === 'client.flow.v2.state.token') {
  173. return 'stateToken';
  174. }
  175. if ($name === 'client.flow.v2.login.token') {
  176. return 'loginToken';
  177. }
  178. return null;
  179. });
  180. $user = $this->createMock(IUser::class);
  181. $user->method('getUID')
  182. ->willReturn('uid');
  183. $user->method('getDisplayName')
  184. ->willReturn('display name');
  185. $this->userSession->method('getUser')
  186. ->willReturn($user);
  187. $flow = new LoginFlowV2();
  188. $this->loginFlowV2Service->method('getByLoginToken')
  189. ->with('loginToken')
  190. ->willReturn($flow);
  191. $result = $this->controller->grantPage('stateToken');
  192. $this->assertSame(Http::STATUS_OK, $result->getStatus());
  193. }
  194. public function testGenerateAppPasswordInvalidStateToken(): void {
  195. $this->session->method('get')
  196. ->willReturnCallback(function ($name) {
  197. return null;
  198. });
  199. $result = $this->controller->generateAppPassword('stateToken');
  200. $this->assertSame(Http::STATUS_FORBIDDEN, $result->getStatus());
  201. }
  202. public function testGenerateAppPassworInvalidLoginToken(): void {
  203. $this->session->method('get')
  204. ->willReturnCallback(function ($name) {
  205. if ($name === 'client.flow.v2.state.token') {
  206. return 'stateToken';
  207. }
  208. if ($name === 'client.flow.v2.login.token') {
  209. return 'loginToken';
  210. }
  211. return null;
  212. });
  213. $this->loginFlowV2Service->method('getByLoginToken')
  214. ->with('loginToken')
  215. ->willThrowException(new LoginFlowV2NotFoundException());
  216. $result = $this->controller->generateAppPassword('stateToken');
  217. $this->assertSame(Http::STATUS_FORBIDDEN, $result->getStatus());
  218. }
  219. public function testGenerateAppPassworValid(): void {
  220. $this->session->method('get')
  221. ->willReturnCallback(function ($name) {
  222. if ($name === 'client.flow.v2.state.token') {
  223. return 'stateToken';
  224. }
  225. if ($name === 'client.flow.v2.login.token') {
  226. return 'loginToken';
  227. }
  228. return null;
  229. });
  230. $flow = new LoginFlowV2();
  231. $this->loginFlowV2Service->method('getByLoginToken')
  232. ->with('loginToken')
  233. ->willReturn($flow);
  234. $clearedState = false;
  235. $clearedLogin = false;
  236. $this->session->method('remove')
  237. ->willReturnCallback(function ($name) use (&$clearedLogin, &$clearedState) {
  238. if ($name === 'client.flow.v2.state.token') {
  239. $clearedState = true;
  240. }
  241. if ($name === 'client.flow.v2.login.token') {
  242. $clearedLogin = true;
  243. }
  244. });
  245. $this->session->method('getId')
  246. ->willReturn('sessionId');
  247. $this->loginFlowV2Service->expects($this->once())
  248. ->method('flowDone')
  249. ->with(
  250. 'loginToken',
  251. 'sessionId',
  252. 'https://server',
  253. 'user'
  254. )->willReturn(true);
  255. $this->request->method('getServerProtocol')
  256. ->willReturn('https');
  257. $this->request->method('getRequestUri')
  258. ->willReturn('/login/v2');
  259. $this->request->method('getServerHost')
  260. ->willReturn('server');
  261. $result = $this->controller->generateAppPassword('stateToken');
  262. $this->assertSame(Http::STATUS_OK, $result->getStatus());
  263. $this->assertTrue($clearedLogin);
  264. $this->assertTrue($clearedState);
  265. }
  266. }