-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace
pytest
with Django's built-in runner (#18)
* Replace `pytest` with Django's built-in runner * Drop whitenoise public directory * Correctly collect coverage for parallel test runs * Note why certain fixtures are created with each test
- Loading branch information
1 parent
fed96ed
commit 87d5cc1
Showing
10 changed files
with
65 additions
and
204 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,42 @@ | ||
import pytest | ||
from django.test import RequestFactory, TestCase | ||
from wagtail.models import Site | ||
|
||
from cms.core.context_processors import global_vars | ||
from cms.core.models import Tracking | ||
|
||
pytestmark = pytest.mark.django_db | ||
|
||
class ContextProcessorTestCase(TestCase): | ||
"""Tests for context processors.""" | ||
def setUp(self): | ||
request_factory = RequestFactory() | ||
|
||
def test_when_no_tracking_settings_defined(rf): | ||
"""Check the global vars include sensible defaults when no Tracking settings defined.""" | ||
request = rf.get("/") | ||
assert global_vars(request) == { | ||
"GOOGLE_TAG_MANAGER_ID": "", | ||
"SEO_NOINDEX": False, | ||
"LANGUAGE_CODE": "en-gb", | ||
"IS_EXTERNAL_ENV": False, | ||
} | ||
# Request is created with each test to avoid mutation side-effects | ||
self.request = request_factory.get("/") | ||
|
||
def test_when_no_tracking_settings_defined(self): | ||
"""Check the global vars include sensible defaults when no Tracking settings defined.""" | ||
self.assertEqual( | ||
global_vars(self.request), | ||
{ | ||
"GOOGLE_TAG_MANAGER_ID": "", | ||
"SEO_NOINDEX": False, | ||
"LANGUAGE_CODE": "en-gb", | ||
"IS_EXTERNAL_ENV": False, | ||
}, | ||
) | ||
|
||
def test_when_tracking_settings_defined(rf): | ||
"""Confirm the global vars include Tracking settings when defined.""" | ||
Tracking.objects.create( | ||
site=Site.objects.get(is_default_site=True), | ||
google_tag_manager_id="GTM-123456", | ||
) | ||
request = rf.get("/") | ||
assert global_vars(request) == { | ||
"GOOGLE_TAG_MANAGER_ID": "GTM-123456", | ||
"SEO_NOINDEX": False, | ||
"LANGUAGE_CODE": "en-gb", | ||
"IS_EXTERNAL_ENV": False, | ||
} | ||
def test_when_tracking_settings_defined(self): | ||
"""Confirm the global vars include Tracking settings when defined.""" | ||
Tracking.objects.create( | ||
site=Site.objects.get(is_default_site=True), | ||
google_tag_manager_id="GTM-123456", | ||
) | ||
self.assertEqual( | ||
global_vars(self.request), | ||
{ | ||
"GOOGLE_TAG_MANAGER_ID": "GTM-123456", | ||
"SEO_NOINDEX": False, | ||
"LANGUAGE_CODE": "en-gb", | ||
"IS_EXTERNAL_ENV": False, | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,19 @@ | ||
import logging | ||
|
||
import pytest | ||
from django.test import Client, TestCase | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_csrf_token_mismatch_logs_an_error(csrf_check_client, caplog, enable_console_logging): # pylint: disable=unused-argument | ||
"""Check that the custom csrf error view logs CSRF failures.""" | ||
csrf_check_client.cookies["csrftoken"] = "wrong" | ||
class CSRFTestCase(TestCase): | ||
"""Tests for CSRF enforcement.""" | ||
def setUp(self): | ||
# Client is created with each test to avoid mutation side-effects | ||
self.client = Client(enforce_csrf_checks=True) | ||
|
||
with caplog.at_level(logging.ERROR, logger="django.security.csrf"): | ||
csrf_check_client.post("/admin/login/", {}) | ||
def test_csrf_token_mismatch_logs_an_error(self): | ||
"""Check that the custom csrf error view logs CSRF failures.""" | ||
self.client.cookies["csrftoken"] = "wrong" | ||
|
||
assert "CSRF Failure: CSRF cookie" in caplog.text | ||
with self.assertLogs(logger="django.security.csrf", level=logging.ERROR) as logs: | ||
self.client.post("/admin/login/", {}) | ||
|
||
self.assertIn("CSRF Failure: CSRF cookie", logs.output[0]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
import pytest | ||
from django.test import TestCase | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_referrer_policy(client): | ||
"""Test that we have a Referrer-Policy header.""" | ||
response = client.get("/") | ||
assert response["Referrer-Policy"] == "no-referrer-when-downgrade" | ||
class ReferrerPolicyTestCase(TestCase): | ||
"""Tests for the Referrer-Policy header.""" | ||
def test_referrer_policy(self): | ||
"""Test that we have a Referrer-Policy header.""" | ||
response = self.client.get("/") | ||
self.assertEqual(response["Referrer-Policy"], "no-referrer-when-downgrade") |
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters