def test_synced_before_and_after_bug_resolution_200(self): before = SimplifiedSyncLog( user_id=self.restore_user.user_id, date=datetime(2016, 7, 19, 18, 0) # synced before bug was introduced ) before.save() restore_config = RestoreConfig(project=self.project, restore_user=self.restore_user, params=RestoreParams( sync_log_id=before._id, version="2.0", ), cache_settings=RestoreCacheSettings()) response = restore_config.get_response() self.assertEqual(response.status_code, 200) after = SimplifiedSyncLog( user_id=self.restore_user.user_id, previous_log_id=before._id, date=datetime(2016, 7, 21, 19, 0) # after resolution ) after.save() restore_config = RestoreConfig(project=self.project, restore_user=self.restore_user, params=RestoreParams( sync_log_id=after._id, version="2.0", ), cache_settings=RestoreCacheSettings()) response = restore_config.get_response() self.assertEqual(response.status_code, 200)
def get_restore_response(domain, couch_user, since=None, version='1.0', state=None, items=False): # not a view just a view util if not couch_user.is_commcare_user(): return HttpResponse("No linked chw found for %s" % couch_user.username, status=401) # Authentication Failure elif domain != couch_user.domain: return HttpResponse("%s was not in the domain %s" % (couch_user.username, domain), status=401) project = Domain.get_by_name(domain) commtrack_settings = project.commtrack_settings stock_settings = commtrack_settings.get_ota_restore_settings( ) if commtrack_settings else None restore_config = RestoreConfig( couch_user.to_casexml_user(), since, version, state, caching_enabled=project.ota_restore_caching, items=items, stock_settings=stock_settings, ) return restore_config.get_response()
def get_restore_response(domain, couch_user, since=None, version='1.0', state=None, items=False, force_cache=False, cache_timeout=None, overwrite_cache=False, force_restore_mode=None): # not a view just a view util if not couch_user.is_commcare_user(): return HttpResponse("No linked chw found for %s" % couch_user.username, status=401) # Authentication Failure elif domain != couch_user.domain: return HttpResponse("%s was not in the domain %s" % (couch_user.username, domain), status=401) project = Domain.get_by_name(domain) restore_config = RestoreConfig( project=project, user=couch_user.to_casexml_user(), params=RestoreParams( sync_log_id=since, version=version, state_hash=state, include_item_count=items, force_restore_mode=force_restore_mode, ), cache_settings=RestoreCacheSettings( force_cache=force_cache, cache_timeout=cache_timeout, overwrite_cache=overwrite_cache ), ) return restore_config.get_response()
def get_restore_response(domain, couch_user, app_id=None, since=None, version='1.0', state=None, items=False, force_cache=False, cache_timeout=None, overwrite_cache=False, force_restore_mode=None): # not a view just a view util if not couch_user.is_commcare_user(): return HttpResponse("No linked chw found for %s" % couch_user.username, status=401) # Authentication Failure elif domain != couch_user.domain: return HttpResponse("%s was not in the domain %s" % (couch_user.username, domain), status=401) project = Domain.get_by_name(domain) app = get_app(domain, app_id) if app_id else None restore_config = RestoreConfig( project=project, user=couch_user.to_casexml_user(), params=RestoreParams( sync_log_id=since, version=version, state_hash=state, include_item_count=items, force_restore_mode=force_restore_mode, app=app, ), cache_settings=RestoreCacheSettings( force_cache=force_cache, cache_timeout=cache_timeout, overwrite_cache=overwrite_cache ), ) return restore_config.get_response()
def test_synced_during_and_after_bug_resolution_returns_200(self): during = SimplifiedSyncLog( user_id=self.restore_user.user_id, date=datetime(2016, 7, 19, 20, 0) # during bug ) during.save() after = SimplifiedSyncLog( user_id=self.restore_user.user_id, previous_log_id=during._id, date=datetime(2016, 7, 21, 19, 0) # after resolution ) after.save() restore_config = RestoreConfig( project=self.project, restore_user=self.restore_user, params=RestoreParams( sync_log_id=after._id, version="2.0", ), cache_settings=RestoreCacheSettings() ) response = restore_config.get_response() self.assertEqual(response.status_code, 200)
def get_restore_response(domain, couch_user, app_id=None, since=None, version='1.0', state=None, items=False, force_cache=False, cache_timeout=None, overwrite_cache=False, force_restore_mode=None, as_user=None, device_id=None, has_data_cleanup_privelege=False, openrosa_version=OPENROSA_DEFAULT_VERSION): # not a view just a view util is_permitted, message = is_permitted_to_restore( domain, couch_user, as_user, has_data_cleanup_privelege, ) if not is_permitted: return HttpResponse(message, status=401), None if couch_user.is_commcare_user() and couch_user.is_demo_user: # if user is in demo-mode, return demo restore return demo_user_restore_response(couch_user), None restore_user = get_restore_user(domain, couch_user, as_user) if not restore_user: return HttpResponse('Could not find user', status=404), None project = Domain.get_by_name(domain) app = get_app(domain, app_id) if app_id else None async_restore_enabled = (toggles.ASYNC_RESTORE.enabled(domain) and LooseVersion(openrosa_version) >= LooseVersion( OPENROSA_VERSION_MAP['ASYNC_RESTORE'])) restore_config = RestoreConfig( project=project, restore_user=restore_user, params=RestoreParams( sync_log_id=since, version=version, state_hash=state, include_item_count=items, app=app, device_id=device_id, ), cache_settings=RestoreCacheSettings(force_cache=force_cache or async_restore_enabled, cache_timeout=cache_timeout, overwrite_cache=overwrite_cache), async=async_restore_enabled) return restore_config.get_response(), restore_config.timing_context
def generate_restore_response(project, user, restore_id="", version=V1, state_hash="", items=False): config = RestoreConfig( project=project, user=user, params=RestoreParams( sync_log_id=restore_id, version=version, state_hash=state_hash, include_item_count=items ) ) return config.get_response()
def test_return_412_between_bug_dates(self): log = SimplifiedSyncLog(user_id=self.restore_user.user_id, date=datetime(2016, 7, 19, 19, 20)) log.save() restore_config = RestoreConfig(project=self.project, restore_user=self.restore_user, params=RestoreParams( sync_log_id=log._id, version="2.0", ), cache_settings=RestoreCacheSettings()) response = restore_config.get_response() self.assertEqual(response.status_code, 412)
def get_restore_response(domain, couch_user, app_id=None, since=None, version='1.0', state=None, items=False, force_cache=False, cache_timeout=None, overwrite_cache=False, force_restore_mode=None, as_user=None, has_data_cleanup_privelege=False, openrosa_version=OPENROSA_DEFAULT_VERSION): # not a view just a view util is_permitted, message = is_permitted_to_restore( domain, couch_user, as_user, has_data_cleanup_privelege, ) if not is_permitted: return HttpResponse(message, status=401), None if couch_user.is_commcare_user() and couch_user.is_demo_user: # if user is in demo-mode, return demo restore return demo_user_restore_response(couch_user), None restore_user = get_restore_user(domain, couch_user, as_user) if not restore_user: return HttpResponse('Could not find user', status=404), None project = Domain.get_by_name(domain) app = get_app(domain, app_id) if app_id else None async_restore_enabled = ( toggles.ASYNC_RESTORE.enabled(domain) and LooseVersion(openrosa_version) >= LooseVersion(OPENROSA_VERSION_MAP['ASYNC_RESTORE']) ) restore_config = RestoreConfig( project=project, restore_user=restore_user, params=RestoreParams( sync_log_id=since, version=version, state_hash=state, include_item_count=items, app=app, ), cache_settings=RestoreCacheSettings( force_cache=force_cache or async_restore_enabled, cache_timeout=cache_timeout, overwrite_cache=overwrite_cache ), async=async_restore_enabled ) return restore_config.get_response(), restore_config.timing_context
def get_restore_response(domain, couch_user, since=None, version='1.0', state=None): # not a view just a view util if not couch_user.is_commcare_user(): return HttpResponse("No linked chw found for %s" % couch_user.username, status=401) # Authentication Failure elif domain != couch_user.domain: return HttpResponse("%s was not in the domain %s" % (couch_user.username, domain), status=401) project = Domain.get_by_name(domain) restore_config = RestoreConfig( couch_user.to_casexml_user(), since, version, state, caching_enabled=project.ota_restore_caching, ) return restore_config.get_response()
def get_restore_response(domain, couch_user, since=None, version='1.0', state=None, items=False): # not a view just a view util if not couch_user.is_commcare_user(): return HttpResponse("No linked chw found for %s" % couch_user.username, status=401) # Authentication Failure elif domain != couch_user.domain: return HttpResponse("%s was not in the domain %s" % (couch_user.username, domain), status=401) project = Domain.get_by_name(domain) commtrack_settings = project.commtrack_settings stock_settings = commtrack_settings.get_ota_restore_settings() if commtrack_settings else None restore_config = RestoreConfig( couch_user.to_casexml_user(), since, version, state, items=items, stock_settings=stock_settings, domain=project ) return restore_config.get_response()
def get_restore_response(domain, couch_user, since=None, version='1.0', state=None, items=False, force_cache=False, cache_timeout=None, overwrite_cache=False): # not a view just a view util if not couch_user.is_commcare_user(): return HttpResponse("No linked chw found for %s" % couch_user.username, status=401) # Authentication Failure elif domain != couch_user.domain: return HttpResponse("%s was not in the domain %s" % (couch_user.username, domain), status=401) project = Domain.get_by_name(domain) restore_config = RestoreConfig( couch_user.to_casexml_user(), since, version, state, items=items, domain=project, force_cache=force_cache, cache_timeout=cache_timeout, overwrite_cache=overwrite_cache ) return restore_config.get_response()
def get_restore_response(domain, couch_user, app_id=None, since=None, version='1.0', state=None, items=False, force_cache=False, cache_timeout=None, overwrite_cache=False, as_user=None, device_id=None, user_id=None, openrosa_version=None, case_sync=None): """ :param domain: Domain being restored from :param couch_user: User performing restore :param app_id: App ID of the app making the request :param since: ID of current sync log used to generate incremental sync :param version: Version of the sync response required :param state: Hash value of the current database of cases on the device for consistency checking :param items: Include item count if True :param force_cache: Force response to be cached :param cache_timeout: Override the default cache timeout of 1 hour. :param overwrite_cache: Ignore cached response if True :param as_user: Username of user to generate restore for (if different from current user) :param device_id: ID of device performing restore :param user_id: ID of user performing restore (used in case of deleted user with same username) :param openrosa_version: :param case_sync: Override default case sync algorithm :return: Tuple of (http response, timing context or None) """ if user_id and user_id != couch_user.user_id: # sync with a user that has been deleted but a new # user was created with the same username and password from couchforms.openrosa_response import get_simple_response_xml from couchforms.openrosa_response import ResponseNature response = get_simple_response_xml( 'Attempt to sync with invalid user.', ResponseNature.OTA_RESTORE_ERROR) return HttpResponse(response, content_type="text/xml; charset=utf-8", status=412), None is_demo_restore = couch_user.is_commcare_user() and couch_user.is_demo_user if is_demo_restore: # if user is in demo-mode, return demo restore return demo_user_restore_response(couch_user), None uses_login_as = bool(as_user) as_user_obj = CouchUser.get_by_username(as_user) if uses_login_as else None if uses_login_as and not as_user_obj: msg = _('Invalid restore as user {}').format(as_user) return HttpResponse(msg, status=401), None is_permitted, message = is_permitted_to_restore( domain, couch_user, as_user_obj, ) if not is_permitted: return HttpResponse(message, status=401), None restore_user = get_restore_user(domain, couch_user, as_user_obj) if not restore_user: return HttpResponse('Could not find user', status=404), None project = Domain.get_by_name(domain) async_restore_enabled = (toggles.ASYNC_RESTORE.enabled(domain) and openrosa_version and LooseVersion(openrosa_version) >= LooseVersion( OPENROSA_VERSION_MAP['ASYNC_RESTORE'])) app = get_app_cached(domain, app_id) if app_id else None restore_config = RestoreConfig( project=project, restore_user=restore_user, params=RestoreParams( sync_log_id=since, version=version, state_hash=state, include_item_count=items, app=app, device_id=device_id, openrosa_version=openrosa_version, ), cache_settings=RestoreCacheSettings(force_cache=force_cache or async_restore_enabled, cache_timeout=cache_timeout, overwrite_cache=overwrite_cache), is_async=async_restore_enabled, case_sync=case_sync, ) return restore_config.get_response(), restore_config.timing_context
def generate_restore_response(user, restore_id="", version=V1, state_hash="", items=False): config = RestoreConfig(user, restore_id, version, state_hash, items=items) return config.get_response()
def get_restore_response(domain, couch_user, app_id=None, since=None, version='1.0', state=None, items=False, force_cache=False, cache_timeout=None, overwrite_cache=False, force_restore_mode=None, as_user=None, device_id=None, user_id=None, has_data_cleanup_privelege=False, openrosa_version=OPENROSA_DEFAULT_VERSION): if user_id and user_id != couch_user.user_id: # sync with a user that has been deleted but a new # user was created with the same username and password from couchforms.openrosa_response import get_simple_response_xml from couchforms.openrosa_response import ResponseNature response = get_simple_response_xml( 'Attempt to sync with invalid user.', ResponseNature.OTA_RESTORE_ERROR) return HttpResponse(response, content_type="text/xml; charset=utf-8", status=412), None # not a view just a view util is_permitted, message = is_permitted_to_restore( domain, couch_user, as_user, has_data_cleanup_privelege, ) if not is_permitted: return HttpResponse(message, status=401), None is_demo_restore = couch_user.is_commcare_user() and couch_user.is_demo_user is_enikshay = toggles.ENIKSHAY.enabled(domain) if is_enikshay: couch_restore_user = couch_user if not is_demo_restore and as_user is not None: couch_restore_user = CouchUser.get_by_username(as_user) update_device_id(couch_restore_user, device_id) if is_demo_restore: # if user is in demo-mode, return demo restore return demo_user_restore_response(couch_user), None restore_user = get_restore_user(domain, couch_user, as_user) if not restore_user: return HttpResponse('Could not find user', status=404), None project = Domain.get_by_name(domain) app = get_app(domain, app_id) if app_id else None async_restore_enabled = (toggles.ASYNC_RESTORE.enabled(domain) and LooseVersion(openrosa_version) >= LooseVersion( OPENROSA_VERSION_MAP['ASYNC_RESTORE'])) restore_config = RestoreConfig( project=project, restore_user=restore_user, params=RestoreParams( sync_log_id=since, version=version, state_hash=state, include_item_count=items, app=app, device_id=device_id, ), cache_settings=RestoreCacheSettings(force_cache=force_cache or async_restore_enabled, cache_timeout=cache_timeout, overwrite_cache=overwrite_cache), async=async_restore_enabled) return restore_config.get_response(), restore_config.timing_context
def get_restore_response(domain, couch_user, app_id=None, since=None, version='1.0', state=None, items=False, force_cache=False, cache_timeout=None, overwrite_cache=False, as_user=None, device_id=None, user_id=None, openrosa_version=None, case_sync=None): """ :param domain: Domain being restored from :param couch_user: User performing restore :param app_id: App ID of the app making the request :param since: ID of current sync log used to generate incremental sync :param version: Version of the sync response required :param state: Hash value of the current database of cases on the device for consistency checking :param items: Include item count if True :param force_cache: Force response to be cached :param cache_timeout: Override the default cache timeout of 1 hour. :param overwrite_cache: Ignore cached response if True :param as_user: Username of user to generate restore for (if different from current user) :param device_id: ID of device performing restore :param user_id: ID of user performing restore (used in case of deleted user with same username) :param openrosa_version: :param case_sync: Override default case sync algorithm :return: Tuple of (http response, timing context or None) """ if user_id and user_id != couch_user.user_id: # sync with a user that has been deleted but a new # user was created with the same username and password from couchforms.openrosa_response import get_simple_response_xml from couchforms.openrosa_response import ResponseNature response = get_simple_response_xml( 'Attempt to sync with invalid user.', ResponseNature.OTA_RESTORE_ERROR ) return HttpResponse(response, content_type="text/xml; charset=utf-8", status=412), None is_demo_restore = couch_user.is_commcare_user() and couch_user.is_demo_user if is_demo_restore: # if user is in demo-mode, return demo restore return demo_user_restore_response(couch_user), None uses_login_as = bool(as_user) as_user_obj = CouchUser.get_by_username(as_user) if uses_login_as else None if uses_login_as and not as_user_obj: msg = _('Invalid restore as user {}').format(as_user) return HttpResponse(msg, status=401), None is_permitted, message = is_permitted_to_restore( domain, couch_user, as_user_obj, ) if not is_permitted: return HttpResponse(message, status=401), None restore_user = get_restore_user(domain, couch_user, as_user_obj) if not restore_user: return HttpResponse('Could not find user', status=404), None project = Domain.get_by_name(domain) async_restore_enabled = ( toggles.ASYNC_RESTORE.enabled(domain) and openrosa_version and LooseVersion(openrosa_version) >= LooseVersion(OPENROSA_VERSION_MAP['ASYNC_RESTORE']) ) app = get_app_cached(domain, app_id) if app_id else None restore_config = RestoreConfig( project=project, restore_user=restore_user, params=RestoreParams( sync_log_id=since, version=version, state_hash=state, include_item_count=items, app=app, device_id=device_id, openrosa_version=openrosa_version, ), cache_settings=RestoreCacheSettings( force_cache=force_cache or async_restore_enabled, cache_timeout=cache_timeout, overwrite_cache=overwrite_cache ), is_async=async_restore_enabled, case_sync=case_sync, ) return restore_config.get_response(), restore_config.timing_context
def get_restore_response(domain, couch_user, app_id=None, since=None, version='1.0', state=None, items=False, force_cache=False, cache_timeout=None, overwrite_cache=False, as_user=None, device_id=None, user_id=None, openrosa_version=None, case_sync=None): if user_id and user_id != couch_user.user_id: # sync with a user that has been deleted but a new # user was created with the same username and password from couchforms.openrosa_response import get_simple_response_xml from couchforms.openrosa_response import ResponseNature response = get_simple_response_xml( 'Attempt to sync with invalid user.', ResponseNature.OTA_RESTORE_ERROR) return HttpResponse(response, content_type="text/xml; charset=utf-8", status=412), None is_demo_restore = couch_user.is_commcare_user() and couch_user.is_demo_user if is_demo_restore: # if user is in demo-mode, return demo restore return demo_user_restore_response(couch_user), None uses_login_as = bool(as_user) as_user_obj = CouchUser.get_by_username(as_user) if uses_login_as else None if uses_login_as and not as_user_obj: msg = _('Invalid restore as user {}').format(as_user) return HttpResponse(msg, status=401), None is_permitted, message = is_permitted_to_restore( domain, couch_user, as_user_obj, ) if not is_permitted: return HttpResponse(message, status=401), None restore_user = get_restore_user(domain, couch_user, as_user_obj) if not restore_user: return HttpResponse('Could not find user', status=404), None project = Domain.get_by_name(domain) async_restore_enabled = (toggles.ASYNC_RESTORE.enabled(domain) and openrosa_version and LooseVersion(openrosa_version) >= LooseVersion( OPENROSA_VERSION_MAP['ASYNC_RESTORE'])) app = get_app_cached(domain, app_id) if app_id else None restore_config = RestoreConfig( project=project, restore_user=restore_user, params=RestoreParams( sync_log_id=since, version=version, state_hash=state, include_item_count=items, app=app, device_id=device_id, openrosa_version=openrosa_version, ), cache_settings=RestoreCacheSettings(force_cache=force_cache or async_restore_enabled, cache_timeout=cache_timeout, overwrite_cache=overwrite_cache), async=async_restore_enabled, case_sync=case_sync, ) return restore_config.get_response(), restore_config.timing_context