avatarcontrollertest.php 15 KB

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