test_attachmentadmin_views.py 5.1 KB

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