test_creating_and_deleting_css_links.py 6.1 KB

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