avatarcontrollertest.php 14 KB

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