AvatarControllerTest.php 17 KB

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