Fix merge between existing and user-defined user profiles

Change-Id: I0cfe2421cb8b2a0e87cdb0f3b3b3873edcbd8eb5
Closes-Bug: #1484180
This commit is contained in:
Ilya Shakhat 2015-08-17 18:24:19 +03:00
parent db04e74e01
commit 09e359b643
3 changed files with 105 additions and 5 deletions

View File

@ -190,11 +190,8 @@ def _store_users(runtime_storage_inst, users):
for user in users:
stored_user = user_processor.load_user(runtime_storage_inst,
user_id=user['user_id'])
if stored_user:
stored_user.update(user)
user = stored_user
user['static'] = True
user_processor.store_user(runtime_storage_inst, user)
updated_user = user_processor.update_user_profile(stored_user, user)
user_processor.store_user(runtime_storage_inst, updated_user)
def _store_companies(runtime_storage_inst, companies):

View File

@ -12,6 +12,7 @@
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import copy
from oslo_log import log as logging
@ -88,3 +89,16 @@ def load_user(runtime_storage_inst, seq=None, user_id=None, email=None,
def delete_user(runtime_storage_inst, user):
LOG.debug('Delete user: %s', user)
runtime_storage_inst.delete_by_key('user:%s' % user['seq'])
def update_user_profile(stored_user, user):
# update stored_user with user and return it
if stored_user:
updated_user = copy.deepcopy(stored_user)
updated_user.update(user)
updated_user['emails'] = list(set(stored_user.get('emails', [])) |
set(user.get('emails', [])))
else:
updated_user = copy.deepcopy(user)
updated_user['static'] = True
return updated_user

View File

@ -0,0 +1,89 @@
# Copyright (c) 2015 Mirantis Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import testtools
from stackalytics.processor import user_processor
class TestUserProcessor(testtools.TestCase):
def setUp(self):
super(TestUserProcessor, self).setUp()
def test_update_user(self):
user = {
"launchpad_id": "user",
"companies": [
{
"company_name": "Rackspace",
"end_date": "2011-Nov-20"
},
{
"company_name": "IBM",
"end_date": None
}
],
"user_name": "John Smith",
"emails": ["john@smith.com"]
}
stored_user = {
"launchpad_id": "user",
"companies": [
{
"company_name": "Rackspace",
"end_date": "2011-Nov-20"
},
{
"company_name": "IBM",
"end_date": None
}
],
"user_name": "Johnny",
"emails": ["john@smith.com", "mapped_email@gmail.com"],
"static": True
}
updated_user = user_processor.update_user_profile(stored_user, user)
# merge emails from profile with those discovered by Stackalytics
self.assertEqual(set(stored_user['emails']),
set(updated_user['emails']))
# name from the profile has higher priority over mined
self.assertEqual(user['user_name'], updated_user['user_name'])
# static flag must present
self.assertTrue(updated_user.get('static'))
def test_update_user_unknown_user(self):
user = {
"launchpad_id": "user",
"companies": [
{
"company_name": "Rackspace",
"end_date": "2011-Nov-20"
},
{
"company_name": "IBM",
"end_date": None
}
],
"user_name": "John Smith",
"emails": ["john@smith.com"]
}
stored_user = None
updated_user = user_processor.update_user_profile(stored_user, user)
self.assertTrue(updated_user.get('static'))