def _get_restore_state(restore_user): from casexml.apps.phone.restore import RestoreState from casexml.apps.phone.restore import RestoreParams params = RestoreParams(version=V2) return RestoreState(restore_user.project, restore_user, params, is_async=False, overwrite_cache=False)
def call_fixture_generator(gen, restore_user, project=None, last_sync=None, app=None, device_id=''): """ Convenience function for use in unit tests TODO move to MockDevice since most arguments are members of that class """ from casexml.apps.phone.restore import RestoreState from casexml.apps.phone.restore import RestoreParams from corehq.apps.domain.models import Domain params = RestoreParams(version=V2, app=app, device_id=device_id) restore_state = RestoreState( project or Domain(name=restore_user.domain), restore_user, params, is_async=False, overwrite_cache=False ) if last_sync: params.sync_log_id = last_sync._id restore_state._last_sync_log = last_sync return gen(restore_state)
def migration_restore(request, domain, case_id): """Restore endpoint used in bulk case migrations Accepts the provided case_id and returns a restore for the user containing: * Registration block * The passed in case and its full network of cases """ domain_obj = Domain.get_by_name(domain) restore_user = request.couch_user restore_params = RestoreParams(device_id="case_migration", version=V2) restore_state = RestoreState(domain_obj, restore_user.to_ota_restore_user(domain), restore_params) restore_state.start_sync() timing_context = TimingContext('migration-restore-{}-{}'.format( domain, restore_user.username)) case_ids = get_related_case_ids(domain, case_id) with RestoreContent(restore_user.username) as content: content.append(get_registration_element(restore_user)) sync_payload = CleanOwnerSyncPayload(timing_context, case_ids, restore_state) sync_payload.extend_response(content) payload = content.get_fileobj() restore_state.finish_sync() return RestoreResponse(payload).get_http_response()
def testUserRestoreWithCase(self): file_path = os.path.join(os.path.dirname(__file__), "data", "create_short.xml") with open(file_path, "rb") as f: xml_data = f.read() form = post_xform_to_couch(xml_data, domain=self.project.name) # implicit length assertion [newcase] = process_cases(form) user = dummy_user() self.assertEqual(1, len(list( BatchedCaseSyncOperation(RestoreState(self.project, user, RestoreParams())).get_all_case_updates() ))) expected_case_block = """ <case> <case_id>asdf</case_id> <date_modified>2010-06-29T13:42:50.000000Z</date_modified> <create> <case_type_id>test_case_type</case_type_id> <user_id>foo</user_id> <case_name>test case name</case_name> <external_id>someexternal</external_id> </create> </case>""" check_xml_line_by_line(self, expected_case_block, xml.get_case_xml(newcase, [case_const.CASE_ACTION_CREATE, case_const.CASE_ACTION_UPDATE])) # check v2 expected_v2_case_block = """ <case case_id="asdf" date_modified="2010-06-29T13:42:50.000000Z" user_id="foo" xmlns="http://commcarehq.org/case/transaction/v2" > <create> <case_type>test_case_type</case_type> <case_name>test case name</case_name> <owner_id>foo</owner_id> </create> <update> <external_id>someexternal</external_id> </update> </case>""" check_xml_line_by_line( self, expected_v2_case_block, xml.get_case_xml( newcase, [case_const.CASE_ACTION_CREATE, case_const.CASE_ACTION_UPDATE], version="2.0", ), ) restore_payload = generate_restore_payload( project=self.project, user=dummy_user(), items=True, ) sync_log_id = SyncLog.view( "phone/sync_logs_by_user", include_docs=True, reduce=False, ).one().get_id check_xml_line_by_line( self, dummy_restore_xml(sync_log_id, expected_case_block, items=4), restore_payload )
def _dummy_restore_state(force_restore_mode=None): return RestoreState( project=Domain(name='clean'), user=CommCareUser(username='******'), params=RestoreParams(force_restore_mode=force_restore_mode))