test_reordering_css.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. import pytest
  2. from django.urls import reverse
  3. from ....test import assert_has_error_message
  4. from ..css import get_next_css_order
  5. FIRST = 0
  6. MIDDLE = 1
  7. LAST = 2
  8. @pytest.fixture
  9. def css_list(theme):
  10. return [
  11. theme.css.create(name="CSS", url="https://test.cdn/font.css", order=FIRST),
  12. theme.css.create(name="CSS", url="https://test.cdn/font.css", order=MIDDLE),
  13. theme.css.create(name="CSS", url="https://test.cdn/font.css", order=LAST),
  14. ]
  15. @pytest.fixture
  16. def move_up(admin_client):
  17. def move_up_client(theme, css):
  18. url = reverse(
  19. "misago:admin:appearance:themes:move-css-up",
  20. kwargs={"pk": theme.pk, "css_pk": css.pk},
  21. )
  22. return admin_client.post(url)
  23. return move_up_client
  24. @pytest.fixture
  25. def move_down(admin_client):
  26. def move_down_client(theme, css):
  27. url = reverse(
  28. "misago:admin:appearance:themes:move-css-down",
  29. kwargs={"pk": theme.pk, "css_pk": css.pk},
  30. )
  31. return admin_client.post(url)
  32. return move_down_client
  33. def test_first_css_cant_be_moved_up(move_up, theme, css_list):
  34. first_css = css_list[FIRST]
  35. move_up(theme, first_css)
  36. first_css.refresh_from_db()
  37. assert first_css.order == FIRST
  38. def test_last_css_cant_be_moved_down(move_down, theme, css_list):
  39. last_css = css_list[LAST]
  40. move_down(theme, last_css)
  41. last_css.refresh_from_db()
  42. assert last_css.order == LAST
  43. def test_first_css_can_be_moved_down(move_down, theme, css_list):
  44. first_css = css_list[FIRST]
  45. move_down(theme, first_css)
  46. first_css.refresh_from_db()
  47. assert first_css.order == MIDDLE
  48. def test_last_css_can_be_moved_up(move_up, theme, css_list):
  49. last_css = css_list[LAST]
  50. move_up(theme, last_css)
  51. last_css.refresh_from_db()
  52. assert last_css.order == MIDDLE
  53. def test_middle_css_can_be_moved_down(move_down, theme, css_list):
  54. middle_css = css_list[MIDDLE]
  55. move_down(theme, middle_css)
  56. middle_css.refresh_from_db()
  57. assert middle_css.order == LAST
  58. def test_middle_css_can_be_moved_up(move_up, theme, css_list):
  59. middle_css = css_list[MIDDLE]
  60. move_up(theme, middle_css)
  61. middle_css.refresh_from_db()
  62. assert middle_css.order == FIRST
  63. def test_middle_css_can_be_moved_down(move_down, theme, css_list):
  64. middle_css = css_list[MIDDLE]
  65. move_down(theme, middle_css)
  66. middle_css.refresh_from_db()
  67. assert middle_css.order == LAST
  68. def test_middle_css_can_be_moved_up(move_up, theme, css_list):
  69. middle_css = css_list[MIDDLE]
  70. move_up(theme, middle_css)
  71. middle_css.refresh_from_db()
  72. assert middle_css.order == FIRST
  73. def test_first_css_changes_order_with_middle_css_when_moved_down(
  74. move_down, theme, css_list
  75. ):
  76. move_down(theme, css_list[FIRST])
  77. middle_css = css_list[MIDDLE]
  78. middle_css.refresh_from_db()
  79. assert middle_css.order == FIRST
  80. def test_last_css_changes_order_with_middle_css_when_moved_up(move_up, theme, css_list):
  81. move_up(theme, css_list[LAST])
  82. middle_css = css_list[MIDDLE]
  83. middle_css.refresh_from_db()
  84. assert middle_css.order == LAST
  85. def test_middle_css_changes_order_with_last_css_when_moved_down(
  86. move_down, theme, css_list
  87. ):
  88. move_down(theme, css_list[MIDDLE])
  89. last_css = css_list[LAST]
  90. last_css.refresh_from_db()
  91. assert last_css.order == MIDDLE
  92. def test_middle_css_changes_order_with_first_css_when_moved_up(
  93. move_up, theme, css_list
  94. ):
  95. move_up(theme, css_list[MIDDLE])
  96. first_css = css_list[FIRST]
  97. first_css.refresh_from_db()
  98. assert first_css.order == MIDDLE
  99. def test_first_css_changes_order_with_last_css_when_moved_down_after_middle_deletion(
  100. move_down, theme, css_list
  101. ):
  102. css_list[MIDDLE].delete()
  103. move_down(theme, css_list[FIRST])
  104. last_css = css_list[LAST]
  105. last_css.refresh_from_db()
  106. assert last_css.order == FIRST
  107. def test_last_css_changes_order_with_first_css_when_moved_up_after_middle_deletion(
  108. move_up, theme, css_list
  109. ):
  110. css_list[MIDDLE].delete()
  111. move_up(theme, css_list[LAST])
  112. first_css = css_list[FIRST]
  113. first_css.refresh_from_db()
  114. assert first_css.order == LAST
  115. def test_if_css_doesnt_belong_to_theme_move_down_action_sets_error_message(
  116. move_down, other_theme, css_list
  117. ):
  118. response = move_down(other_theme, css_list[MIDDLE])
  119. assert_has_error_message(response)
  120. def test_if_css_doesnt_belong_to_theme_move_up_action_sets_error_message(
  121. move_up, other_theme, css_list
  122. ):
  123. response = move_up(other_theme, css_list[MIDDLE])
  124. assert_has_error_message(response)
  125. def test_if_ran_for_default_theme_move_down_action_sets_error_message(
  126. move_down, default_theme, css_list
  127. ):
  128. response = move_down(default_theme, css_list[MIDDLE])
  129. assert_has_error_message(response)
  130. def test_if_ran_for_default_theme_move_up_action_sets_error_message(
  131. move_up, default_theme, css_list
  132. ):
  133. response = move_up(default_theme, css_list[MIDDLE])
  134. assert_has_error_message(response)
  135. def test_if_given_nonexisting_css_id_move_down_action_sets_error_message(
  136. mocker, move_down, theme, css_list
  137. ):
  138. response = move_down(theme, mocker.Mock(pk=css_list[LAST].pk + 1))
  139. assert_has_error_message(response)
  140. def test_if_given_nonexisting_css_id_move_up_action_sets_error_message(
  141. mocker, move_up, theme, css_list
  142. ):
  143. response = move_up(theme, mocker.Mock(pk=css_list[LAST].pk + 1))
  144. assert_has_error_message(response)
  145. def test_if_given_nonexisting_theme_id_move_down_action_sets_error_message(
  146. mocker, move_down, nonexisting_theme, css_list
  147. ):
  148. response = move_down(nonexisting_theme, css_list[FIRST])
  149. assert_has_error_message(response)
  150. def test_if_given_nonexisting_theme_id_move_up_action_sets_error_message(
  151. mocker, move_up, nonexisting_theme, css_list
  152. ):
  153. response = move_up(nonexisting_theme, css_list[LAST])
  154. assert_has_error_message(response)
  155. def test_next_new_css_order_is_larger_than_largest_existing_css_order(theme):
  156. theme.css.create(name="CSS", url="https://test.cdn/font.css", order=4),
  157. assert get_next_css_order(theme) == 5