Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pydotorg/context_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def url_name(request):
try:
match = resolve(request.path)
except Resolver404:
return {}
return {'URL_NAMESPACE': None, 'URL_NAME': None}
else:
namespace, url_name_ = match.namespace, match.url_name
if namespace:
Expand Down
22 changes: 19 additions & 3 deletions pydotorg/tests/test_context_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ def test_url_name(self):
self.assertEqual({'URL_NAMESPACE': 'events', 'URL_NAME': 'events:calendar_list'}, context_processors.url_name(request))

request = self.factory.get('/getit-404/releases/3.3.3/not-an-actual-thing/')
self.assertEqual({}, context_processors.url_name(request))
self.assertEqual({'URL_NAMESPACE': None, 'URL_NAME': None}, context_processors.url_name(request))

request = self.factory.get('/getit-404/releases/3.3.3/\r\n/')
self.assertEqual({}, context_processors.url_name(request))
self.assertEqual({'URL_NAMESPACE': None, 'URL_NAME': None}, context_processors.url_name(request))

request = self.factory.get('/nothing/here/')
self.assertEqual({}, context_processors.url_name(request))
self.assertEqual({'URL_NAMESPACE': None, 'URL_NAME': None}, context_processors.url_name(request))

def test_blog_url(self):
request = self.factory.get('/about/')
Expand Down Expand Up @@ -114,3 +114,19 @@ def test_user_nav_bar_links_for_anonymous_user(self):
request.user = AnonymousUser()

self.assertEqual({"USER_NAV_BAR": {}}, context_processors.user_nav_bar_links(request))

def test_url_name_always_returns_keys(self):
"""Ensure URL_NAME and URL_NAMESPACE are always present in context, even for 404s.
Because it makes sentry unhappy: https://python-software-foundation.sentry.io/issues/6931306293/
"""
Comment on lines +119 to +121
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid docstrings so that test runners print out the method name, not the docstring:

Suggested change
"""Ensure URL_NAME and URL_NAMESPACE are always present in context, even for 404s.
Because it makes sentry unhappy: https://python-software-foundation.sentry.io/issues/6931306293/
"""
# Ensure URL_NAME and URL_NAMESPACE are always present in context, even for 404s,
# otherwise it makes sentry unhappy: https://python-software-foundation.sentry.io/issues/6931306293/

# test with a 404 path
request = self.factory.get('/this-does-not-exist/')
result = context_processors.url_name(request)

# kyes should always be present
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# kyes should always be present
# keys should always be present

self.assertIn('URL_NAME', result)
self.assertIn('URL_NAMESPACE', result)

# values should be None for unresolved URLs
self.assertIsNone(result['URL_NAME'])
self.assertIsNone(result['URL_NAMESPACE'])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO testing for keys and values could be just one assertion.