test_attachments_api.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. import os
  2. from PIL import Image
  3. from django.urls import reverse
  4. from django.utils import six
  5. from misago.acl.models import Role
  6. from misago.acl.testutils import override_acl
  7. from misago.conf import settings
  8. from misago.threads.models import Attachment, AttachmentType
  9. from misago.users.testutils import AuthenticatedUserTestCase
  10. TESTFILES_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'testfiles')
  11. TEST_DOCUMENT_PATH = os.path.join(TESTFILES_DIR, 'document.pdf')
  12. TEST_LARGEPNG_PATH = os.path.join(TESTFILES_DIR, 'large.png')
  13. TEST_SMALLJPG_PATH = os.path.join(TESTFILES_DIR, 'small.jpg')
  14. TEST_ANIMATEDGIF_PATH = os.path.join(TESTFILES_DIR, 'animated.gif')
  15. TEST_CORRUPTEDIMG_PATH = os.path.join(TESTFILES_DIR, 'corrupted.gif')
  16. class AttachmentsApiTestCase(AuthenticatedUserTestCase):
  17. def setUp(self):
  18. super(AttachmentsApiTestCase, self).setUp()
  19. AttachmentType.objects.all().delete()
  20. self.api_link = reverse('misago:api:attachment-list')
  21. def override_acl(self, new_acl=None):
  22. if new_acl:
  23. acl = self.user.acl_cache.copy()
  24. acl.update(new_acl)
  25. override_acl(self.user, acl)
  26. def test_anonymous(self):
  27. """user has to be authenticated to be able to upload files"""
  28. self.logout_user()
  29. response = self.client.post(self.api_link)
  30. self.assertEqual(response.status_code, 403)
  31. def test_no_permission(self):
  32. """user needs permission to upload files"""
  33. self.override_acl({'max_attachment_size': 0})
  34. response = self.client.post(self.api_link)
  35. self.assertEqual(response.status_code, 403)
  36. self.assertEqual(response.json(), {
  37. 'detail': "You don't have permission to upload new files.",
  38. })
  39. def test_no_file_uploaded(self):
  40. """no file uploaded scenario is handled"""
  41. response = self.client.post(self.api_link)
  42. self.assertEqual(response.status_code, 400)
  43. self.assertEqual(response.json(), {
  44. 'upload': ["No file was submitted."],
  45. })
  46. def test_invalid_extension(self):
  47. """uploaded file's extension is rejected as invalid"""
  48. AttachmentType.objects.create(
  49. name="Test extension",
  50. extensions='jpg,jpeg',
  51. mimetypes=None,
  52. )
  53. with open(TEST_DOCUMENT_PATH, 'rb') as upload:
  54. response = self.client.post(
  55. self.api_link, data={
  56. 'upload': upload,
  57. }
  58. )
  59. self.assertEqual(response.status_code, 400)
  60. self.assertEqual(response.json(), {
  61. 'upload': ["You can't upload files of this type."],
  62. })
  63. def test_invalid_mime(self):
  64. """uploaded file's mimetype is rejected as invalid"""
  65. AttachmentType.objects.create(
  66. name="Test extension",
  67. extensions='png',
  68. mimetypes='loremipsum',
  69. )
  70. with open(TEST_DOCUMENT_PATH, 'rb') as upload:
  71. response = self.client.post(
  72. self.api_link, data={
  73. 'upload': upload,
  74. }
  75. )
  76. self.assertEqual(response.status_code, 400)
  77. self.assertEqual(response.json(), {
  78. 'upload': ["You can't upload files of this type."],
  79. })
  80. def test_no_perm_to_type(self):
  81. """user needs permission to upload files of this type"""
  82. attachment_type = AttachmentType.objects.create(
  83. name="Test extension",
  84. extensions='png',
  85. mimetypes='application/pdf',
  86. )
  87. user_roles = (r.pk for r in self.user.get_roles())
  88. attachment_type.limit_uploads_to.set(Role.objects.exclude(id__in=user_roles))
  89. with open(TEST_DOCUMENT_PATH, 'rb') as upload:
  90. response = self.client.post(
  91. self.api_link, data={
  92. 'upload': upload,
  93. }
  94. )
  95. self.assertEqual(response.status_code, 400)
  96. self.assertEqual(response.json(), {
  97. 'upload': ["You can't upload files of this type."],
  98. })
  99. def test_type_is_locked(self):
  100. """new uploads for this filetype are locked"""
  101. AttachmentType.objects.create(
  102. name="Test extension",
  103. extensions='png',
  104. mimetypes='application/pdf',
  105. status=AttachmentType.LOCKED,
  106. )
  107. with open(TEST_DOCUMENT_PATH, 'rb') as upload:
  108. response = self.client.post(
  109. self.api_link, data={
  110. 'upload': upload,
  111. }
  112. )
  113. self.assertEqual(response.status_code, 400)
  114. self.assertEqual(response.json(), {
  115. 'upload': ["You can't upload files of this type."],
  116. })
  117. def test_type_is_disabled(self):
  118. """new uploads for this filetype are disabled"""
  119. AttachmentType.objects.create(
  120. name="Test extension",
  121. extensions='png',
  122. mimetypes='application/pdf',
  123. status=AttachmentType.DISABLED,
  124. )
  125. with open(TEST_DOCUMENT_PATH, 'rb') as upload:
  126. response = self.client.post(
  127. self.api_link, data={
  128. 'upload': upload,
  129. }
  130. )
  131. self.assertEqual(response.status_code, 400)
  132. self.assertEqual(response.json(), {
  133. 'upload': ["You can't upload files of this type."],
  134. })
  135. def test_upload_too_big_for_type(self):
  136. """too big uploads are rejected"""
  137. AttachmentType.objects.create(
  138. name="Test extension",
  139. extensions='png',
  140. mimetypes='image/png',
  141. size_limit=100,
  142. )
  143. with open(TEST_LARGEPNG_PATH, 'rb') as upload:
  144. response = self.client.post(
  145. self.api_link, data={
  146. 'upload': upload,
  147. }
  148. )
  149. self.assertEqual(response.status_code, 400)
  150. self.assertEqual(response.json(), {
  151. 'upload': [
  152. "You can't upload files of this type larger "
  153. "than 100.0\xa0KB (your file has 253.9\xa0KB)."
  154. ],
  155. })
  156. def test_upload_too_big_for_user(self):
  157. """too big uploads are rejected"""
  158. self.override_acl({'max_attachment_size': 100})
  159. AttachmentType.objects.create(
  160. name="Test extension",
  161. extensions='png',
  162. mimetypes='image/png',
  163. )
  164. with open(TEST_LARGEPNG_PATH, 'rb') as upload:
  165. response = self.client.post(
  166. self.api_link, data={
  167. 'upload': upload,
  168. }
  169. )
  170. self.assertEqual(response.status_code, 400)
  171. self.assertEqual(response.json(), {
  172. 'upload': [
  173. "You can't upload files larger than 100.0\xa0KB (your file has 253.9\xa0KB)."
  174. ],
  175. })
  176. def test_corrupted_image_upload(self):
  177. """corrupted image upload is handled"""
  178. AttachmentType.objects.create(
  179. name="Test extension",
  180. extensions='gif',
  181. )
  182. with open(TEST_CORRUPTEDIMG_PATH, 'rb') as upload:
  183. response = self.client.post(
  184. self.api_link, data={
  185. 'upload': upload,
  186. }
  187. )
  188. self.assertEqual(response.status_code, 400)
  189. self.assertEqual(response.json(), {
  190. 'upload': ["Uploaded image was corrupted or invalid."],
  191. })
  192. def test_document_upload(self):
  193. """successful upload creates orphan attachment"""
  194. AttachmentType.objects.create(
  195. name="Test extension",
  196. extensions='pdf',
  197. mimetypes='application/pdf',
  198. )
  199. with open(TEST_DOCUMENT_PATH, 'rb') as upload:
  200. response = self.client.post(
  201. self.api_link, data={
  202. 'upload': upload,
  203. }
  204. )
  205. self.assertEqual(response.status_code, 200)
  206. response_json = response.json()
  207. attachment = Attachment.objects.get(id=response_json['id'])
  208. self.assertEqual(attachment.filename, 'document.pdf')
  209. self.assertTrue(attachment.is_file)
  210. self.assertFalse(attachment.is_image)
  211. self.assertIsNotNone(attachment.file)
  212. self.assertTrue(not attachment.image)
  213. self.assertTrue(not attachment.thumbnail)
  214. self.assertTrue(six.text_type(attachment.file).endswith('document.pdf'))
  215. self.assertIsNone(response_json['post'])
  216. self.assertEqual(response_json['uploader_name'], self.user.username)
  217. self.assertFalse(response_json['has_thumbnail'])
  218. # files associated with attachment are deleted on its deletion
  219. file_path = attachment.file.path
  220. self.assertTrue(os.path.exists(file_path))
  221. attachment.delete()
  222. self.assertFalse(os.path.exists(file_path))
  223. def test_small_image_upload(self):
  224. """successful small image upload creates orphan attachment without thumbnail"""
  225. AttachmentType.objects.create(
  226. name="Test extension",
  227. extensions='jpeg,jpg',
  228. mimetypes='image/jpeg',
  229. )
  230. with open(TEST_SMALLJPG_PATH, 'rb') as upload:
  231. response = self.client.post(
  232. self.api_link, data={
  233. 'upload': upload,
  234. }
  235. )
  236. self.assertEqual(response.status_code, 200)
  237. response_json = response.json()
  238. attachment = Attachment.objects.get(id=response_json['id'])
  239. self.assertEqual(attachment.filename, 'small.jpg')
  240. self.assertFalse(attachment.is_file)
  241. self.assertTrue(attachment.is_image)
  242. self.assertTrue(not attachment.file)
  243. self.assertIsNotNone(attachment.image)
  244. self.assertTrue(not attachment.thumbnail)
  245. self.assertTrue(six.text_type(attachment.image).endswith('small.jpg'))
  246. self.assertIsNone(response_json['post'])
  247. self.assertEqual(response_json['uploader_name'], self.user.username)
  248. self.assertFalse(response_json['has_thumbnail'])
  249. def test_large_image_upload(self):
  250. """successful large image upload creates orphan attachment with thumbnail"""
  251. self.override_acl({'max_attachment_size': 10 * 1024})
  252. AttachmentType.objects.create(
  253. name="Test extension",
  254. extensions='png',
  255. mimetypes='image/png',
  256. )
  257. with open(TEST_LARGEPNG_PATH, 'rb') as upload:
  258. response = self.client.post(
  259. self.api_link, data={
  260. 'upload': upload,
  261. }
  262. )
  263. self.assertEqual(response.status_code, 200)
  264. response_json = response.json()
  265. attachment = Attachment.objects.get(id=response_json['id'])
  266. self.assertEqual(attachment.filename, 'large.png')
  267. self.assertFalse(attachment.is_file)
  268. self.assertTrue(attachment.is_image)
  269. self.assertTrue(not attachment.file)
  270. self.assertIsNotNone(attachment.image)
  271. self.assertIsNotNone(attachment.thumbnail)
  272. self.assertTrue(six.text_type(attachment.image).endswith('large.png'))
  273. self.assertTrue(six.text_type(attachment.thumbnail).endswith('large.png'))
  274. self.assertIsNone(response_json['post'])
  275. self.assertEqual(response_json['uploader_name'], self.user.username)
  276. self.assertTrue(response_json['has_thumbnail'])
  277. # thumbnail was scaled down
  278. thumbnail = Image.open(attachment.thumbnail.path)
  279. self.assertEqual(thumbnail.size[0], settings.MISAGO_ATTACHMENT_IMAGE_SIZE_LIMIT[0])
  280. self.assertLess(thumbnail.size[1], settings.MISAGO_ATTACHMENT_IMAGE_SIZE_LIMIT[1])
  281. # files associated with attachment are deleted on its deletion
  282. image_path = attachment.image.path
  283. thumbnail_path = attachment.thumbnail.path
  284. self.assertTrue(os.path.exists(image_path))
  285. self.assertTrue(os.path.exists(thumbnail_path))
  286. attachment.delete()
  287. self.assertFalse(os.path.exists(image_path))
  288. self.assertFalse(os.path.exists(thumbnail_path))
  289. def test_animated_image_upload(self):
  290. """successful gif upload creates orphan attachment with thumbnail"""
  291. AttachmentType.objects.create(
  292. name="Test extension",
  293. extensions='gif',
  294. mimetypes='image/gif',
  295. )
  296. with open(TEST_ANIMATEDGIF_PATH, 'rb') as upload:
  297. response = self.client.post(
  298. self.api_link, data={
  299. 'upload': upload,
  300. }
  301. )
  302. self.assertEqual(response.status_code, 200)
  303. response_json = response.json()
  304. attachment = Attachment.objects.get(id=response_json['id'])
  305. self.assertEqual(attachment.filename, 'animated.gif')
  306. self.assertFalse(attachment.is_file)
  307. self.assertTrue(attachment.is_image)
  308. self.assertTrue(not attachment.file)
  309. self.assertIsNotNone(attachment.image)
  310. self.assertIsNotNone(attachment.thumbnail)
  311. self.assertTrue(six.text_type(attachment.image).endswith('animated.gif'))
  312. self.assertTrue(six.text_type(attachment.thumbnail).endswith('animated.gif'))
  313. self.assertIsNone(response_json['post'])
  314. self.assertEqual(response_json['uploader_name'], self.user.username)
  315. self.assertTrue(response_json['has_thumbnail'])