test_css_links_creation_and_deletion.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. import pytest
  2. from django.urls import reverse
  3. from ....cache.test import assert_invalidates_cache
  4. from ....test import assert_contains, assert_has_error_message
  5. from ... import THEME_CACHE
  6. @pytest.fixture
  7. def create_link(theme):
  8. return reverse(
  9. "misago:admin:appearance:themes:new-css-link", kwargs={"pk": theme.pk}
  10. )
  11. @pytest.fixture
  12. def edit_link(theme, css_link):
  13. return reverse(
  14. "misago:admin:appearance:themes:edit-css-link",
  15. kwargs={"pk": theme.pk, "css_pk": css_link.pk},
  16. )
  17. @pytest.fixture
  18. def data():
  19. return {"name": "CSS link", "url": "https://example.com/cdn.css"}
  20. def test_css_link_creation_form_is_displayed(admin_client, create_link):
  21. response = admin_client.get(create_link)
  22. assert response.status_code == 200
  23. assert_contains(response, "New CSS link")
  24. def test_css_link_can_be_created(theme, admin_client, create_link, data):
  25. admin_client.post(create_link, data)
  26. assert theme.css.exists()
  27. def test_css_link_is_created_with_entered_name(theme, admin_client, create_link, data):
  28. admin_client.post(create_link, data)
  29. assert theme.css.last().name == data["name"]
  30. def test_css_link_name_can_be_descriptive(theme, admin_client, create_link, data):
  31. data["name"] = "font (from font.hosting.com)"
  32. admin_client.post(create_link, data)
  33. assert theme.css.last().name == data["name"]
  34. def test_css_link_creation_fails_if_name_is_not_given(
  35. theme, admin_client, create_link, data
  36. ):
  37. data["name"] = ""
  38. admin_client.post(create_link, data)
  39. assert not theme.css.exists()
  40. def test_css_link_creation_fails_if_name_is_already_taken_by_other_css_in_theme(
  41. theme, admin_client, create_link, data, css
  42. ):
  43. data["name"] = css.name
  44. admin_client.post(create_link, data)
  45. assert theme.css.count() == 1
  46. def test_css_link_name_usage_check_passess_if_name_is_used_by_other_theme_css(
  47. other_theme, admin_client, data, css
  48. ):
  49. create_link = reverse(
  50. "misago:admin:appearance:themes:new-css-link", kwargs={"pk": other_theme.pk}
  51. )
  52. data["name"] = css.name
  53. admin_client.post(create_link, data)
  54. assert other_theme.css.exists()
  55. def test_css_link_creation_fails_if_url_is_not_given(
  56. theme, admin_client, create_link, data
  57. ):
  58. data["url"] = ""
  59. admin_client.post(create_link, data)
  60. assert not theme.css.exists()
  61. def test_css_link_creation_fails_if_url_is_not_valid(
  62. theme, admin_client, create_link, data
  63. ):
  64. data["url"] = "invalid-url"
  65. admin_client.post(create_link, data)
  66. assert not theme.css.exists()
  67. def test_css_link_is_created_with_correct_order(
  68. theme, admin_client, create_link, css, data
  69. ):
  70. admin_client.post(create_link, data)
  71. css_link = theme.css.get(name=data["name"])
  72. assert css_link.order == 1
  73. def test_css_link_creation_queues_task_to_download_remote_css_size(
  74. theme, admin_client, create_link, data, mock_update_remote_css_size
  75. ):
  76. admin_client.post(create_link, data)
  77. css_link = theme.css.last()
  78. mock_update_remote_css_size.assert_called_once_with(css_link.pk)
  79. def test_css_link_creation_invalidates_theme_cache(
  80. theme, admin_client, create_link, data
  81. ):
  82. with assert_invalidates_cache(THEME_CACHE):
  83. admin_client.post(create_link, data)
  84. def test_error_message_is_set_if_user_attempts_to_create_css_link_in_default_theme(
  85. default_theme, admin_client
  86. ):
  87. create_link = reverse(
  88. "misago:admin:appearance:themes:new-css-link", kwargs={"pk": default_theme.pk}
  89. )
  90. response = admin_client.get(create_link)
  91. assert_has_error_message(response)
  92. def test_error_message_is_set_if_user_attempts_to_create_css_link_in_nonexisting_theme(
  93. nonexisting_theme, admin_client
  94. ):
  95. create_link = reverse(
  96. "misago:admin:appearance:themes:new-css-link",
  97. kwargs={"pk": nonexisting_theme.pk},
  98. )
  99. response = admin_client.get(create_link)
  100. assert_has_error_message(response)
  101. def test_css_link_creation_form_redirects_user_to_new_creation_form_after_creation(
  102. theme, admin_client, create_link, data
  103. ):
  104. data["stay"] = "1"
  105. response = admin_client.post(create_link, data)
  106. assert response["location"] == reverse(
  107. "misago:admin:appearance:themes:new-css-link", kwargs={"pk": theme.pk}
  108. )
  109. def test_css_link_edition_form_is_displayed(admin_client, edit_link, css_link):
  110. response = admin_client.get(edit_link)
  111. assert response.status_code == 200
  112. assert_contains(response, css_link.name)
  113. def test_css_link_name_can_be_changed(admin_client, edit_link, css_link, data):
  114. data["name"] = "new link name"
  115. admin_client.post(edit_link, data)
  116. css_link.refresh_from_db()
  117. assert css_link.name == data["name"]
  118. def test_css_link_url_can_be_changed(admin_client, edit_link, css_link, data):
  119. data["url"] = "https://new.css-link.com/test.css"
  120. admin_client.post(edit_link, data)
  121. css_link.refresh_from_db()
  122. assert css_link.url == data["url"]
  123. def test_changing_css_link_url_queues_task_to_download_remote_css_size(
  124. admin_client, edit_link, css_link, data, mock_update_remote_css_size
  125. ):
  126. data["url"] = "https://new.css-link.com/test.css"
  127. admin_client.post(edit_link, data)
  128. css_link.refresh_from_db()
  129. mock_update_remote_css_size.assert_called_once_with(css_link.pk)
  130. def test_not_changing_css_link_url_doesnt_queue_task_to_download_remote_css_size(
  131. admin_client, edit_link, css_link, data, mock_update_remote_css_size
  132. ):
  133. admin_client.post(edit_link, data)
  134. css_link.refresh_from_db()
  135. mock_update_remote_css_size.assert_not_called()
  136. def test_changing_css_link_invalidates_theme_cache(admin_client, edit_link, data):
  137. with assert_invalidates_cache(THEME_CACHE):
  138. admin_client.post(edit_link, data)
  139. def test_css_order_stays_the_same_after_edit(admin_client, edit_link, css_link, data):
  140. original_order = css_link.order
  141. data["name"] = "changed link"
  142. admin_client.post(edit_link, data)
  143. css_link.refresh_from_db()
  144. assert css_link.order == original_order
  145. def test_error_message_is_set_if_user_attempts_to_edit_css_file_with_link_form(
  146. theme, admin_client, css
  147. ):
  148. edit_link = reverse(
  149. "misago:admin:appearance:themes:edit-css-link",
  150. kwargs={"pk": theme.pk, "css_pk": css.pk},
  151. )
  152. response = admin_client.get(edit_link)
  153. assert_has_error_message(response)