Ejemplo n.º 1
0
 def _invalidate_restore_payload_path_cache(self, xform, device_id):
     """invalidate cached initial restores"""
     restore_payload_path_cache = RestorePayloadPathCache(
         domain=self.domain,
         user_id=xform.user_id,
         sync_log_id=xform.last_sync_token,
         device_id=device_id,
     )
     restore_payload_path_cache.invalidate()
Ejemplo n.º 2
0
 def _invalidate_restore_payload_path_cache(self, xform, device_id):
     """invalidate cached initial restores"""
     restore_payload_path_cache = RestorePayloadPathCache(
         domain=self.domain,
         user_id=xform.user_id,
         sync_log_id=xform.last_sync_token,
         device_id=device_id,
     )
     restore_payload_path_cache.invalidate()
Ejemplo n.º 3
0
    def test_restore_in_progress_form_submitted_kills_old_jobs(self):
        """If the user submits a form somehow while a job is running, the job should be terminated
        """
        last_sync_token = '0a72d5a3c2ec53e85c1af27ee5717e0d'
        device_id = 'RSMCHBA8PJNQIGMONN2JZT6E'
        async_restore_task_id_cache = AsyncRestoreTaskIdCache(
            domain=self.domain,
            user_id=self.user.user_id,
            sync_log_id=last_sync_token,
            device_id=device_id,
        )
        restore_payload_path_cache = RestorePayloadPathCache(
            domain=self.domain,
            user_id=self.user.user_id,
            device_id=device_id,
            sync_log_id=last_sync_token,
        )
        async_restore_task_id = '0edecc20d89d6f4a09f2e992c0c24b5f'
        initial_sync_path = 'path/to/payload'
        restore_config = self._restore_config(async=True)
        # pretend we have a task running
        async_restore_task_id_cache.set_value(async_restore_task_id)
        restore_payload_path_cache.set_value(initial_sync_path)

        def submit_form(user_id, device_id, last_sync_token):
            form = """
            <data xmlns="http://openrosa.org/formdesigner/blah">
                <meta>
                    <userID>{user_id}</userID>
                    <deviceID>{device_id}</deviceID>
                </meta>
            </data>
            """
            submit_form_locally(
                form.format(user_id=user_id, device_id=device_id),
                self.domain,
                last_sync_token=last_sync_token,
            )

        with mock.patch('corehq.form_processor.submission_post.revoke_celery_task') as revoke:
            # with a different user in the same domain, task doesn't get killed
            submit_form(user_id="other_user", device_id='OTHERDEVICEID', last_sync_token='othersynctoken')
            self.assertFalse(revoke.called)
            self.assertEqual(async_restore_task_id_cache.get_value(), async_restore_task_id)
            self.assertEqual(restore_payload_path_cache.get_value(), initial_sync_path)

            # task gets killed when the user submits a form
            submit_form(user_id=self.user.user_id, device_id=device_id, last_sync_token=last_sync_token)
            revoke.assert_called_with(async_restore_task_id)
            self.assertIsNone(async_restore_task_id_cache.get_value())
            self.assertIsNone(restore_payload_path_cache.get_value())
Ejemplo n.º 4
0
 def initial_restore_payload_path_cache(self):
     return RestorePayloadPathCache(
         domain=self.domain,
         user_id=self.restore_user.user_id,
         sync_log_id='',
         device_id=self.params.device_id,
     )
Ejemplo n.º 5
0
 def restore_payload_path_cache(self):
     return RestorePayloadPathCache(
         domain=self.domain,
         user_id=self.restore_user.user_id,
         sync_log_id=self.sync_log._id if self.sync_log else '',
         device_id=self.params.device_id,
     )
Ejemplo n.º 6
0
def _cached_restore(testcase,
                    user,
                    restore_id="",
                    version=V2,
                    purge_restore_cache=False):
    """DEPRECATED use <MockDevice>.sync().cases"""
    assert not hasattr(testcase, 'restore_config'), testcase
    assert not hasattr(testcase, 'payload_string'), testcase

    if restore_id and purge_restore_cache:
        RestorePayloadPathCache(
            domain=user.domain,
            user_id=user.user_id,
            sync_log_id=restore_id,
            device_id=None,
        ).invalidate()

    testcase.restore_config = RestoreConfig(
        project=user.project,
        restore_user=user,
        params=RestoreParams(restore_id, version=version),
        **getattr(testcase, 'restore_options', {}))
    testcase.payload_string = testcase.restore_config.get_payload().as_string()
    try:
        yield
    finally:
        del testcase.restore_config, testcase.payload_string
Ejemplo n.º 7
0
 def has_cached_payload(self, version):
     """Check if a cached payload exists for this sync result"""
     return bool(RestorePayloadPathCache(
         domain=self.config.domain,
         user_id=self.config.restore_user.user_id,
         sync_log_id=self.restore_id,
         device_id=self.device.id,
     ).get_value())
Ejemplo n.º 8
0
 def test_restore_caches_cleared(self):
     sync_log_id = 'a8cac9222f42480764d6875c908040d5'
     device_id = 'CBNMP7XCGTIIAPCIMNI2KRGY'
     restore_payload_path_cache = RestorePayloadPathCache(
         domain=DOMAIN,
         user_id='user_id',
         sync_log_id=sync_log_id,
         device_id=device_id,
     )
     restore_payload_path_cache.set_value('test-thing')
     self.assertEqual(restore_payload_path_cache.get_value(), 'test-thing')
     self._submit_dummy_form(
         domain=DOMAIN,
         user_id='user_id',
         device_id=device_id,
         sync_log_id=sync_log_id,
     )
     self.assertIsNone(restore_payload_path_cache.get_value())
Ejemplo n.º 9
0
    def test_restore_in_progress_form_submitted_kills_old_jobs(self):
        """If the user submits a form somehow while a job is running, the job should be terminated
        """
        last_sync_token = '0a72d5a3c2ec53e85c1af27ee5717e0d'
        device_id = 'RSMCHBA8PJNQIGMONN2JZT6E'
        async_restore_task_id_cache = AsyncRestoreTaskIdCache(
            domain=self.domain,
            user_id=self.user.user_id,
            sync_log_id=last_sync_token,
            device_id=device_id,
        )
        restore_payload_path_cache = RestorePayloadPathCache(
            domain=self.domain,
            user_id=self.user.user_id,
            device_id=device_id,
            sync_log_id=last_sync_token,
        )
        async_restore_task_id = '0edecc20d89d6f4a09f2e992c0c24b5f'
        initial_sync_path = 'path/to/payload'
        restore_config = self._restore_config(async=True)
        # pretend we have a task running
        async_restore_task_id_cache.set_value(async_restore_task_id)
        restore_payload_path_cache.set_value(initial_sync_path)

        def submit_form(user_id, device_id, last_sync_token):
            form = """
            <data xmlns="http://openrosa.org/formdesigner/blah">
                <meta>
                    <userID>{user_id}</userID>
                    <deviceID>{device_id}</deviceID>
                </meta>
            </data>
            """
            submit_form_locally(
                form.format(user_id=user_id, device_id=device_id),
                self.domain,
                last_sync_token=last_sync_token,
            )

        with mock.patch(
                'corehq.form_processor.submission_post.revoke_celery_task'
        ) as revoke:
            # with a different user in the same domain, task doesn't get killed
            submit_form(user_id="other_user",
                        device_id='OTHERDEVICEID',
                        last_sync_token='othersynctoken')
            self.assertFalse(revoke.called)
            self.assertEqual(async_restore_task_id_cache.get_value(),
                             async_restore_task_id)
            self.assertEqual(restore_payload_path_cache.get_value(),
                             initial_sync_path)

            # task gets killed when the user submits a form
            submit_form(user_id=self.user.user_id,
                        device_id=device_id,
                        last_sync_token=last_sync_token)
            revoke.assert_called_with(async_restore_task_id)
            self.assertIsNone(async_restore_task_id_cache.get_value())
            self.assertIsNone(restore_payload_path_cache.get_value())
Ejemplo n.º 10
0
 def test_restore_caches_cleared(self):
     sync_log_id = 'a8cac9222f42480764d6875c908040d5'
     device_id = 'CBNMP7XCGTIIAPCIMNI2KRGY'
     restore_payload_path_cache = RestorePayloadPathCache(
         domain=DOMAIN,
         user_id='user_id',
         sync_log_id=sync_log_id,
         device_id=device_id,
     )
     restore_payload_path_cache.set_value('test-thing')
     self.assertEqual(restore_payload_path_cache.get_value(), 'test-thing')
     self._submit_dummy_form(
         domain=DOMAIN,
         user_id='user_id',
         device_id=device_id,
         sync_log_id=sync_log_id,
     )
     self.assertIsNone(restore_payload_path_cache.get_value())