test_uploading_css.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import os
  2. import pytest
  3. from django.urls import reverse
  4. from ....test import assert_has_error_message
  5. TESTS_DIR = os.path.dirname(os.path.abspath(__file__))
  6. @pytest.fixture
  7. def css_file():
  8. return os.path.join(TESTS_DIR, "css", "test.css")
  9. @pytest.fixture
  10. def other_file():
  11. return os.path.join(TESTS_DIR, "images", "test.png")
  12. @pytest.fixture
  13. def hashed_css_file():
  14. return os.path.join(TESTS_DIR, "css", "test.4846cb3b.css")
  15. @pytest.fixture
  16. def upload(admin_client):
  17. def post_upload(theme, asset_files=None):
  18. url = reverse(
  19. "misago:admin:appearance:themes:upload-css", kwargs={"pk": theme.pk}
  20. )
  21. if asset_files:
  22. data = asset_files if isinstance(asset_files, list) else [asset_files]
  23. else:
  24. data = None
  25. return admin_client.post(url, {"assets": data})
  26. return post_upload
  27. def test_css_file_can_be_uploaded(upload, theme, css_file):
  28. with open(css_file) as fp:
  29. upload(theme, fp)
  30. assert theme.css.exists()
  31. def test_multiple_css_files_can_be_uploaded_at_once(
  32. upload, theme, css_file, hashed_css_file
  33. ):
  34. with open(css_file) as fp1:
  35. with open(hashed_css_file) as fp2:
  36. upload(theme, [fp1, fp2])
  37. assert theme.css.exists()
  38. assert theme.css.count() == 2
  39. def test_uploaded_file_is_rejected_if_its_not_css_file(upload, theme, other_file):
  40. with open(other_file, "rb") as fp:
  41. upload(theme, fp)
  42. assert not theme.css.exists()
  43. def test_error_message_is_set_if_uploaded_file_is_not_css(upload, theme, other_file):
  44. with open(other_file, "rb") as fp:
  45. response = upload(theme, fp)
  46. assert_has_error_message(response)
  47. def test_if_some_of_uploaded_files_are_incorrect_only_css_files_are_added_to_theme(
  48. upload, theme, css_file, other_file
  49. ):
  50. with open(css_file) as fp1:
  51. with open(other_file, "rb") as fp2:
  52. upload(theme, [fp1, fp2])
  53. assert theme.css.exists()
  54. assert theme.css.count() == 1
  55. css = theme.css.last()
  56. expected_filename = str(css_file).split("/")[-1]
  57. assert css.name == expected_filename
  58. def test_css_file_is_uploaded_to_theme_directory(upload, theme, css_file):
  59. with open(css_file) as fp:
  60. upload(theme, fp)
  61. css = theme.css.last()
  62. assert theme.dirname in str(css.source_file)
  63. def test_css_file_name_is_set_as_asset_name(upload, theme, css_file):
  64. with open(css_file) as fp:
  65. upload(theme, fp)
  66. css = theme.css.last()
  67. expected_filename = str(css_file).split("/")[-1]
  68. assert css.name == expected_filename
  69. def test_hash_is_added_to_uploaded_css_file_name(
  70. upload, theme, css_file, hashed_css_file
  71. ):
  72. with open(css_file) as fp:
  73. upload(theme, fp)
  74. css = theme.css.last()
  75. filename = str(css.source_file.path).split("/")[-1]
  76. expected_filename = str(hashed_css_file).split("/")[-1]
  77. assert filename == expected_filename
  78. def test_hash_is_set_on_css_source_asset(upload, theme, css_file):
  79. with open(css_file) as fp:
  80. upload(theme, fp)
  81. css = theme.css.last()
  82. assert css.source_hash
  83. def test_css_file_name_is_preserved_if_it_already_contains_correct_hash(
  84. upload, theme, hashed_css_file
  85. ):
  86. with open(hashed_css_file) as fp:
  87. upload(theme, fp)
  88. css = theme.css.last()
  89. filename = str(css.source_file.path).split("/")[-1]
  90. expected_filename = str(hashed_css_file).split("/")[-1]
  91. assert filename == expected_filename
  92. def test_new_hash_is_added_to_css_file_name_if_it_contains_incorrect_hash(
  93. upload, theme
  94. ):
  95. incorrectly_hashed_css_file = os.path.join(TESTS_DIR, "css", "test.0046cb3b.css")
  96. with open(incorrectly_hashed_css_file) as fp:
  97. upload(theme, fp)
  98. css = theme.css.last()
  99. filename = str(css.source_file.path).split("/")[-1]
  100. assert css.source_hash in filename
  101. def test_error_message_is_set_if_no_css_file_was_uploaded(upload, theme):
  102. response = upload(theme)
  103. assert_has_error_message(response)
  104. def test_error_message_is_set_if_user_attempts_to_upload_css_file_to_default_theme(
  105. upload, default_theme
  106. ):
  107. response = upload(default_theme)
  108. assert_has_error_message(response)
  109. def test_error_message_is_set_if_user_attempts_to_upload_css_file_to_nonexisting_theme(
  110. upload, nonexisting_theme
  111. ):
  112. response = upload(nonexisting_theme)
  113. assert_has_error_message(response)