Skip to content

Commit

Permalink
Merge pull request #9 from open-craft/deps-update
Browse files Browse the repository at this point in the history
Upgrade to Django 1.11
  • Loading branch information
itsjeyd authored Mar 21, 2019
2 parents 162b722 + b110b12 commit 1b2aaff
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 41 deletions.
3 changes: 2 additions & 1 deletion django_lti_tool_provider/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from abc import ABCMeta, abstractmethod
import logging
from django.core.urlresolvers import resolve

from django.urls import resolve
from six import add_metaclass


Expand Down
2 changes: 1 addition & 1 deletion django_lti_tool_provider/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Migration(migrations.Migration):
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('edx_lti_parameters', jsonfield.fields.JSONField(default={})),
('custom_key', models.CharField(default=b'', max_length=190)),
('user', models.ForeignKey(to=settings.AUTH_USER_MODEL)),
('user', models.ForeignKey(to=settings.AUTH_USER_MODEL, on_delete=models.CASCADE)),
],
),
migrations.AlterUniqueTogether(
Expand Down
7 changes: 5 additions & 2 deletions django_lti_tool_provider/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ class WrongUserError(Exception):


class LtiUserData(models.Model):
user = models.ForeignKey(User)
user = models.ForeignKey(
User,
on_delete=models.CASCADE,
)
edx_lti_parameters = JSONField(default={})
custom_key = models.CharField(max_length=190, null=False, default='')

Expand Down Expand Up @@ -108,4 +111,4 @@ def store_lti_parameters(cls, user, authentication_manager, lti_params):
def __unicode__(self):
return u"{classname} for {user} and (vary_key: {custom_key})".format(
classname=self.__class__.__name__, user=self.user, custom_key=self.custom_key
)
)
42 changes: 19 additions & 23 deletions django_lti_tool_provider/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def test_given_incorrect_payload_throws_bad_request(self):
def test_given_correct_requests_sets_session_variable(self):
response = self.send_lti_request(self.get_correct_lti_payload())

self._verify_redirected_to(response, self._url_base + self.DEFAULT_REDIRECT)
self._verify_redirected_to(response, self.DEFAULT_REDIRECT)

self._verify_session_lti_contents(self.client.session, self._data)

Expand All @@ -123,8 +123,8 @@ class AuthenticatedLtiRequestTests(LtiRequestsTestBase):
def _authentication_hook(self, request, user_id=None, username=None, email=None, **kwargs):
user = User.objects.create_user(username or user_id, password='1234', email=email)
user.save()
user = authenticate(username=user.username, password='1234')
login(request, user)
authenticated_user = authenticate(request, username=user.username, password='1234')
login(request, authenticated_user)
return user

def setUp(self):
Expand All @@ -150,7 +150,7 @@ def test_no_session_correct_payload_processes_lti_request(self, patched_send_lti
# Should have been created.
user = User.objects.all()[0]
self._verify_lti_created(user, self._data)
self._verify_redirected_to(response, self._url_base + self.DEFAULT_REDIRECT)
self._verify_redirected_to(response, self.DEFAULT_REDIRECT)
self._verify_lti_updated_signal_is_sent(patched_send_lti_received, user)

def test_given_session_and_lti_uses_lti(self, patched_send_lti_received):
Expand All @@ -166,7 +166,7 @@ def test_given_session_and_lti_uses_lti(self, patched_send_lti_received):
# Should have been created.
user = User.objects.all()[0]
self._verify_lti_created(user, self._data)
self._verify_redirected_to(response, self._url_base + self.DEFAULT_REDIRECT)
self._verify_redirected_to(response, self.DEFAULT_REDIRECT)
self._verify_lti_updated_signal_is_sent(patched_send_lti_received, user)

def test_force_login_change(self, patched_send_lti_received):
Expand All @@ -189,18 +189,18 @@ def test_force_login_change(self, patched_send_lti_received):
request = self.send_lti_request(payload, client=RequestFactory())
request.session = engine.SessionStore()
request.user = None
user = authenticate(username=new_user.username, password='1234')
self.assertTrue(user)
login(request, user)
authenticated_user = authenticate(request, username=new_user.username, password='1234')
self.assertTrue(authenticated_user)
login(request, authenticated_user)
LTIView.as_view()(request)
self.assertEqual(request.user, user)
self.assertEqual(user, new_user)
self.assertEqual(request.user, authenticated_user)
self.assertEqual(authenticated_user, new_user)

self.assertEqual(LtiUserData.objects.all().count(), 1)

@ddt.ddt
class AuthenticationManagerIntegrationTests(LtiRequestsTestBase):
TEST_URLS = ("/some_url", False), ("/some_other_url", False), ("http://qwe.asd.zxc.com", True)
TEST_URLS = "/some_url", "/some_other_url", "http://qwe.asd.zxc.com"

def setUp(self):
super(AuthenticationManagerIntegrationTests, self).setUp()
Expand All @@ -215,8 +215,8 @@ def _authenticate_user(self, request, user_id=None, username=None, email=None, *
password = "test_password"

user = User.objects.create_user(username=username, email=email, password=password)
authenticated = authenticate(username=username, password=password)
login(request, authenticated)
authenticated_user = authenticate(request, username=username, password=password)
login(request, authenticated_user)

self.addCleanup(lambda: user.delete())

Expand All @@ -226,7 +226,7 @@ def test_authentication_hook_executed_if_not_authenticated(self):
args, user_data = self.hook_manager.authentication_hook.call_args
request = args[0]
self.assertEqual(request.body, payload)
self.assertFalse(request.user.is_authenticated())
self.assertFalse(request.user.is_authenticated)
expected_user_data = {
'username': self._data['lis_person_sourcedid'],
'email': self._data['lis_person_contact_email_primary'],
Expand All @@ -242,7 +242,7 @@ def test_authentication_hook_passes_optional_lti_data(self):
args, user_data = self.hook_manager.authentication_hook.call_args
request = args[0]
self.assertEqual(request.body, payload)
self.assertFalse(request.user.is_authenticated())
self.assertFalse(request.user.is_authenticated)
expected_user_data = {
'username': self._data['lis_person_sourcedid'],
'email': self._data['lis_person_contact_email_primary'],
Expand All @@ -255,12 +255,10 @@ def test_authentication_hook_passes_optional_lti_data(self):
self.assertEqual(user_data, expected_user_data)

@ddt.data(*TEST_URLS)
@ddt.unpack
def test_anonymous_lti_is_processed_if_hook_does_not_authenticate_user(self, url, absolute):
self.hook_manager.anonymous_redirect_to.return_value = url
def test_anonymous_lti_is_processed_if_hook_does_not_authenticate_user(self, expected_url):
self.hook_manager.anonymous_redirect_to.return_value = expected_url
response = self.send_lti_request(self.get_correct_lti_payload())

expected_url = url if absolute else "{base}{url}".format(base=self._url_base, url=url)
self._verify_redirected_to(response, expected_url)

self._verify_session_lti_contents(self.client.session, self._data)
Expand All @@ -271,13 +269,11 @@ def test_anonymous_lti_is_processed_if_hook_does_not_authenticate_user(self, url
self._verify_lti_data(lti_data, self._data)

@ddt.data(*TEST_URLS)
@ddt.unpack
def test_authenticated_lti_is_processed_if_hook_authenticates_user(self, url, absolute):
def test_authenticated_lti_is_processed_if_hook_authenticates_user(self, expected_url):
self.hook_manager.authentication_hook.side_effect = self._authenticate_user
self.hook_manager.authenticated_redirect_to.return_value = url
self.hook_manager.authenticated_redirect_to.return_value = expected_url
response = self.send_lti_request(self.get_correct_lti_payload())

expected_url = url if absolute else "{base}{url}".format(base=self._url_base, url=url)
self._verify_redirected_to(response, expected_url)

# verifying correct parameters were passed to auth manager hook
Expand Down
8 changes: 5 additions & 3 deletions django_lti_tool_provider/tests/urls.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from django.conf.urls import patterns, url
from django.conf.urls import url
from django.contrib.auth.views import LoginView

from django_lti_tool_provider import views as lti_views


urlpatterns = [
url(r'', lti_views.LTIView.as_view(), name='home'),
url('^accounts/login/$', 'django.contrib.auth.views.login'),
url('^accounts/login/$', LoginView.as_view()),
url(r'^lti$', lti_views.LTIView.as_view(), name='lti')
]
]
3 changes: 3 additions & 0 deletions django_lti_tool_provider/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

from django_lti_tool_provider import views as lti_views


app_name = 'django_lti_tool_provider'

urlpatterns = [
url(r'', lti_views.LTIView.as_view(), name='lti')
]
8 changes: 5 additions & 3 deletions django_lti_tool_provider/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def post(self, request, *args, **kwargs):
return self.process_request(request)

def process_request(self, request):
if request.user.is_authenticated():
if request.user.is_authenticated:
try:
lti_parameters = self._get_lti_parameters_from_request(request)
if not self._right_user(request.user, lti_parameters):
Expand All @@ -55,7 +55,7 @@ def process_request(self, request):
except (oauth2.Error, AttributeError):
# Not a new visit, or better to keep existing auth.
pass
if not request.user.is_authenticated():
if not request.user.is_authenticated:
try:
lti_parameters = self._get_lti_parameters_from_request(request)
except oauth2.Error as e:
Expand All @@ -78,9 +78,11 @@ def process_request(self, request):

self.authentication_manager.authentication_hook(request, **lti_data)

if request.user.is_authenticated():
if request.user.is_authenticated:
_logger.info('Processing authenticated LTI request')
return self.process_authenticated_lti(request)
else:
_logger.info('Processing anonymous LTI request')
return self.process_anonymous_lti(request)

@classmethod
Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Django==1.8
Django==1.11.20
jsonfield
oauth2==1.5.211
PyYAML
Expand All @@ -10,4 +10,4 @@ six
# test requirements
mock
ddt
prospector
prospector
17 changes: 12 additions & 5 deletions run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
'django.contrib.sessions',
'django_lti_tool_provider'
],
MIDDLEWARE_CLASSES=[
MIDDLEWARE=[
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
Expand All @@ -31,9 +31,16 @@
],
ROOT_URLCONF='django_lti_tool_provider.tests.urls',
REDIRECT_AFTER_LTI='home',
TEMPLATE_CONTEXT_PROCESSORS=(
"django.contrib.auth.context_processors.auth"
),
TEMPLATES=[
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"OPTIONS": {
"context_processors": [
"django.contrib.auth.context_processors.auth"
]
}
}
],
USE_TZ=True,
SOUTH_TESTS_MIGRATE=True,
LTI_CLIENT_KEY='lti_client_key',
Expand All @@ -47,4 +54,4 @@
if not paths:
paths = ["django_lti_tool_provider/tests"]
options = [arg for arg in args if arg not in paths]
execute_from_command_line([sys.argv[0], "test"] + paths + options)
execute_from_command_line([sys.argv[0], "test"] + paths + options)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def package_data(pkg, root_list):
# Main ##############################################################
setup(
name='django_lti_tool_provider',
version='0.1.3',
version='0.1.6',
license="GNU AFFERO GENERAL PUBLIC LICENSE",
description='IMS LTI Tool Provider Django Applocation',
long_description=README,
Expand Down

0 comments on commit 1b2aaff

Please sign in to comment.