2016-03-04 14:08:10 +01:00
|
|
|
import unittest
|
|
|
|
import os
|
2016-03-04 14:47:48 +01:00
|
|
|
import base64
|
2016-03-04 15:19:01 +01:00
|
|
|
import httpretty
|
|
|
|
import json
|
2016-03-04 14:08:10 +01:00
|
|
|
|
2016-03-04 15:19:01 +01:00
|
|
|
BLENDER_ID_ENDPOINT = 'http://127.0.0.1:8001' # nonexistant server, no trailing slash!
|
2016-03-04 14:08:10 +01:00
|
|
|
TEST_EMAIL_USER = 'koro'
|
|
|
|
TEST_EMAIL_ADDRESS = '%s@testing.blender.org' % TEST_EMAIL_USER
|
|
|
|
|
2016-03-04 15:19:01 +01:00
|
|
|
os.environ['BLENDER_ID_ENDPOINT'] = BLENDER_ID_ENDPOINT
|
2016-03-04 14:08:10 +01:00
|
|
|
os.environ['MONGO_DBNAME'] = 'unittest'
|
2016-03-23 12:01:54 +01:00
|
|
|
os.environ['EVE_SETTINGS'] = os.path.join(
|
|
|
|
os.path.dirname(os.path.dirname(__file__)),
|
|
|
|
'pillar', 'settings.py')
|
2016-03-04 14:08:10 +01:00
|
|
|
|
|
|
|
from application import app
|
2016-03-04 14:49:48 +01:00
|
|
|
from application.utils import authentication as auth
|
2016-03-04 14:47:48 +01:00
|
|
|
|
2016-03-25 12:22:31 +01:00
|
|
|
app.config['BLENDER_ID_ENDPOINT'] = BLENDER_ID_ENDPOINT
|
|
|
|
|
2016-03-04 14:47:48 +01:00
|
|
|
|
|
|
|
def make_header(username, password=''):
|
|
|
|
"""Returns a Basic HTTP Authentication header value."""
|
|
|
|
|
|
|
|
return 'basic ' + base64.b64encode('%s:%s' % (username, password))
|
2016-03-04 14:08:10 +01:00
|
|
|
|
|
|
|
|
2016-03-04 15:19:01 +01:00
|
|
|
class AuthenticationTests(unittest.TestCase):
|
2016-03-04 14:08:10 +01:00
|
|
|
def setUp(self):
|
|
|
|
self.app = app.test_client()
|
2016-03-04 15:19:01 +01:00
|
|
|
with app.test_request_context():
|
|
|
|
self.delete_test_data()
|
2016-03-04 14:08:10 +01:00
|
|
|
|
|
|
|
def tearDown(self):
|
2016-03-04 15:19:01 +01:00
|
|
|
with app.test_request_context():
|
|
|
|
self.delete_test_data()
|
2016-03-04 14:08:10 +01:00
|
|
|
|
|
|
|
def test_make_unique_username(self):
|
|
|
|
|
|
|
|
with app.test_request_context():
|
|
|
|
# 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-03-04 15:19:01 +01:00
|
|
|
def delete_test_data(self):
|
|
|
|
app.data.driver.db.drop_collection('users')
|
|
|
|
app.data.driver.db.drop_collection('tokens')
|
|
|
|
|
|
|
|
def blenderid_validate_unhappy(self):
|
|
|
|
"""Sets up HTTPretty to mock unhappy validation flow."""
|
|
|
|
|
|
|
|
httpretty.register_uri(httpretty.POST,
|
|
|
|
'%s/u/validate_token' % BLENDER_ID_ENDPOINT,
|
|
|
|
body=json.dumps({'data': {'token': 'Token is invalid'}, 'status': 'fail'}),
|
|
|
|
content_type="application/json")
|
|
|
|
|
|
|
|
def blenderid_validate_happy(self):
|
|
|
|
"""Sets up HTTPretty to mock happy validation flow."""
|
|
|
|
|
|
|
|
httpretty.register_uri(httpretty.POST,
|
|
|
|
'%s/u/validate_token' % BLENDER_ID_ENDPOINT,
|
|
|
|
body=json.dumps({'data': {'user': {'email': TEST_EMAIL_ADDRESS, 'id': 5123}},
|
|
|
|
'status': 'success'}),
|
|
|
|
content_type="application/json")
|
|
|
|
|
2016-03-04 18:43:20 +01:00
|
|
|
@httpretty.activate
|
|
|
|
def test_validate_token__not_logged_in(self):
|
|
|
|
with app.test_request_context():
|
|
|
|
self.assertFalse(auth.validate_token())
|
|
|
|
|
2016-03-04 15:19:01 +01:00
|
|
|
@httpretty.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."""
|
|
|
|
|
|
|
|
self.blenderid_validate_unhappy()
|
2016-03-04 14:47:48 +01:00
|
|
|
with app.test_request_context(headers={'Authorization': make_header('unknowntoken')}):
|
2016-03-04 14:49:48 +01:00
|
|
|
self.assertFalse(auth.validate_token())
|
2016-03-04 15:19:01 +01:00
|
|
|
|
|
|
|
@httpretty.activate
|
|
|
|
def test_validate_token__unknown_but_valid_token(self):
|
|
|
|
"""Test validating of valid token, unknown to us but known to Blender ID."""
|
|
|
|
|
|
|
|
self.blenderid_validate_happy()
|
|
|
|
with app.test_request_context(headers={'Authorization': make_header('knowntoken')}):
|
|
|
|
self.assertTrue(auth.validate_token())
|