test_attachmentadmin_views.py 4.6 KB

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