test_deleting_assets.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. from pathlib import Path
  2. import pytest
  3. from django.core.files.base import ContentFile
  4. from django.urls import reverse
  5. from ....cache.test import assert_invalidates_cache
  6. from ....test import assert_has_success_message
  7. from ... import THEME_CACHE
  8. @pytest.fixture
  9. def delete_css(admin_client):
  10. def delete_assets(theme, assets):
  11. url = reverse("misago:admin:themes:delete-css", kwargs={"pk": theme.pk})
  12. return admin_client.post(url, {"item": [i.pk for i in assets]})
  13. return delete_assets
  14. @pytest.fixture
  15. def delete_media(admin_client):
  16. def delete_assets(theme, assets):
  17. url = reverse("misago:admin:themes:delete-media", kwargs={"pk": theme.pk})
  18. return admin_client.post(url, {"item": [i.pk for i in assets]})
  19. return delete_assets
  20. def test_theme_css_can_be_deleted(theme, delete_css, css):
  21. delete_css(theme, [css])
  22. assert not theme.css.exists()
  23. def test_deleting_css_also_deletes_css_source_files(theme, delete_css, css):
  24. delete_css(theme, [css])
  25. assert not Path(css.source_file.path).exists()
  26. def test_deleting_css_also_deletes_css_build_files(theme, delete_css, css):
  27. css.build_file = ContentFile("body {}", name="test.css")
  28. css.build_hash = "abcdefgh"
  29. css.save()
  30. delete_css(theme, [css])
  31. assert not Path(css.build_file.path).exists()
  32. def test_theme_css_link_can_be_deleted(theme, delete_css, css_link):
  33. delete_css(theme, [css_link])
  34. assert not theme.css.exists()
  35. def test_multiple_theme_css_can_be_deleted_at_single_time(
  36. theme, delete_css, css, css_link
  37. ):
  38. delete_css(theme, [css, css_link])
  39. assert not theme.css.exists()
  40. def test_theme_media_can_be_deleted(theme, delete_media, media):
  41. delete_media(theme, [media])
  42. assert not theme.media.exists()
  43. def test_deleting_media_also_deletes_files(theme, delete_media, media):
  44. delete_media(theme, [media])
  45. assert not Path(media.file.path).exists()
  46. def test_theme_images_can_be_deleted(theme, delete_media, image):
  47. delete_media(theme, [image])
  48. assert not theme.media.exists()
  49. def test_deleting_image_also_deletes_files(theme, delete_media, image):
  50. delete_media(theme, [image])
  51. assert not Path(image.thumbnail.path).exists()
  52. def test_multiple_theme_media_can_be_deleted_at_single_time(
  53. theme, delete_media, media, image
  54. ):
  55. delete_media(theme, [media, image])
  56. assert not theme.media.exists()
  57. def test_success_message_is_set_after_css_is_deleted(theme, delete_css, css):
  58. response = delete_css(theme, [css])
  59. assert_has_success_message(response)
  60. def test_success_message_is_set_after_media_is_deleted(theme, delete_media, media):
  61. response = delete_media(theme, [media])
  62. assert_has_success_message(response)
  63. def test_selecting_no_css_to_delete_causes_no_errors(theme, delete_css, css):
  64. delete_css(theme, [])
  65. assert theme.css.exists()
  66. def test_selecting_no_media_to_delete_causes_no_errors(theme, delete_media, media):
  67. delete_media(theme, [])
  68. assert theme.media.exists()
  69. def test_selecting_invalid_css_id_to_delete_causes_no_errors(
  70. mocker, theme, delete_css, css
  71. ):
  72. delete_css(theme, [mocker.Mock(pk="str")])
  73. assert theme.css.exists()
  74. def test_selecting_invalid_media_id_to_delete_causes_no_errors(
  75. mocker, theme, delete_media, media
  76. ):
  77. delete_media(theme, [mocker.Mock(pk="str")])
  78. assert theme.media.exists()
  79. def test_selecting_nonexisting_css_id_to_delete_causes_no_errors(
  80. mocker, theme, delete_css, css
  81. ):
  82. delete_css(theme, [mocker.Mock(pk=css.pk + 1)])
  83. assert theme.css.exists()
  84. def test_selecting_nonexisting_media_id_to_delete_causes_no_errors(
  85. mocker, theme, delete_media, media
  86. ):
  87. delete_media(theme, [mocker.Mock(pk=media.pk + 1)])
  88. assert theme.media.exists()
  89. def test_other_theme_css_is_not_deleted(delete_css, theme, other_theme, css):
  90. delete_css(other_theme, [css])
  91. assert theme.css.exists()
  92. def test_other_theme_media_is_not_deleted(delete_media, theme, other_theme, media):
  93. delete_media(other_theme, [media])
  94. assert theme.media.exists()
  95. def test_deleting_css_invalidates_theme_cache(theme, delete_css, css):
  96. with assert_invalidates_cache(THEME_CACHE):
  97. delete_css(theme, [css])