avatarcontrollertest.php 12 KB

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