ClientFlowLoginV2ControllerTest.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2019, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  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 Test\Core\Controller;
  25. use OC\Core\Controller\ClientFlowLoginV2Controller;
  26. use OC\Core\Data\LoginFlowV2Credentials;
  27. use OC\Core\Db\LoginFlowV2;
  28. use OC\Core\Exception\LoginFlowV2NotFoundException;
  29. use OC\Core\Service\LoginFlowV2Service;
  30. use OCP\AppFramework\Http;
  31. use OCP\Defaults;
  32. use OCP\IL10N;
  33. use OCP\IRequest;
  34. use OCP\ISession;
  35. use OCP\IURLGenerator;
  36. use OCP\Security\ISecureRandom;
  37. use PHPUnit\Framework\MockObject\MockObject;
  38. use Test\TestCase;
  39. class ClientFlowLoginV2ControllerTest extends TestCase {
  40. /** @var IRequest|MockObject */
  41. private $request;
  42. /** @var LoginFlowV2Service|MockObject */
  43. private $loginFlowV2Service;
  44. /** @var IURLGenerator|MockObject */
  45. private $urlGenerator;
  46. /** @var ISession|MockObject */
  47. private $session;
  48. /** @var ISecureRandom|MockObject */
  49. private $random;
  50. /** @var Defaults|MockObject */
  51. private $defaults;
  52. /** @var IL10N|MockObject */
  53. private $l;
  54. /** @var ClientFlowLoginV2Controller */
  55. private $controller;
  56. public function setUp() {
  57. parent::setUp();
  58. $this->request = $this->createMock(IRequest::class);
  59. $this->loginFlowV2Service = $this->createMock(LoginFlowV2Service::class);
  60. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  61. $this->session = $this->createMock(ISession::class);
  62. $this->random = $this->createMock(ISecureRandom::class);
  63. $this->defaults = $this->createMock(Defaults::class);
  64. $this->l = $this->createMock(IL10N::class);
  65. $this->controller = new ClientFlowLoginV2Controller(
  66. 'core',
  67. $this->request,
  68. $this->loginFlowV2Service,
  69. $this->urlGenerator,
  70. $this->session,
  71. $this->random,
  72. $this->defaults,
  73. 'user',
  74. $this->l
  75. );
  76. }
  77. public function testPollInvalid() {
  78. $this->loginFlowV2Service->method('poll')
  79. ->with('token')
  80. ->willThrowException(new LoginFlowV2NotFoundException());
  81. $result = $this->controller->poll('token');
  82. $this->assertSame([], $result->getData());
  83. $this->assertSame(Http::STATUS_NOT_FOUND, $result->getStatus());
  84. }
  85. public function testPollValid() {
  86. $creds = new LoginFlowV2Credentials('server', 'login', 'pass');
  87. $this->loginFlowV2Service->method('poll')
  88. ->with('token')
  89. ->willReturn($creds);
  90. $result = $this->controller->poll('token');
  91. $this->assertSame($creds, $result->getData());
  92. $this->assertSame(Http::STATUS_OK, $result->getStatus());
  93. }
  94. public function testLandingInvalid() {
  95. $this->session->expects($this->never())
  96. ->method($this->anything());
  97. $this->loginFlowV2Service->method('startLoginFlow')
  98. ->with('token')
  99. ->willReturn(false);
  100. $result = $this->controller->landing('token');
  101. $this->assertSame(Http::STATUS_FORBIDDEN, $result->getStatus());
  102. $this->assertInstanceOf(Http\StandaloneTemplateResponse::class, $result);
  103. }
  104. public function testLandingValid() {
  105. $this->session->expects($this->once())
  106. ->method('set')
  107. ->with('client.flow.v2.login.token', 'token');
  108. $this->loginFlowV2Service->method('startLoginFlow')
  109. ->with('token')
  110. ->willReturn(true);
  111. $this->urlGenerator->method('linkToRouteAbsolute')
  112. ->with('core.ClientFlowLoginV2.showAuthPickerPage')
  113. ->willReturn('https://server/path');
  114. $result = $this->controller->landing('token');
  115. $this->assertInstanceOf(Http\RedirectResponse::class, $result);
  116. $this->assertSame(Http::STATUS_SEE_OTHER, $result->getStatus());
  117. $this->assertSame('https://server/path', $result->getRedirectURL());
  118. }
  119. public function testShowAuthPickerNoLoginToken() {
  120. $this->session->method('get')
  121. ->willReturn(null);
  122. $result = $this->controller->showAuthPickerPage();
  123. $this->assertSame(Http::STATUS_FORBIDDEN, $result->getStatus());
  124. }
  125. public function testShowAuthPickerInvalidLoginToken() {
  126. $this->session->method('get')
  127. ->with('client.flow.v2.login.token')
  128. ->willReturn('loginToken');
  129. $this->loginFlowV2Service->method('getByLoginToken')
  130. ->with('loginToken')
  131. ->willThrowException(new LoginFlowV2NotFoundException());
  132. $result = $this->controller->showAuthPickerPage();
  133. $this->assertSame(Http::STATUS_FORBIDDEN, $result->getStatus());
  134. }
  135. public function testShowAuthPickerValidLoginToken() {
  136. $this->session->method('get')
  137. ->with('client.flow.v2.login.token')
  138. ->willReturn('loginToken');
  139. $flow = new LoginFlowV2();
  140. $this->loginFlowV2Service->method('getByLoginToken')
  141. ->with('loginToken')
  142. ->willReturn($flow);
  143. $this->random->method('generate')
  144. ->with(64, ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_DIGITS)
  145. ->willReturn('random');
  146. $this->session->expects($this->once())
  147. ->method('set')
  148. ->with('client.flow.v2.state.token', 'random');
  149. $this->controller->showAuthPickerPage();
  150. }
  151. public function testGrantPageInvalidStateToken() {
  152. $this->session->method('get')
  153. ->will($this->returnCallback(function($name) {
  154. return null;
  155. }));
  156. $result = $this->controller->grantPage('stateToken');
  157. $this->assertSame(Http::STATUS_FORBIDDEN, $result->getStatus());
  158. }
  159. public function testGrantPageInvalidLoginToken() {
  160. $this->session->method('get')
  161. ->will($this->returnCallback(function($name) {
  162. if ($name === 'client.flow.v2.state.token') {
  163. return 'stateToken';
  164. }
  165. if ($name === 'client.flow.v2.login.token') {
  166. return 'loginToken';
  167. }
  168. return null;
  169. }));
  170. $this->loginFlowV2Service->method('getByLoginToken')
  171. ->with('loginToken')
  172. ->willThrowException(new LoginFlowV2NotFoundException());
  173. $result = $this->controller->grantPage('stateToken');
  174. $this->assertSame(Http::STATUS_FORBIDDEN, $result->getStatus());
  175. }
  176. public function testGrantPageValid() {
  177. $this->session->method('get')
  178. ->will($this->returnCallback(function($name) {
  179. if ($name === 'client.flow.v2.state.token') {
  180. return 'stateToken';
  181. }
  182. if ($name === 'client.flow.v2.login.token') {
  183. return 'loginToken';
  184. }
  185. return null;
  186. }));
  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() {
  195. $this->session->method('get')
  196. ->will($this->returnCallback(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() {
  203. $this->session->method('get')
  204. ->will($this->returnCallback(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() {
  220. $this->session->method('get')
  221. ->will($this->returnCallback(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. ->will($this->returnCallback(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. }