You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AuthSettingsControllerTest.php 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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\Activity\IEvent;
  29. use OCP\Activity\IManager;
  30. use OCP\AppFramework\Http\JSONResponse;
  31. use OCP\ILogger;
  32. use OCP\IRequest;
  33. use OCP\ISession;
  34. use OCP\Security\ISecureRandom;
  35. use OCP\Session\Exceptions\SessionNotAvailableException;
  36. use Test\TestCase;
  37. class AuthSettingsControllerTest extends TestCase {
  38. /** @var AuthSettingsController */
  39. private $controller;
  40. /** @var IRequest|\PHPUnit_Framework_MockObject_MockObject */
  41. private $request;
  42. /** @var IProvider|\PHPUnit_Framework_MockObject_MockObject */
  43. private $tokenProvider;
  44. /** @var ISession|\PHPUnit_Framework_MockObject_MockObject */
  45. private $session;
  46. /** @var ISecureRandom|\PHPUnit_Framework_MockObject_MockObject */
  47. private $secureRandom;
  48. /** @var IManager|\PHPUnit_Framework_MockObject_MockObject */
  49. private $activityManager;
  50. private $uid = 'jane';
  51. protected function setUp() {
  52. parent::setUp();
  53. $this->request = $this->createMock(IRequest::class);
  54. $this->tokenProvider = $this->createMock(IProvider::class);
  55. $this->session = $this->createMock(ISession::class);
  56. $this->secureRandom = $this->createMock(ISecureRandom::class);
  57. $this->activityManager = $this->createMock(IManager::class);
  58. /** @var ILogger|\PHPUnit_Framework_MockObject_MockObject $logger */
  59. $logger = $this->createMock(ILogger::class);
  60. $this->controller = new AuthSettingsController(
  61. 'core',
  62. $this->request,
  63. $this->tokenProvider,
  64. $this->session,
  65. $this->secureRandom,
  66. $this->uid,
  67. $this->activityManager,
  68. $logger
  69. );
  70. }
  71. public function testCreate() {
  72. $name = 'Nexus 4';
  73. $sessionToken = $this->createMock(IToken::class);
  74. $deviceToken = $this->createMock(IToken::class);
  75. $password = '123456';
  76. $this->session->expects($this->once())
  77. ->method('getId')
  78. ->willReturn('sessionid');
  79. $this->tokenProvider->expects($this->once())
  80. ->method('getToken')
  81. ->with('sessionid')
  82. ->willReturn($sessionToken);
  83. $this->tokenProvider->expects($this->once())
  84. ->method('getPassword')
  85. ->with($sessionToken, 'sessionid')
  86. ->willReturn($password);
  87. $sessionToken->expects($this->once())
  88. ->method('getLoginName')
  89. ->willReturn('User13');
  90. $this->secureRandom->expects($this->exactly(5))
  91. ->method('generate')
  92. ->with(5, ISecureRandom::CHAR_HUMAN_READABLE)
  93. ->willReturn('XXXXX');
  94. $newToken = 'XXXXX-XXXXX-XXXXX-XXXXX-XXXXX';
  95. $this->tokenProvider->expects($this->once())
  96. ->method('generateToken')
  97. ->with($newToken, $this->uid, 'User13', $password, $name, IToken::PERMANENT_TOKEN)
  98. ->willReturn($deviceToken);
  99. $deviceToken->expects($this->once())
  100. ->method('jsonSerialize')
  101. ->willReturn(['dummy' => 'dummy', 'canDelete' => true]);
  102. $this->mockActivityManager();
  103. $expected = [
  104. 'token' => $newToken,
  105. 'deviceToken' => ['dummy' => 'dummy', 'canDelete' => true, 'canRename' => true],
  106. 'loginName' => 'User13',
  107. ];
  108. $response = $this->controller->create($name);
  109. $this->assertInstanceOf(JSONResponse::class, $response);
  110. $this->assertEquals($expected, $response->getData());
  111. }
  112. public function testCreateSessionNotAvailable() {
  113. $name = 'personal phone';
  114. $this->session->expects($this->once())
  115. ->method('getId')
  116. ->will($this->throwException(new SessionNotAvailableException()));
  117. $expected = new JSONResponse();
  118. $expected->setStatus(Http::STATUS_SERVICE_UNAVAILABLE);
  119. $this->assertEquals($expected, $this->controller->create($name));
  120. }
  121. public function testCreateInvalidToken() {
  122. $name = 'Company IPhone';
  123. $this->session->expects($this->once())
  124. ->method('getId')
  125. ->willReturn('sessionid');
  126. $this->tokenProvider->expects($this->once())
  127. ->method('getToken')
  128. ->with('sessionid')
  129. ->will($this->throwException(new InvalidTokenException()));
  130. $expected = new JSONResponse();
  131. $expected->setStatus(Http::STATUS_SERVICE_UNAVAILABLE);
  132. $this->assertEquals($expected, $this->controller->create($name));
  133. }
  134. public function testDestroy() {
  135. $tokenId = 124;
  136. $token = $this->createMock(DefaultToken::class);
  137. $this->mockGetTokenById($tokenId, $token);
  138. $this->mockActivityManager();
  139. $token->expects($this->exactly(2))
  140. ->method('getId')
  141. ->willReturn($tokenId);
  142. $token->expects($this->once())
  143. ->method('getUID')
  144. ->willReturn('jane');
  145. $this->tokenProvider->expects($this->once())
  146. ->method('invalidateTokenById')
  147. ->with($this->uid, $tokenId);
  148. $this->assertEquals([], $this->controller->destroy($tokenId));
  149. }
  150. public function testDestroyWrongUser() {
  151. $tokenId = 124;
  152. $token = $this->createMock(DefaultToken::class);
  153. $this->mockGetTokenById($tokenId, $token);
  154. $token->expects($this->once())
  155. ->method('getUID')
  156. ->willReturn('foobar');
  157. $response = $this->controller->destroy($tokenId);
  158. $this->assertSame([], $response->getData());
  159. $this->assertSame(\OCP\AppFramework\Http::STATUS_NOT_FOUND, $response->getStatus());
  160. }
  161. public function dataRenameToken(): array {
  162. return [
  163. 'App password => Other token name' => ['App password', 'Other token name'],
  164. 'Other token name => App password' => ['Other token name', 'App password'],
  165. ];
  166. }
  167. /**
  168. * @dataProvider dataRenameToken
  169. * @param string $name
  170. * @param string $newName
  171. */
  172. public function testUpdateRename(string $name, string $newName): void {
  173. $tokenId = 42;
  174. $token = $this->createMock(DefaultToken::class);
  175. $this->mockGetTokenById($tokenId, $token);
  176. $this->mockActivityManager();
  177. $token->expects($this->once())
  178. ->method('getUID')
  179. ->willReturn('jane');
  180. $token->expects($this->once())
  181. ->method('getName')
  182. ->willReturn($name);
  183. $token->expects($this->once())
  184. ->method('getScopeAsArray')
  185. ->willReturn(['filesystem' => true]);
  186. $token->expects($this->once())
  187. ->method('setName')
  188. ->with($this->equalTo($newName));
  189. $this->tokenProvider->expects($this->once())
  190. ->method('updateToken')
  191. ->with($this->equalTo($token));
  192. $this->assertSame([], $this->controller->update($tokenId, ['filesystem' => true], $newName));
  193. }
  194. public function dataUpdateFilesystemScope(): array {
  195. return [
  196. 'Grant filesystem access' => [false, true],
  197. 'Revoke filesystem access' => [true, false],
  198. ];
  199. }
  200. /**
  201. * @dataProvider dataUpdateFilesystemScope
  202. * @param bool $filesystem
  203. * @param bool $newFilesystem
  204. */
  205. public function testUpdateFilesystemScope(bool $filesystem, bool $newFilesystem): void {
  206. $tokenId = 42;
  207. $token = $this->createMock(DefaultToken::class);
  208. $this->mockGetTokenById($tokenId, $token);
  209. $this->mockActivityManager();
  210. $token->expects($this->once())
  211. ->method('getUID')
  212. ->willReturn('jane');
  213. $token->expects($this->once())
  214. ->method('getName')
  215. ->willReturn('App password');
  216. $token->expects($this->once())
  217. ->method('getScopeAsArray')
  218. ->willReturn(['filesystem' => $filesystem]);
  219. $token->expects($this->once())
  220. ->method('setScope')
  221. ->with($this->equalTo(['filesystem' => $newFilesystem]));
  222. $this->tokenProvider->expects($this->once())
  223. ->method('updateToken')
  224. ->with($this->equalTo($token));
  225. $this->assertSame([], $this->controller->update($tokenId, ['filesystem' => $newFilesystem], 'App password'));
  226. }
  227. public function testUpdateNoChange(): void {
  228. $tokenId = 42;
  229. $token = $this->createMock(DefaultToken::class);
  230. $this->mockGetTokenById($tokenId, $token);
  231. $token->expects($this->once())
  232. ->method('getUID')
  233. ->willReturn('jane');
  234. $token->expects($this->once())
  235. ->method('getName')
  236. ->willReturn('App password');
  237. $token->expects($this->once())
  238. ->method('getScopeAsArray')
  239. ->willReturn(['filesystem' => true]);
  240. $token->expects($this->never())
  241. ->method('setName');
  242. $token->expects($this->never())
  243. ->method('setScope');
  244. $this->tokenProvider->expects($this->once())
  245. ->method('updateToken')
  246. ->with($this->equalTo($token));
  247. $this->assertSame([], $this->controller->update($tokenId, ['filesystem' => true], 'App password'));
  248. }
  249. public function testUpdateTokenWrongUser() {
  250. $tokenId = 42;
  251. $token = $this->createMock(DefaultToken::class);
  252. $this->mockGetTokenById($tokenId, $token);
  253. $token->expects($this->once())
  254. ->method('getUID')
  255. ->willReturn('foobar');
  256. $token->expects($this->never())
  257. ->method('setScope');
  258. $this->tokenProvider->expects($this->never())
  259. ->method('updateToken');
  260. $response = $this->controller->update($tokenId, ['filesystem' => true], 'App password');
  261. $this->assertSame([], $response->getData());
  262. $this->assertSame(\OCP\AppFramework\Http::STATUS_NOT_FOUND, $response->getStatus());
  263. }
  264. public function testUpdateTokenNonExisting() {
  265. $this->tokenProvider->expects($this->once())
  266. ->method('getTokenById')
  267. ->with($this->equalTo(42))
  268. ->willThrowException(new InvalidTokenException('Token does not exist'));
  269. $this->tokenProvider->expects($this->never())
  270. ->method('updateToken');
  271. $response = $this->controller->update(42, ['filesystem' => true], 'App password');
  272. $this->assertSame([], $response->getData());
  273. $this->assertSame(\OCP\AppFramework\Http::STATUS_NOT_FOUND, $response->getStatus());
  274. }
  275. private function mockActivityManager(): void {
  276. $this->activityManager->expects($this->once())
  277. ->method('generateEvent')
  278. ->willReturn($this->createMock(IEvent::class));
  279. $this->activityManager->expects($this->once())
  280. ->method('publish');
  281. }
  282. /**
  283. * @param int $tokenId
  284. * @param $token
  285. */
  286. private function mockGetTokenById(int $tokenId, $token): void {
  287. $this->tokenProvider->expects($this->once())
  288. ->method('getTokenById')
  289. ->with($this->equalTo($tokenId))
  290. ->willReturn($token);
  291. }
  292. }