2020-02-18 21:38:56 +00:00
|
|
|
from crc import db
|
|
|
|
from crc.models.user import UserModel
|
|
|
|
from tests.base_test import BaseTest
|
|
|
|
|
|
|
|
|
|
|
|
class TestAuthentication(BaseTest):
|
|
|
|
|
|
|
|
def test_auth_token(self):
|
2020-02-20 20:35:07 +00:00
|
|
|
self.load_example_data()
|
2020-02-18 21:38:56 +00:00
|
|
|
user = UserModel(uid="dhf8r")
|
|
|
|
auth_token = user.encode_auth_token()
|
|
|
|
self.assertTrue(isinstance(auth_token, bytes))
|
2020-03-24 18:15:21 +00:00
|
|
|
self.assertEqual("dhf8r", user.decode_auth_token(auth_token).get("sub"))
|
2020-02-18 21:38:56 +00:00
|
|
|
|
|
|
|
def test_auth_creates_user(self):
|
2020-02-27 15:30:16 +00:00
|
|
|
new_uid = 'czn1z';
|
2020-02-20 20:35:07 +00:00
|
|
|
self.load_example_data()
|
2020-02-27 15:30:16 +00:00
|
|
|
user = db.session.query(UserModel).filter(UserModel.uid == new_uid).first()
|
2020-02-18 21:38:56 +00:00
|
|
|
self.assertIsNone(user)
|
|
|
|
|
2020-02-27 15:30:16 +00:00
|
|
|
user_info = {'uid': new_uid, 'first_name': 'Cordi', 'last_name': 'Nator',
|
|
|
|
'email_address': 'czn1z@virginia.edu'}
|
2020-02-24 21:59:16 +00:00
|
|
|
redirect_url = 'http://worlds.best.website/admin'
|
|
|
|
query_string = self.user_info_to_query_string(user_info, redirect_url)
|
2020-02-21 17:03:14 +00:00
|
|
|
url = '/v1.0/sso_backdoor%s' % query_string
|
|
|
|
rv_1 = self.app.get(url, follow_redirects=False)
|
2020-02-20 22:23:10 +00:00
|
|
|
self.assertTrue(rv_1.status_code == 302)
|
2020-02-24 21:59:16 +00:00
|
|
|
self.assertTrue(str.startswith(rv_1.location, redirect_url))
|
2020-02-20 22:23:10 +00:00
|
|
|
|
2020-02-27 15:30:16 +00:00
|
|
|
user = db.session.query(UserModel).filter(UserModel.uid == new_uid).first()
|
2020-02-18 21:38:56 +00:00
|
|
|
self.assertIsNotNone(user)
|
|
|
|
self.assertIsNotNone(user.display_name)
|
|
|
|
self.assertIsNotNone(user.email_address)
|
|
|
|
|
2020-02-20 22:23:10 +00:00
|
|
|
# Hitting the same endpoint again with the same info should not cause an error
|
2020-02-21 17:03:14 +00:00
|
|
|
rv_2 = self.app.get(url, follow_redirects=False)
|
2020-02-24 21:59:16 +00:00
|
|
|
self.assertTrue(rv_2.status_code == 302)
|
|
|
|
self.assertTrue(str.startswith(rv_2.location, redirect_url))
|
2020-02-20 22:23:10 +00:00
|
|
|
|
2020-02-18 21:38:56 +00:00
|
|
|
def test_current_user_status(self):
|
2020-02-20 20:35:07 +00:00
|
|
|
self.load_example_data()
|
2020-02-18 21:38:56 +00:00
|
|
|
rv = self.app.get('/v1.0/user')
|
|
|
|
self.assert_failure(rv, 401)
|
|
|
|
|
|
|
|
rv = self.app.get('/v1.0/user', headers=self.logged_in_headers())
|
|
|
|
self.assert_success(rv)
|
2020-02-20 20:35:07 +00:00
|
|
|
|
|
|
|
user = UserModel(uid="ajl2j", first_name='Aaron', last_name='Louie', email_address='ajl2j@virginia.edu')
|
2020-02-24 21:59:16 +00:00
|
|
|
rv = self.app.get('/v1.0/user', headers=self.logged_in_headers(user, redirect_url='http://omg.edu/lolwut'))
|
2020-02-20 20:35:07 +00:00
|
|
|
self.assert_success(rv)
|