test_theme_creation_and_edition.py 6.7 KB

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