subclient tokens: bugfix & return proper data.
Also introduces responses, as an alternative to httpretty (it works better).
This commit is contained in:
@@ -3,6 +3,7 @@ import copy
|
||||
import sys
|
||||
import logging
|
||||
import os
|
||||
import base64
|
||||
|
||||
from bson import ObjectId
|
||||
from eve.tests import TestMinimal
|
||||
@@ -101,3 +102,7 @@ class AbstractPillarTest(TestMinimal):
|
||||
'status': 'success'}),
|
||||
content_type="application/json")
|
||||
|
||||
def make_header(self, username, password=''):
|
||||
"""Returns a Basic HTTP Authentication header value."""
|
||||
|
||||
return 'basic ' + base64.b64encode('%s:%s' % (username, password))
|
||||
|
@@ -1,15 +1,8 @@
|
||||
import base64
|
||||
import httpretty
|
||||
|
||||
from common_test_class import AbstractPillarTest, TEST_EMAIL_USER, TEST_EMAIL_ADDRESS
|
||||
|
||||
|
||||
def make_header(username, password=''):
|
||||
"""Returns a Basic HTTP Authentication header value."""
|
||||
|
||||
return 'basic ' + base64.b64encode('%s:%s' % (username, password))
|
||||
|
||||
|
||||
class AuthenticationTests(AbstractPillarTest):
|
||||
def test_make_unique_username(self):
|
||||
from application.utils import authentication as auth
|
||||
@@ -36,7 +29,8 @@ class AuthenticationTests(AbstractPillarTest):
|
||||
from application.utils import authentication as auth
|
||||
|
||||
self.htp_blenderid_validate_unhappy()
|
||||
with self.app.test_request_context(headers={'Authorization': make_header('unknowntoken')}):
|
||||
with self.app.test_request_context(
|
||||
headers={'Authorization': self.make_header('unknowntoken')}):
|
||||
self.assertFalse(auth.validate_token())
|
||||
|
||||
@httpretty.activate
|
||||
@@ -46,5 +40,6 @@ class AuthenticationTests(AbstractPillarTest):
|
||||
from application.utils import authentication as auth
|
||||
|
||||
self.htp_blenderid_validate_happy()
|
||||
with self.app.test_request_context(headers={'Authorization': make_header('knowntoken')}):
|
||||
with self.app.test_request_context(
|
||||
headers={'Authorization': self.make_header('knowntoken')}):
|
||||
self.assertTrue(auth.validate_token())
|
||||
|
55
tests/test_blender_id_subclient.py
Normal file
55
tests/test_blender_id_subclient.py
Normal file
@@ -0,0 +1,55 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
|
||||
import responses
|
||||
import json
|
||||
|
||||
from bson import ObjectId
|
||||
|
||||
from common_test_class import AbstractPillarTest
|
||||
|
||||
TEST_FULL_NAME = u'врач Сергей'
|
||||
TEST_EMAIL = 'jemoeder@example.com'
|
||||
TEST_SUBCLIENT_TOKEN = 'my-subclient-token-for-pillar'
|
||||
BLENDER_ID_TEST_USERID = 1896
|
||||
BLENDER_ID_USER_RESPONSE = {'status': 'success',
|
||||
'user': {'email': TEST_EMAIL, 'full_name': TEST_FULL_NAME}}
|
||||
|
||||
|
||||
class BlenderIdSubclientTest(AbstractPillarTest):
|
||||
@responses.activate
|
||||
def test_store_scst_new_user(self):
|
||||
self._common_user_test(201)
|
||||
|
||||
@responses.activate
|
||||
def test_store_scst_existing_user(self):
|
||||
# Make sure the user exists in our database.
|
||||
from application.utils.authentication import create_new_user
|
||||
with self.app.test_request_context():
|
||||
create_new_user(TEST_EMAIL, 'apekoppie', BLENDER_ID_TEST_USERID)
|
||||
|
||||
self._common_user_test(200)
|
||||
|
||||
def _common_user_test(self, expected_status_code):
|
||||
responses.add(responses.POST,
|
||||
'%s/subclients/validate_token' % self.app.config['BLENDER_ID_ENDPOINT'],
|
||||
json=BLENDER_ID_USER_RESPONSE,
|
||||
status=200)
|
||||
|
||||
resp = self.client.post('/blender_id/store_scst',
|
||||
data={'user_id': BLENDER_ID_TEST_USERID,
|
||||
'scst': TEST_SUBCLIENT_TOKEN})
|
||||
self.assertEqual(expected_status_code, resp.status_code)
|
||||
|
||||
user_info = json.loads(resp.data) # {'status': 'success', 'subclient_user_id': '...'}
|
||||
self.assertEqual('success', user_info['status'])
|
||||
# Check that the user was correctly updated
|
||||
with self.app.test_request_context():
|
||||
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'])
|
||||
|
||||
self.assertEqual(TEST_EMAIL, db_user['email'])
|
||||
self.assertEqual(TEST_FULL_NAME, db_user['full_name'])
|
||||
self.assertEqual(TEST_SUBCLIENT_TOKEN, db_user['auth'][0]['token'])
|
||||
self.assertEqual(str(BLENDER_ID_TEST_USERID), db_user['auth'][0]['user_id'])
|
||||
self.assertEqual('blender-id', db_user['auth'][0]['provider'])
|
Reference in New Issue
Block a user