test_css_files_creation_and_edition.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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_css_file_is_created_with_correct_order(
  92. theme, admin_client, create_link, css_link, data
  93. ):
  94. admin_client.post(create_link, data)
  95. css = theme.css.get(name=data["name"])
  96. assert css.order == 1
  97. def test_error_message_is_set_if_user_attempts_to_create_css_in_default_theme(
  98. default_theme, admin_client
  99. ):
  100. create_link = reverse(
  101. "misago:admin:appearance:themes:new-css-file", kwargs={"pk": default_theme.pk}
  102. )
  103. response = admin_client.get(create_link)
  104. assert_has_error_message(response)
  105. def test_error_message_is_set_if_user_attempts_to_create_css_in_nonexisting_theme(
  106. nonexisting_theme, admin_client
  107. ):
  108. create_link = reverse(
  109. "misago:admin:appearance:themes:new-css-file",
  110. kwargs={"pk": nonexisting_theme.pk},
  111. )
  112. response = admin_client.get(create_link)
  113. assert_has_error_message(response)
  114. def test_css_creation_form_redirects_user_to_edition_after_creation(
  115. theme, admin_client, create_link, data
  116. ):
  117. data["stay"] = "1"
  118. response = admin_client.post(create_link, data)
  119. assert response["location"] == reverse(
  120. "misago:admin:appearance:themes:edit-css-file",
  121. kwargs={"pk": theme.pk, "css_pk": theme.css.last().pk},
  122. )
  123. def test_css_edition_form_is_displayed(admin_client, edit_link, css):
  124. response = admin_client.get(edit_link)
  125. assert response.status_code == 200
  126. assert_contains(response, css.name)
  127. def test_css_edition_form_contains_source_file_contents(admin_client, edit_link, css):
  128. response = admin_client.get(edit_link)
  129. assert_contains(response, css.source_file.read().decode("utf-8"))
  130. def test_css_name_can_be_changed(admin_client, edit_link, css, data):
  131. data["name"] = "new-name.css"
  132. admin_client.post(edit_link, data)
  133. css.refresh_from_db()
  134. assert css.name == data["name"]
  135. def test_css_name_change_also_changes_source_file_name(
  136. admin_client, edit_link, css, data
  137. ):
  138. data["name"] = "new-name.css"
  139. admin_client.post(edit_link, data)
  140. css.refresh_from_db()
  141. assert "new-name" in str(css.source_file)
  142. def test_css_source_can_be_changed(admin_client, edit_link, css, data):
  143. data["source"] = ".misago-footer { display: none; }"
  144. admin_client.post(edit_link, data)
  145. css.refresh_from_db()
  146. assert css.source_file.read().decode("utf-8") == data["source"]
  147. def test_changing_css_source_also_changes_source_hash(
  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 css.source_hash != original_hash
  155. def test_changing_css_source_also_changes_hash_in_filename(
  156. admin_client, edit_link, css, data
  157. ):
  158. original_hash = css.source_hash
  159. data["source"] = ".misago-footer { display: none; }"
  160. admin_client.post(edit_link, data)
  161. css.refresh_from_db()
  162. assert original_hash not in str(css.source_file)
  163. assert css.source_hash in str(css.source_file)
  164. def test_hash_stays_same_if_source_is_not_changed(admin_client, edit_link, css, data):
  165. original_hash = css.source_hash
  166. data["name"] = "changed.css"
  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_hash == css.source_hash
  171. def test_file_is_not_updated_if_form_data_has_no_changes(
  172. admin_client, edit_link, css, data
  173. ):
  174. original_mtime = os.path.getmtime(css.source_file.path)
  175. data["source"] = css.source_file.read().decode("utf-8")
  176. admin_client.post(edit_link, data)
  177. css.refresh_from_db()
  178. assert original_mtime == os.path.getmtime(css.source_file.path)
  179. def test_css_order_stays_the_same_after_edit(admin_client, edit_link, css, data):
  180. original_order = css.order
  181. data["name"] = "changed.css"
  182. admin_client.post(edit_link, data)
  183. css.refresh_from_db()
  184. assert css.order == original_order
  185. def test_css_edit_form_redirects_user_to_edition_after_saving(
  186. theme, admin_client, edit_link, css, data
  187. ):
  188. data["stay"] = "1"
  189. response = admin_client.post(edit_link, data)
  190. assert response["location"] == edit_link
  191. def test_error_message_is_set_if_user_attempts_to_edit_css_file_in_default_theme(
  192. default_theme, admin_client
  193. ):
  194. edit_link = reverse(
  195. "misago:admin:appearance:themes:edit-css-file",
  196. kwargs={"pk": default_theme.pk, "css_pk": 1},
  197. )
  198. response = admin_client.get(edit_link)
  199. assert_has_error_message(response)
  200. def test_error_message_is_set_if_user_attempts_to_edit_css_file_in_nonexisting_theme(
  201. nonexisting_theme, admin_client
  202. ):
  203. edit_link = reverse(
  204. "misago:admin:appearance:themes:edit-css-file",
  205. kwargs={"pk": nonexisting_theme.pk, "css_pk": 1},
  206. )
  207. response = admin_client.get(edit_link)
  208. assert_has_error_message(response)
  209. def test_error_message_is_set_if_user_attempts_to_edit_css_belonging_to_other_theme(
  210. other_theme, admin_client, css
  211. ):
  212. edit_link = reverse(
  213. "misago:admin:appearance:themes:edit-css-file",
  214. kwargs={"pk": other_theme.pk, "css_pk": css.pk},
  215. )
  216. response = admin_client.get(edit_link)
  217. assert_has_error_message(response)
  218. def test_error_message_is_set_if_user_attempts_to_edit_nonexisting_css(
  219. theme, admin_client
  220. ):
  221. edit_link = reverse(
  222. "misago:admin:appearance:themes:edit-css-file",
  223. kwargs={"pk": theme.pk, "css_pk": 1},
  224. )
  225. response = admin_client.get(edit_link)
  226. assert_has_error_message(response)
  227. def test_error_message_is_set_if_user_attempts_to_edit_css_link_with_file_form(
  228. theme, admin_client, css_link
  229. ):
  230. edit_link = reverse(
  231. "misago:admin:appearance:themes:edit-css-file",
  232. kwargs={"pk": theme.pk, "css_pk": css_link.pk},
  233. )
  234. response = admin_client.get(edit_link)
  235. assert_has_error_message(response)