test_css_creation_and_edition.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. import os
  2. import pytest
  3. from django.urls import reverse
  4. from ....test import assert_contains, assert_has_error_message
  5. @pytest.fixture
  6. def create_link(theme):
  7. return reverse(
  8. "misago:admin:appearance:themes:new-css-file", kwargs={"pk": theme.pk}
  9. )
  10. @pytest.fixture
  11. def edit_link(theme, css):
  12. return reverse(
  13. "misago:admin:appearance:themes:edit-css-file",
  14. kwargs={"pk": theme.pk, "css_pk": css.pk},
  15. )
  16. @pytest.fixture
  17. def data():
  18. return {"name": "test.css", "source": (".page-header { padding: 0}")}
  19. def test_css_creation_form_is_displayed(admin_client, create_link):
  20. response = admin_client.get(create_link)
  21. assert response.status_code == 200
  22. assert_contains(response, "New CSS")
  23. def test_css_can_be_created(theme, admin_client, create_link, data):
  24. admin_client.post(create_link, data)
  25. assert theme.css.exists()
  26. def test_css_is_created_with_entered_name(theme, admin_client, create_link, data):
  27. admin_client.post(create_link, data)
  28. assert theme.css.last().name == data["name"]
  29. def test_created_source_file_contains_css_entered_by_user(
  30. theme, admin_client, create_link, data
  31. ):
  32. admin_client.post(create_link, data)
  33. css = theme.css.last()
  34. assert css.source_file.read().decode("utf-8") == data["source"]
  35. def test_source_file_is_created_in_theme_directory(
  36. theme, admin_client, create_link, data
  37. ):
  38. admin_client.post(create_link, data)
  39. css = theme.css.last()
  40. assert theme.dirname in str(css.source_file)
  41. def test_created_source_file_name_starts_with_asset_name(
  42. theme, admin_client, create_link, data
  43. ):
  44. admin_client.post(create_link, data)
  45. css = theme.css.last()
  46. source_filename = str(css.source_file).split("/")[-1]
  47. assert source_filename.startswith("test.")
  48. def test_created_source_file_has_css_extension(theme, admin_client, create_link, data):
  49. admin_client.post(create_link, data)
  50. css = theme.css.last()
  51. source_filename = str(css.source_file).split("/")[-1]
  52. assert source_filename.endswith(".css")
  53. def test_created_source_file_is_hashed(theme, admin_client, create_link, data):
  54. admin_client.post(create_link, data)
  55. css = theme.css.last()
  56. source_filename = str(css.source_file).split("/")[-1]
  57. assert ".%s." % css.source_hash in source_filename
  58. def test_css_creation_fails_if_name_is_not_given(
  59. theme, admin_client, create_link, data
  60. ):
  61. data["name"] = ""
  62. admin_client.post(create_link, data)
  63. assert not theme.css.exists()
  64. def test_css_creation_fails_if_name_is_not_valid(
  65. theme, admin_client, create_link, data
  66. ):
  67. data["name"] = "missing-extension"
  68. admin_client.post(create_link, data)
  69. assert not theme.css.exists()
  70. def test_css_creation_fails_if_name_is_already_taken_by_other_css_in_theme(
  71. theme, admin_client, create_link, data, css
  72. ):
  73. data["name"] = css.name
  74. admin_client.post(create_link, data)
  75. assert theme.css.count() == 1
  76. def test_css_name_usage_check_passess_if_name_is_used_by_other_theme_css(
  77. other_theme, admin_client, data, css
  78. ):
  79. create_link = reverse(
  80. "misago:admin:appearance:themes:new-css-file", kwargs={"pk": other_theme.pk}
  81. )
  82. data["name"] = css.name
  83. admin_client.post(create_link, data)
  84. assert other_theme.css.exists()
  85. def test_css_creation_fails_if_source_is_not_given(
  86. theme, admin_client, create_link, data
  87. ):
  88. data["source"] = ""
  89. admin_client.post(create_link, data)
  90. assert not theme.css.exists()
  91. def test_error_message_is_set_if_user_attempts_to_create_css_in_default_theme(
  92. default_theme, admin_client
  93. ):
  94. create_link = reverse(
  95. "misago:admin:appearance:themes:new-css-file", kwargs={"pk": default_theme.pk}
  96. )
  97. response = admin_client.get(create_link)
  98. assert_has_error_message(response)
  99. def test_error_message_is_set_if_user_attempts_to_create_css_in_nonexisting_theme(
  100. nonexisting_theme, admin_client
  101. ):
  102. create_link = reverse(
  103. "misago:admin:appearance:themes:new-css-file",
  104. kwargs={"pk": nonexisting_theme.pk},
  105. )
  106. response = admin_client.get(create_link)
  107. assert_has_error_message(response)
  108. def test_css_creation_form_redirects_user_to_edition_after_creation(
  109. theme, admin_client, create_link, data
  110. ):
  111. data["stay"] = "1"
  112. response = admin_client.post(create_link, data)
  113. assert response["location"] == reverse(
  114. "misago:admin:appearance:themes:edit-css-file",
  115. kwargs={"pk": theme.pk, "css_pk": theme.css.last().pk},
  116. )
  117. def test_css_edition_form_is_displayed(admin_client, edit_link, css):
  118. response = admin_client.get(edit_link)
  119. assert response.status_code == 200
  120. assert_contains(response, css.name)
  121. def test_css_edition_form_contains_source_file_contents(admin_client, edit_link, css):
  122. response = admin_client.get(edit_link)
  123. assert_contains(response, css.source_file.read().decode("utf-8"))
  124. def test_name_can_be_changed(admin_client, edit_link, css, data):
  125. data["name"] = "new-name.css"
  126. admin_client.post(edit_link, data)
  127. css.refresh_from_db()
  128. assert css.name == data["name"]
  129. def test_name_change_also_changes_source_file_name(admin_client, edit_link, css, data):
  130. data["name"] = "new-name.css"
  131. admin_client.post(edit_link, data)
  132. css.refresh_from_db()
  133. assert "new-name" in str(css.source_file)
  134. def test_css_source_can_be_changed(admin_client, edit_link, css, data):
  135. data["source"] = ".misago-footer { display: none; }"
  136. admin_client.post(edit_link, data)
  137. css.refresh_from_db()
  138. assert css.source_file.read().decode("utf-8") == data["source"]
  139. def test_changing_css_source_also_changes_source_hash(
  140. admin_client, edit_link, css, data
  141. ):
  142. original_hash = css.source_hash
  143. data["source"] = ".misago-footer { display: none; }"
  144. admin_client.post(edit_link, data)
  145. css.refresh_from_db()
  146. assert css.source_hash != original_hash
  147. def test_changing_css_source_also_changes_hash_in_filename(
  148. admin_client, edit_link, css, data
  149. ):
  150. original_hash = css.source_hash
  151. data["source"] = ".misago-footer { display: none; }"
  152. admin_client.post(edit_link, data)
  153. css.refresh_from_db()
  154. assert original_hash not in str(css.source_file)
  155. assert css.source_hash in str(css.source_file)
  156. def test_hash_stays_same_if_source_is_not_changed(admin_client, edit_link, css, data):
  157. original_hash = css.source_hash
  158. data["name"] = "changed.css"
  159. data["source"] = css.source_file.read().decode("utf-8")
  160. admin_client.post(edit_link, data)
  161. css.refresh_from_db()
  162. assert original_hash == css.source_hash
  163. def test_file_is_not_updated_if_form_data_has_no_changes(
  164. admin_client, edit_link, css, data
  165. ):
  166. original_mtime = os.path.getmtime(css.source_file.path)
  167. data["source"] = css.source_file.read().decode("utf-8")
  168. admin_client.post(edit_link, data)
  169. css.refresh_from_db()
  170. assert original_mtime == os.path.getmtime(css.source_file.path)
  171. def test_file_order_stays_the_same_after_edit(admin_client, edit_link, css, data):
  172. original_order = css.order
  173. data["name"] = "changed.css"
  174. admin_client.post(edit_link, data)
  175. css.refresh_from_db()
  176. assert css.order == original_order
  177. def test_css_edit_form_redirects_user_to_edition_after_saving(
  178. theme, admin_client, edit_link, css, data
  179. ):
  180. data["stay"] = "1"
  181. response = admin_client.post(edit_link, data)
  182. assert response["location"] == edit_link
  183. def test_error_message_is_set_if_user_attempts_to_edit_css_in_default_theme(
  184. default_theme, admin_client
  185. ):
  186. edit_link = reverse(
  187. "misago:admin:appearance:themes:edit-css-file",
  188. kwargs={"pk": default_theme.pk, "css_pk": 1},
  189. )
  190. response = admin_client.get(edit_link)
  191. assert_has_error_message(response)
  192. def test_error_message_is_set_if_user_attempts_to_edit_css_in_nonexisting_theme(
  193. nonexisting_theme, admin_client
  194. ):
  195. edit_link = reverse(
  196. "misago:admin:appearance:themes:edit-css-file",
  197. kwargs={"pk": nonexisting_theme.pk, "css_pk": 1},
  198. )
  199. response = admin_client.get(edit_link)
  200. assert_has_error_message(response)
  201. def test_error_message_is_set_if_user_attempts_to_edit_css_belonging_to_other_theme(
  202. other_theme, admin_client, css
  203. ):
  204. edit_link = reverse(
  205. "misago:admin:appearance:themes:edit-css-file",
  206. kwargs={"pk": other_theme.pk, "css_pk": css.pk},
  207. )
  208. response = admin_client.get(edit_link)
  209. assert_has_error_message(response)
  210. def test_error_message_is_set_if_user_attempts_to_edit_nonexisting_css(
  211. theme, admin_client
  212. ):
  213. edit_link = reverse(
  214. "misago:admin:appearance:themes:edit-css-file",
  215. kwargs={"pk": theme.pk, "css_pk": 1},
  216. )
  217. response = admin_client.get(edit_link)
  218. assert_has_error_message(response)