Added test for successfully logging in new user

This commit is contained in:
Tsa6 2017-09-29 17:21:42 -04:00
parent a6d4ebd1ba
commit 8d3f54ae3f
3 changed files with 46 additions and 0 deletions

View File

View File

@ -0,0 +1,46 @@
from django.test import TestCase,Client,override_settings
import responses
from LandingPage.models import User
from urllib import parse
@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'})
rm.add(responses.GET,'http://icynet.test/api/user',json={'uuid':'935a41b5-b38d-42c3-96ef-653402fc44ca','email':'johnsmith@gmail.com','display_name':'Mr. Smith'})
# 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')
self.assertEqual(query['scope'][0],'email')
# 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
users = User.objects.all()
self.assertEqual(len(users), 1)
user = users[0]
self.assertEqual(user.user_id,'935a41b5-b38d-42c3-96ef-653402fc44ca')
self.assertEqual(user.email,'johnsmith@gmail.com')
self.assertEqual(user.display_name, 'Mr. Smith')
# Check appropriate values are in the session
self.assertEqual(client.session['user_id'], '935a41b5-b38d-42c3-96ef-653402fc44ca')
self.assertEqual(client.session['token'],'1accesstoken1')
self.assertEqual(client.session['disp_name'], 'Mr. Smith')

0
tests/__init__.py Normal file
View File