Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/es/web-user-locations' into auto…
Browse files Browse the repository at this point in the history
…staging
  • Loading branch information
orangejenny committed Apr 16, 2024
2 parents eb1d215 + 311663a commit 7e935f4
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 19 deletions.
10 changes: 9 additions & 1 deletion corehq/apps/callcenter/sync_usercase.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import uuid
from collections import namedtuple
from itertools import chain
from lxml import etree

from django.core.cache import cache

from lxml import etree

from casexml.apps.case.mock import CaseBlock
from dimagi.utils.couch import CriticalSection

Expand All @@ -14,6 +15,7 @@
from corehq.apps.export.tasks import add_inferred_export_properties
from corehq.apps.hqcase.utils import submit_case_blocks
from corehq.apps.locations.models import SQLLocation
from corehq.apps.users.util import user_location_data
from corehq.form_processor.models import CommCareCase
from corehq.toggles import USH_USERCASES_FOR_WEB_USERS

Expand Down Expand Up @@ -133,6 +135,12 @@ def valid_element_name(name):
# remove any keys that aren't valid XML element names
fields = {k: v for k, v in user.get_user_data(domain).items() if
valid_element_name(k)}

if user.is_web_user():
fields['commcare_location_id'] = user.get_location_id(domain)
fields['commcare_location_ids'] = user_location_data(user.get_location_ids(domain))
fields['commcare_primary_case_sharing_id'] = user.get_location_id(domain)

# language or phone_number can be null and will break
# case submission
fields.update({
Expand Down
26 changes: 25 additions & 1 deletion corehq/apps/callcenter/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,18 @@
from corehq.apps.domain.signals import commcare_domain_post_save
from corehq.apps.es.domains import domain_adapter
from corehq.apps.es.tests.utils import es_test
from corehq.apps.locations.models import LocationType
from corehq.apps.locations.tests.util import make_loc
from corehq.apps.user_importer.importer import (
create_or_update_commcare_users_and_groups,
)
from corehq.apps.user_importer.models import UserUploadRecord
from corehq.apps.users.models import CommCareUser
from corehq.apps.users.models import CommCareUser, WebUser
from corehq.apps.users.util import format_username
from corehq.apps.users.views.mobile.custom_data_fields import UserFieldsView
from corehq.form_processor.models import CommCareCase, XFormInstance
from corehq.util.context_managers import drop_connected_signals
from corehq.util.test_utils import flag_enabled

TEST_DOMAIN = 'cc-util-test'
CASE_TYPE = 'cc-flw'
Expand Down Expand Up @@ -216,6 +219,9 @@ def _get_user_case(self, user_id=None):


class CallCenterUtilsUsercaseTests(TestCase):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

@classmethod
def setUpClass(cls):
super().setUpClass()
Expand Down Expand Up @@ -393,6 +399,24 @@ def test_bulk_upload_usercases(self):
self.assertEqual(new_user_case.owner_id, new_user.get_id)
self.assertEqual(1, len(new_user_case.xform_ids))

@flag_enabled('USH_USERCASES_FOR_WEB_USERS')
def test_web_user_location_fields_sync(self):
self.user.save()
web_user = WebUser.create(TEST_DOMAIN, 'user3', '***', None, None)
self.addCleanup(web_user.delete, TEST_DOMAIN, deleted_by=None)
lt = LocationType.objects.create(
domain=TEST_DOMAIN, name='lt2'
)
self.loc1 = make_loc('loc1', type=lt.name, domain=TEST_DOMAIN)
self.loc2 = make_loc('loc2', type=lt.name, domain=TEST_DOMAIN)
web_user.set_location(TEST_DOMAIN, self.loc1)
web_user.add_to_assigned_locations(TEST_DOMAIN, self.loc2)
case_json = CommCareCase.objects.get_case_by_external_id(TEST_DOMAIN,
web_user._id, USERCASE_TYPE).case_json
self.assertEqual(case_json['commcare_location_id'], self.loc1.location_id)
self.assertEqual(case_json['commcare_location_ids'], self.loc1.location_id + ' ' + self.loc2.location_id)
self.assertEqual(case_json['commcare_primary_case_sharing_id'], self.loc1.location_id)


class DomainTimezoneTests(SimpleTestCase):

Expand Down
8 changes: 8 additions & 0 deletions corehq/apps/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2603,6 +2603,14 @@ def get_location(self, domain):
def get_usercase_by_domain(self, domain):
return CommCareCase.objects.get_case_by_external_id(domain, self._id, USERCASE_TYPE)

def get_user_session_data(self, domain):
# TODO can we do this for both types of users and remove the fields from user data?
session_data = super(WebUser, self).get_user_session_data(domain)
session_data['commcare_location_id'] = self.get_location_id(domain)
session_data['commcare_location_ids'] = user_location_data(self.get_location_ids(domain))
session_data['commcare_primary_case_sharing_id'] = self.get_location_id(domain)
return session_data


class FakeUser(WebUser):
"""
Expand Down
13 changes: 11 additions & 2 deletions corehq/ex-submodules/casexml/apps/phone/tests/dummy.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
DUMMY_PASSWORD = "changeme"
DUMMY_PROJECT = "domain"

LOCATION_IDS_DATA_FIELDS = """
<data key="commcare_location_id"/>
<data key="commcare_location_ids"/>"""
COMMCARE_PRIMARY_CASE_SHARING_ID = """
<data key="commcare_primary_case_sharing_id"/>"""


def dummy_user_xml(user=None):
username = user.username if user else DUMMY_USERNAME
Expand All @@ -24,8 +30,8 @@ def dummy_user_xml(user=None):
<date>{}</date>
<user_data>
<data key="commcare_first_name"/>
<data key="commcare_last_name"/>
<data key="commcare_phone_number"/>
<data key="commcare_last_name"/>{}
<data key="commcare_phone_number"/>{}
<data key="commcare_profile"/>
<data key="commcare_project">{}</data>
<data key="commcare_user_type">{}</data>
Expand All @@ -36,10 +42,13 @@ def dummy_user_xml(user=None):
password,
user_id,
date_to_xml_string(date_joined),
LOCATION_IDS_DATA_FIELDS if user_type == 'web' else '',
COMMCARE_PRIMARY_CASE_SHARING_ID if user_type == 'web' else '',
project,
user_type,
)


DUMMY_RESTORE_XML_TEMPLATE = ("""
<OpenRosaResponse xmlns="http://openrosa.org/http/response"%(items_xml)s>
<message nature="ota_restore_success">%(message)s</message>
Expand Down
51 changes: 36 additions & 15 deletions corehq/ex-submodules/casexml/apps/phone/tests/test_ota_restore.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os
import uuid
from datetime import date

from django.test import TestCase

Expand All @@ -14,6 +13,8 @@
delete_all_sync_logs,
delete_all_xforms,
)
from corehq.apps.locations.models import LocationType
from corehq.apps.locations.tests.util import make_loc
from casexml.apps.phone import xml
from casexml.apps.phone.models import SyncLogSQL, properly_wrap_sync_log
from casexml.apps.phone.restore import CachedResponse
Expand All @@ -35,6 +36,14 @@ def get_registration_xml(restore_user):
return xml.tostring(xml.get_registration_element(restore_user)).decode('utf-8')


def _expected_payload(key, val):
if val is None:
expected = f'<data key="{key}"/>'
else:
expected = f'<data key="{key}">{val}</data>'
return expected


class SimpleOtaRestoreTest(TestCase):

def setUp(self):
Expand Down Expand Up @@ -62,18 +71,10 @@ def test_name_and_number(self):
phone_number='555555',
)
payload = get_registration_xml(user)

def assertRegistrationData(key, val):
if val is None:
expected = f'<data key="{key}"/>'
else:
expected = f'<data key="{key}">{val}</data>'
self.assertIn(expected, payload)

assertRegistrationData("commcare_first_name", "mclovin")
assertRegistrationData("commcare_last_name", None)
assertRegistrationData("commcare_phone_number", "555555")
assertRegistrationData("commcare_user_type", "commcare")
self.assertIn(_expected_payload("commcare_first_name", "mclovin"), payload)
self.assertIn(_expected_payload("commcare_last_name", None), payload)
self.assertIn(_expected_payload("commcare_phone_number", "555555"), payload)
self.assertIn(_expected_payload("commcare_user_type", "commcare"), payload)


class BaseOtaRestoreTest(TestCase, TestFileMixin):
Expand Down Expand Up @@ -285,8 +286,8 @@ def get_all_syncslogs():
)
all_sync_logs = get_all_syncslogs()
[even_latest_log] = [log for log in all_sync_logs
if log.get_id != sync_log_id and
log.get_id != latest_log.get_id]
if log.get_id != sync_log_id
and log.get_id != latest_log.get_id]

# case block should come back
expected_sync_restore_payload = dummy_restore_xml(
Expand Down Expand Up @@ -385,3 +386,23 @@ def setUp(self):
super(WebUserOtaRestoreTest, self).setUp()
delete_all_users()
self.restore_user = create_restore_user(self.project.name, is_mobile_user=False)

def test_location_ids_in_restore(self):
web_user = self.restore_user._couch_user
domain = self.project.name

# Location setup
lt = LocationType.objects.create(
domain=domain, name='lt2'
)
self.loc1 = make_loc('loc1', type=lt.name, domain=domain)
self.loc2 = make_loc('loc2', type=lt.name, domain=domain)
web_user.set_location(domain, self.loc1)
web_user.add_to_assigned_locations(domain, self.loc2)

payload = get_registration_xml(self.restore_user)

self.assertIn(_expected_payload("commcare_location_id", self.loc1.location_id), payload)
self.assertIn(_expected_payload("commcare_location_ids",
' '.join([self.loc1.location_id, self.loc2.location_id])), payload)
self.assertIn(_expected_payload("commcare_primary_case_sharing_id", self.loc1.location_id), payload)

0 comments on commit 7e935f4

Please sign in to comment.