test_attachmentadmin_views.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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().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. filename='testfile_{}.zip'.format(Attachment.objects.count() + 1),
  23. file=None,
  24. image=None,
  25. thumbnail=None,
  26. )
  27. def test_link_registered(self):
  28. """admin nav contains attachments link"""
  29. response = self.client.get(reverse('misago:admin:system:settings:index'))
  30. self.assertContains(response, self.admin_link)
  31. def test_list_view(self):
  32. """attachments list returns 200 and renders all attachments"""
  33. final_link = self.client.get(self.admin_link)['location']
  34. response = self.client.get(final_link)
  35. self.assertEqual(response.status_code, 200)
  36. attachments = [
  37. self.mock_attachment(self.post, file='somefile.pdf'),
  38. self.mock_attachment(image='someimage.jpg'),
  39. self.mock_attachment(
  40. self.post,
  41. image='somelargeimage.png',
  42. thumbnail='somethumb.png',
  43. ),
  44. ]
  45. response = self.client.get(final_link)
  46. self.assertEqual(response.status_code, 200)
  47. for attachment in attachments:
  48. delete_link = reverse(
  49. 'misago:admin:system:attachments:delete', kwargs={
  50. 'pk': attachment.pk,
  51. }
  52. )
  53. self.assertContains(response, attachment.filename)
  54. self.assertContains(response, delete_link)
  55. self.assertContains(response, attachment.get_absolute_url())
  56. self.assertContains(response, attachment.uploader.username)
  57. self.assertContains(response, attachment.uploader.get_absolute_url())
  58. if attachment.thumbnail:
  59. self.assertContains(response, attachment.get_thumbnail_url())
  60. def test_delete_multiple(self):
  61. """mass delete tool on list works"""
  62. attachments = [
  63. self.mock_attachment(self.post, file='somefile.pdf'),
  64. self.mock_attachment(image='someimage.jpg'),
  65. self.mock_attachment(
  66. self.post,
  67. image='somelargeimage.png',
  68. thumbnail='somethumb.png',
  69. ),
  70. ]
  71. self.post.attachments_cache = [{'id': attachments[-1].pk}]
  72. self.post.save()
  73. response = self.client.post(
  74. self.admin_link,
  75. data={
  76. 'action': 'delete',
  77. 'selected_items': [a.pk for a in attachments],
  78. }
  79. )
  80. self.assertEqual(response.status_code, 302)
  81. self.assertEqual(Attachment.objects.count(), 0)
  82. # assert attachments were removed from post's cache
  83. attachments_cache = self.category.post_set.get(pk=self.post.pk).attachments_cache
  84. self.assertIsNone(attachments_cache)
  85. def test_delete_view(self):
  86. """delete attachment view has no showstoppers"""
  87. attachment = self.mock_attachment(self.post)
  88. self.post.attachments_cache = [
  89. {
  90. 'id': attachment.pk + 1
  91. },
  92. {
  93. 'id': attachment.pk
  94. },
  95. {
  96. 'id': attachment.pk + 2
  97. },
  98. ]
  99. self.post.save()
  100. action_link = reverse(
  101. 'misago:admin:system:attachments:delete', kwargs={
  102. 'pk': attachment.pk,
  103. }
  104. )
  105. response = self.client.post(action_link)
  106. self.assertEqual(response.status_code, 302)
  107. # clean alert about item, grab final list url
  108. final_link = self.client.get(self.admin_link)['location']
  109. response = self.client.get(final_link)
  110. self.assertEqual(response.status_code, 200)
  111. self.assertNotContains(response, action_link)
  112. # assert it was removed from post's attachments cache
  113. attachments_cache = self.category.post_set.get(pk=self.post.pk).attachments_cache
  114. self.assertEqual(
  115. attachments_cache, [
  116. {
  117. 'id': attachment.pk + 1,
  118. },
  119. {
  120. 'id': attachment.pk + 2,
  121. },
  122. ]
  123. )