AvatarControllerTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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\Core\Application;
  31. use OCP\AppFramework\IAppContainer;
  32. use OCP\AppFramework\Http;
  33. use OCP\Files\File;
  34. use OCP\Files\NotFoundException;
  35. use OCP\IUser;
  36. use OCP\IAvatar;
  37. use Punic\Exception;
  38. use Test\Traits\UserTrait;
  39. /**
  40. * Class AvatarControllerTest
  41. *
  42. * @group DB
  43. *
  44. * @package OC\Core\Controller
  45. */
  46. class AvatarControllerTest extends \Test\TestCase {
  47. use UserTrait;
  48. /** @var IAppContainer */
  49. private $container;
  50. /** @var \OC\Core\Controller\AvatarController */
  51. private $avatarController;
  52. /** @var IAvatar */
  53. private $avatarMock;
  54. /** @var IUser */
  55. private $userMock;
  56. /** @var File */
  57. private $avatarFile;
  58. protected function setUp() {
  59. parent::setUp();
  60. $this->createUser('userid', 'pass');
  61. $this->loginAsUser('userid');
  62. $app = new Application;
  63. $this->container = $app->getContainer();
  64. $this->container['AppName'] = 'core';
  65. $this->container['AvatarManager'] = $this->getMockBuilder('OCP\IAvatarManager')->getMock();
  66. $this->container['Cache'] = $this->getMockBuilder('OC\Cache\File')
  67. ->disableOriginalConstructor()->getMock();
  68. $this->container['L10N'] = $this->getMockBuilder('OCP\IL10N')->getMock();
  69. $this->container['L10N']->method('t')->will($this->returnArgument(0));
  70. $this->container['UserManager'] = $this->getMockBuilder('OCP\IUserManager')->getMock();
  71. $this->container['UserSession'] = $this->getMockBuilder('OCP\IUserSession')->getMock();
  72. $this->container['Request'] = $this->getMockBuilder('OCP\IRequest')->getMock();
  73. $this->container['UserFolder'] = $this->getMockBuilder('OCP\Files\Folder')->getMock();
  74. $this->container['Logger'] = $this->getMockBuilder('OCP\ILogger')->getMock();
  75. $this->avatarMock = $this->getMockBuilder('OCP\IAvatar')->getMock();
  76. $this->userMock = $this->getMockBuilder('OCP\IUser')->getMock();
  77. $this->avatarController = $this->container['AvatarController'];
  78. // Configure userMock
  79. $this->userMock->method('getDisplayName')->willReturn('displayName');
  80. $this->userMock->method('getUID')->willReturn('userId');
  81. $this->container['UserManager']->method('get')
  82. ->willReturnMap([['userId', $this->userMock]]);
  83. $this->container['UserSession']->method('getUser')->willReturn($this->userMock);
  84. $this->avatarFile = $this->getMockBuilder('OCP\Files\File')->getMock();
  85. $this->avatarFile->method('getContent')->willReturn('image data');
  86. $this->avatarFile->method('getMimeType')->willReturn('image type');
  87. $this->avatarFile->method('getEtag')->willReturn('my etag');
  88. }
  89. public function tearDown() {
  90. $this->logout();
  91. parent::tearDown();
  92. }
  93. /**
  94. * Fetch an avatar if a user has no avatar
  95. */
  96. public function testGetAvatarNoAvatar() {
  97. $this->container['AvatarManager']->method('getAvatar')->willReturn($this->avatarMock);
  98. $this->avatarMock->method('getFile')->will($this->throwException(new NotFoundException()));
  99. $response = $this->avatarController->getAvatar('userId', 32);
  100. //Comment out until JS is fixed
  101. //$this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus());
  102. $this->assertEquals(Http::STATUS_OK, $response->getStatus());
  103. $this->assertEquals('displayName', $response->getData()['data']['displayname']);
  104. }
  105. /**
  106. * Fetch the user's avatar
  107. */
  108. public function testGetAvatar() {
  109. $this->avatarMock->method('getFile')->willReturn($this->avatarFile);
  110. $this->container['AvatarManager']->method('getAvatar')->with('userId')->willReturn($this->avatarMock);
  111. $response = $this->avatarController->getAvatar('userId', 32);
  112. $this->assertEquals(Http::STATUS_OK, $response->getStatus());
  113. $this->assertArrayHasKey('Content-Type', $response->getHeaders());
  114. $this->assertEquals('image type', $response->getHeaders()['Content-Type']);
  115. $this->assertEquals('my etag', $response->getEtag());
  116. }
  117. /**
  118. * Fetch the avatar of a non-existing user
  119. */
  120. public function testGetAvatarNoUser() {
  121. $this->container['AvatarManager']
  122. ->method('getAvatar')
  123. ->with('userDoesNotExist')
  124. ->will($this->throwException(new \Exception('user does not exist')));
  125. $response = $this->avatarController->getAvatar('userDoesNotExist', 32);
  126. //Comment out until JS is fixed
  127. //$this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus());
  128. $this->assertEquals(Http::STATUS_OK, $response->getStatus());
  129. $this->assertEquals('', $response->getData()['data']['displayname']);
  130. }
  131. /**
  132. * Make sure we get the correct size
  133. */
  134. public function testGetAvatarSize() {
  135. $this->avatarMock->expects($this->once())
  136. ->method('getFile')
  137. ->with($this->equalTo(32))
  138. ->willReturn($this->avatarFile);
  139. $this->container['AvatarManager']->method('getAvatar')->willReturn($this->avatarMock);
  140. $this->avatarController->getAvatar('userId', 32);
  141. }
  142. /**
  143. * We cannot get avatars that are 0 or negative
  144. */
  145. public function testGetAvatarSizeMin() {
  146. $this->avatarMock->expects($this->once())
  147. ->method('getFile')
  148. ->with($this->equalTo(64))
  149. ->willReturn($this->avatarFile);
  150. $this->container['AvatarManager']->method('getAvatar')->willReturn($this->avatarMock);
  151. $this->avatarController->getAvatar('userId', 0);
  152. }
  153. /**
  154. * We do not support avatars larger than 2048*2048
  155. */
  156. public function testGetAvatarSizeMax() {
  157. $this->avatarMock->expects($this->once())
  158. ->method('getFile')
  159. ->with($this->equalTo(2048))
  160. ->willReturn($this->avatarFile);
  161. $this->container['AvatarManager']->method('getAvatar')->willReturn($this->avatarMock);
  162. $this->avatarController->getAvatar('userId', 2049);
  163. }
  164. /**
  165. * Remove an avatar
  166. */
  167. public function testDeleteAvatar() {
  168. $this->container['AvatarManager']->method('getAvatar')->willReturn($this->avatarMock);
  169. $response = $this->avatarController->deleteAvatar();
  170. $this->assertEquals(Http::STATUS_OK, $response->getStatus());
  171. }
  172. /**
  173. * Test what happens if the removing of the avatar fails
  174. */
  175. public function testDeleteAvatarException() {
  176. $this->avatarMock->method('remove')->will($this->throwException(new \Exception("foo")));
  177. $this->container['AvatarManager']->method('getAvatar')->willReturn($this->avatarMock);
  178. $this->container['Logger']->expects($this->once())
  179. ->method('logException')
  180. ->with(new \Exception("foo"));
  181. $expectedResponse = new Http\DataResponse(['data' => ['message' => 'An error occurred. Please contact your admin.']], Http::STATUS_BAD_REQUEST);
  182. $this->assertEquals($expectedResponse, $this->avatarController->deleteAvatar());
  183. }
  184. /**
  185. * Trying to get a tmp avatar when it is not available. 404
  186. */
  187. public function testTmpAvatarNoTmp() {
  188. $response = $this->avatarController->getTmpAvatar();
  189. $this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus());
  190. }
  191. /**
  192. * Fetch tmp avatar
  193. */
  194. public function testTmpAvatarValid() {
  195. $this->container['Cache']->method('get')->willReturn(file_get_contents(\OC::$SERVERROOT.'/tests/data/testimage.jpg'));
  196. $response = $this->avatarController->getTmpAvatar();
  197. $this->assertEquals(Http::STATUS_OK, $response->getStatus());
  198. }
  199. /**
  200. * When trying to post a new avatar a path or image should be posted.
  201. */
  202. public function testPostAvatarNoPathOrImage() {
  203. $response = $this->avatarController->postAvatar(null);
  204. $this->assertEquals(Http::STATUS_BAD_REQUEST, $response->getStatus());
  205. }
  206. /**
  207. * Test a correct post of an avatar using POST
  208. */
  209. public function testPostAvatarFile() {
  210. //Create temp file
  211. $fileName = tempnam(null, "avatarTest");
  212. $copyRes = copy(\OC::$SERVERROOT.'/tests/data/testimage.jpg', $fileName);
  213. $this->assertTrue($copyRes);
  214. //Create file in cache
  215. $this->container['Cache']->method('get')->willReturn(file_get_contents(\OC::$SERVERROOT.'/tests/data/testimage.jpg'));
  216. //Create request return
  217. $reqRet = ['error' => [0], 'tmp_name' => [$fileName], 'size' => [filesize(\OC::$SERVERROOT.'/tests/data/testimage.jpg')]];
  218. $this->container['Request']->method('getUploadedFile')->willReturn($reqRet);
  219. $response = $this->avatarController->postAvatar(null);
  220. //On correct upload always respond with the notsquare message
  221. $this->assertEquals('notsquare', $response->getData()['data']);
  222. //File should be deleted
  223. $this->assertFalse(file_exists($fileName));
  224. }
  225. /**
  226. * Test invalid post os an avatar using POST
  227. */
  228. public function testPostAvatarInvalidFile() {
  229. //Create request return
  230. $reqRet = ['error' => [1], 'tmp_name' => ['foo']];
  231. $this->container['Request']->method('getUploadedFile')->willReturn($reqRet);
  232. $response = $this->avatarController->postAvatar(null);
  233. $this->assertEquals(Http::STATUS_BAD_REQUEST, $response->getStatus());
  234. }
  235. /**
  236. * Check what happens when we upload a GIF
  237. */
  238. public function testPostAvatarFileGif() {
  239. //Create temp file
  240. $fileName = tempnam(null, "avatarTest");
  241. $copyRes = copy(\OC::$SERVERROOT.'/tests/data/testimage.gif', $fileName);
  242. $this->assertTrue($copyRes);
  243. //Create file in cache
  244. $this->container['Cache']->method('get')->willReturn(file_get_contents(\OC::$SERVERROOT.'/tests/data/testimage.gif'));
  245. //Create request return
  246. $reqRet = ['error' => [0], 'tmp_name' => [$fileName], 'size' => filesize(\OC::$SERVERROOT.'/tests/data/testimage.gif')];
  247. $this->container['Request']->method('getUploadedFile')->willReturn($reqRet);
  248. $response = $this->avatarController->postAvatar(null);
  249. $this->assertEquals('Unknown filetype', $response->getData()['data']['message']);
  250. //File should be deleted
  251. $this->assertFalse(file_exists($fileName));
  252. }
  253. /**
  254. * Test posting avatar from existing file
  255. */
  256. public function testPostAvatarFromFile() {
  257. //Mock node API call
  258. $file = $this->getMockBuilder('OCP\Files\File')
  259. ->disableOriginalConstructor()->getMock();
  260. $file->method('getContent')->willReturn(file_get_contents(\OC::$SERVERROOT.'/tests/data/testimage.jpg'));
  261. $this->container['UserFolder']->method('get')->willReturn($file);
  262. //Create request return
  263. $response = $this->avatarController->postAvatar('avatar.jpg');
  264. //On correct upload always respond with the notsquare message
  265. $this->assertEquals('notsquare', $response->getData()['data']);
  266. }
  267. /**
  268. * Test posting avatar from existing folder
  269. */
  270. public function testPostAvatarFromNoFile() {
  271. $file = $this->getMockBuilder('OCP\Files\Node')->getMock();
  272. $this->container['UserFolder']
  273. ->method('get')
  274. ->with('folder')
  275. ->willReturn($file);
  276. //Create request return
  277. $response = $this->avatarController->postAvatar('folder');
  278. //On correct upload always respond with the notsquare message
  279. $this->assertEquals(['data' => ['message' => 'Please select a file.']], $response->getData());
  280. }
  281. /**
  282. * Test what happens if the upload of the avatar fails
  283. */
  284. public function testPostAvatarException() {
  285. $this->container['Cache']->expects($this->once())
  286. ->method('set')
  287. ->will($this->throwException(new \Exception("foo")));
  288. $file = $this->getMockBuilder('OCP\Files\File')
  289. ->disableOriginalConstructor()->getMock();
  290. $file->method('getContent')->willReturn(file_get_contents(\OC::$SERVERROOT.'/tests/data/testimage.jpg'));
  291. $this->container['UserFolder']->method('get')->willReturn($file);
  292. $this->container['Logger']->expects($this->once())
  293. ->method('logException')
  294. ->with(new \Exception("foo"));
  295. $expectedResponse = new Http\DataResponse(['data' => ['message' => 'An error occurred. Please contact your admin.']], Http::STATUS_OK);
  296. $this->assertEquals($expectedResponse, $this->avatarController->postAvatar('avatar.jpg'));
  297. }
  298. /**
  299. * Test invalid crop argument
  300. */
  301. public function testPostCroppedAvatarInvalidCrop() {
  302. $response = $this->avatarController->postCroppedAvatar([]);
  303. $this->assertEquals(Http::STATUS_BAD_REQUEST, $response->getStatus());
  304. }
  305. /**
  306. * Test no tmp avatar to crop
  307. */
  308. public function testPostCroppedAvatarNoTmpAvatar() {
  309. $response = $this->avatarController->postCroppedAvatar(['x' => 0, 'y' => 0, 'w' => 10, 'h' => 10]);
  310. $this->assertEquals(Http::STATUS_BAD_REQUEST, $response->getStatus());
  311. }
  312. /**
  313. * Test with non square crop
  314. */
  315. public function testPostCroppedAvatarNoSquareCrop() {
  316. $this->container['Cache']->method('get')->willReturn(file_get_contents(\OC::$SERVERROOT.'/tests/data/testimage.jpg'));
  317. $this->avatarMock->method('set')->will($this->throwException(new \OC\NotSquareException));
  318. $this->container['AvatarManager']->method('getAvatar')->willReturn($this->avatarMock);
  319. $response = $this->avatarController->postCroppedAvatar(['x' => 0, 'y' => 0, 'w' => 10, 'h' => 11]);
  320. $this->assertEquals(Http::STATUS_BAD_REQUEST, $response->getStatus());
  321. }
  322. /**
  323. * Check for proper reply on proper crop argument
  324. */
  325. public function testPostCroppedAvatarValidCrop() {
  326. $this->container['Cache']->method('get')->willReturn(file_get_contents(\OC::$SERVERROOT.'/tests/data/testimage.jpg'));
  327. $this->container['AvatarManager']->method('getAvatar')->willReturn($this->avatarMock);
  328. $response = $this->avatarController->postCroppedAvatar(['x' => 0, 'y' => 0, 'w' => 10, 'h' => 10]);
  329. $this->assertEquals(Http::STATUS_OK, $response->getStatus());
  330. $this->assertEquals('success', $response->getData()['status']);
  331. }
  332. /**
  333. * Test what happens if the cropping of the avatar fails
  334. */
  335. public function testPostCroppedAvatarException() {
  336. $this->container['Cache']->method('get')->willReturn(file_get_contents(\OC::$SERVERROOT.'/tests/data/testimage.jpg'));
  337. $this->avatarMock->method('set')->will($this->throwException(new \Exception('foo')));
  338. $this->container['AvatarManager']->method('getAvatar')->willReturn($this->avatarMock);
  339. $this->container['Logger']->expects($this->once())
  340. ->method('logException')
  341. ->with(new \Exception('foo'));
  342. $expectedResponse = new Http\DataResponse(['data' => ['message' => 'An error occurred. Please contact your admin.']], Http::STATUS_BAD_REQUEST);
  343. $this->assertEquals($expectedResponse, $this->avatarController->postCroppedAvatar(['x' => 0, 'y' => 0, 'w' => 10, 'h' => 11]));
  344. }
  345. /**
  346. * Check for proper reply on proper crop argument
  347. */
  348. public function testFileTooBig() {
  349. $fileName = \OC::$SERVERROOT.'/tests/data/testimage.jpg';
  350. //Create request return
  351. $reqRet = ['error' => [0], 'tmp_name' => [$fileName], 'size' => [21*1024*1024]];
  352. $this->container['Request']->method('getUploadedFile')->willReturn($reqRet);
  353. $response = $this->avatarController->postAvatar(null);
  354. $this->assertEquals('File is too big', $response->getData()['data']['message']);
  355. }
  356. }