Browse Source

Add tests for changing active theme, extra failture tests for theme edition

rafalp 6 years ago
parent
commit
76fba69e09

+ 50 - 0
misago/themes/admin/tests/test_changing_active_theme.py

@@ -0,0 +1,50 @@
+import pytest
+from django.urls import reverse
+
+from ....cache.test import assert_invalidates_cache
+from ....test import assert_has_error_message
+from ... import THEME_CACHE
+from ...models import Theme
+
+
+@pytest.fixture
+def activate_link(theme):
+    return reverse("misago:admin:appearance:themes:activate", kwargs={"pk": theme.pk})
+
+
+def test_active_theme_can_changed(admin_client, activate_link, theme):
+    admin_client.post(activate_link)
+    theme.refresh_from_db()
+    assert theme.is_active
+
+
+def test_default_theme_can_be_set_as_active_theme(admin_client, default_theme):
+    activate_link = reverse(
+        "misago:admin:appearance:themes:activate", kwargs={"pk": default_theme.pk}
+    )
+    admin_client.post(activate_link)
+    default_theme.refresh_from_db()
+    assert default_theme.is_active
+
+
+def test_changing_active_theme_removes_active_status_from_previous_active_theme(
+    admin_client, activate_link, theme
+):
+    admin_client.post(activate_link)
+    # objets.get() will raise if more than one theme is active
+    assert Theme.objects.get(is_active=True) == theme
+
+
+def test_changing_active_theme_to_nonexisting_theme_sets_error_message(
+    admin_client, nonexisting_theme
+):
+    activate_link = reverse(
+        "misago:admin:appearance:themes:activate", kwargs={"pk": nonexisting_theme.pk}
+    )
+    response = admin_client.post(activate_link)
+    assert_has_error_message(response)
+
+
+def test_changing_active_theme_invalidates_themes_cache(admin_client, activate_link):
+    with assert_invalidates_cache(THEME_CACHE):
+        admin_client.post(activate_link)

+ 21 - 2
misago/themes/admin/tests/test_theme_creation_and_edition.py

@@ -1,7 +1,9 @@
 import pytest
 import pytest
 from django.urls import reverse
 from django.urls import reverse
 
 
+from ....cache.test import assert_invalidates_cache
 from ....test import assert_contains, assert_has_error_message
 from ....test import assert_contains, assert_has_error_message
+from ... import THEME_CACHE
 from ...models import Theme
 from ...models import Theme
 
 
 
 
@@ -208,10 +210,20 @@ def test_theme_edition_fails_if_parent_theme_doesnt_exist(
     admin_client, edit_link, theme, nonexisting_theme
     admin_client, edit_link, theme, nonexisting_theme
 ):
 ):
     admin_client.post(
     admin_client.post(
-        edit_link, {"name": "Edited theme", "parent": nonexisting_theme.pk}
+        edit_link, {"name": "Edited Theme", "parent": nonexisting_theme.pk}
     )
     )
     theme.refresh_from_db()
     theme.refresh_from_db()
-    assert theme.name != "Edited theme"
+    assert theme.name != "Edited Theme"
+
+
+def test_error_message_is_set_if_user_attempts_to_edit_default_theme(
+    admin_client, default_theme
+):
+    edit_link = reverse(
+        "misago:admin:appearance:themes:edit", kwargs={"pk": default_theme.pk}
+    )
+    response = admin_client.get(edit_link)
+    assert_has_error_message(response)
 
 
 
 
 def test_error_message_is_set_if_user_attempts_to_edit_nonexisting_theme(
 def test_error_message_is_set_if_user_attempts_to_edit_nonexisting_theme(
@@ -222,3 +234,10 @@ def test_error_message_is_set_if_user_attempts_to_edit_nonexisting_theme(
     )
     )
     response = admin_client.get(edit_link)
     response = admin_client.get(edit_link)
     assert_has_error_message(response)
     assert_has_error_message(response)
+
+
+def test_moving_theme_invalidates_themes_cache(
+    admin_client, edit_link, theme, default_theme
+):
+    with assert_invalidates_cache(THEME_CACHE):
+        admin_client.post(edit_link, {"name": theme.name, "parent": default_theme.pk})

+ 7 - 2
misago/themes/admin/views.py

@@ -45,6 +45,11 @@ class EditTheme(ThemeAdmin, generic.ModelFormView):
         if target.is_default:
         if target.is_default:
             return gettext("Default theme can't be edited.")
             return gettext("Default theme can't be edited.")
 
 
+    def handle_form(self, form, request, target):
+        super().handle_form(form, request, target)
+        if "parent" in form.changed_data:
+            clear_theme_cache()
+
 
 
 class DeleteTheme(ThemeAdmin, generic.ModelFormView):
 class DeleteTheme(ThemeAdmin, generic.ModelFormView):
     message_submit = _('Theme "%(name)s" has been deleted.')
     message_submit = _('Theme "%(name)s" has been deleted.')
@@ -294,7 +299,7 @@ class EditThemeCss(NewThemeCss):
         return form(instance=css, initial=initial_data)
         return form(instance=css, initial=initial_data)
 
 
     def handle_form(self, form, request, theme, css):
     def handle_form(self, form, request, theme, css):
-        if form.has_changed():
+        if "source" in form.changed_data:
             form.save()
             form.save()
             if css.source_needs_building:
             if css.source_needs_building:
                 build_single_theme_css.delay(css.pk)
                 build_single_theme_css.delay(css.pk)
@@ -321,7 +326,7 @@ class NewThemeCssLink(ThemeCssFormAdmin):
 
 
     def handle_form(self, form, *args):
     def handle_form(self, form, *args):
         super().handle_form(form, *args)
         super().handle_form(form, *args)
-        if form.has_changed():
+        if "url" in form.changed_data:
             update_remote_css_size.delay(form.instance.pk)
             update_remote_css_size.delay(form.instance.pk)
             clear_theme_cache()
             clear_theme_cache()