Updated tests to new user model

This commit is contained in:
Tsa6 2017-11-13 16:13:25 -05:00
parent 926ecbceb3
commit 20d3ac7aec
No known key found for this signature in database
GPG Key ID: D197B1197B2D4D68

View File

@ -1,7 +1,8 @@
from django.test import TestCase,Client,override_settings from django.test import TestCase,Client,override_settings
import responses import responses
from LandingPage.models import User from LandingPage.models import ExternalUser
from urllib import parse from urllib import parse
from django.contrib.auth import get_user_model
@override_settings( @override_settings(
AUTH_TOKEN_ENDPOINT='http://icynet.test/api/', AUTH_TOKEN_ENDPOINT='http://icynet.test/api/',
@ -15,7 +16,7 @@ class TestLogin(TestCase):
# Set up responses to control network flow # Set up responses to control network flow
with responses.RequestsMock() as rm: with responses.RequestsMock() as rm:
rm.add(responses.POST,'http://icynet.test/api/token',json={'access_token':'1accesstoken1'}) rm.add(responses.POST,'http://icynet.test/api/token',json={'access_token':'1accesstoken1'})
rm.add(responses.GET,'http://icynet.test/api/user',json={'uuid':'935a41b5-b38d-42c3-96ef-653402fc44ca','email':'johnsmith@gmail.com','display_name':'Mr. Smith'}) rm.add(responses.GET,'http://icynet.test/api/user',json={'uuid':'935a41b5-b38d-42c3-96ef-653402fc44ca','email':'johnsmith@gmail.com','display_name':'Mr. Smith','username':'mrsmith'})
# Make initial request to redirect endpoint # Make initial request to redirect endpoint
client = Client() client = Client()
@ -26,24 +27,23 @@ class TestLogin(TestCase):
self.assertEqual(query['client_id'][0],'clid') self.assertEqual(query['client_id'][0],'clid')
self.assertEqual(query['response_type'][0],'code') self.assertEqual(query['response_type'][0],'code')
self.assertEqual(query['redirect_uri'][0],'http://redirect.test') self.assertEqual(query['redirect_uri'][0],'http://redirect.test')
self.assertEqual(query['scope'][0],'email') self.assertSetEqual(set(query['scope'][0].split()),set(['email','privilege']))
# Make connection to the real endpoint # Make connection to the real endpoint
resp = client.get('/login/redirect?state=%s&code=%s'%(state, 'code')) resp = client.get('/login/redirect?state=%s&code=%s'%(state, 'code'))
self.assertEqual(resp.status_code, 302) self.assertEqual(resp.status_code, 302)
# Check that the database is all good # Check that the database is all good
users = User.objects.all() users = ExternalUser.objects.all()
self.assertEqual(len(users), 1) self.assertEqual(len(users), 1)
user = users[0] user = users[0]
self.assertEqual(user.user_id,'935a41b5-b38d-42c3-96ef-653402fc44ca') self.assertEqual(user.icy_id,'935a41b5-b38d-42c3-96ef-653402fc44ca')
self.assertEqual(user.email,'johnsmith@gmail.com') self.assertEqual(user.user.email,'johnsmith@gmail.com')
self.assertEqual(user.user.username,'mrsmith')
self.assertEqual(user.display_name, 'Mr. Smith') self.assertEqual(user.display_name, 'Mr. Smith')
# Check appropriate values are in the session # Check that the user has been logged in
self.assertEqual(client.session['user_id'], '935a41b5-b38d-42c3-96ef-653402fc44ca') self.assertEqual(client.get('/').context['user'], user.user)
self.assertEqual(client.session['token'],'1accesstoken1')
self.assertEqual(client.session['disp_name'], 'Mr. Smith')
def test_reject_bad_state(self): def test_reject_bad_state(self):
with responses.RequestsMock() as rm: with responses.RequestsMock() as rm:
@ -58,7 +58,7 @@ class TestLogin(TestCase):
rm.add(responses.GET,'http://icynet.test/api/user',json={'uuid':'935a41b5-b38d-42c3-96ef-653402fc44ca','email':'johnsmith@gmail.com','display_name':'Mr. Smith'}) rm.add(responses.GET,'http://icynet.test/api/user',json={'uuid':'935a41b5-b38d-42c3-96ef-653402fc44ca','email':'johnsmith@gmail.com','display_name':'Mr. Smith'})
# Set up the database # Set up the database
user = User(user_id='935a41b5-b38d-42c3-96ef-653402fc44ca',email='johnsmith@gmail.com',display_name='Mr. Smith') user = ExternalUser(user=get_user_model().objects.create_user(username='mrsmith',email='johnsmith@gmail.com'),icy_id='935a41b5-b38d-42c3-96ef-653402fc44ca',display_name='Mr. Smith')
user.save() user.save()
# Make initial request to redirect endpoint # Make initial request to redirect endpoint
@ -71,17 +71,15 @@ class TestLogin(TestCase):
self.assertEqual(resp.status_code, 302) self.assertEqual(resp.status_code, 302)
# Check that the database is all good # Check that the database is all good
users = User.objects.all() users = ExternalUser.objects.all()
self.assertEqual(len(users), 1) self.assertEqual(len(users), 1)
user = users[0] user = users[0]
self.assertEqual(user.user_id,'935a41b5-b38d-42c3-96ef-653402fc44ca') self.assertEqual(user.icy_id,'935a41b5-b38d-42c3-96ef-653402fc44ca')
self.assertEqual(user.email,'johnsmith@gmail.com') self.assertEqual(user.user.email,'johnsmith@gmail.com')
self.assertEqual(user.display_name, 'Mr. Smith') self.assertEqual(user.display_name, 'Mr. Smith')
# Check appropriate values are in the session # Check that the user has been logged in
self.assertEqual(client.session['user_id'], '935a41b5-b38d-42c3-96ef-653402fc44ca') self.assertEqual(client.get('/').context['user'], user.user)
self.assertEqual(client.session['token'],'1accesstoken1')
self.assertEqual(client.session['disp_name'], 'Mr. Smith')
def test_states_unique(self): def test_states_unique(self):
with responses.RequestsMock() as rm: with responses.RequestsMock() as rm: