test_reordering_css.py 5.7 KB

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