test_css_files_creation_and_edition.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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_without_url_is_created_without_rebuilding_flag(
  92. theme, admin_client, create_link, data
  93. ):
  94. admin_client.post(create_link, data)
  95. css = theme.css.last()
  96. assert not css.source_needs_building
  97. def test_css_file_with_image_url_is_created_with_rebuilding_flag(
  98. theme, admin_client, create_link, data, image
  99. ):
  100. data["source"] = "body { background-image: url(/static/%s); }" % image.name
  101. admin_client.post(create_link, data)
  102. css = theme.css.last()
  103. assert css.source_needs_building
  104. def test_css_file_is_created_with_correct_order(
  105. theme, admin_client, create_link, css_link, data
  106. ):
  107. admin_client.post(create_link, data)
  108. css = theme.css.get(name=data["name"])
  109. assert css.order == 1
  110. def test_error_message_is_set_if_user_attempts_to_create_css_in_default_theme(
  111. default_theme, admin_client
  112. ):
  113. create_link = reverse(
  114. "misago:admin:appearance:themes:new-css-file", kwargs={"pk": default_theme.pk}
  115. )
  116. response = admin_client.get(create_link)
  117. assert_has_error_message(response)
  118. def test_error_message_is_set_if_user_attempts_to_create_css_in_nonexisting_theme(
  119. nonexisting_theme, admin_client
  120. ):
  121. create_link = reverse(
  122. "misago:admin:appearance:themes:new-css-file",
  123. kwargs={"pk": nonexisting_theme.pk},
  124. )
  125. response = admin_client.get(create_link)
  126. assert_has_error_message(response)
  127. def test_css_creation_form_redirects_user_to_edition_after_creation(
  128. theme, admin_client, create_link, data
  129. ):
  130. data["stay"] = "1"
  131. response = admin_client.post(create_link, data)
  132. assert response["location"] == reverse(
  133. "misago:admin:appearance:themes:edit-css-file",
  134. kwargs={"pk": theme.pk, "css_pk": theme.css.last().pk},
  135. )
  136. def test_css_edition_form_is_displayed(admin_client, edit_link, css):
  137. response = admin_client.get(edit_link)
  138. assert response.status_code == 200
  139. assert_contains(response, css.name)
  140. def test_css_edition_form_contains_source_file_contents(admin_client, edit_link, css):
  141. response = admin_client.get(edit_link)
  142. assert_contains(response, css.source_file.read().decode("utf-8"))
  143. def test_css_name_can_be_changed(admin_client, edit_link, css, data):
  144. data["name"] = "new-name.css"
  145. admin_client.post(edit_link, data)
  146. css.refresh_from_db()
  147. assert css.name == data["name"]
  148. def test_css_name_change_also_changes_source_file_name(
  149. admin_client, edit_link, css, data
  150. ):
  151. data["name"] = "new-name.css"
  152. admin_client.post(edit_link, data)
  153. css.refresh_from_db()
  154. assert "new-name" in str(css.source_file)
  155. def test_css_source_can_be_changed(admin_client, edit_link, css, data):
  156. data["source"] = ".misago-footer { display: none; }"
  157. admin_client.post(edit_link, data)
  158. css.refresh_from_db()
  159. assert css.source_file.read().decode("utf-8") == data["source"]
  160. def test_changing_css_source_also_changes_source_hash(
  161. admin_client, edit_link, css, data
  162. ):
  163. original_hash = css.source_hash
  164. data["source"] = ".misago-footer { display: none; }"
  165. admin_client.post(edit_link, data)
  166. css.refresh_from_db()
  167. assert css.source_hash != original_hash
  168. def test_changing_css_source_also_changes_hash_in_filename(
  169. admin_client, edit_link, css, data
  170. ):
  171. original_hash = css.source_hash
  172. data["source"] = ".misago-footer { display: none; }"
  173. admin_client.post(edit_link, data)
  174. css.refresh_from_db()
  175. assert original_hash not in str(css.source_file)
  176. assert css.source_hash in str(css.source_file)
  177. def test_hash_stays_same_if_source_is_not_changed(admin_client, edit_link, css, data):
  178. original_hash = css.source_hash
  179. data["name"] = "changed.css"
  180. data["source"] = css.source_file.read().decode("utf-8")
  181. admin_client.post(edit_link, data)
  182. css.refresh_from_db()
  183. assert original_hash == css.source_hash
  184. def test_file_is_not_updated_if_form_data_has_no_changes(
  185. admin_client, edit_link, css, data
  186. ):
  187. original_mtime = os.path.getmtime(css.source_file.path)
  188. data["source"] = css.source_file.read().decode("utf-8")
  189. admin_client.post(edit_link, data)
  190. css.refresh_from_db()
  191. assert original_mtime == os.path.getmtime(css.source_file.path)
  192. def test_adding_image_url_to_edited_file_sets_rebuilding_flag(
  193. theme, admin_client, edit_link, css, data, image
  194. ):
  195. data["source"] = "body { background-image: url(/static/%s); }" % image.name
  196. admin_client.post(edit_link, data)
  197. css.refresh_from_db()
  198. assert css.source_needs_building
  199. def test_removing_url_from_edited_file_removes_rebuilding_flag(
  200. theme, admin_client, edit_link, css, data
  201. ):
  202. css.source_needs_building = True
  203. css.save()
  204. data["source"] = "body { background-image: none; }"
  205. admin_client.post(edit_link, data)
  206. css.refresh_from_db()
  207. assert not css.source_needs_building
  208. def test_css_order_stays_the_same_after_edit(admin_client, edit_link, css, data):
  209. original_order = css.order
  210. data["name"] = "changed.css"
  211. admin_client.post(edit_link, data)
  212. css.refresh_from_db()
  213. assert css.order == original_order
  214. def test_css_edit_form_redirects_user_to_edition_after_saving(
  215. theme, admin_client, edit_link, css, data
  216. ):
  217. data["stay"] = "1"
  218. response = admin_client.post(edit_link, data)
  219. assert response["location"] == edit_link
  220. def test_error_message_is_set_if_user_attempts_to_edit_css_file_in_default_theme(
  221. default_theme, admin_client
  222. ):
  223. edit_link = reverse(
  224. "misago:admin:appearance:themes:edit-css-file",
  225. kwargs={"pk": default_theme.pk, "css_pk": 1},
  226. )
  227. response = admin_client.get(edit_link)
  228. assert_has_error_message(response)
  229. def test_error_message_is_set_if_user_attempts_to_edit_css_file_in_nonexisting_theme(
  230. nonexisting_theme, admin_client
  231. ):
  232. edit_link = reverse(
  233. "misago:admin:appearance:themes:edit-css-file",
  234. kwargs={"pk": nonexisting_theme.pk, "css_pk": 1},
  235. )
  236. response = admin_client.get(edit_link)
  237. assert_has_error_message(response)
  238. def test_error_message_is_set_if_user_attempts_to_edit_css_belonging_to_other_theme(
  239. other_theme, admin_client, css
  240. ):
  241. edit_link = reverse(
  242. "misago:admin:appearance:themes:edit-css-file",
  243. kwargs={"pk": other_theme.pk, "css_pk": css.pk},
  244. )
  245. response = admin_client.get(edit_link)
  246. assert_has_error_message(response)
  247. def test_error_message_is_set_if_user_attempts_to_edit_nonexisting_css(
  248. theme, admin_client
  249. ):
  250. edit_link = reverse(
  251. "misago:admin:appearance:themes:edit-css-file",
  252. kwargs={"pk": theme.pk, "css_pk": 1},
  253. )
  254. response = admin_client.get(edit_link)
  255. assert_has_error_message(response)
  256. def test_error_message_is_set_if_user_attempts_to_edit_css_link_with_file_form(
  257. theme, admin_client, css_link
  258. ):
  259. edit_link = reverse(
  260. "misago:admin:appearance:themes:edit-css-file",
  261. kwargs={"pk": theme.pk, "css_pk": css_link.pk},
  262. )
  263. response = admin_client.get(edit_link)
  264. assert_has_error_message(response)