diff --git a/config/settings/base.py b/config/settings/base.py index dadad30..e4c397f 100644 --- a/config/settings/base.py +++ b/config/settings/base.py @@ -178,7 +178,6 @@ # TEMPLATES # ------------------------------------------------------------------------------ # https://docs.djangoproject.com/en/dev/ref/settings/#templates - TEMPLATES = [ { # https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-TEMPLATES-BACKEND diff --git a/config/settings/test.py b/config/settings/test.py index 157b86d..140e84c 100644 --- a/config/settings/test.py +++ b/config/settings/test.py @@ -14,7 +14,7 @@ ) # https://docs.djangoproject.com/en/dev/ref/settings/#test-runner TEST_RUNNER = "django.test.runner.DiscoverRunner" -# Admin site is only enabled during development +# Admin site is not available in production, but should still be tested INSTALLED_APPS += ["django.contrib.admin"] # noqa F405 # PASSWORDS @@ -22,6 +22,12 @@ # https://docs.djangoproject.com/en/dev/ref/settings/#password-hashers PASSWORD_HASHERS = ["django.contrib.auth.hashers.MD5PasswordHasher"] +# TEMPLATES +# ------------------------------------------------------------------------------ +# https://docs.djangoproject.com/en/dev/ref/settings/#templates +# Needed to let template test coverage be detected +TEMPLATES[0]["OPTIONS"]["debug"] = True # type: ignore[index] # noqa F405 + # EMAIL # ------------------------------------------------------------------------------ # https://docs.djangoproject.com/en/dev/ref/settings/#email-backend diff --git a/democrasite/users/tests/test_admin.py b/democrasite/users/tests/test_admin.py index 6d15fc1..668e117 100644 --- a/democrasite/users/tests/test_admin.py +++ b/democrasite/users/tests/test_admin.py @@ -1,23 +1,39 @@ # pylint: disable=too-few-public-methods,no-self-use +from importlib import reload + import pytest -from django.conf import settings -from django.test import Client, override_settings +from django.contrib.admin.sites import AlreadyRegistered +from django.test import Client from django.urls import reverse +from pytest_django.asserts import assertRedirects from democrasite.users.models import User class TestUserAdmin: - # TODO: Test DJANGO_ADMIN_FORCE_ALLAUTH setting - @pytest.mark.xfail(reason="This test fails on GitHub Actions for some reason") - @override_settings(DJANGO_ADMIN_FORCE_ALLAUTH=True) - def test_allauth_login(self, admin_client: Client): - # Client starts logged in for some reason - admin_client.logout() + @pytest.fixture(autouse=True) + def enable_admin(self, settings): + # Admin site is only enabled during development + settings.DEBUG = True + + @pytest.fixture + def force_allauth(self, settings): + settings.DJANGO_ADMIN_FORCE_ALLAUTH = True + # Reload the admin module to apply the setting change + import democrasite.users.admin as users_admin # pylint: disable=import-outside-toplevel + + try: + reload(users_admin) + except AlreadyRegistered: + pass + + @pytest.mark.usefixtures("force_allauth") + def test_allauth_login(self, client: Client, settings): url = reverse("admin:login") - response = admin_client.get(url, follow=True) + response = client.get(url) - assert response.request["PATH_INFO"] == reverse(settings.LOGIN_URL) + expected_url = reverse(settings.LOGIN_URL) + "?next=" + url + assertRedirects(response, expected_url, status_code=302, target_status_code=200) def test_changelist(self, admin_client: Client): url = reverse("admin:users_user_changelist") diff --git a/democrasite/users/tests/test_templates.py b/democrasite/users/tests/test_templates.py index 678d9f4..d5e458e 100644 --- a/democrasite/users/tests/test_templates.py +++ b/democrasite/users/tests/test_templates.py @@ -8,7 +8,7 @@ from django.shortcuts import render from django.test import Client, RequestFactory from django.urls import reverse -from django.views.defaults import page_not_found, permission_denied +from django.views.defaults import page_not_found, permission_denied, server_error from democrasite.users.models import User from democrasite.users.tests.factories import UserFactory @@ -40,47 +40,45 @@ def test_base(self, rf: RequestFactory, user: User): assert b"login-dropdown" not in response.content, "should not be visible to logged in users" assert b"logout-form" in response.content, "should be visible to logged out users" - def test_403_with_message(self, rf: RequestFactory): # client: Client): + def test_403_with_message(self, rf: RequestFactory): request = rf.get("/fake-url/") response = permission_denied(request, exception=Exception("Test message")) - print(response.content) assert response.status_code == 403 assert b"Test message" in response.content - def test_403_without_message(self, rf: RequestFactory): # client: Client): + def test_403_without_message(self, rf: RequestFactory): request = rf.get("/fake-url/") response = permission_denied(request, exception=Exception("")) - print(response.content) assert response.status_code == 403 assert b"You're not allowed to access this page." in response.content - def test_404_without_message(self, rf: RequestFactory): # client: Client): + def test_404_without_message(self, rf: RequestFactory): request = rf.get("/fake-url/") response = page_not_found(request, exception=Exception("")) - print(response.content) assert response.status_code == 404 assert b"This is not the page you were looking for." in response.content - def test_404_with_message(self, rf: RequestFactory): # client: Client): + def test_404_with_message(self, rf: RequestFactory): request = rf.get("/fake-url/") response = page_not_found(request, exception=Exception("Test message")) - print(response.content) assert response.status_code == 404 assert b"Test message" in response.content - def test_500(self, client: Client): - response = client.get("/500/") + def test_500(self, rf: RequestFactory): + request = rf.get("/fake-url/") + + response = server_error(request) assert response.status_code == 500 - assert response.templates[0].name == "500.html" + assert b"Error" in response.content def test_page_disabled(self, rf: RequestFactory): request = rf.get("/fake-url/") diff --git a/pyproject.toml b/pyproject.toml index 70fdbd1..83c060c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -3,7 +3,6 @@ minversion = "6.0" addopts = "--reuse-db" DJANGO_SETTINGS_MODULE = "config.settings.test" -django_debug_mode = "True" python_files = ["tests.py", "test_*.py"] # ==== Coverage ====