test_uploading_media.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 png_file():
  8. return os.path.join(TESTS_DIR, "images", "test.png")
  9. @pytest.fixture
  10. def svg_file():
  11. return os.path.join(TESTS_DIR, "images", "test.svg")
  12. @pytest.fixture
  13. def hashable_file():
  14. return os.path.join(TESTS_DIR, "css", "test.css")
  15. @pytest.fixture
  16. def hashed_file():
  17. return os.path.join(TESTS_DIR, "css", "test.4846cb3b.css")
  18. @pytest.fixture
  19. def upload(admin_client):
  20. def post_upload(theme, asset_files=None):
  21. url = reverse(
  22. "misago:admin:appearance:themes:upload-media", kwargs={"pk": theme.pk}
  23. )
  24. if asset_files:
  25. data = asset_files if isinstance(asset_files, list) else [asset_files]
  26. else:
  27. data = None
  28. return admin_client.post(url, {"assets": data})
  29. return post_upload
  30. def test_font_file_can_be_uploaded(upload, theme):
  31. font_file = os.path.join(TESTS_DIR, "font", "Lato.ttf")
  32. with open(font_file, "rb") as fp:
  33. upload(theme, fp)
  34. assert theme.media.exists()
  35. def test_text_file_can_be_uploaded(upload, theme):
  36. text_file = os.path.join(TESTS_DIR, "font", "OFL.txt")
  37. with open(text_file) as fp:
  38. upload(theme, fp)
  39. assert theme.media.exists()
  40. def test_svg_file_can_be_uploaded(upload, theme):
  41. svg_file = os.path.join(TESTS_DIR, "images", "test.svg")
  42. with open(svg_file) as fp:
  43. upload(theme, fp)
  44. assert theme.media.exists()
  45. def test_png_file_can_be_uploaded(upload, theme, png_file):
  46. with open(png_file, "rb") as fp:
  47. upload(theme, fp)
  48. assert theme.media.exists()
  49. def test_media_file_name_is_set_as_asset_name(upload, theme, svg_file):
  50. with open(svg_file) as fp:
  51. upload(theme, fp)
  52. media = theme.media.last()
  53. expected_filename = str(svg_file).split("/")[-1]
  54. assert media.name == expected_filename
  55. def test_media_file_is_uploaded_to_theme_directory(upload, theme, svg_file):
  56. with open(svg_file) as fp:
  57. upload(theme, fp)
  58. media = theme.media.last()
  59. assert theme.dirname in str(media.file)
  60. def test_hash_is_added_to_uploaded_media_file_name(
  61. upload, theme, hashable_file, hashed_file
  62. ):
  63. with open(hashable_file) as fp:
  64. upload(theme, fp)
  65. media = theme.media.last()
  66. filename = str(media.file.path).split("/")[-1]
  67. expected_filename = str(hashed_file).split("/")[-1]
  68. assert filename == expected_filename
  69. def test_hash_is_set_on_media_asset(upload, theme, hashed_file):
  70. with open(hashed_file) as fp:
  71. upload(theme, fp)
  72. media = theme.media.last()
  73. assert media.hash
  74. def test_media_file_name_is_preserved_if_it_already_contains_correct_hash(
  75. upload, theme, hashed_file
  76. ):
  77. with open(hashed_file) as fp:
  78. upload(theme, fp)
  79. media = theme.media.last()
  80. filename = str(media.file.path).split("/")[-1]
  81. expected_filename = str(hashed_file).split("/")[-1]
  82. assert filename == expected_filename
  83. def test_new_hash_is_added_to_media_file_name_if_it_contains_incorrect_hash(
  84. upload, theme
  85. ):
  86. incorrectly_hashed_file = os.path.join(TESTS_DIR, "css", "test.0046cb3b.css")
  87. with open(incorrectly_hashed_file) as fp:
  88. upload(theme, fp)
  89. media = theme.media.last()
  90. filename = str(media.file.path).split("/")[-1]
  91. assert media.hash in filename
  92. def test_image_dimensions_are_set_for_uploaded_image_file(upload, theme, png_file):
  93. with open(png_file, "rb") as fp:
  94. upload(theme, fp)
  95. media = theme.media.last()
  96. assert media.width
  97. assert media.height
  98. def test_thumbnail_is_generated_in_theme_directory_for_uploaded_image_file(
  99. upload, theme, png_file
  100. ):
  101. with open(png_file, "rb") as fp:
  102. upload(theme, fp)
  103. media = theme.media.last()
  104. assert theme.dirname in str(media.thumbnail)
  105. def test_error_message_is_set_if_no_media_was_uploaded(upload, theme):
  106. response = upload(theme)
  107. assert_has_error_message(response)
  108. def test_error_message_is_set_if_user_attempts_to_upload_file_to_default_theme(
  109. upload, default_theme
  110. ):
  111. response = upload(default_theme)
  112. assert_has_error_message(response)
  113. def test_error_message_is_set_if_user_attempts_to_upload_file_to_nonexisting_theme(
  114. upload, nonexisting_theme
  115. ):
  116. response = upload(nonexisting_theme)
  117. assert_has_error_message(response)