test_creating_and_editing_themes.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. import pytest
  2. from django.urls import reverse
  3. from ....cache.test import assert_invalidates_cache
  4. from ....test import assert_contains, assert_has_error_message
  5. from ... import THEME_CACHE
  6. from ...models import Theme
  7. @pytest.fixture
  8. def create_link():
  9. return reverse("misago:admin:appearance:themes:new")
  10. @pytest.fixture
  11. def edit_link(theme):
  12. return reverse("misago:admin:appearance:themes:edit", kwargs={"pk": theme.pk})
  13. def test_theme_creation_form_is_displayed(admin_client, create_link):
  14. response = admin_client.get(create_link)
  15. assert response.status_code == 200
  16. assert_contains(response, "New theme")
  17. def test_theme_creation_form_reads_parent_from_url_and_preselects_it_in_parent_select(
  18. admin_client, create_link, theme
  19. ):
  20. response = admin_client.get("%s?parent=%s" % (create_link, theme.pk))
  21. assert_contains(response, '<option value="%s" selected>' % theme.pk)
  22. def test_theme_creation_form_reads_parent_from_url_and_discards_invalid_value(
  23. admin_client, create_link, theme
  24. ):
  25. response = admin_client.get("%s?parent=%s" % (create_link, theme.pk + 1))
  26. assert response.status_code == 200
  27. def test_theme_can_be_created(admin_client, create_link):
  28. admin_client.post(create_link, {"name": "New Theme"})
  29. Theme.objects.get(name="New Theme")
  30. def test_child_theme_for_custom_theme_can_be_created(admin_client, create_link, theme):
  31. admin_client.post(create_link, {"name": "New Theme", "parent": theme.pk})
  32. child_theme = Theme.objects.get(name="New Theme")
  33. assert child_theme.parent == theme
  34. def test_child_theme_for_default_theme_can_be_created(
  35. admin_client, create_link, default_theme
  36. ):
  37. admin_client.post(create_link, {"name": "New Theme", "parent": default_theme.pk})
  38. child_theme = Theme.objects.get(name="New Theme")
  39. assert child_theme.parent == default_theme
  40. def test_creating_child_theme_updates_themes_tree(admin_client, create_link, theme):
  41. admin_client.post(create_link, {"name": "New Theme", "parent": theme.pk})
  42. child_theme = Theme.objects.get(name="New Theme")
  43. assert child_theme.tree_id == theme.tree_id
  44. assert child_theme.lft == 2
  45. assert child_theme.rght == 3
  46. theme.refresh_from_db()
  47. assert theme.lft == 1
  48. assert theme.rght == 4
  49. def test_theme_creation_fails_if_name_is_not_given(admin_client, create_link):
  50. admin_client.post(create_link, {"name": ""})
  51. assert Theme.objects.count() == 1
  52. def test_theme_creation_fails_if_parent_theme_doesnt_exist(
  53. admin_client, create_link, nonexisting_theme
  54. ):
  55. admin_client.post(
  56. create_link, {"name": "New Theme", "parent": nonexisting_theme.pk}
  57. )
  58. assert Theme.objects.count() == 1
  59. def test_theme_edition_form_is_displayed(admin_client, edit_link, theme):
  60. response = admin_client.get(edit_link)
  61. assert response.status_code == 200
  62. assert_contains(response, theme.name)
  63. def test_theme_name_can_be_edited(admin_client, edit_link, theme):
  64. admin_client.post(edit_link, {"name": "Edited Theme"})
  65. theme.refresh_from_db()
  66. assert theme.name == "Edited Theme"
  67. def test_theme_can_be_moved_under_other_theme(
  68. admin_client, edit_link, theme, default_theme
  69. ):
  70. admin_client.post(edit_link, {"name": theme.name, "parent": default_theme.pk})
  71. theme.refresh_from_db()
  72. assert theme.parent == default_theme
  73. def test_moving_theme_under_other_theme_updates_themes_tree(
  74. admin_client, edit_link, theme, default_theme
  75. ):
  76. admin_client.post(edit_link, {"name": theme.name, "parent": default_theme.pk})
  77. default_theme.refresh_from_db()
  78. theme.refresh_from_db()
  79. assert theme.tree_id == default_theme.tree_id
  80. assert theme.lft == 2
  81. assert theme.rght == 3
  82. assert default_theme.lft == 1
  83. assert default_theme.rght == 4
  84. def test_theme_cant_be_moved_under_itself(admin_client, edit_link, theme):
  85. admin_client.post(edit_link, {"name": theme.name, "parent": theme.pk})
  86. theme.refresh_from_db()
  87. assert not theme.parent
  88. def test_theme_cant_be_moved_under_its_child_theme(admin_client, edit_link, theme):
  89. child_theme = Theme.objects.create(name="Child Theme", parent=theme)
  90. admin_client.post(edit_link, {"name": theme.name, "parent": child_theme.pk})
  91. theme.refresh_from_db()
  92. assert not theme.parent
  93. def test_moving_child_theme_under_other_theme_updates_both_themes_trees(
  94. admin_client, theme, default_theme
  95. ):
  96. child_theme = Theme.objects.create(name="Child Theme", parent=theme)
  97. edit_link = reverse(
  98. "misago:admin:appearance:themes:edit", kwargs={"pk": child_theme.pk}
  99. )
  100. admin_client.post(edit_link, {"name": child_theme.name, "parent": default_theme.pk})
  101. default_theme.refresh_from_db()
  102. child_theme.refresh_from_db()
  103. theme.refresh_from_db()
  104. assert child_theme.parent == default_theme
  105. assert child_theme.tree_id == default_theme.tree_id
  106. assert child_theme.lft == 2
  107. assert child_theme.rght == 3
  108. assert default_theme.lft == 1
  109. assert default_theme.rght == 4
  110. assert theme.tree_id != default_theme.tree_id
  111. assert theme.lft == 1
  112. assert theme.rght == 2
  113. def test_moving_theme_under_root_updates_theme_tree_and_creates_new_one(
  114. admin_client, edit_link, theme, default_theme
  115. ):
  116. theme.parent = default_theme
  117. theme.save()
  118. admin_client.post(edit_link, {"name": theme.name})
  119. default_theme.refresh_from_db()
  120. theme.refresh_from_db()
  121. assert theme.parent is None
  122. assert theme.tree_id != default_theme.tree_id
  123. assert theme.lft == 1
  124. assert theme.rght == 2
  125. assert default_theme.lft == 1
  126. assert default_theme.rght == 2
  127. def test_moving_theme_with_child_under_root_updates_theme_tree_and_creates_new_one(
  128. admin_client, edit_link, theme, default_theme
  129. ):
  130. theme.parent = default_theme
  131. theme.save()
  132. child_theme = Theme.objects.create(name="Child Theme", parent=theme)
  133. admin_client.post(edit_link, {"name": theme.name})
  134. default_theme.refresh_from_db()
  135. child_theme.refresh_from_db()
  136. theme.refresh_from_db()
  137. assert theme.tree_id != default_theme.tree_id
  138. assert theme.lft == 1
  139. assert theme.rght == 4
  140. assert child_theme.parent == theme
  141. assert child_theme.tree_id == theme.tree_id
  142. assert child_theme.lft == 2
  143. assert child_theme.rght == 3
  144. assert default_theme.lft == 1
  145. assert default_theme.rght == 2
  146. def test_theme_edition_fails_if_parent_theme_doesnt_exist(
  147. admin_client, edit_link, theme, nonexisting_theme
  148. ):
  149. admin_client.post(
  150. edit_link, {"name": "Edited Theme", "parent": nonexisting_theme.pk}
  151. )
  152. theme.refresh_from_db()
  153. assert theme.name != "Edited Theme"
  154. def test_error_message_is_set_if_user_attempts_to_edit_default_theme(
  155. admin_client, default_theme
  156. ):
  157. edit_link = reverse(
  158. "misago:admin:appearance:themes:edit", kwargs={"pk": default_theme.pk}
  159. )
  160. response = admin_client.get(edit_link)
  161. assert_has_error_message(response)
  162. def test_error_message_is_set_if_user_attempts_to_edit_nonexisting_theme(
  163. admin_client, nonexisting_theme
  164. ):
  165. edit_link = reverse(
  166. "misago:admin:appearance:themes:edit", kwargs={"pk": nonexisting_theme.pk}
  167. )
  168. response = admin_client.get(edit_link)
  169. assert_has_error_message(response)
  170. def test_moving_theme_invalidates_themes_cache(
  171. admin_client, edit_link, theme, default_theme
  172. ):
  173. with assert_invalidates_cache(THEME_CACHE):
  174. admin_client.post(edit_link, {"name": theme.name, "parent": default_theme.pk})