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 12KB

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