Skip to content

Commit

Permalink
Fixed django tests to work with Django 1.11.
Browse files Browse the repository at this point in the history
  • Loading branch information
ebpetway committed Apr 12, 2017
1 parent 5354439 commit c2e593a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
Empty file modified cdu/manage.py
100644 → 100755
Empty file.
1 change: 1 addition & 0 deletions warrant/django/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 34 additions & 6 deletions warrant/django/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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')

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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')

Expand Down Expand Up @@ -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'
Expand Down

0 comments on commit c2e593a

Please sign in to comment.