test_reordering_css.py 5.9 KB

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