test_uploading_css.py 3.7 KB

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