From 8a400c5c0f3a658096f52fb04d6feb1d0c922183 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Fri, 8 Dec 2017 14:03:30 +0100 Subject: [PATCH] Gracefully handle users with empty full_name --- pillar/api/utils/authentication.py | 4 ++ tests/test_api/test_auth.py | 68 ++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/pillar/api/utils/authentication.py b/pillar/api/utils/authentication.py index 65a50b79..4ac9c84b 100644 --- a/pillar/api/utils/authentication.py +++ b/pillar/api/utils/authentication.py @@ -371,6 +371,10 @@ def upsert_user(db_user): raise wz_exceptions.InternalServerError( 'Non-ObjectID string found in user.groups: %s' % db_user) + if not db_user['full_name']: + # Blender ID doesn't need a full name, but we do. + db_user['full_name'] = db_user['username'] + r = {} for retry in range(5): if '_id' in db_user: diff --git a/tests/test_api/test_auth.py b/tests/test_api/test_auth.py index 13191454..8c930db8 100644 --- a/tests/test_api/test_auth.py +++ b/tests/test_api/test_auth.py @@ -717,6 +717,74 @@ class UserCreationTest(AbstractPillarTest): db_user = users_coll.find()[0] self.assertEqual(db_user['email'], TEST_EMAIL_ADDRESS) + @responses.activate + def test_create_by_auth_no_full_name(self): + """Blender ID does not require full name, we do.""" + + with self.app.test_request_context(): + users_coll = self.app.db().users + self.assertEqual(0, users_coll.count()) + + bid_resp = {'status': 'success', + 'user': {'email': TEST_EMAIL_ADDRESS, + 'full_name': '', + 'id': ctd.BLENDER_ID_TEST_USERID}, + 'token_expires': 'Mon, 1 Jan 2218 01:02:03 GMT'} + + responses.add(responses.POST, + '%s/u/validate_token' % self.app.config['BLENDER_ID_ENDPOINT'], + json=bid_resp, + status=200) + + token = 'this is my life now' + self.get('/api/users/me', auth_token=token) + + with self.app.test_request_context(): + users_coll = self.app.db().users + self.assertEqual(1, users_coll.count()) + + db_user = users_coll.find()[0] + self.assertEqual(db_user['email'], TEST_EMAIL_ADDRESS) + self.assertNotEqual('', db_user['full_name']) + + @responses.activate + def test_update_by_auth_no_full_name(self): + """Blender ID does not require full name, we do.""" + self.enter_app_context() + users_coll = self.app.db().users + self.assertEqual(0, users_coll.count()) + + # First request will create the user, the 2nd request will update. + self.mock_blenderid_validate_happy() + bid_resp = {'status': 'success', + 'user': {'email': TEST_EMAIL_ADDRESS, + 'full_name': '', + 'id': ctd.BLENDER_ID_TEST_USERID}, + 'token_expires': 'Mon, 1 Jan 2218 01:02:03 GMT'} + responses.add(responses.POST, + '%s/u/validate_token' % self.app.config['BLENDER_ID_ENDPOINT'], + json=bid_resp, + status=200) + + token = 'this is my life now' + self.get('/api/users/me', auth_token=token) + + # Clear out the full name of the user. This could happen for some + # reason, and it shouldn't break the login flow. + users_coll.update_many({}, {'$set': {'full_name': ''}}) + + # Delete all tokens to force a re-check with Blender ID + tokens_coll = self.app.db('tokens') + tokens_coll.delete_many({}) + + self.get('/api/users/me', auth_token=token) + + self.assertEqual(1, users_coll.count()) + + db_user = users_coll.find()[0] + self.assertEqual(db_user['email'], TEST_EMAIL_ADDRESS) + self.assertNotEqual('', db_user['full_name']) + def test_user_without_email_address(self): """Regular users should always have an email address.