From 40d8d1f5e537b23fcbdc6716cbb0d2181656ec18 Mon Sep 17 00:00:00 2001 From: Tsa6 Date: Fri, 29 Sep 2017 17:34:57 -0400 Subject: [PATCH] Added test for logging in a pre-existing user --- tests/LandingPage/test_views.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/tests/LandingPage/test_views.py b/tests/LandingPage/test_views.py index 1e31b59..e8fe503 100644 --- a/tests/LandingPage/test_views.py +++ b/tests/LandingPage/test_views.py @@ -50,3 +50,35 @@ class TestLogin(TestCase): client = Client() resp = client.get('/login/redirect?state=%s&code=%s'%('bad_state', 'code')) self.assertEqual(resp.status_code, 400) + + 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 + user = User(user_id='935a41b5-b38d-42c3-96ef-653402fc44ca',email='johnsmith@gmail.com',display_name='Mr. Smith') + 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 + 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')