test_attachmentadmin_views.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. from django.urls import reverse
  2. from misago.admin.testutils import AdminTestCase
  3. from misago.categories.models import Category
  4. from misago.threads import testutils
  5. from misago.threads.models import Attachment, AttachmentType
  6. class AttachmentAdminViewsTests(AdminTestCase):
  7. def setUp(self):
  8. super(AttachmentAdminViewsTests, self).setUp()
  9. self.category = Category.objects.get(slug='first-category')
  10. self.post = testutils.post_thread(category=self.category).first_post
  11. self.filetype = AttachmentType.objects.order_by('id').first()
  12. self.admin_link = reverse('misago:admin:system:attachments:index')
  13. def mock_attachment(self, post=None, file=None, image=None, thumbnail=None):
  14. return Attachment.objects.create(
  15. secret=Attachment.generate_new_secret(),
  16. filetype=self.filetype,
  17. post=post,
  18. size=1000,
  19. uploader=self.user,
  20. uploader_name=self.user.username,
  21. uploader_slug=self.user.slug,
  22. uploader_ip='127.0.0.1',
  23. filename='testfile_{}.zip'.format(Attachment.objects.count() + 1),
  24. file=None,
  25. image=None,
  26. thumbnail=None
  27. )
  28. def test_link_registered(self):
  29. """admin nav contains attachments link"""
  30. response = self.client.get(reverse('misago:admin:system:settings:index'))
  31. self.assertContains(response, self.admin_link)
  32. def test_list_view(self):
  33. """attachments list returns 200 and renders all attachments"""
  34. final_link = self.client.get(self.admin_link)['location']
  35. response = self.client.get(final_link)
  36. self.assertEqual(response.status_code, 200)
  37. attachments = [
  38. self.mock_attachment(self.post, file='somefile.pdf'),
  39. self.mock_attachment(image='someimage.jpg'),
  40. self.mock_attachment(self.post, image='somelargeimage.png', thumbnail='somethumb.png'),
  41. ]
  42. response = self.client.get(final_link)
  43. self.assertEqual(response.status_code, 200)
  44. for attachment in attachments:
  45. delete_link = reverse(
  46. 'misago:admin:system:attachments:delete', kwargs={'pk': attachment.pk}
  47. )
  48. self.assertContains(response, attachment.filename)
  49. self.assertContains(response, delete_link)
  50. self.assertContains(response, attachment.get_absolute_url())
  51. self.assertContains(response, attachment.uploader.username)
  52. self.assertContains(response, attachment.uploader.get_absolute_url())
  53. if attachment.thumbnail:
  54. self.assertContains(response, attachment.get_thumbnail_url())
  55. def test_delete_multiple(self):
  56. """mass delete tool on list works"""
  57. attachments = [
  58. self.mock_attachment(self.post, file='somefile.pdf'),
  59. self.mock_attachment(image='someimage.jpg'),
  60. self.mock_attachment(self.post, image='somelargeimage.png', thumbnail='somethumb.png'),
  61. ]
  62. self.post.attachments_cache = [{'id': attachments[-1].pk}]
  63. self.post.save()
  64. response = self.client.post(
  65. self.admin_link,
  66. data={'action': 'delete',
  67. 'selected_items': [a.pk for a in attachments]}
  68. )
  69. self.assertEqual(response.status_code, 302)
  70. self.assertEqual(Attachment.objects.count(), 0)
  71. # assert attachments were removed from post's cache
  72. attachments_cache = self.category.post_set.get(pk=self.post.pk).attachments_cache
  73. self.assertIsNone(attachments_cache)
  74. def test_delete_view(self):
  75. """delete attachment view has no showstoppers"""
  76. attachment = self.mock_attachment(self.post)
  77. self.post.attachments_cache = [{
  78. 'id': attachment.pk + 1
  79. }, {
  80. 'id': attachment.pk
  81. }, {
  82. 'id': attachment.pk + 2
  83. }]
  84. self.post.save()
  85. action_link = reverse(
  86. 'misago:admin:system:attachments:delete', kwargs={'pk': attachment.pk}
  87. )
  88. response = self.client.post(action_link)
  89. self.assertEqual(response.status_code, 302)
  90. # clean alert about item, grab final list url
  91. final_link = self.client.get(self.admin_link)['location']
  92. response = self.client.get(final_link)
  93. self.assertEqual(response.status_code, 200)
  94. self.assertNotContains(response, action_link)
  95. # assert it was removed from post's attachments cache
  96. attachments_cache = self.category.post_set.get(pk=self.post.pk).attachments_cache
  97. self.assertEqual(attachments_cache, [{'id': attachment.pk + 1}, {'id': attachment.pk + 2}])