Skip to content

Commit

Permalink
feat: Support Django 4
Browse files Browse the repository at this point in the history
No functional changes are needed to upgrade existing DGPF projects,
as long as they don't otherwise require Django-4 related changes.
  • Loading branch information
NickolausDS committed Oct 17, 2022
1 parent cab912f commit 269bb7c
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 98 deletions.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
django<4.0.0
django<5
globus_sdk<4.0.0
social-auth-app-django<6.0.0>=3.0.0
python-jose[cryptography]>=3.0.1
Expand Down
51 changes: 0 additions & 51 deletions tests/mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@
import pytz
import pathlib
from django.contrib.auth.models import User
from django.urls import path, include
from social_django.models import UserSocialAuth
from globus_portal_framework.urls import register_custom_index
import globus_sdk

from globus_portal_framework.views import logout
import json

mocks_path = pathlib.Path(__file__).parent / 'data'
Expand Down Expand Up @@ -98,51 +95,3 @@ def mock_user(username, resource_servers):
user.save()
soc_auth.save()
return user


# def get_logged_in_client(username, tokens):
# c = Client()
# user = mock_user(username, tokens)
# # Password is set in mocks, and is always 'globusrocks' for this func
# c.login(username=username, password='globusrocks')
# return c

#
# def globus_client_is_loaded_with_authorizer(client):
# return isinstance(client.kwargs.get('authorizer'),
# globus_sdk.AccessTokenAuthorizer)


def rebuild_index_urlpatterns(old_urlpatterns, indices):
"""
This fixes pre-complied paths not matching new test paths. Since paths
are compiled at import time, if you override settings with new
SEARCH_INDEXES, your new search indexes won't have urls that match due to
the regexes already being compiled. The problem stems from the UrlConverter
containing explicit names of the SEARCH_INDEXES which don't handle change
well. Use this function to rebuild the names to pick up on your test index
names.
:param old_urlpatterns: patterns you want to rebuild
:param indices: The list of new search indices you're using
Example: ['mytestindex', 'myothertestindex']
:return: urlpatterns
"""
urlpatterns = [
path('logout/', logout, name='logout'),
path('', include('social_django.urls', namespace='social')),
# FIXME Remove after merging #55 python-social-auth-upgrade
path('', include('django.contrib.auth.urls'))
]

register_custom_index('custom_index', indices)

for url in old_urlpatterns:
if '<index:index>' in str(url.pattern):
new_pattern = str(url.pattern).split('/')
new_pattern[0] = '<custom_index:index>'
new_pattern = '/'.join(new_pattern)
urlpatterns.append(path(new_pattern, url.callback, name=url.name))
else:
urlpatterns.append(url)

return urlpatterns
53 changes: 7 additions & 46 deletions tests/test_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,12 @@

from django.urls import reverse, path
from django.http import HttpResponse
import tests

from tests.mocks import rebuild_index_urlpatterns

from globus_portal_framework.urls import (search_urlpatterns,
register_custom_index,
urlpatterns as dgpf_urlpatterns)

urlpatterns = []


@pytest.fixture
@pytest.mark.urls('tests.test_urls')
def register_patterns(settings):
SEARCH_INDEXES = {
'myindex': {
# Randomly generated and not real
'uuid': '1e0be00f-8156-499e-980d-f7fb26157c02'
},
'my_custom_index': {
# Randomly generated and not real
'uuid': '1e0be00f-8156-499e-980d-f7fb26157c02'
}
}
settings.SEARCH_INDEXES = SEARCH_INDEXES
register_custom_index('my_custom_index', ['my_custom_index'])

urlpatterns = rebuild_index_urlpatterns(
search_urlpatterns + dgpf_urlpatterns,
list(SEARCH_INDEXES.keys()))
custom_search = mock.Mock(
return_value=HttpResponse('custom_search_view'))
custom_path = path('<my_custom_index:index>/', custom_search,
name='search')
urlpatterns.insert(0, custom_path)
setattr(tests.test_urls, 'urlpatterns', urlpatterns)
from globus_portal_framework.urls import register_custom_index

urlpatterns = [
path('<index>/foo/bar', lambda r, index: HttpResponse('hello foo'), name='foo')
]

def test_register_non_existent_index_raises_error(settings):
settings.SEARCH_INDEXES = {'myindex': {
Expand All @@ -52,15 +21,7 @@ def test_register_non_existent_index_raises_error(settings):


@pytest.mark.urls('tests.test_urls')
def test_custom_search_view(client, register_patterns):
r = client.get(reverse('search', args=['my_custom_index']))
assert r.status_code == 200
assert r.content == b'custom_search_view'


@pytest.mark.urls('tests.test_urls')
def test_regular_search_view_not_affected(client, register_patterns,
mock_data_search):
r = client.get(reverse('search', args=['myindex']))
def test_custom_index_converter_view(client):
r = client.get(reverse('foo', args=['my_custom_index']))
assert r.status_code == 200
assert b'custom_search_view' not in r.content
assert r.content == b'hello foo'

0 comments on commit 269bb7c

Please sign in to comment.