def test_subsequent_syncs_when_job_complete(self): # First sync, return a timout. Ensure that the async_task_id gets set async_restore_task_id_cache = AsyncRestoreTaskIdCache( domain=self.domain, user_id=self.user.user_id, sync_log_id=None, device_id=None, ) with mock.patch('casexml.apps.phone.restore.get_async_restore_payload') as task: delay = mock.MagicMock() delay.id = 'random_task_id' delay.get = mock.MagicMock(side_effect=TimeoutError()) # task not finished task.delay.return_value = delay restore_config = self._restore_config(async=True) initial_payload = restore_config.get_payload() self.assertIsNotNone(async_restore_task_id_cache.get_value()) self.assertIsInstance(initial_payload, AsyncRestoreResponse) # new synclog should not have been created self.assertIsNone(restore_config.restore_state.current_sync_log) # Second sync, don't timeout (can't use AsyncResult in tests, so mock # the return value). restore_response = mock.MagicMock(return_value=RestoreResponse(None)) with mock.patch.object(AsyncResult, 'get', restore_response) as get_result: with mock.patch.object(AsyncResult, 'status', ASYNC_RESTORE_SENT): subsequent_restore = self._restore_config(async=True) self.assertIsNotNone(async_restore_task_id_cache.get_value()) subsequent_restore.get_payload() # if the task actually ran, the cache should now not have the task id, # however, the task is not run in this test. See `test_completed_task_deletes_cache` # self.assertIsNone(restore_config.cache.get(cache_id)) get_result.assert_called_with(timeout=1)
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 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 """ restore_user = request.couch_user with RestoreContent(restore_user.username) as content: content.append(get_registration_element(restore_user)) for case in get_case_and_descendants(domain, case_id): # Formplayer will be creating these cases for the first time, so # include create blocks content.append(get_case_element(case, ('create', 'update'), V2)) payload = content.get_fileobj() return RestoreResponse(payload).get_http_response()
from corehq.apps.reports.view_helpers import get_case_hierarchy return [ c for c in get_case_hierarchy(case, {})['case_list'] if not c.closed ] @formplayer_auth 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 """ try: case = CaseAccessors(domain).get_case(case_id) if case.domain != domain or case.is_deleted: raise Http404 except CaseNotFound: raise Http404 with RestoreContent('Case[{}]'.format(case_id)) as content: content.append(get_registration_element_for_case(case)) for case in get_case_hierarchy_for_restore(case): # Formplayer will be creating these cases for the first time, so # include create blocks content.append(get_case_element(case, ('create', 'update'), V2)) payload = content.get_fileobj() return RestoreResponse(payload).get_http_response()