AvatarControllerTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Core\Controller;
  8. /**
  9. * Overwrite is_uploaded_file in the OC\Core\Controller namespace to allow
  10. * proper unit testing of the postAvatar call.
  11. */
  12. function is_uploaded_file($filename) {
  13. return file_exists($filename);
  14. }
  15. namespace Tests\Core\Controller;
  16. use OC\AppFramework\Utility\TimeFactory;
  17. use OC\Core\Controller\AvatarController;
  18. use OCP\AppFramework\Http;
  19. use OCP\Files\File;
  20. use OCP\Files\IRootFolder;
  21. use OCP\Files\NotFoundException;
  22. use OCP\Files\NotPermittedException;
  23. use OCP\Files\SimpleFS\ISimpleFile;
  24. use OCP\IAvatar;
  25. use OCP\IAvatarManager;
  26. use OCP\ICache;
  27. use OCP\IL10N;
  28. use OCP\IRequest;
  29. use OCP\IUser;
  30. use OCP\IUserManager;
  31. use Psr\Log\LoggerInterface;
  32. /**
  33. * Class AvatarControllerTest
  34. *
  35. * @package OC\Core\Controller
  36. */
  37. class AvatarControllerTest extends \Test\TestCase {
  38. /** @var AvatarController */
  39. private $avatarController;
  40. /** @var IAvatar|\PHPUnit\Framework\MockObject\MockObject */
  41. private $avatarMock;
  42. /** @var IUser|\PHPUnit\Framework\MockObject\MockObject */
  43. private $userMock;
  44. /** @var ISimpleFile|\PHPUnit\Framework\MockObject\MockObject */
  45. private $avatarFile;
  46. /** @var IAvatarManager|\PHPUnit\Framework\MockObject\MockObject */
  47. private $avatarManager;
  48. /** @var ICache|\PHPUnit\Framework\MockObject\MockObject */
  49. private $cache;
  50. /** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */
  51. private $l;
  52. /** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */
  53. private $userManager;
  54. /** @var IRootFolder|\PHPUnit\Framework\MockObject\MockObject */
  55. private $rootFolder;
  56. /** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */
  57. private $logger;
  58. /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
  59. private $request;
  60. /** @var TimeFactory|\PHPUnit\Framework\MockObject\MockObject */
  61. private $timeFactory;
  62. protected function setUp(): void {
  63. parent::setUp();
  64. $this->avatarManager = $this->getMockBuilder('OCP\IAvatarManager')->getMock();
  65. $this->cache = $this->getMockBuilder('OCP\ICache')
  66. ->disableOriginalConstructor()->getMock();
  67. $this->l = $this->getMockBuilder(IL10N::class)->getMock();
  68. $this->l->method('t')->willReturnArgument(0);
  69. $this->userManager = $this->getMockBuilder(IUserManager::class)->getMock();
  70. $this->request = $this->getMockBuilder(IRequest::class)->getMock();
  71. $this->rootFolder = $this->getMockBuilder('OCP\Files\IRootFolder')->getMock();
  72. $this->logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
  73. $this->timeFactory = $this->getMockBuilder('OC\AppFramework\Utility\TimeFactory')->getMock();
  74. $this->avatarMock = $this->getMockBuilder('OCP\IAvatar')->getMock();
  75. $this->userMock = $this->getMockBuilder(IUser::class)->getMock();
  76. $this->avatarController = new AvatarController(
  77. 'core',
  78. $this->request,
  79. $this->avatarManager,
  80. $this->cache,
  81. $this->l,
  82. $this->userManager,
  83. $this->rootFolder,
  84. $this->logger,
  85. 'userid',
  86. $this->timeFactory
  87. );
  88. // Configure userMock
  89. $this->userMock->method('getDisplayName')->willReturn('displayName');
  90. $this->userMock->method('getUID')->willReturn('userId');
  91. $this->userManager->method('get')
  92. ->willReturnMap([['userId', $this->userMock]]);
  93. $this->avatarFile = $this->getMockBuilder(ISimpleFile::class)->getMock();
  94. $this->avatarFile->method('getContent')->willReturn('image data');
  95. $this->avatarFile->method('getMimeType')->willReturn('image type');
  96. $this->avatarFile->method('getEtag')->willReturn('my etag');
  97. $this->avatarFile->method('getName')->willReturn('my name');
  98. $this->avatarFile->method('getMTime')->willReturn(42);
  99. }
  100. protected function tearDown(): void {
  101. parent::tearDown();
  102. }
  103. /**
  104. * Fetch an avatar if a user has no avatar
  105. */
  106. public function testGetAvatarNoAvatar() {
  107. $this->avatarManager->method('getAvatar')->willReturn($this->avatarMock);
  108. $this->avatarMock->method('getFile')->will($this->throwException(new NotFoundException()));
  109. $response = $this->avatarController->getAvatar('userId', 32);
  110. //Comment out until JS is fixed
  111. $this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus());
  112. }
  113. /**
  114. * Fetch the user's avatar
  115. */
  116. public function testGetAvatar() {
  117. $this->avatarMock->method('getFile')->willReturn($this->avatarFile);
  118. $this->avatarManager->method('getAvatar')->with('userId')->willReturn($this->avatarMock);
  119. $this->avatarMock->expects($this->once())
  120. ->method('isCustomAvatar')
  121. ->willReturn(true);
  122. $response = $this->avatarController->getAvatar('userId', 32);
  123. $this->assertEquals(Http::STATUS_OK, $response->getStatus());
  124. $this->assertArrayHasKey('Content-Type', $response->getHeaders());
  125. $this->assertEquals('image type', $response->getHeaders()['Content-Type']);
  126. $this->assertArrayHasKey('X-NC-IsCustomAvatar', $response->getHeaders());
  127. $this->assertEquals('1', $response->getHeaders()['X-NC-IsCustomAvatar']);
  128. $this->assertEquals('my etag', $response->getETag());
  129. }
  130. /**
  131. * Fetch the user's avatar
  132. */
  133. public function testGetGeneratedAvatar() {
  134. $this->avatarMock->method('getFile')->willReturn($this->avatarFile);
  135. $this->avatarManager->method('getAvatar')->with('userId')->willReturn($this->avatarMock);
  136. $response = $this->avatarController->getAvatar('userId', 32);
  137. $this->assertEquals(Http::STATUS_OK, $response->getStatus());
  138. $this->assertArrayHasKey('Content-Type', $response->getHeaders());
  139. $this->assertEquals('image type', $response->getHeaders()['Content-Type']);
  140. $this->assertArrayHasKey('X-NC-IsCustomAvatar', $response->getHeaders());
  141. $this->assertEquals('0', $response->getHeaders()['X-NC-IsCustomAvatar']);
  142. $this->assertEquals('my etag', $response->getETag());
  143. }
  144. /**
  145. * Fetch the avatar of a non-existing user
  146. */
  147. public function testGetAvatarNoUser() {
  148. $this->avatarManager
  149. ->method('getAvatar')
  150. ->with('userDoesNotExist')
  151. ->will($this->throwException(new \Exception('user does not exist')));
  152. $response = $this->avatarController->getAvatar('userDoesNotExist', 32);
  153. //Comment out until JS is fixed
  154. $this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus());
  155. }
  156. public function testGetAvatarSize64(): void {
  157. $this->avatarMock->expects($this->once())
  158. ->method('getFile')
  159. ->with($this->equalTo(64))
  160. ->willReturn($this->avatarFile);
  161. $this->avatarManager->method('getAvatar')->willReturn($this->avatarMock);
  162. $this->logger->expects($this->never())
  163. ->method('debug');
  164. $this->avatarController->getAvatar('userId', 64);
  165. }
  166. public function testGetAvatarSize512(): void {
  167. $this->avatarMock->expects($this->once())
  168. ->method('getFile')
  169. ->with($this->equalTo(512))
  170. ->willReturn($this->avatarFile);
  171. $this->avatarManager->method('getAvatar')->willReturn($this->avatarMock);
  172. $this->logger->expects($this->never())
  173. ->method('debug');
  174. $this->avatarController->getAvatar('userId', 512);
  175. }
  176. /**
  177. * Small sizes return 64 and generate a log
  178. */
  179. public function testGetAvatarSizeTooSmall(): void {
  180. $this->avatarMock->expects($this->once())
  181. ->method('getFile')
  182. ->with($this->equalTo(64))
  183. ->willReturn($this->avatarFile);
  184. $this->avatarManager->method('getAvatar')->willReturn($this->avatarMock);
  185. $this->logger->expects($this->once())
  186. ->method('debug')
  187. ->with('Avatar requested in deprecated size 32');
  188. $this->avatarController->getAvatar('userId', 32);
  189. }
  190. /**
  191. * Avatars between 64 and 512 are upgraded to 512
  192. */
  193. public function testGetAvatarSizeBetween(): void {
  194. $this->avatarMock->expects($this->once())
  195. ->method('getFile')
  196. ->with($this->equalTo(512))
  197. ->willReturn($this->avatarFile);
  198. $this->avatarManager->method('getAvatar')->willReturn($this->avatarMock);
  199. $this->logger->expects($this->once())
  200. ->method('debug')
  201. ->with('Avatar requested in deprecated size 65');
  202. $this->avatarController->getAvatar('userId', 65);
  203. }
  204. /**
  205. * We do not support avatars larger than 512
  206. */
  207. public function testGetAvatarSizeTooBig(): void {
  208. $this->avatarMock->expects($this->once())
  209. ->method('getFile')
  210. ->with($this->equalTo(512))
  211. ->willReturn($this->avatarFile);
  212. $this->avatarManager->method('getAvatar')->willReturn($this->avatarMock);
  213. $this->logger->expects($this->once())
  214. ->method('debug')
  215. ->with('Avatar requested in deprecated size 513');
  216. $this->avatarController->getAvatar('userId', 513);
  217. }
  218. /**
  219. * Remove an avatar
  220. */
  221. public function testDeleteAvatar() {
  222. $this->avatarManager->method('getAvatar')->willReturn($this->avatarMock);
  223. $response = $this->avatarController->deleteAvatar();
  224. $this->assertEquals(Http::STATUS_OK, $response->getStatus());
  225. }
  226. /**
  227. * Test what happens if the removing of the avatar fails
  228. */
  229. public function testDeleteAvatarException() {
  230. $this->avatarMock->method('remove')->will($this->throwException(new \Exception("foo")));
  231. $this->avatarManager->method('getAvatar')->willReturn($this->avatarMock);
  232. $this->logger->expects($this->once())
  233. ->method('error')
  234. ->with('foo', ['exception' => new \Exception("foo"), 'app' => 'core']);
  235. $expectedResponse = new Http\JSONResponse(['data' => ['message' => 'An error occurred. Please contact your admin.']], Http::STATUS_BAD_REQUEST);
  236. $this->assertEquals($expectedResponse, $this->avatarController->deleteAvatar());
  237. }
  238. /**
  239. * Trying to get a tmp avatar when it is not available. 404
  240. */
  241. public function testTmpAvatarNoTmp() {
  242. $response = $this->avatarController->getTmpAvatar();
  243. $this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus());
  244. }
  245. /**
  246. * Fetch tmp avatar
  247. */
  248. public function testTmpAvatarValid() {
  249. $this->cache->method('get')->willReturn(file_get_contents(\OC::$SERVERROOT.'/tests/data/testimage.jpg'));
  250. $response = $this->avatarController->getTmpAvatar();
  251. $this->assertEquals(Http::STATUS_OK, $response->getStatus());
  252. }
  253. /**
  254. * When trying to post a new avatar a path or image should be posted.
  255. */
  256. public function testPostAvatarNoPathOrImage() {
  257. $response = $this->avatarController->postAvatar(null);
  258. $this->assertEquals(Http::STATUS_BAD_REQUEST, $response->getStatus());
  259. }
  260. /**
  261. * Test a correct post of an avatar using POST
  262. */
  263. public function testPostAvatarFile() {
  264. //Create temp file
  265. $fileName = tempnam('', "avatarTest");
  266. $copyRes = copy(\OC::$SERVERROOT.'/tests/data/testimage.jpg', $fileName);
  267. $this->assertTrue($copyRes);
  268. //Create file in cache
  269. $this->cache->method('get')->willReturn(file_get_contents(\OC::$SERVERROOT.'/tests/data/testimage.jpg'));
  270. //Create request return
  271. $reqRet = ['error' => [0], 'tmp_name' => [$fileName], 'size' => [filesize(\OC::$SERVERROOT.'/tests/data/testimage.jpg')]];
  272. $this->request->method('getUploadedFile')->willReturn($reqRet);
  273. $response = $this->avatarController->postAvatar(null);
  274. //On correct upload always respond with the notsquare message
  275. $this->assertEquals('notsquare', $response->getData()['data']);
  276. //File should be deleted
  277. $this->assertFalse(file_exists($fileName));
  278. }
  279. /**
  280. * Test invalid post os an avatar using POST
  281. */
  282. public function testPostAvatarInvalidFile() {
  283. //Create request return
  284. $reqRet = ['error' => [1], 'tmp_name' => ['foo']];
  285. $this->request->method('getUploadedFile')->willReturn($reqRet);
  286. $response = $this->avatarController->postAvatar(null);
  287. $this->assertEquals(Http::STATUS_BAD_REQUEST, $response->getStatus());
  288. }
  289. /**
  290. * Check what happens when we upload a GIF
  291. */
  292. public function testPostAvatarFileGif() {
  293. //Create temp file
  294. $fileName = tempnam('', "avatarTest");
  295. $copyRes = copy(\OC::$SERVERROOT.'/tests/data/testimage.gif', $fileName);
  296. $this->assertTrue($copyRes);
  297. //Create file in cache
  298. $this->cache->method('get')->willReturn(file_get_contents(\OC::$SERVERROOT.'/tests/data/testimage.gif'));
  299. //Create request return
  300. $reqRet = ['error' => [0], 'tmp_name' => [$fileName], 'size' => [filesize(\OC::$SERVERROOT.'/tests/data/testimage.gif')]];
  301. $this->request->method('getUploadedFile')->willReturn($reqRet);
  302. $response = $this->avatarController->postAvatar(null);
  303. $this->assertEquals('Unknown filetype', $response->getData()['data']['message']);
  304. //File should be deleted
  305. $this->assertFalse(file_exists($fileName));
  306. }
  307. /**
  308. * Test posting avatar from existing file
  309. */
  310. public function testPostAvatarFromFile() {
  311. //Mock node API call
  312. $file = $this->getMockBuilder('OCP\Files\File')
  313. ->disableOriginalConstructor()->getMock();
  314. $file->expects($this->once())
  315. ->method('getContent')
  316. ->willReturn(file_get_contents(\OC::$SERVERROOT.'/tests/data/testimage.jpg'));
  317. $file->expects($this->once())
  318. ->method('getMimeType')
  319. ->willReturn('image/jpeg');
  320. $userFolder = $this->getMockBuilder('OCP\Files\Folder')->getMock();
  321. $this->rootFolder->method('getUserFolder')->with('userid')->willReturn($userFolder);
  322. $userFolder->method('get')->willReturn($file);
  323. //Create request return
  324. $response = $this->avatarController->postAvatar('avatar.jpg');
  325. //On correct upload always respond with the notsquare message
  326. $this->assertEquals('notsquare', $response->getData()['data']);
  327. }
  328. /**
  329. * Test posting avatar from existing folder
  330. */
  331. public function testPostAvatarFromNoFile() {
  332. $file = $this->getMockBuilder('OCP\Files\Node')->getMock();
  333. $userFolder = $this->getMockBuilder('OCP\Files\Folder')->getMock();
  334. $this->rootFolder->method('getUserFolder')->with('userid')->willReturn($userFolder);
  335. $userFolder
  336. ->method('get')
  337. ->with('folder')
  338. ->willReturn($file);
  339. //Create request return
  340. $response = $this->avatarController->postAvatar('folder');
  341. //On correct upload always respond with the notsquare message
  342. $this->assertEquals(['data' => ['message' => 'Please select a file.']], $response->getData());
  343. }
  344. public function testPostAvatarInvalidType() {
  345. $file = $this->getMockBuilder('OCP\Files\File')
  346. ->disableOriginalConstructor()->getMock();
  347. $file->expects($this->never())
  348. ->method('getContent');
  349. $file->expects($this->exactly(2))
  350. ->method('getMimeType')
  351. ->willReturn('text/plain');
  352. $userFolder = $this->getMockBuilder('OCP\Files\Folder')->getMock();
  353. $this->rootFolder->method('getUserFolder')->with('userid')->willReturn($userFolder);
  354. $userFolder->method('get')->willReturn($file);
  355. $expectedResponse = new Http\JSONResponse(['data' => ['message' => 'The selected file is not an image.']], Http::STATUS_BAD_REQUEST);
  356. $this->assertEquals($expectedResponse, $this->avatarController->postAvatar('avatar.jpg'));
  357. }
  358. public function testPostAvatarNotPermittedException() {
  359. $file = $this->getMockBuilder('OCP\Files\File')
  360. ->disableOriginalConstructor()->getMock();
  361. $file->expects($this->once())
  362. ->method('getContent')
  363. ->willThrowException(new NotPermittedException());
  364. $file->expects($this->once())
  365. ->method('getMimeType')
  366. ->willReturn('image/jpeg');
  367. $userFolder = $this->getMockBuilder('OCP\Files\Folder')->getMock();
  368. $this->rootFolder->method('getUserFolder')->with('userid')->willReturn($userFolder);
  369. $userFolder->method('get')->willReturn($file);
  370. $expectedResponse = new Http\JSONResponse(['data' => ['message' => 'The selected file cannot be read.']], Http::STATUS_BAD_REQUEST);
  371. $this->assertEquals($expectedResponse, $this->avatarController->postAvatar('avatar.jpg'));
  372. }
  373. /**
  374. * Test what happens if the upload of the avatar fails
  375. */
  376. public function testPostAvatarException() {
  377. $this->cache->expects($this->once())
  378. ->method('set')
  379. ->will($this->throwException(new \Exception("foo")));
  380. $file = $this->getMockBuilder('OCP\Files\File')
  381. ->disableOriginalConstructor()->getMock();
  382. $file->expects($this->once())
  383. ->method('getContent')
  384. ->willReturn(file_get_contents(\OC::$SERVERROOT.'/tests/data/testimage.jpg'));
  385. $file->expects($this->once())
  386. ->method('getMimeType')
  387. ->willReturn('image/jpeg');
  388. $userFolder = $this->getMockBuilder('OCP\Files\Folder')->getMock();
  389. $this->rootFolder->method('getUserFolder')->with('userid')->willReturn($userFolder);
  390. $userFolder->method('get')->willReturn($file);
  391. $this->logger->expects($this->once())
  392. ->method('error')
  393. ->with('foo', ['exception' => new \Exception("foo"), 'app' => 'core']);
  394. $expectedResponse = new Http\JSONResponse(['data' => ['message' => 'An error occurred. Please contact your admin.']], Http::STATUS_OK);
  395. $this->assertEquals($expectedResponse, $this->avatarController->postAvatar('avatar.jpg'));
  396. }
  397. /**
  398. * Test invalid crop argument
  399. */
  400. public function testPostCroppedAvatarInvalidCrop() {
  401. $response = $this->avatarController->postCroppedAvatar([]);
  402. $this->assertEquals(Http::STATUS_BAD_REQUEST, $response->getStatus());
  403. }
  404. /**
  405. * Test no tmp avatar to crop
  406. */
  407. public function testPostCroppedAvatarNoTmpAvatar() {
  408. $response = $this->avatarController->postCroppedAvatar(['x' => 0, 'y' => 0, 'w' => 10, 'h' => 10]);
  409. $this->assertEquals(Http::STATUS_BAD_REQUEST, $response->getStatus());
  410. }
  411. /**
  412. * Test with non square crop
  413. */
  414. public function testPostCroppedAvatarNoSquareCrop() {
  415. $this->cache->method('get')->willReturn(file_get_contents(\OC::$SERVERROOT.'/tests/data/testimage.jpg'));
  416. $this->avatarMock->method('set')->will($this->throwException(new \OC\NotSquareException));
  417. $this->avatarManager->method('getAvatar')->willReturn($this->avatarMock);
  418. $response = $this->avatarController->postCroppedAvatar(['x' => 0, 'y' => 0, 'w' => 10, 'h' => 11]);
  419. $this->assertEquals(Http::STATUS_BAD_REQUEST, $response->getStatus());
  420. }
  421. /**
  422. * Check for proper reply on proper crop argument
  423. */
  424. public function testPostCroppedAvatarValidCrop() {
  425. $this->cache->method('get')->willReturn(file_get_contents(\OC::$SERVERROOT.'/tests/data/testimage.jpg'));
  426. $this->avatarManager->method('getAvatar')->willReturn($this->avatarMock);
  427. $response = $this->avatarController->postCroppedAvatar(['x' => 0, 'y' => 0, 'w' => 10, 'h' => 10]);
  428. $this->assertEquals(Http::STATUS_OK, $response->getStatus());
  429. $this->assertEquals('success', $response->getData()['status']);
  430. }
  431. /**
  432. * Test what happens if the cropping of the avatar fails
  433. */
  434. public function testPostCroppedAvatarException() {
  435. $this->cache->method('get')->willReturn(file_get_contents(\OC::$SERVERROOT.'/tests/data/testimage.jpg'));
  436. $this->avatarMock->method('set')->will($this->throwException(new \Exception('foo')));
  437. $this->avatarManager->method('getAvatar')->willReturn($this->avatarMock);
  438. $this->logger->expects($this->once())
  439. ->method('error')
  440. ->with('foo', ['exception' => new \Exception("foo"), 'app' => 'core']);
  441. $expectedResponse = new Http\JSONResponse(['data' => ['message' => 'An error occurred. Please contact your admin.']], Http::STATUS_BAD_REQUEST);
  442. $this->assertEquals($expectedResponse, $this->avatarController->postCroppedAvatar(['x' => 0, 'y' => 0, 'w' => 10, 'h' => 11]));
  443. }
  444. /**
  445. * Check for proper reply on proper crop argument
  446. */
  447. public function testFileTooBig() {
  448. $fileName = \OC::$SERVERROOT.'/tests/data/testimage.jpg';
  449. //Create request return
  450. $reqRet = ['error' => [0], 'tmp_name' => [$fileName], 'size' => [21 * 1024 * 1024]];
  451. $this->request->method('getUploadedFile')->willReturn($reqRet);
  452. $response = $this->avatarController->postAvatar(null);
  453. $this->assertEquals('File is too big', $response->getData()['data']['message']);
  454. }
  455. }