Skip to content

Commit

Permalink
Fixed tests when 'PARENT_HOST' setting is set
Browse files Browse the repository at this point in the history
A number of used the hardcoded host 'djangoproject.localhost:8000'.
Setting the 'PARENT_HOST' setting to a different value, e.g. if
running Django on another machine, would result in tests failing. This
commit changes the tests to use the 'PARENT_HOST' setting in URLs
where relevant.
  • Loading branch information
pbratkowski committed Dec 16, 2024
1 parent 9470ac4 commit 09d7622
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ The secrets file may contain the following fields:
* ``trac_db_password``: must match the value of ``POSTGRES_PASSWORD`` for the ``tracdb`` service.
* ``allowed_hosts``: an array that will be appended to Django's ``ALLOWED_HOSTS`` setting.
* ``internal_ips``: an array of IPs assigned to the ``INTERNAL_IPS`` Django Debug Toolbar setting. Defaults to ``["127.0.0.1"]``.
* ``parent_host``: the django-hosts ``PARENT_HOST`` setting. Useful if you run docker on a machine other than ``localhost``, but setting it breaks some tests. Defaults to ``djangoproject.localhost:8000``.
* ``parent_host``: the django-hosts ``PARENT_HOST`` setting. Useful if you run docker on a machine other than ``localhost``. Defaults to ``djangoproject.localhost:8000``.

Pre-commit checks
-----------------
Expand Down
10 changes: 5 additions & 5 deletions docs/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def test_team_url(self):
def test_internals_team(self):
response = self.client.get(
"/en/dev/internals/team/",
headers={"host": "docs.djangoproject.localhost:8000"},
headers={"host": f"docs.{settings.PARENT_HOST}"},
)
self.assertRedirects(
response,
Expand All @@ -191,7 +191,7 @@ def tearDownClass(cls):

def test_empty_get(self):
response = self.client.get(
"/en/dev/search/", headers={"host": "docs.djangoproject.localhost:8000"}
"/en/dev/search/", headers={"host": f"docs.{settings.PARENT_HOST}"}
)
self.assertEqual(response.status_code, 200)

Expand Down Expand Up @@ -376,12 +376,12 @@ def tearDownClass(cls):

def test_sitemap_index(self):
response = self.client.get(
"/sitemap.xml", headers={"host": "docs.djangoproject.localhost:8000"}
"/sitemap.xml", headers={"host": f"docs.{settings.PARENT_HOST}"}
)
self.assertContains(response, "<sitemap>", count=2)
self.assertContains(
response,
"<loc>http://docs.djangoproject.localhost:8000/sitemap-en.xml</loc>",
f"<loc>http://docs.{settings.PARENT_HOST}/sitemap-en.xml</loc>",
)

def test_sitemap(self):
Expand All @@ -395,7 +395,7 @@ def test_sitemap(self):

def test_sitemap_404(self):
response = self.client.get(
"/sitemap-xx.xml", headers={"host": "docs.djangoproject.localhost:8000"}
"/sitemap-xx.xml", headers={"host": f"docs.{settings.PARENT_HOST}"}
)
self.assertEqual(response.status_code, 404)
self.assertEqual(
Expand Down
3 changes: 2 additions & 1 deletion members/test_admin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from datetime import date, timedelta

from django.conf import settings
from django.contrib import admin
from django.test import TestCase

Expand Down Expand Up @@ -43,7 +44,7 @@ def test_membership_expires(self):

def test_renewal_link(self):
expected_str = (
'<a href="http://www.djangoproject.localhost:8000/foundation/'
f'<a href="http://www.{settings.PARENT_HOST}/foundation/'
"corporate-membership/renew/"
)
modeladmin = CorporateMemberAdmin(CorporateMember, admin.site)
Expand Down
2 changes: 1 addition & 1 deletion members/test_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def test_mail(self):
msg.body,
)
self.assertIn(
"http://www.djangoproject.localhost:8000/foundation/"
f"http://www.{settings.PARENT_HOST}/foundation/"
"corporate-membership/renew/",
msg.body,
)
Expand Down
9 changes: 5 additions & 4 deletions releases/tests.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import datetime

from django.conf import settings
from django.contrib.redirects.models import Redirect
from django.test import TestCase, override_settings
from django.urls import reverse
Expand Down Expand Up @@ -43,12 +44,12 @@ def test_release_notes(self):
self.assertIsInstance(output, SafeString)
self.assertEqual(
output,
'<a href="http://docs.djangoproject.localhost:8000/en/1.8/releases/1.8/">'
f'<a href="http://docs.{settings.PARENT_HOST}/en/1.8/releases/1.8/">'
"Online documentation</a>",
)
self.assertEqual(
release_notes("1.8", show_version=True),
'<a href="http://docs.djangoproject.localhost:8000/en/1.8/releases/1.8/">'
f'<a href="http://docs.{settings.PARENT_HOST}/en/1.8/releases/1.8/">'
"1.8 release notes</a>",
)

Expand All @@ -57,12 +58,12 @@ def test_release_notes_1_10(self):
self.assertIsInstance(output, SafeString)
self.assertEqual(
output,
'<a href="http://docs.djangoproject.localhost:8000/en/1.10/releases/1.10/">'
f'<a href="http://docs.{settings.PARENT_HOST}/en/1.10/releases/1.10/">'
"Online documentation</a>",
)
self.assertEqual(
release_notes("1.10", show_version=True),
'<a href="http://docs.djangoproject.localhost:8000/en/1.10/releases/1.10/">'
f'<a href="http://docs.{settings.PARENT_HOST}/en/1.10/releases/1.10/">'
"1.10 release notes</a>",
)

Expand Down

0 comments on commit 09d7622

Please sign in to comment.