Skip to content

Commit

Permalink
Merge pull request #33818 from dimagi/es/user-data-fix
Browse files Browse the repository at this point in the history
Fix datetime user data
  • Loading branch information
esoergel authored Nov 30, 2023
2 parents 43c11e2 + 199f8b9 commit 1b67098
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 38 deletions.
2 changes: 1 addition & 1 deletion corehq/apps/api/tests/user_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def test_create(self):
self.assertEqual(user_back.email, "[email protected]")
self.assertEqual(user_back.language, "en")
self.assertEqual(user_back.get_group_ids()[0], group._id)
self.assertEqual(user_back.user_data["chw_id"], "13/43/DFA")
self.assertEqual(user_back.get_user_data(self.domain.name)["chw_id"], "13/43/DFA")
self.assertEqual(user_back.default_phone_number, "50253311399")

@flag_enabled('COMMCARE_CONNECT')
Expand Down
41 changes: 6 additions & 35 deletions corehq/apps/users/tests/test_user_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,8 @@ def test_user_data_accessor(self):
'cruise': 'control',
'this': 'road',
})
# Normally you shouldn't use `user.user_data` directly - I'm demonstrating that it's updated
self.assertEqual(user.user_data, {
'cruise': 'control',
'this': 'road',
})
del user_data['cruise']
self.assertEqual(user.user_data, {
'this': 'road',
})
user_data.update({'this': 'field'})
self.assertEqual(user.user_data, {
'this': 'field',
})
# Normally you shouldn't use `user.user_data` directly - I'm demonstrating that it's not updated
self.assertEqual(user.user_data, {})

def test_web_users(self):
# This behavior is bad - data isn't fully scoped to domain
Expand All @@ -67,33 +56,18 @@ def test_web_users(self):
'commcare_profile': '',
'start': 'sometimes',
})
# Only the original domain was modified
self.assertEqual(web_user.get_user_data('ANOTHER_DOMAIN').to_dict(), {
'commcare_project': 'ANOTHER_DOMAIN',
'commcare_profile': '',
'start': 'sometimes', # whoops, domain 1 affects other domains!
})
web_user.save()
# at this point, user data has been initialized for each domain, and
# will no longer share a namespace
web_user = WebUser.get_by_user_id(web_user.user_id)
user_data = web_user.get_user_data(self.domain)['new_field'] = 'not_cross_domain'
self.assertEqual(web_user.get_user_data(self.domain).to_dict(), {
'commcare_project': self.domain,
'commcare_profile': '',
'start': 'sometimes',
'new_field': 'not_cross_domain',
})
self.assertEqual(web_user.get_user_data('ANOTHER_DOMAIN').to_dict(), {
'commcare_project': 'ANOTHER_DOMAIN',
'commcare_profile': '',
'start': 'sometimes',
})

def test_lazy_init_and_save(self):
# Mimic user created the old way, with data stored in couch
user = CommCareUser.create(self.domain, 'riggan', '***', None, None)
self.addCleanup(user.delete, self.domain, deleted_by=None)
user['user_data'] = {'favorite_color': 'purple'}
user['user_data'] = {'favorite_color': 'purple',
'start_date': '2023-01-01T00:00:00.000000Z'}
user.save()
with self.assertRaises(SQLUserData.DoesNotExist):
SQLUserData.objects.get(domain=self.domain, user_id=user.user_id)
Expand All @@ -102,6 +76,7 @@ def test_lazy_init_and_save(self):
self.assertEqual(user.get_user_data(self.domain)['favorite_color'], 'purple')
sql_data = SQLUserData.objects.get(domain=self.domain, user_id=user.user_id)
self.assertEqual(sql_data.data['favorite_color'], 'purple')
self.assertEqual(sql_data.data['start_date'], '2023-01-01T00:00:00.000000Z')

# Making a modification works immediately, but isn't persisted until user save
user.get_user_data(self.domain)['favorite_color'] = 'blue'
Expand All @@ -112,10 +87,6 @@ def test_lazy_init_and_save(self):
sql_data.refresh_from_db()
self.assertEqual(sql_data.data['favorite_color'], 'blue')

# The modification is also propagated back to couch for now
user_doc = CommCareUser.get_db().get(user.user_id)
self.assertEqual(user_doc['user_data']['favorite_color'], 'blue')

def test_get_users_without_user_data(self):
users_without_data = [
self.make_commcare_user(),
Expand Down
4 changes: 2 additions & 2 deletions corehq/apps/users/user_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ def __init__(self, raw_user_data, couch_user, domain, profile_id=None):
def lazy_init(cls, couch_user, domain):
# To be used during initial rollout - lazily create user_data objs from
# existing couch data
raw_user_data = couch_user.user_data.copy()
raw_user_data = couch_user.to_json().get('user_data', {}).copy()
raw_user_data.pop(COMMCARE_PROJECT, None)
profile_id = raw_user_data.pop(PROFILE_SLUG, None)
sql_data, _ = SQLUserData.objects.get_or_create(
user_id=couch_user.user_id,
domain=domain,
defaults={
'data': couch_user.user_data,
'data': raw_user_data,
'django_user': couch_user.get_django_user(),
'profile_id': profile_id,
}
Expand Down

0 comments on commit 1b67098

Please sign in to comment.