Skip to content

Commit

Permalink
Test improvementes (#48)
Browse files Browse the repository at this point in the history
* disable debug in tests

* fix test_allauth_login

* improve coverage
  • Loading branch information
mfosterw authored Jan 16, 2024
1 parent 9a85d1d commit c09e151
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 25 deletions.
1 change: 0 additions & 1 deletion config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion config/settings/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,20 @@
)
# 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
# ------------------------------------------------------------------------------
# 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
Expand Down
36 changes: 26 additions & 10 deletions democrasite/users/tests/test_admin.py
Original file line number Diff line number Diff line change
@@ -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")
Expand Down
22 changes: 10 additions & 12 deletions democrasite/users/tests/test_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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/")
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 ====
Expand Down

0 comments on commit c09e151

Please sign in to comment.