-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from Harvard-University-iCommons/feature/djang…
…o1_10_monkeypatching_fix Feature/django1 10 monkeypatching fix
- Loading branch information
Showing
10 changed files
with
186 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,5 @@ dist | |
*.egg-info | ||
*.egg | ||
.idea | ||
.tox/ | ||
.eggs/ |
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 |
---|---|---|
@@ -1,15 +1,15 @@ | ||
#!/usr/bin/env python | ||
import os | ||
import sys | ||
|
||
import django | ||
from django.conf import settings | ||
|
||
|
||
def runtests(): | ||
settings.configure( | ||
# App-specific setttings here | ||
) | ||
# settings must be configured for this import to work | ||
from django.test.runner import DiscoverRunner | ||
DiscoverRunner(interactive=False, failfast=False).run_tests(['django_auth_lti']) | ||
|
||
if __name__ == '__main__': | ||
runtests() | ||
from django.test.utils import get_runner | ||
|
||
if __name__ == "__main__": | ||
os.environ['DJANGO_SETTINGS_MODULE'] = 'tests.test_settings' | ||
django.setup() | ||
TestRunner = get_runner(settings) | ||
test_runner = TestRunner() | ||
failures = test_runner.run_tests(["tests"]) | ||
sys.exit(bool(failures)) |
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
Empty file.
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,67 @@ | ||
import unittest | ||
import mock | ||
from mock import patch | ||
from django.test import RequestFactory | ||
from django.contrib.auth import models | ||
from django_auth_lti.middleware import LTIAuthMiddleware | ||
|
||
|
||
@patch('django_auth_lti.middleware.logger') | ||
class TestLTIAuthMiddleware(unittest.TestCase): | ||
longMessage = True | ||
|
||
def setUp(self): | ||
self.mw = LTIAuthMiddleware() | ||
|
||
def build_lti_launch_request(self, post_data): | ||
""" | ||
Utility method that builds a fake lti launch request with custom data. | ||
""" | ||
# Add message type to post data | ||
post_data.update(lti_message_type='basic-lti-launch-request') | ||
# Add resource_link_id to post data | ||
post_data.update(resource_link_id='d202fb112a14f27107149ed874bf630aa8e029a5') | ||
|
||
request = RequestFactory().post('/fake/lti/launch', post_data) | ||
request.user = mock.Mock(name='User', spec=models.User) | ||
request.session = {} | ||
return request | ||
|
||
@patch('django_auth_lti.middleware.auth') | ||
def test_roles_merged_with_custom_roles(self, mock_auth, mock_logger): | ||
""" | ||
Assert that 'roles' list in session contains merged set of roles when custom role key is | ||
defined and values have been passed in. | ||
""" | ||
request = self.build_lti_launch_request({ | ||
'roles': 'RoleOne,RoleTwo', | ||
'test_custom_role_key': 'My,Custom,Roles', | ||
}) | ||
with patch('django_auth_lti.middleware.settings', LTI_CUSTOM_ROLE_KEY='test_custom_role_key'): | ||
self.mw.process_request(request) | ||
self.assertEqual(request.LTI.get('roles'), ['RoleOne', 'RoleTwo', 'My', 'Custom', 'Roles']) | ||
|
||
@patch('django_auth_lti.middleware.auth') | ||
def test_roles_merge_with_empty_custom_roles(self, mock_auth, mock_logger): | ||
""" | ||
Assert that 'roles' list in session contains original set when custom role key is defined with empty data. | ||
""" | ||
request = self.build_lti_launch_request({ | ||
'roles': 'RoleOne,RoleTwo', | ||
'test_custom_role_key': '', | ||
}) | ||
with patch('django_auth_lti.middleware.settings', LTI_CUSTOM_ROLE_KEY='test_custom_role_key'): | ||
self.mw.process_request(request) | ||
self.assertEqual(request.LTI.get('roles'), ['RoleOne', 'RoleTwo']) | ||
|
||
@patch('django_auth_lti.middleware.auth') | ||
def test_roles_not_merged_with_no_role_key(self, mock_auth, mock_logger): | ||
""" | ||
Assert that 'roles' list in session contains original set when no custom role key is defined. | ||
""" | ||
request = self.build_lti_launch_request({ | ||
'roles': 'RoleOne,RoleTwo', | ||
'test_custom_role_key': 'My,Custom,Roles', | ||
}) | ||
self.mw.process_request(request) | ||
self.assertEqual(request.LTI.get('roles'), ['RoleOne', 'RoleTwo']) |
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,29 @@ | ||
import os | ||
|
||
# Build paths inside the project like this: os.path.join(BASE_DIR, ...) | ||
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | ||
|
||
SECRET_KEY = 'fake-key' | ||
|
||
INSTALLED_APPS = [ | ||
'django.contrib.auth', | ||
'django.contrib.contenttypes', | ||
'django.contrib.sessions', | ||
'django.contrib.messages', | ||
'django.contrib.staticfiles', | ||
'django_auth_lti', | ||
] | ||
|
||
TEMPLATES = [ | ||
{ | ||
'BACKEND': 'django.template.backends.django.DjangoTemplates', | ||
'APP_DIRS': True, | ||
}, | ||
] | ||
|
||
DATABASES = { | ||
'default': { | ||
'ENGINE': 'django.db.backends.sqlite3', | ||
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), | ||
} | ||
} |
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,36 @@ | ||
from unittest import TestCase | ||
from mock import MagicMock | ||
from django_auth_lti.verification import is_allowed | ||
from django.core.exceptions import ImproperlyConfigured, PermissionDenied | ||
|
||
class TestVerification(TestCase): | ||
|
||
def test_is_allowed_success(self): | ||
request = MagicMock(LTI={"roles": ["admin"]}) | ||
allowed_roles = ["admin", "student"] | ||
user_is_allowed = is_allowed(request, allowed_roles, False) | ||
self.assertTrue(user_is_allowed) | ||
|
||
def test_is_allowed_success_one_role(self): | ||
request = MagicMock(LTI={"roles": ["admin"]}) | ||
allowed_roles = "admin" | ||
user_is_allowed = is_allowed(request, allowed_roles, False) | ||
self.assertTrue(user_is_allowed) | ||
|
||
def test_is_allowed_failure(self): | ||
request = MagicMock(LTI={"roles":[]}) | ||
allowed_roles = ["admin", "student"] | ||
user_is_allowed = is_allowed(request, allowed_roles, False) | ||
self.assertFalse(user_is_allowed) | ||
|
||
def test_is_allowed_failure_one_role(self): | ||
request = MagicMock(LTI={"roles":[]}) | ||
allowed_roles = "admin" | ||
user_is_allowed = is_allowed(request, allowed_roles, False) | ||
self.assertFalse(user_is_allowed) | ||
|
||
def test_is_allowed_exception(self): | ||
request = MagicMock(LTI={"roles":["TF"]}) | ||
allowed_roles = ["admin", "student"] | ||
self.assertRaises(PermissionDenied, is_allowed, | ||
request, allowed_roles, True) |
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,10 @@ | ||
[tox] | ||
envlist = | ||
py27-django{18,19,110} | ||
[testenv] | ||
deps = | ||
py27: mock | ||
django18: Django >= 1.8, < 1.9 | ||
django19: Django >= 1.9, < 1.10 | ||
django110: Django >= 1.10, < 1.11 | ||
commands = python run_tests.py |