-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
47 changed files
with
1,076 additions
and
232 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from django import template | ||
from django.urls import reverse | ||
from core.logic import reverse_with_next | ||
from urllib.parse import quote | ||
|
||
|
||
register = template.Library() | ||
|
||
@register.simple_tag(takes_context=True) | ||
def url_with_next(context, url_name, next_url_name='', *args, **kwargs): | ||
""" | ||
A template tag to use instead of 'url' when you want | ||
the reversed URL to include the same 'next' parameter that | ||
already exists in the GET or POST data of the request, | ||
or you want to introduce a new next url by Django URL name. | ||
""" | ||
if next_url_name: | ||
next_url = reverse(next_url_name) | ||
else: | ||
next_url = '' | ||
request = context.get('request') | ||
return reverse_with_next( | ||
url_name, | ||
request, | ||
next_url=next_url, | ||
*args, | ||
**kwargs, | ||
) | ||
|
||
|
||
@register.simple_tag(takes_context=True) | ||
def url_with_return(context, url_name, *args, **kwargs): | ||
""" | ||
A template tag to use instead of 'url' when you want | ||
the reversed URL to include a new 'next' parameter that | ||
contains the full path of the current request. | ||
""" | ||
request = context.get('request') | ||
next_url = quote(request.get_full_path()) | ||
return reverse_with_next( | ||
url_name, | ||
request, | ||
next_url=next_url, | ||
*args, | ||
**kwargs, | ||
) |
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,18 +1,37 @@ | ||
__copyright__ = "Copyright 2024 Birkbeck, University of London" | ||
__author__ = "Open Library of Humanities" | ||
__license__ = "AGPL v3" | ||
__maintainer__ = "Open Library of Humanities" | ||
|
||
import uuid | ||
from mock import patch | ||
|
||
from django.test import TestCase | ||
|
||
from core import logic | ||
from core.models import SettingGroup | ||
from utils.testing import helpers | ||
|
||
class TestLogic(TestCase): | ||
def setUp(self): | ||
self.press = helpers.create_press() | ||
self.press.save() | ||
self.journal_one, self.journal_two = helpers.create_journals() | ||
self.request = helpers.Request() | ||
self.request.press = self.press | ||
self.request.journal = self.journal_one | ||
self.request.site_type = self.journal_one | ||
|
||
@classmethod | ||
def setUpTestData(cls): | ||
cls.press = helpers.create_press() | ||
cls.press.save() | ||
cls.journal_one, cls.journal_two = helpers.create_journals() | ||
cls.request = helpers.Request() | ||
cls.request.press = cls.press | ||
cls.request.journal = cls.journal_one | ||
cls.request.site_type = cls.journal_one | ||
cls.request.GET = {} | ||
cls.request.POST = {} | ||
cls.inactive_user = helpers.create_user('[email protected]') | ||
cls.inactive_user.is_active = False | ||
cls.inactive_user.confirmation_code = '8bd3cdc9-1c3c-4ec9-99bc-9ea0b86a3c55' | ||
cls.inactive_user.clean() | ||
cls.inactive_user.save() | ||
|
||
# The result of passing a URL through the |urlencode template filter | ||
cls.next_url_encoded = '/target/page/%3Fq%3Da' | ||
|
||
def test_render_nested_settings(self): | ||
expected_rendered_setting = "<p>For help with Janeway, contact <a href=\"mailto:--No support email set--\">--No support email set--</a>.</p>" | ||
|
@@ -23,3 +42,44 @@ def test_render_nested_settings(self): | |
nested_settings=[('support_email','general')], | ||
) | ||
self.assertEqual(expected_rendered_setting, rendered_setting) | ||
|
||
@patch('core.logic.reverse') | ||
def test_reverse_with_next_in_get_request(self, mock_reverse): | ||
mock_reverse.return_value = '/my/path/?my=params' | ||
self.request.GET = {'next': self.next_url_encoded} | ||
reversed_url = logic.reverse_with_next('/test/', self.request) | ||
self.assertIn(self.next_url_encoded, reversed_url) | ||
|
||
@patch('core.logic.reverse') | ||
def test_reverse_with_next_in_post_request(self, mock_reverse): | ||
mock_reverse.return_value = '/my/path/?my=params' | ||
self.request.POST = {'next': self.next_url_encoded} | ||
reversed_url = logic.reverse_with_next('/test/', self.request) | ||
self.assertIn(self.next_url_encoded, reversed_url) | ||
|
||
@patch('core.logic.reverse') | ||
def test_reverse_with_next_in_kwarg(self, mock_reverse): | ||
mock_reverse.return_value = '/my/path/?my=params' | ||
reversed_url = logic.reverse_with_next( | ||
'/test/', | ||
self.request, | ||
next_url=self.next_url_encoded, | ||
) | ||
self.assertIn(self.next_url_encoded, reversed_url) | ||
|
||
@patch('core.logic.reverse') | ||
def test_reverse_with_next_no_next(self, mock_reverse): | ||
mock_reverse.return_value = '/my/url/?my=params' | ||
reversed_url = logic.reverse_with_next('/test/', self.request) | ||
self.assertEqual(mock_reverse.return_value, reversed_url) | ||
|
||
def test_get_confirm_account_url(self): | ||
url = logic.get_confirm_account_url( | ||
self.request, | ||
self.inactive_user, | ||
next_url=self.next_url_encoded, | ||
) | ||
self.assertIn( | ||
f'/register/step/2/8bd3cdc9-1c3c-4ec9-99bc-9ea0b86a3c55/?next={ self.next_url_encoded }', | ||
url, | ||
) |
Oops, something went wrong.