def test_first_claim_none(self): """ get_first_claim should return None if not found """ claim = get_first_claims(DOMAIN, self.user.user_id, [self.host_case_id]) self.assertEqual(len(claim), 0)
def test_first_claim_one(self): """ get_first_claim should return one claim """ claim_case(DOMAIN, self.restore_user, self.host_case_id, host_type=self.host_case_type, host_name=self.host_case_name) claim = get_first_claims(DOMAIN, self.user.user_id, [self.host_case_id]) self.assertEqual(len(claim), 1)
def test_closed_claim(self): """ get_first_claim should return None if claim case is closed """ claim_id = claim_case(DOMAIN, self.restore_user, self.host_case_id, host_type=self.host_case_type, host_name=self.host_case_name) self._close_case(claim_id) first_claim = get_first_claims(DOMAIN, self.user.user_id, [self.host_case_id]) self.assertEqual(len(first_claim), 0)
def claim(request, domain): """ Allows a user to claim a case that they don't own. """ as_user = unquote(request.POST.get('commcare_login_as', '')) as_user_obj = CouchUser.get_by_username(as_user) if as_user else None restore_user = get_restore_user(domain, request.couch_user, as_user_obj) case_ids = request.POST.getlist('case_id') if not case_ids: return HttpResponse('A case_id is required', status=400) try: case_ids_already_claimed = get_first_claims(domain, restore_user.user_id, case_ids) case_ids_to_claim = { case for case in case_ids if case not in case_ids_already_claimed } except CaseNotFound as err: return HttpResponse(f'No cases claimed. Case IDs "{err}" not found', status=410) for case_id in case_ids_to_claim: claim_case(domain, restore_user, case_id, host_type=unquote(request.POST.get('case_type', '')), host_name=unquote(request.POST.get('case_name', '')), device_id=__name__ + ".claim") def phone_holds_all_cases(request): try: synclog = get_properly_wrapped_sync_log(request.last_sync_token) missing_case_ids_on_phone = set( case_ids) - synclog.case_ids_on_phone return not missing_case_ids_on_phone except MissingSyncLog: return False if not case_ids_to_claim and phone_holds_all_cases(request): return HttpResponse(status=204) return HttpResponse(status=201)
def test_get_first_claims_index_not_host(self): # create a claim case with the incorrect index identifier # method still find the case and recognise it as a claim case_block = CaseBlock(create=True, case_id=uuid4().hex, case_name="claim", case_type=CLAIM_CASE_TYPE, owner_id=self.user.user_id, index={ "not_host": IndexAttrs( case_type=self.host_case_type, case_id=self.host_case_id, relationship=CASE_INDEX_EXTENSION, ) }).as_xml() post_case_blocks([case_block], {'domain': DOMAIN}) first_claim = get_first_claims(DOMAIN, self.user.user_id, [self.host_case_id]) self.assertEqual(first_claim, {self.host_case_id})
def test_claim_index_deleted(self): """ get_first_claim should return None if claim case is closed """ claim_id = claim_case(DOMAIN, self.restore_user, self.host_case_id, host_type=self.host_case_type, host_name=self.host_case_name) # delete the case index case_block = CaseBlock(create=False, case_id=claim_id, index={ "host": (self.host_case_type, "") }).as_xml() post_case_blocks([case_block], {'domain': DOMAIN}) first_claim = get_first_claims(DOMAIN, self.user.user_id, [self.host_case_id]) self.assertEqual(len(first_claim), 0)