2016-04-15 16:27:24 +02:00
|
|
|
import datetime
|
2016-04-13 15:33:54 +02:00
|
|
|
import responses
|
2016-04-15 16:27:24 +02:00
|
|
|
from bson import tz_util
|
2016-03-04 14:08:10 +01:00
|
|
|
|
2016-03-25 16:05:36 +01:00
|
|
|
from common_test_class import AbstractPillarTest, TEST_EMAIL_USER, TEST_EMAIL_ADDRESS
|
2016-03-25 12:22:31 +01:00
|
|
|
|
2016-03-04 14:47:48 +01:00
|
|
|
|
2016-03-25 15:57:17 +01:00
|
|
|
class AuthenticationTests(AbstractPillarTest):
|
2016-03-04 14:08:10 +01:00
|
|
|
def test_make_unique_username(self):
|
2016-03-25 15:57:17 +01:00
|
|
|
from application.utils import authentication as auth
|
2016-03-04 14:08:10 +01:00
|
|
|
|
2016-03-25 15:57:17 +01:00
|
|
|
with self.app.test_request_context():
|
2016-03-04 14:08:10 +01:00
|
|
|
# This user shouldn't exist yet.
|
2016-03-04 14:49:48 +01:00
|
|
|
self.assertEqual(TEST_EMAIL_USER, auth.make_unique_username(TEST_EMAIL_ADDRESS))
|
2016-03-04 14:08:10 +01:00
|
|
|
|
|
|
|
# Add a user, then test again.
|
2016-03-04 14:49:48 +01:00
|
|
|
auth.create_new_user(TEST_EMAIL_ADDRESS, TEST_EMAIL_USER, 'test1234')
|
2016-03-04 15:19:01 +01:00
|
|
|
self.assertEqual('%s1' % TEST_EMAIL_USER, auth.make_unique_username(TEST_EMAIL_ADDRESS))
|
2016-03-04 14:47:48 +01:00
|
|
|
|
2016-04-13 15:33:54 +02:00
|
|
|
@responses.activate
|
2016-03-04 18:43:20 +01:00
|
|
|
def test_validate_token__not_logged_in(self):
|
2016-03-25 15:57:17 +01:00
|
|
|
from application.utils import authentication as auth
|
|
|
|
|
|
|
|
with self.app.test_request_context():
|
2016-03-04 18:43:20 +01:00
|
|
|
self.assertFalse(auth.validate_token())
|
|
|
|
|
2016-04-13 15:33:54 +02:00
|
|
|
@responses.activate
|
2016-03-04 14:47:48 +01:00
|
|
|
def test_validate_token__unknown_token(self):
|
2016-03-04 15:19:01 +01:00
|
|
|
"""Test validating of invalid token, unknown both to us and Blender ID."""
|
|
|
|
|
2016-03-25 15:57:17 +01:00
|
|
|
from application.utils import authentication as auth
|
|
|
|
|
2016-04-13 15:33:54 +02:00
|
|
|
self.mock_blenderid_validate_unhappy()
|
2016-04-12 15:24:50 +02:00
|
|
|
with self.app.test_request_context(
|
|
|
|
headers={'Authorization': self.make_header('unknowntoken')}):
|
2016-03-04 14:49:48 +01:00
|
|
|
self.assertFalse(auth.validate_token())
|
2016-03-04 15:19:01 +01:00
|
|
|
|
2016-04-13 15:33:54 +02:00
|
|
|
@responses.activate
|
2016-03-04 15:19:01 +01:00
|
|
|
def test_validate_token__unknown_but_valid_token(self):
|
|
|
|
"""Test validating of valid token, unknown to us but known to Blender ID."""
|
|
|
|
|
2016-03-25 15:57:17 +01:00
|
|
|
from application.utils import authentication as auth
|
|
|
|
|
2016-04-13 15:33:54 +02:00
|
|
|
self.mock_blenderid_validate_happy()
|
2016-04-12 15:24:50 +02:00
|
|
|
with self.app.test_request_context(
|
|
|
|
headers={'Authorization': self.make_header('knowntoken')}):
|
2016-03-04 15:19:01 +01:00
|
|
|
self.assertTrue(auth.validate_token())
|
2016-04-15 16:27:24 +02:00
|
|
|
|
|
|
|
@responses.activate
|
|
|
|
def test_find_token(self):
|
|
|
|
"""Test finding of various tokens."""
|
|
|
|
|
|
|
|
from application.utils import authentication as auth
|
|
|
|
|
|
|
|
user_id = self.create_user()
|
|
|
|
|
|
|
|
now = datetime.datetime.now(tz_util.utc)
|
|
|
|
future = now + datetime.timedelta(days=1)
|
|
|
|
past = now - datetime.timedelta(days=1)
|
|
|
|
subclient = self.app.config['BLENDER_ID_SUBCLIENT_ID']
|
|
|
|
|
|
|
|
with self.app.test_request_context():
|
|
|
|
auth.store_token(user_id, 'nonexpired-main', future, None)
|
|
|
|
auth.store_token(user_id, 'nonexpired-sub', future, subclient)
|
|
|
|
token3 = auth.store_token(user_id, 'expired-sub', past, subclient)
|
|
|
|
|
|
|
|
with self.app.test_request_context(
|
|
|
|
headers={'Authorization': self.make_header('nonexpired-main')}):
|
|
|
|
self.assertTrue(auth.validate_token())
|
|
|
|
|
|
|
|
with self.app.test_request_context(
|
|
|
|
headers={'Authorization': self.make_header('nonexpired-main', subclient)}):
|
|
|
|
self.assertFalse(auth.validate_token())
|
|
|
|
|
|
|
|
with self.app.test_request_context(
|
|
|
|
headers={'Authorization': self.make_header('nonexpired-sub')}):
|
|
|
|
self.assertFalse(auth.validate_token())
|
|
|
|
|
|
|
|
with self.app.test_request_context(
|
|
|
|
headers={'Authorization': self.make_header('nonexpired-sub', subclient)}):
|
|
|
|
self.assertTrue(auth.validate_token())
|
|
|
|
|
|
|
|
with self.app.test_request_context(
|
|
|
|
headers={'Authorization': self.make_header('expired-sub', subclient)}):
|
|
|
|
self.assertFalse(auth.validate_token())
|
|
|
|
|
|
|
|
self.mock_blenderid_validate_happy()
|
|
|
|
with self.app.test_request_context(
|
|
|
|
headers={'Authorization': self.make_header('expired-sub', subclient)}):
|
|
|
|
self.assertTrue(auth.validate_token())
|
|
|
|
|
|
|
|
# We now should be able to find a new token for this user.
|
|
|
|
found_token = auth.find_token('expired-sub', subclient)
|
|
|
|
self.assertIsNotNone(found_token)
|
|
|
|
self.assertNotEqual(token3['_id'], found_token['_id'])
|