Exemplo n.º 1
0
class StateHashTest(TestCase):
    
    def setUp(self):
        delete_all_cases()
        delete_all_xforms()
        delete_all_sync_logs()

        self.user = User(user_id="state_hash", username="******",
                         password="******", date_joined=datetime(2011, 6, 9)) 
        
        # this creates the initial blank sync token in the database
        generate_restore_payload(self.user)
        [sync_log] = SyncLog.view("phone/sync_logs_by_user", include_docs=True, reduce=False).all()
        self.sync_log = sync_log
    
    def testEmpty(self):
        empty_hash = CaseStateHash(EMPTY_HASH)
        wrong_hash = CaseStateHash("thisisntright")
        self.assertEqual(empty_hash, self.sync_log.get_state_hash())
        response = generate_restore_response(self.user, self.sync_log.get_id, version=V2)
        self.assertEqual(200, response.status_code)
        
        try:
            response = generate_restore_payload(self.user, self.sync_log.get_id,
                                                version=V2, state_hash=str(wrong_hash))
            self.fail("Call to generate a payload with a bad hash should fail!")
        except BadStateException, e:
            self.assertEqual(empty_hash, e.expected)
            self.assertEqual(wrong_hash, e.actual)
            self.assertEqual(0, len(e.case_ids))

        response = generate_restore_response(self.user, self.sync_log.get_id, version=V2,
                                             state_hash=str(wrong_hash))
        self.assertEqual(412, response.status_code)
Exemplo n.º 2
0
def get_restore_response(couch_user, since=None, version='1.0', state=None):
    # not a view just a view util
    if not couch_user.is_commcare_user():
        response = HttpResponse("No linked chw found for %s" % couch_user.username)
        response.status_code = 401 # Authentication Failure
        return response

    return generate_restore_response(couch_user.to_casexml_user(), since,
                                     version, state)
Exemplo n.º 3
0
 def testEmpty(self):
     empty_hash = CaseStateHash(EMPTY_HASH)
     wrong_hash = CaseStateHash("thisisntright")
     self.assertEqual(empty_hash, self.sync_log.get_state_hash())
     response = generate_restore_response(self.user, self.sync_log.get_id, version=V2)
     self.assertEqual(200, response.status_code)
     
     try:
         response = generate_restore_payload(self.user, self.sync_log.get_id,
                                             version=V2, state_hash=str(wrong_hash))
         self.fail("Call to generate a payload with a bad hash should fail!")
     except BadStateException, e:
         self.assertEqual(empty_hash, e.expected)
         self.assertEqual(wrong_hash, e.actual)
         self.assertEqual(0, len(e.case_ids))
Exemplo n.º 4
0
def restore(request, domain):
    """
    We override restore because we have to supply our own 
    user model (and have the domain in the url)
    """

    user = request.user
    restore_id = request.GET.get('since')
    api_version = request.GET.get('version', "1.0")
    state_hash = request.GET.get('state')
    username = user.username
    couch_user = CouchUser.from_django_user(user)

    if not couch_user.is_commcare_user():
        response = HttpResponse("No linked chw found for %s" % username)
        response.status_code = 401  # Authentication Failure
        return response

    return generate_restore_response(couch_user.to_casexml_user(), restore_id,
                                     api_version, state_hash)
Exemplo n.º 5
0
def restore(request, domain):
    """
    We override restore because we have to supply our own 
    user model (and have the domain in the url)
    """
    
    user = request.user
    restore_id = request.GET.get('since')
    api_version = request.GET.get('version', "1.0")
    state_hash = request.GET.get('state')
    username = user.username
    couch_user = CouchUser.from_django_user(user)
    
    if not couch_user.is_commcare_user():
        response = HttpResponse("No linked chw found for %s" % username)
        response.status_code = 401 # Authentication Failure
        return response
    
    return generate_restore_response(couch_user.to_casexml_user(), restore_id, 
                                         api_version, state_hash)
    
Exemplo n.º 6
0
def restore(request):
    user = User.from_django_user(request.user)
    restore_id = request.GET.get('since')
    return generate_restore_response(user, restore_id)