test_uploading_media.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import os
  2. import pytest
  3. from django.core.files.uploadedfile import UploadedFile
  4. from django.urls import reverse
  5. from ....test import assert_has_error_message
  6. TESTS_DIR = os.path.dirname(os.path.abspath(__file__))
  7. @pytest.fixture
  8. def png_file():
  9. return os.path.join(TESTS_DIR, "images", "test.png")
  10. @pytest.fixture
  11. def svg_file():
  12. return os.path.join(TESTS_DIR, "images", "test.svg")
  13. @pytest.fixture
  14. def hashable_file():
  15. return os.path.join(TESTS_DIR, "css", "test.css")
  16. @pytest.fixture
  17. def hashed_file():
  18. return os.path.join(TESTS_DIR, "css", "test.4846cb3b.css")
  19. @pytest.fixture
  20. def upload(admin_client):
  21. def post_upload(theme, asset_files=None):
  22. url = reverse(
  23. "misago:admin:appearance:themes:upload-media", kwargs={"pk": theme.pk}
  24. )
  25. if asset_files is not None:
  26. data = asset_files if isinstance(asset_files, list) else [asset_files]
  27. else:
  28. data = None
  29. return admin_client.post(url, {"assets": data})
  30. return post_upload
  31. def test_font_file_can_be_uploaded(upload, theme):
  32. font_file = os.path.join(TESTS_DIR, "font", "Lato.ttf")
  33. with open(font_file, "rb") as fp:
  34. upload(theme, fp)
  35. assert theme.media.exists()
  36. def test_text_file_can_be_uploaded(upload, theme):
  37. text_file = os.path.join(TESTS_DIR, "font", "OFL.txt")
  38. with open(text_file) as fp:
  39. upload(theme, fp)
  40. assert theme.media.exists()
  41. def test_svg_file_can_be_uploaded(upload, theme):
  42. svg_file = os.path.join(TESTS_DIR, "images", "test.svg")
  43. with open(svg_file) as fp:
  44. upload(theme, fp)
  45. assert theme.media.exists()
  46. def test_png_file_can_be_uploaded(upload, theme, png_file):
  47. with open(png_file, "rb") as fp:
  48. upload(theme, fp)
  49. assert theme.media.exists()
  50. def test_media_file_name_is_set_as_asset_name(upload, theme, svg_file):
  51. with open(svg_file) as fp:
  52. upload(theme, fp)
  53. media = theme.media.last()
  54. expected_filename = str(svg_file).split("/")[-1]
  55. assert media.name == expected_filename
  56. def test_media_file_is_uploaded_to_theme_directory(upload, theme, svg_file):
  57. with open(svg_file) as fp:
  58. upload(theme, fp)
  59. media = theme.media.last()
  60. assert theme.dirname in str(media.file)
  61. def test_hash_is_added_to_uploaded_media_file_name(
  62. upload, theme, hashable_file, hashed_file
  63. ):
  64. with open(hashable_file) as fp:
  65. upload(theme, fp)
  66. media = theme.media.last()
  67. filename = str(media.file.path).split("/")[-1]
  68. expected_filename = str(hashed_file).split("/")[-1]
  69. assert filename == expected_filename
  70. def test_hash_is_set_on_media_asset(upload, theme, hashed_file):
  71. with open(hashed_file) as fp:
  72. upload(theme, fp)
  73. media = theme.media.last()
  74. assert media.hash
  75. def test_media_file_name_is_preserved_if_it_already_contains_correct_hash(
  76. upload, theme, hashed_file
  77. ):
  78. with open(hashed_file) as fp:
  79. upload(theme, fp)
  80. media = theme.media.last()
  81. filename = str(media.file.path).split("/")[-1]
  82. expected_filename = str(hashed_file).split("/")[-1]
  83. assert filename == expected_filename
  84. def test_new_hash_is_added_to_media_file_name_if_it_contains_incorrect_hash(
  85. upload, theme
  86. ):
  87. incorrectly_hashed_file = os.path.join(TESTS_DIR, "css", "test.0046cb3b.css")
  88. with open(incorrectly_hashed_file) as fp:
  89. upload(theme, fp)
  90. media = theme.media.last()
  91. filename = str(media.file.path).split("/")[-1]
  92. assert media.hash in filename
  93. def test_newly_uploaded_media_file_replaces_old_one_if_file_names_are_same(
  94. upload, theme, hashable_file
  95. ):
  96. with open(hashable_file) as fp:
  97. upload(theme, fp)
  98. original_media = theme.media.get()
  99. with open(os.path.join(TESTS_DIR, "css", "test-changed.css")) as fp:
  100. size = len(fp.read())
  101. fp.seek(0)
  102. upload(
  103. theme, UploadedFile(fp, name="test.css", content_type="text/css", size=size)
  104. )
  105. updated_media = theme.media.last()
  106. assert updated_media.name == original_media.name
  107. assert updated_media.hash != original_media.hash
  108. assert theme.media.count() == 1
  109. def test_image_dimensions_are_set_for_uploaded_image_file(upload, theme, png_file):
  110. with open(png_file, "rb") as fp:
  111. upload(theme, fp)
  112. media = theme.media.last()
  113. assert media.width
  114. assert media.height
  115. def test_thumbnail_is_generated_in_theme_directory_for_uploaded_image_file(
  116. upload, theme, png_file
  117. ):
  118. with open(png_file, "rb") as fp:
  119. upload(theme, fp)
  120. media = theme.media.last()
  121. assert theme.dirname in str(media.thumbnail)
  122. def test_uploading_media_triggers_css_build(
  123. upload, theme, png_file, mock_build_theme_css
  124. ):
  125. with open(png_file, "rb") as fp:
  126. upload(theme, fp)
  127. mock_build_theme_css.assert_called_once_with(theme.pk)
  128. def test_error_message_is_set_if_no_media_was_uploaded(upload, theme):
  129. response = upload(theme)
  130. assert_has_error_message(response)
  131. def test_error_message_is_set_if_user_attempts_to_upload_file_to_default_theme(
  132. upload, default_theme
  133. ):
  134. response = upload(default_theme)
  135. assert_has_error_message(response)
  136. def test_error_message_is_set_if_user_attempts_to_upload_file_to_nonexisting_theme(
  137. upload, nonexisting_theme
  138. ):
  139. response = upload(nonexisting_theme)
  140. assert_has_error_message(response)