2016-04-12 15:24:50 +02:00
|
|
|
# -*- encoding: utf-8 -*-
|
|
|
|
|
|
|
|
import json
|
2018-09-11 17:53:23 +02:00
|
|
|
from urllib.parse import urljoin
|
2016-04-12 15:24:50 +02:00
|
|
|
|
2016-08-19 09:19:06 +02:00
|
|
|
import responses
|
2016-04-12 15:24:50 +02:00
|
|
|
from bson import ObjectId
|
2016-04-13 15:33:54 +02:00
|
|
|
from flask import g
|
2016-09-08 12:03:17 +02:00
|
|
|
from pillar.tests import (AbstractPillarTest, TEST_EMAIL_ADDRESS,
|
2016-08-19 09:19:06 +02:00
|
|
|
TEST_SUBCLIENT_TOKEN, TEST_EMAIL_USER, TEST_FULL_NAME)
|
2016-09-08 12:03:17 +02:00
|
|
|
from pillar.tests import common_test_data as ctd
|
2016-04-12 15:24:50 +02:00
|
|
|
|
|
|
|
|
|
|
|
class BlenderIdSubclientTest(AbstractPillarTest):
|
|
|
|
@responses.activate
|
|
|
|
def test_store_scst_new_user(self):
|
|
|
|
self._common_user_test(201)
|
|
|
|
|
2016-07-05 12:36:32 +02:00
|
|
|
@responses.activate
|
|
|
|
def test_store_scst_new_user_without_full_name(self):
|
|
|
|
|
|
|
|
responses.add(responses.POST,
|
2018-09-11 17:53:23 +02:00
|
|
|
urljoin(self.app.config['BLENDER_ID_ENDPOINT'], 'u/validate_token'),
|
2016-07-05 12:36:32 +02:00
|
|
|
json={'status': 'success',
|
|
|
|
'user': {'email': TEST_EMAIL_ADDRESS,
|
|
|
|
'full_name': None,
|
2016-09-08 12:03:17 +02:00
|
|
|
'id': ctd.BLENDER_ID_TEST_USERID},
|
2016-07-05 12:36:32 +02:00
|
|
|
'token_expires': 'Mon, 1 Jan 2218 01:02:03 GMT'},
|
|
|
|
status=200)
|
|
|
|
|
|
|
|
self._common_user_test(201,
|
|
|
|
expected_full_name=TEST_EMAIL_USER,
|
|
|
|
mock_happy_blender_id=False)
|
|
|
|
|
2016-04-12 15:24:50 +02:00
|
|
|
@responses.activate
|
|
|
|
def test_store_scst_existing_user(self):
|
|
|
|
# Make sure the user exists in our database.
|
2016-08-19 09:19:06 +02:00
|
|
|
from pillar.api.utils.authentication import create_new_user
|
2016-04-12 15:24:50 +02:00
|
|
|
with self.app.test_request_context():
|
2016-09-08 12:03:17 +02:00
|
|
|
create_new_user(TEST_EMAIL_ADDRESS, 'apekoppie', ctd.BLENDER_ID_TEST_USERID)
|
2016-04-12 15:24:50 +02:00
|
|
|
|
2016-05-30 14:32:53 +02:00
|
|
|
self._common_user_test(200, expected_full_name='apekoppie')
|
2016-04-12 15:24:50 +02:00
|
|
|
|
2016-04-13 15:33:54 +02:00
|
|
|
@responses.activate
|
|
|
|
def test_store_multiple_tokens(self):
|
|
|
|
scst1 = '%s-1' % TEST_SUBCLIENT_TOKEN
|
|
|
|
scst2 = '%s-2' % TEST_SUBCLIENT_TOKEN
|
|
|
|
db_user1 = self._common_user_test(201, scst=scst1)
|
|
|
|
db_user2 = self._common_user_test(200, scst=scst2)
|
|
|
|
self.assertEqual(db_user1['_id'], db_user2['_id'])
|
|
|
|
|
|
|
|
# Now there should be two tokens.
|
|
|
|
with self.app.test_request_context():
|
|
|
|
tokens = self.app.data.driver.db['tokens']
|
2018-09-11 16:11:44 +02:00
|
|
|
self.assertIsNotNone(tokens.find_one({'user': db_user1['_id'], 'token': scst1}))
|
|
|
|
self.assertIsNotNone(tokens.find_one({'user': db_user1['_id'], 'token': scst2}))
|
2016-04-13 15:33:54 +02:00
|
|
|
|
|
|
|
# There should still be only one auth element for blender-id in the user doc.
|
|
|
|
self.assertEqual(1, len(db_user1['auth']))
|
|
|
|
|
|
|
|
@responses.activate
|
|
|
|
def test_authenticate_with_scst(self):
|
|
|
|
# Make sure there is a user and SCST.
|
|
|
|
db_user = self._common_user_test(201)
|
2016-04-12 15:24:50 +02:00
|
|
|
|
2016-04-13 15:33:54 +02:00
|
|
|
# Make a call that's authenticated with the SCST
|
2016-08-19 09:19:06 +02:00
|
|
|
from pillar.api.utils import authentication as auth
|
2016-04-13 15:33:54 +02:00
|
|
|
|
|
|
|
subclient_id = self.app.config['BLENDER_ID_SUBCLIENT_ID']
|
|
|
|
auth_header = self.make_header(TEST_SUBCLIENT_TOKEN, subclient_id)
|
|
|
|
|
|
|
|
with self.app.test_request_context(headers={'Authorization': auth_header}):
|
|
|
|
self.assertTrue(auth.validate_token())
|
|
|
|
self.assertIsNotNone(g.current_user)
|
2017-08-18 13:19:34 +02:00
|
|
|
self.assertEqual(db_user['_id'], g.current_user.user_id)
|
2016-04-13 15:33:54 +02:00
|
|
|
|
2016-05-30 14:32:53 +02:00
|
|
|
def _common_user_test(self, expected_status_code, scst=TEST_SUBCLIENT_TOKEN,
|
2016-07-05 12:36:32 +02:00
|
|
|
expected_full_name=TEST_FULL_NAME,
|
|
|
|
mock_happy_blender_id=True):
|
|
|
|
if mock_happy_blender_id:
|
|
|
|
self.mock_blenderid_validate_happy()
|
2016-04-13 15:33:54 +02:00
|
|
|
|
|
|
|
subclient_id = self.app.config['BLENDER_ID_SUBCLIENT_ID']
|
2016-08-19 09:19:06 +02:00
|
|
|
resp = self.client.post('/api/blender_id/store_scst',
|
2016-09-08 12:03:17 +02:00
|
|
|
data={'user_id': ctd.BLENDER_ID_TEST_USERID,
|
2016-04-13 15:33:54 +02:00
|
|
|
'subclient_id': subclient_id,
|
|
|
|
'token': scst})
|
2016-07-05 12:36:32 +02:00
|
|
|
self.assertEqual(expected_status_code, resp.status_code, resp.data)
|
2016-04-12 15:24:50 +02:00
|
|
|
|
|
|
|
user_info = json.loads(resp.data) # {'status': 'success', 'subclient_user_id': '...'}
|
|
|
|
self.assertEqual('success', user_info['status'])
|
2016-04-12 16:05:37 +02:00
|
|
|
|
2016-04-12 15:24:50 +02:00
|
|
|
with self.app.test_request_context():
|
2016-04-13 15:33:54 +02:00
|
|
|
# Check that the user was correctly updated
|
2016-04-12 15:24:50 +02:00
|
|
|
users = self.app.data.driver.db['users']
|
|
|
|
db_user = users.find_one(ObjectId(user_info['subclient_user_id']))
|
|
|
|
self.assertIsNotNone(db_user, 'user %r not found' % user_info['subclient_user_id'])
|
|
|
|
|
2016-04-13 15:33:54 +02:00
|
|
|
self.assertEqual(TEST_EMAIL_ADDRESS, db_user['email'])
|
2016-05-30 14:32:53 +02:00
|
|
|
self.assertEqual(expected_full_name, db_user['full_name'])
|
2016-04-13 15:33:54 +02:00
|
|
|
# self.assertEqual(TEST_SUBCLIENT_TOKEN, db_user['auth'][0]['token'])
|
2016-09-08 12:03:17 +02:00
|
|
|
self.assertEqual(str(ctd.BLENDER_ID_TEST_USERID), db_user['auth'][0]['user_id'])
|
2016-04-12 15:24:50 +02:00
|
|
|
self.assertEqual('blender-id', db_user['auth'][0]['provider'])
|
2016-04-13 15:33:54 +02:00
|
|
|
|
|
|
|
# Check that the token was succesfully stored.
|
|
|
|
tokens = self.app.data.driver.db['tokens']
|
|
|
|
db_token = tokens.find_one({'user': db_user['_id'],
|
2018-09-11 16:11:44 +02:00
|
|
|
'token': scst})
|
2016-04-13 15:33:54 +02:00
|
|
|
self.assertIsNotNone(db_token)
|
|
|
|
|
|
|
|
return db_user
|