test_css_creation_and_edition.py 8.6 KB

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