Add a unit test for new template tag

This commit is contained in:
Neil Martinsen-Burrell 2023-05-17 15:02:59 -05:00
parent 400232a10f
commit 135192fd9f
No known key found for this signature in database
GPG key ID: 6A3C818CC10D0184
3 changed files with 37 additions and 2 deletions

View file

@ -28,7 +28,7 @@ services:
# Tell Django where it is being hosted
- DJANGO_BASE_URL=http://localhost:8080
# Public site URL link
- GETGOV_PUBLIC_SITE_URL="https://beta.get.gov"
- GETGOV_PUBLIC_SITE_URL=https://beta.get.gov
# Set a username for accessing the registry
- REGISTRY_CL_ID=nothing
# Set a password for accessing the registry

View file

@ -29,5 +29,9 @@ def public_site_url(url_path):
variable.
"""
base_url = settings.GETGOV_PUBLIC_SITE_URL
public_url = urljoin(base_url, url_path)
# join the two halves with a single slash
public_url ="/".join([
base_url.rstrip("/"),
url_path.lstrip("/")
])
return public_url

View file

@ -0,0 +1,31 @@
"""Test template tags."""
from django.conf import settings
from django.test import TestCase
from django.template import Context, Template
class TestTemplateTags(TestCase):
def _render_template(self, string, context=None):
"""Helper method to render a template given as a string.
Originally from https://stackoverflow.com/a/1690879
"""
context = context or {}
context = Context(context)
return Template(string).render(context)
def test_public_site_url(self):
result = self._render_template(
"{% load url_helpers %}{% public_site_url 'directory/page' %}"
)
self.assertTrue(result.startswith(settings.GETGOV_PUBLIC_SITE_URL))
self.assertTrue(result.endswith("/directory/page"))
def test_public_site_url_leading_slash(self):
result = self._render_template(
"{% load url_helpers %}{% public_site_url '/directory/page' %}"
)
self.assertTrue(result.startswith(settings.GETGOV_PUBLIC_SITE_URL))
# slash-slash host slash directory slash page
self.assertEqual(result.count("/"), 4)