AuthSettingsControllerTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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\Settings\Controller;
  22. use OC\AppFramework\Http;
  23. use OC\Authentication\Exceptions\InvalidTokenException;
  24. use OC\Authentication\Token\DefaultToken;
  25. use OC\Authentication\Token\IProvider;
  26. use OC\Authentication\Token\IToken;
  27. use OC\Settings\Controller\AuthSettingsController;
  28. use OCP\AppFramework\Http\JSONResponse;
  29. use OCP\IRequest;
  30. use OCP\ISession;
  31. use OCP\IUser;
  32. use OCP\IUserManager;
  33. use OCP\Security\ISecureRandom;
  34. use OCP\Session\Exceptions\SessionNotAvailableException;
  35. use Test\TestCase;
  36. class AuthSettingsControllerTest extends TestCase {
  37. /** @var AuthSettingsController */
  38. private $controller;
  39. private $request;
  40. /** @var IProvider|\PHPUnit_Framework_MockObject_MockObject */
  41. private $tokenProvider;
  42. private $userManager;
  43. private $session;
  44. private $secureRandom;
  45. private $uid;
  46. protected function setUp() {
  47. parent::setUp();
  48. $this->request = $this->createMock(IRequest::class);
  49. $this->tokenProvider = $this->createMock(IProvider::class);
  50. $this->userManager = $this->createMock(IUserManager::class);
  51. $this->session = $this->createMock(ISession::class);
  52. $this->secureRandom = $this->createMock(ISecureRandom::class);
  53. $this->uid = 'jane';
  54. $this->user = $this->createMock(IUser::class);
  55. $this->controller = new AuthSettingsController('core', $this->request, $this->tokenProvider, $this->userManager, $this->session, $this->secureRandom, $this->uid);
  56. }
  57. public function testIndex() {
  58. $token1 = new DefaultToken();
  59. $token1->setId(100);
  60. $token2 = new DefaultToken();
  61. $token2->setId(200);
  62. $tokens = [
  63. $token1,
  64. $token2,
  65. ];
  66. $sessionToken = new DefaultToken();
  67. $sessionToken->setId(100);
  68. $this->tokenProvider->expects($this->once())
  69. ->method('getTokenByUser')
  70. ->with($this->uid)
  71. ->will($this->returnValue($tokens));
  72. $this->session->expects($this->once())
  73. ->method('getId')
  74. ->will($this->returnValue('session123'));
  75. $this->tokenProvider->expects($this->once())
  76. ->method('getToken')
  77. ->with('session123')
  78. ->will($this->returnValue($sessionToken));
  79. $this->assertEquals([
  80. [
  81. 'id' => 100,
  82. 'name' => null,
  83. 'lastActivity' => 0,
  84. 'type' => 0,
  85. 'canDelete' => false,
  86. 'current' => true,
  87. 'scope' => ['filesystem' => true]
  88. ],
  89. [
  90. 'id' => 200,
  91. 'name' => null,
  92. 'lastActivity' => 0,
  93. 'type' => 0,
  94. 'canDelete' => true,
  95. 'scope' => ['filesystem' => true]
  96. ]
  97. ], $this->controller->index());
  98. }
  99. public function testCreate() {
  100. $name = 'Nexus 4';
  101. $sessionToken = $this->createMock(IToken::class);
  102. $deviceToken = $this->createMock(IToken::class);
  103. $password = '123456';
  104. $this->session->expects($this->once())
  105. ->method('getId')
  106. ->will($this->returnValue('sessionid'));
  107. $this->tokenProvider->expects($this->once())
  108. ->method('getToken')
  109. ->with('sessionid')
  110. ->will($this->returnValue($sessionToken));
  111. $this->tokenProvider->expects($this->once())
  112. ->method('getPassword')
  113. ->with($sessionToken, 'sessionid')
  114. ->will($this->returnValue($password));
  115. $sessionToken->expects($this->once())
  116. ->method('getLoginName')
  117. ->will($this->returnValue('User13'));
  118. $this->secureRandom->expects($this->exactly(5))
  119. ->method('generate')
  120. ->with(5, ISecureRandom::CHAR_HUMAN_READABLE)
  121. ->will($this->returnValue('XXXXX'));
  122. $newToken = 'XXXXX-XXXXX-XXXXX-XXXXX-XXXXX';
  123. $this->tokenProvider->expects($this->once())
  124. ->method('generateToken')
  125. ->with($newToken, $this->uid, 'User13', $password, $name, IToken::PERMANENT_TOKEN)
  126. ->will($this->returnValue($deviceToken));
  127. $deviceToken->expects($this->once())
  128. ->method('jsonSerialize')
  129. ->will($this->returnValue(['dummy' => 'dummy', 'canDelete' => true]));
  130. $expected = [
  131. 'token' => $newToken,
  132. 'deviceToken' => ['dummy' => 'dummy', 'canDelete' => true],
  133. 'loginName' => 'User13',
  134. ];
  135. $response = $this->controller->create($name);
  136. $this->assertInstanceOf(JSONResponse::class, $response);
  137. $this->assertEquals($expected, $response->getData());
  138. }
  139. public function testCreateSessionNotAvailable() {
  140. $name = 'personal phone';
  141. $this->session->expects($this->once())
  142. ->method('getId')
  143. ->will($this->throwException(new SessionNotAvailableException()));
  144. $expected = new JSONResponse();
  145. $expected->setStatus(Http::STATUS_SERVICE_UNAVAILABLE);
  146. $this->assertEquals($expected, $this->controller->create($name));
  147. }
  148. public function testCreateInvalidToken() {
  149. $name = 'Company IPhone';
  150. $this->session->expects($this->once())
  151. ->method('getId')
  152. ->will($this->returnValue('sessionid'));
  153. $this->tokenProvider->expects($this->once())
  154. ->method('getToken')
  155. ->with('sessionid')
  156. ->will($this->throwException(new InvalidTokenException()));
  157. $expected = new JSONResponse();
  158. $expected->setStatus(Http::STATUS_SERVICE_UNAVAILABLE);
  159. $this->assertEquals($expected, $this->controller->create($name));
  160. }
  161. public function testDestroy() {
  162. $id = 123;
  163. $user = $this->createMock(IUser::class);
  164. $this->tokenProvider->expects($this->once())
  165. ->method('invalidateTokenById')
  166. ->with($this->uid, $id);
  167. $this->assertEquals([], $this->controller->destroy($id));
  168. }
  169. public function testUpdateToken() {
  170. $token = $this->createMock(DefaultToken::class);
  171. $this->tokenProvider->expects($this->once())
  172. ->method('getTokenById')
  173. ->with($this->equalTo(42))
  174. ->willReturn($token);
  175. $token->expects($this->once())
  176. ->method('getUID')
  177. ->willReturn('jane');
  178. $token->expects($this->once())
  179. ->method('setScope')
  180. ->with($this->equalTo([
  181. 'filesystem' => true
  182. ]));
  183. $this->tokenProvider->expects($this->once())
  184. ->method('updateToken')
  185. ->with($this->equalTo($token));
  186. $this->assertSame([], $this->controller->update(42, ['filesystem' => true]));
  187. }
  188. public function testUpdateTokenWrongUser() {
  189. $token = $this->createMock(DefaultToken::class);
  190. $this->tokenProvider->expects($this->once())
  191. ->method('getTokenById')
  192. ->with($this->equalTo(42))
  193. ->willReturn($token);
  194. $token->expects($this->once())
  195. ->method('getUID')
  196. ->willReturn('foobar');
  197. $token->expects($this->never())
  198. ->method('setScope');
  199. $this->tokenProvider->expects($this->never())
  200. ->method('updateToken');
  201. $response = $this->controller->update(42, ['filesystem' => true]);
  202. $this->assertSame([], $response->getData());
  203. $this->assertSame(\OCP\AppFramework\Http::STATUS_NOT_FOUND, $response->getStatus());
  204. }
  205. public function testUpdateTokenNonExisting() {
  206. $this->tokenProvider->expects($this->once())
  207. ->method('getTokenById')
  208. ->with($this->equalTo(42))
  209. ->willThrowException(new InvalidTokenException('Token does not exist'));
  210. $this->tokenProvider->expects($this->never())
  211. ->method('updateToken');
  212. $response = $this->controller->update(42, ['filesystem' => true]);
  213. $this->assertSame([], $response->getData());
  214. $this->assertSame(\OCP\AppFramework\Http::STATUS_NOT_FOUND, $response->getStatus());
  215. }
  216. }