test_attachments_api.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. import json
  2. import os
  3. from django.core.urlresolvers import reverse
  4. from django.utils import six
  5. from django.utils.encoding import smart_str
  6. from misago.acl.models import Role
  7. from misago.acl.testutils import override_acl
  8. from misago.users.testutils import AuthenticatedUserTestCase
  9. from ..models import Attachment, AttachmentType
  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_ANIMATEDGIT_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.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({
  34. 'max_attachment_size': 0
  35. })
  36. response = self.client.post(self.api_link)
  37. self.assertContains(response, "don't have permission to upload new files", status_code=403)
  38. def test_no_file_uploaded(self):
  39. """no file uploaded scenario is handled"""
  40. response = self.client.post(self.api_link)
  41. self.assertContains(response, "No file has been uploaded.", status_code=400)
  42. def test_invalid_extension(self):
  43. """uploaded file's extension is rejected as invalid"""
  44. AttachmentType.objects.create(
  45. name="Test extension",
  46. extensions='jpg,jpeg',
  47. mimetypes=None
  48. )
  49. with open(TEST_DOCUMENT_PATH, 'rb') as upload:
  50. response = self.client.post(self.api_link, data={
  51. 'upload': upload
  52. })
  53. self.assertContains(response, "You can't upload files of this type.", status_code=400)
  54. def test_invalid_mime(self):
  55. """uploaded file's mimetype is rejected as invalid"""
  56. AttachmentType.objects.create(
  57. name="Test extension",
  58. extensions='png',
  59. mimetypes='loremipsum'
  60. )
  61. with open(TEST_DOCUMENT_PATH, 'rb') as upload:
  62. response = self.client.post(self.api_link, data={
  63. 'upload': upload
  64. })
  65. self.assertContains(response, "You can't upload files of this type.", status_code=400)
  66. def test_no_perm_to_type(self):
  67. """user needs permission to upload files of this type"""
  68. attachment_type = AttachmentType.objects.create(
  69. name="Test extension",
  70. extensions='png',
  71. mimetypes='application/pdf'
  72. )
  73. user_roles = (r.pk for r in self.user.get_roles())
  74. attachment_type.limit_uploads_to.set(Role.objects.exclude(id__in=user_roles))
  75. with open(TEST_DOCUMENT_PATH, 'rb') as upload:
  76. response = self.client.post(self.api_link, data={
  77. 'upload': upload
  78. })
  79. self.assertContains(response, "You can't upload files of this type.", status_code=400)
  80. def test_type_is_locked(self):
  81. """new uploads for this filetype are locked"""
  82. attachment_type = AttachmentType.objects.create(
  83. name="Test extension",
  84. extensions='png',
  85. mimetypes='application/pdf',
  86. status=AttachmentType.LOCKED
  87. )
  88. with open(TEST_DOCUMENT_PATH, 'rb') as upload:
  89. response = self.client.post(self.api_link, data={
  90. 'upload': upload
  91. })
  92. self.assertContains(response, "You can't upload files of this type.", status_code=400)
  93. def test_type_is_disabled(self):
  94. """new uploads for this filetype are disabled"""
  95. attachment_type = AttachmentType.objects.create(
  96. name="Test extension",
  97. extensions='png',
  98. mimetypes='application/pdf',
  99. status=AttachmentType.DISABLED
  100. )
  101. with open(TEST_DOCUMENT_PATH, 'rb') as upload:
  102. response = self.client.post(self.api_link, data={
  103. 'upload': upload
  104. })
  105. self.assertContains(response, "You can't upload files of this type.", status_code=400)
  106. def test_upload_too_big_for_type(self):
  107. """too big uploads are rejected"""
  108. AttachmentType.objects.create(
  109. name="Test extension",
  110. extensions='png',
  111. mimetypes='image/png',
  112. size_limit=100
  113. )
  114. with open(TEST_LARGEPNG_PATH, 'rb') as upload:
  115. response = self.client.post(self.api_link, data={
  116. 'upload': upload
  117. })
  118. self.assertContains(response, "can't upload files of this type larger than", status_code=400)
  119. def test_upload_too_big_for_user(self):
  120. """too big uploads are rejected"""
  121. self.override_acl({
  122. 'max_attachment_size': 100
  123. })
  124. AttachmentType.objects.create(
  125. name="Test extension",
  126. extensions='png',
  127. mimetypes='image/png'
  128. )
  129. with open(TEST_LARGEPNG_PATH, 'rb') as upload:
  130. response = self.client.post(self.api_link, data={
  131. 'upload': upload
  132. })
  133. self.assertContains(response, "can't upload files larger than", status_code=400)
  134. def test_corrupted_image_upload(self):
  135. """corrupted image upload is handled"""
  136. attachment_type = AttachmentType.objects.create(
  137. name="Test extension",
  138. extensions='gif'
  139. )
  140. with open(TEST_CORRUPTEDIMG_PATH, 'rb') as upload:
  141. response = self.client.post(self.api_link, data={
  142. 'upload': upload
  143. })
  144. self.assertContains(response, "Uploaded image was corrupted or invalid.", status_code=400)
  145. def test_document_upload(self):
  146. """successful upload creates orphan attachment"""
  147. attachment_type = AttachmentType.objects.create(
  148. name="Test extension",
  149. extensions='pdf',
  150. mimetypes='application/pdf'
  151. )
  152. with open(TEST_DOCUMENT_PATH, 'rb') as upload:
  153. response = self.client.post(self.api_link, data={
  154. 'upload': upload
  155. })
  156. self.assertEqual(response.status_code, 200)
  157. response_json = json.loads(smart_str(response.content))
  158. attachment = Attachment.objects.get(id=response_json['id'])
  159. self.assertEqual(attachment.filename, 'document.pdf')
  160. self.assertTrue(attachment.is_file)
  161. self.assertFalse(attachment.is_image)
  162. self.assertIsNotNone(attachment.file)
  163. self.assertTrue(not attachment.image)
  164. self.assertTrue(not attachment.thumbnail)
  165. self.assertTrue(six.text_type(attachment.file).endswith('document.pdf'))
  166. self.assertIsNone(response_json['post'])
  167. self.assertEqual(response_json['uploader_name'], self.user.username)
  168. self.assertEqual(response_json['url']['index'], attachment.get_absolute_url())
  169. self.assertIsNone(response_json['url']['thumb'])
  170. self.assertEqual(response_json['url']['uploader'], self.user.get_absolute_url())
  171. def test_small_image_upload(self):
  172. """successful small image upload creates orphan attachment without thumbnail"""
  173. attachment_type = AttachmentType.objects.create(
  174. name="Test extension",
  175. extensions='jpeg,jpg',
  176. mimetypes='image/jpeg'
  177. )
  178. with open(TEST_SMALLJPG_PATH, 'rb') as upload:
  179. response = self.client.post(self.api_link, data={
  180. 'upload': upload
  181. })
  182. self.assertEqual(response.status_code, 200)
  183. response_json = json.loads(smart_str(response.content))
  184. attachment = Attachment.objects.get(id=response_json['id'])
  185. self.assertEqual(attachment.filename, 'small.jpg')
  186. self.assertFalse(attachment.is_file)
  187. self.assertTrue(attachment.is_image)
  188. self.assertTrue(not attachment.file)
  189. self.assertIsNotNone(attachment.image)
  190. self.assertTrue(not attachment.thumbnail)
  191. self.assertTrue(six.text_type(attachment.image).endswith('small.jpg'))
  192. self.assertIsNone(response_json['post'])
  193. self.assertEqual(response_json['uploader_name'], self.user.username)
  194. self.assertEqual(response_json['url']['index'], attachment.get_absolute_url())
  195. self.assertIsNone(response_json['url']['thumb'])
  196. self.assertEqual(response_json['url']['uploader'], self.user.get_absolute_url())
  197. def test_large_image_upload(self):
  198. """successful large image upload creates orphan attachment with thumbnail"""
  199. self.override_acl({
  200. 'max_attachment_size': 10 * 1024
  201. })
  202. attachment_type = AttachmentType.objects.create(
  203. name="Test extension",
  204. extensions='png',
  205. mimetypes='image/png'
  206. )
  207. with open(TEST_LARGEPNG_PATH, 'rb') as upload:
  208. response = self.client.post(self.api_link, data={
  209. 'upload': upload
  210. })
  211. self.assertEqual(response.status_code, 200)
  212. response_json = json.loads(smart_str(response.content))
  213. attachment = Attachment.objects.get(id=response_json['id'])
  214. self.assertEqual(attachment.filename, 'large.png')
  215. self.assertFalse(attachment.is_file)
  216. self.assertTrue(attachment.is_image)
  217. self.assertTrue(not attachment.file)
  218. self.assertIsNotNone(attachment.image)
  219. self.assertIsNotNone(attachment.thumbnail)
  220. self.assertTrue(six.text_type(attachment.image).endswith('large.png'))
  221. self.assertTrue(six.text_type(attachment.thumbnail).endswith('large.png'))
  222. self.assertIsNone(response_json['post'])
  223. self.assertEqual(response_json['uploader_name'], self.user.username)
  224. self.assertEqual(response_json['url']['index'], attachment.get_absolute_url())
  225. self.assertEqual(response_json['url']['thumb'], attachment.get_thumbnail_url())
  226. self.assertEqual(response_json['url']['uploader'], self.user.get_absolute_url())
  227. def test_animated_image_upload(self):
  228. """successful gif upload creates orphan attachment with thumbnail"""
  229. attachment_type = AttachmentType.objects.create(
  230. name="Test extension",
  231. extensions='gif',
  232. mimetypes='image/gif'
  233. )
  234. with open(TEST_ANIMATEDGIT_PATH, 'rb') as upload:
  235. response = self.client.post(self.api_link, data={
  236. 'upload': upload
  237. })
  238. self.assertEqual(response.status_code, 200)
  239. response_json = json.loads(smart_str(response.content))
  240. attachment = Attachment.objects.get(id=response_json['id'])
  241. self.assertEqual(attachment.filename, 'animated.gif')
  242. self.assertFalse(attachment.is_file)
  243. self.assertTrue(attachment.is_image)
  244. self.assertTrue(not attachment.file)
  245. self.assertIsNotNone(attachment.image)
  246. self.assertIsNotNone(attachment.thumbnail)
  247. self.assertTrue(six.text_type(attachment.image).endswith('animated.gif'))
  248. self.assertTrue(six.text_type(attachment.thumbnail).endswith('animated.gif'))
  249. self.assertIsNone(response_json['post'])
  250. self.assertEqual(response_json['uploader_name'], self.user.username)
  251. self.assertEqual(response_json['url']['index'], attachment.get_absolute_url())
  252. self.assertEqual(response_json['url']['thumb'], attachment.get_thumbnail_url())
  253. self.assertEqual(response_json['url']['uploader'], self.user.get_absolute_url())