test_reordering_css.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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_first_css_changes_order_with_middle_css_when_moved_down(
  64. move_down, theme, css_list
  65. ):
  66. move_down(theme, css_list[FIRST])
  67. middle_css = css_list[MIDDLE]
  68. middle_css.refresh_from_db()
  69. assert middle_css.order == FIRST
  70. def test_last_css_changes_order_with_middle_css_when_moved_up(move_up, theme, css_list):
  71. move_up(theme, css_list[LAST])
  72. middle_css = css_list[MIDDLE]
  73. middle_css.refresh_from_db()
  74. assert middle_css.order == LAST
  75. def test_middle_css_changes_order_with_last_css_when_moved_down(
  76. move_down, theme, css_list
  77. ):
  78. move_down(theme, css_list[MIDDLE])
  79. last_css = css_list[LAST]
  80. last_css.refresh_from_db()
  81. assert last_css.order == MIDDLE
  82. def test_middle_css_changes_order_with_first_css_when_moved_up(
  83. move_up, theme, css_list
  84. ):
  85. move_up(theme, css_list[MIDDLE])
  86. first_css = css_list[FIRST]
  87. first_css.refresh_from_db()
  88. assert first_css.order == MIDDLE
  89. def test_first_css_changes_order_with_last_css_when_moved_down_after_middle_deletion(
  90. move_down, theme, css_list
  91. ):
  92. css_list[MIDDLE].delete()
  93. move_down(theme, css_list[FIRST])
  94. last_css = css_list[LAST]
  95. last_css.refresh_from_db()
  96. assert last_css.order == FIRST
  97. def test_last_css_changes_order_with_first_css_when_moved_up_after_middle_deletion(
  98. move_up, theme, css_list
  99. ):
  100. css_list[MIDDLE].delete()
  101. move_up(theme, css_list[LAST])
  102. first_css = css_list[FIRST]
  103. first_css.refresh_from_db()
  104. assert first_css.order == LAST
  105. def test_if_css_doesnt_belong_to_theme_move_down_action_sets_error_message(
  106. move_down, other_theme, css_list
  107. ):
  108. response = move_down(other_theme, css_list[MIDDLE])
  109. assert_has_error_message(response)
  110. def test_if_css_doesnt_belong_to_theme_move_up_action_sets_error_message(
  111. move_up, other_theme, css_list
  112. ):
  113. response = move_up(other_theme, css_list[MIDDLE])
  114. assert_has_error_message(response)
  115. def test_if_ran_for_default_theme_move_down_action_sets_error_message(
  116. move_down, default_theme, css_list
  117. ):
  118. response = move_down(default_theme, css_list[MIDDLE])
  119. assert_has_error_message(response)
  120. def test_if_ran_for_default_theme_move_up_action_sets_error_message(
  121. move_up, default_theme, css_list
  122. ):
  123. response = move_up(default_theme, css_list[MIDDLE])
  124. assert_has_error_message(response)
  125. def test_if_given_nonexisting_css_id_move_down_action_sets_error_message(
  126. mocker, move_down, theme, css_list
  127. ):
  128. response = move_down(theme, mocker.Mock(pk=css_list[LAST].pk + 1))
  129. assert_has_error_message(response)
  130. def test_if_given_nonexisting_css_id_move_up_action_sets_error_message(
  131. mocker, move_up, theme, css_list
  132. ):
  133. response = move_up(theme, mocker.Mock(pk=css_list[LAST].pk + 1))
  134. assert_has_error_message(response)
  135. def test_if_given_nonexisting_theme_id_move_down_action_sets_error_message(
  136. mocker, move_down, nonexisting_theme, css_list
  137. ):
  138. response = move_down(nonexisting_theme, css_list[FIRST])
  139. assert_has_error_message(response)
  140. def test_if_given_nonexisting_theme_id_move_up_action_sets_error_message(
  141. mocker, move_up, nonexisting_theme, css_list
  142. ):
  143. response = move_up(nonexisting_theme, css_list[LAST])
  144. assert_has_error_message(response)
  145. def test_next_new_css_order_is_larger_than_largest_existing_css_order(theme):
  146. theme.css.create(name="CSS", url="https://test.cdn/font.css", order=4)
  147. assert get_next_css_order(theme) == 5