2017-09-29 21:21:42 +00:00
|
|
|
from django.test import TestCase,Client,override_settings
|
|
|
|
import responses
|
2017-11-13 21:13:25 +00:00
|
|
|
from LandingPage.models import ExternalUser
|
2017-09-29 21:21:42 +00:00
|
|
|
from urllib import parse
|
2017-11-13 21:13:25 +00:00
|
|
|
from django.contrib.auth import get_user_model
|
2017-09-29 21:21:42 +00:00
|
|
|
|
|
|
|
@override_settings(
|
|
|
|
AUTH_TOKEN_ENDPOINT='http://icynet.test/api/',
|
|
|
|
AUTH_CLIENT_ID='clid',
|
|
|
|
AUTH_B64='Y2xpZDpjbGlzZWM=',
|
|
|
|
AUTH_REDIRECT_URL='http://redirect.test'
|
|
|
|
)
|
|
|
|
class TestLogin(TestCase):
|
|
|
|
|
|
|
|
def test_login_new_user(self):
|
|
|
|
# Set up responses to control network flow
|
|
|
|
with responses.RequestsMock() as rm:
|
|
|
|
rm.add(responses.POST,'http://icynet.test/api/token',json={'access_token':'1accesstoken1'})
|
2017-11-13 21:13:25 +00:00
|
|
|
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'})
|
2017-09-29 21:21:42 +00:00
|
|
|
|
|
|
|
# Make initial request to redirect endpoint
|
|
|
|
client = Client()
|
|
|
|
resp = client.get('/login')
|
|
|
|
self.assertEqual(resp.status_code, 302)
|
|
|
|
query = parse.parse_qs(parse.urlparse(resp['Location']).query)
|
|
|
|
state = query['state'][0]
|
|
|
|
self.assertEqual(query['client_id'][0],'clid')
|
|
|
|
self.assertEqual(query['response_type'][0],'code')
|
|
|
|
self.assertEqual(query['redirect_uri'][0],'http://redirect.test')
|
2017-11-13 21:13:25 +00:00
|
|
|
self.assertSetEqual(set(query['scope'][0].split()),set(['email','privilege']))
|
2017-09-29 21:21:42 +00:00
|
|
|
|
|
|
|
# Make connection to the real endpoint
|
|
|
|
resp = client.get('/login/redirect?state=%s&code=%s'%(state, 'code'))
|
|
|
|
self.assertEqual(resp.status_code, 302)
|
|
|
|
|
|
|
|
# Check that the database is all good
|
2017-11-13 21:13:25 +00:00
|
|
|
users = ExternalUser.objects.all()
|
2017-09-29 21:21:42 +00:00
|
|
|
self.assertEqual(len(users), 1)
|
|
|
|
user = users[0]
|
2017-11-13 21:13:25 +00:00
|
|
|
self.assertEqual(user.icy_id,'935a41b5-b38d-42c3-96ef-653402fc44ca')
|
|
|
|
self.assertEqual(user.user.email,'johnsmith@gmail.com')
|
|
|
|
self.assertEqual(user.user.username,'mrsmith')
|
2017-09-29 21:21:42 +00:00
|
|
|
self.assertEqual(user.display_name, 'Mr. Smith')
|
2017-11-13 21:13:25 +00:00
|
|
|
|
|
|
|
# Check that the user has been logged in
|
|
|
|
self.assertEqual(client.get('/').context['user'], user.user)
|
2017-09-29 21:25:12 +00:00
|
|
|
|
|
|
|
def test_reject_bad_state(self):
|
|
|
|
with responses.RequestsMock() as rm:
|
|
|
|
client = Client()
|
|
|
|
resp = client.get('/login/redirect?state=%s&code=%s'%('bad_state', 'code'))
|
|
|
|
self.assertEqual(resp.status_code, 400)
|
2017-09-29 21:34:57 +00:00
|
|
|
|
|
|
|
def test_login_old_user(self):
|
|
|
|
# Set up responses to control network flow
|
|
|
|
with responses.RequestsMock() as rm:
|
|
|
|
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'})
|
|
|
|
|
|
|
|
# Set up the database
|
2017-11-13 21:13:25 +00:00
|
|
|
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')
|
2017-09-29 21:34:57 +00:00
|
|
|
user.save()
|
|
|
|
|
|
|
|
# Make initial request to redirect endpoint
|
|
|
|
client = Client()
|
|
|
|
resp = client.get('/login')
|
|
|
|
state = parse.parse_qs(parse.urlparse(resp['Location']).query)['state'][0]
|
|
|
|
|
|
|
|
# Make connection to the real endpoint
|
|
|
|
resp = client.get('/login/redirect?state=%s&code=%s'%(state, 'code'))
|
|
|
|
self.assertEqual(resp.status_code, 302)
|
|
|
|
|
|
|
|
# Check that the database is all good
|
2017-11-13 21:13:25 +00:00
|
|
|
users = ExternalUser.objects.all()
|
2017-09-29 21:34:57 +00:00
|
|
|
self.assertEqual(len(users), 1)
|
|
|
|
user = users[0]
|
2017-11-13 21:13:25 +00:00
|
|
|
self.assertEqual(user.icy_id,'935a41b5-b38d-42c3-96ef-653402fc44ca')
|
|
|
|
self.assertEqual(user.user.email,'johnsmith@gmail.com')
|
2017-09-29 21:34:57 +00:00
|
|
|
self.assertEqual(user.display_name, 'Mr. Smith')
|
2017-11-13 21:13:25 +00:00
|
|
|
|
|
|
|
# Check that the user has been logged in
|
|
|
|
self.assertEqual(client.get('/').context['user'], user.user)
|
2017-09-29 21:40:50 +00:00
|
|
|
|
|
|
|
def test_states_unique(self):
|
|
|
|
with responses.RequestsMock() as rm:
|
|
|
|
client1 = Client()
|
|
|
|
resp1 = client1.get('/login')
|
|
|
|
state1 = parse.parse_qs(parse.urlparse(resp1['Location']).query)['state'][0]
|
|
|
|
|
|
|
|
client2 = Client()
|
|
|
|
resp2 = client2.get('/login')
|
|
|
|
state2 = parse.parse_qs(parse.urlparse(resp2['Location']).query)['state'][0]
|
|
|
|
|
|
|
|
self.assertNotEqual(state1,state2)
|