diff --git a/cdu/manage.py b/cdu/manage.py old mode 100644 new mode 100755 diff --git a/warrant/django/__init__.py b/warrant/django/__init__.py index eba5a325..fe27dd77 100644 --- a/warrant/django/__init__.py +++ b/warrant/django/__init__.py @@ -6,6 +6,7 @@ def add_user_tokens(sender, user, **kwargs): """ Add Cognito tokens to the session upon login """ + print 'ADD USER TOKENS SIGNAL' if user.backend == 'warrant.django.backend.CognitoBackend': request = kwargs['request'] request.session['ACCESS_TOKEN'] = user.access_token diff --git a/warrant/django/tests.py b/warrant/django/tests.py index 2f1290f0..bd2d4ef3 100644 --- a/warrant/django/tests.py +++ b/warrant/django/tests.py @@ -3,11 +3,15 @@ from mock import patch, MagicMock from botocore.exceptions import ClientError from middleware import APIKeyMiddleware +from importlib import import_module +from unittest import skipIf +from django import VERSION as DJANGO_VERSION from django.contrib.auth.models import AnonymousUser, User from django.conf import settings -from django.contrib.auth import authenticate, get_user_model, signals +from django.contrib.auth import get_user_model, signals, authenticate as django_authenticate from django.contrib.sessions.middleware import SessionMiddleware +from django.http import HttpRequest from django.test import override_settings, TestCase, TransactionTestCase from django.test.client import RequestFactory from django.utils.six import iteritems @@ -85,13 +89,38 @@ def get_user(cls, *args, **kwargs): metadata=user_metadata) +def create_request(): + request = HttpRequest() + engine = import_module(settings.SESSION_ENGINE) + session = engine.SessionStore() + session.save() + request.session = session + + return request + + +def authenticate(username, password): + if DJANGO_VERSION[1] > 10: + request = create_request() + return django_authenticate(request=request, username=username, password=password) + else: + return django_authenticate(username=username, password=password) + + +def login(client, username, password): + if DJANGO_VERSION[1] > 10: + request = create_request() + return client.login(request=request, username=username, password=password) + else: + return client.login(username=username, password=password) + + class AuthTests(TransactionTestCase): @patch.object(Cognito, 'authenticate') @patch.object(Cognito, 'get_user') def test_user_authentication(self, mock_get_user, mock_authenticate): Cognito.authenticate = set_tokens Cognito.get_user = get_user - user = authenticate(username='testuser', password='password') @@ -133,8 +162,7 @@ def test_user_authentication_wrong_username(self, mock_authenticate): def test_client_login(self, mock_get_user, mock_authenticate): Cognito.authenticate = set_tokens Cognito.get_user = get_user - - user = self.client.login(username='testuser', + user = login(self.client, username='testuser', password='password') self.assertTrue(user) @@ -165,7 +193,6 @@ def test_new_user_created(self, mock_get_user, mock_authenticate): User = get_user_model() self.assertEqual(User.objects.count(), 0) - user = authenticate(username='testuser', password='password') @@ -222,7 +249,8 @@ def test_user_not_found_disabled_create_unknown_user(self, mock_get_user, mock_a self.assertIsNone(user) - def test_add_user_tokens(self): + @skipIf(DJANGO_VERSION[1] > 10, "Signal not used if Django>1.10") + def test_add_user_tokens_signal(self): User = get_user_model() user = User.objects.create(username=settings.COGNITO_TEST_USERNAME) user.access_token = 'access_token_value'