Exemplo n.º 1
0
    def test_initial_restore(self, config):
        user_id = str(uuid4())
        case_attrs = {
            'create': True,
            'user_id': user_id,
            'owner_id': user_id,
            'case_type': 'duck',
            'update': {'identity': 'mallard'}
        }
        factory = CaseFactory(
            config.receiver_url,
            domain=config.domain,
            form_extras={
                'user_id': user_id,
            }
        )
        [case_block] = factory.create_or_update_cases([
            CaseStructure(attrs=case_attrs),
        ])

        restore_payload = get_restore_payload(config.restore_url, config.domain, dummy_user(user_id))
        synclog_id = synclog_id_from_restore_payload(restore_payload)
        case_xml = case_block.as_string()
        check_xml_line_by_line(
            dummy_restore_xml(dummy_user(user_id), synclog_id, case_xml=case_xml, items=4),
            restore_payload,
        )
Exemplo n.º 2
0
    def test_wrong_state_hash(self, config):
        user_id = str(uuid4())
        response = get_restore_response(config.restore_url, config.domain, dummy_user(user_id))
        assert response.status_code == 200

        synclog_id = synclog_id_from_restore_payload(
            response.text
        )

        response = get_restore_response(config.restore_url, config.domain, dummy_user(user_id),
                                        since=synclog_id, state_hash='ccsh:wrong')
        assert response.status_code == 412
        assert message_nature_from_restore_payload(response.text) == NATURE_OTA_RESTORE_ERROR
Exemplo n.º 3
0
    def test_user_restore_with_case(self, testapp, client):
        with testapp.app_context():
            factory = CaseFactory(
                self._get_backend(),
                client,
                domain=DOMAIN,
                case_defaults={
                    'user_id': self.user_id,
                    'owner_id': self.user_id,
                    'case_type': 'duck',
                }
            )
            new_case, = factory.create_or_update_case(
                CaseStructure(self.case_id, attrs={
                    'create': True,
                    'case_name': 'Fish',
                    'update': {'last_name': 'Mooney'}})
            )

        user = dummy_user(self.user_id)
        restore_payload = generate_restore_response(client, DOMAIN, user, self._get_restore_url_snippet())

        synclog = self._get_one_synclog()

        case_xml = xml.get_case_xml(
            new_case, [
                const.CASE_ACTION_CREATE,
                const.CASE_ACTION_UPDATE],
            version=V2)

        check_xml_line_by_line(
            dummy_restore_xml(user, synclog.id, case_xml=case_xml, items=4),
            restore_payload,
        )
Exemplo n.º 4
0
    def test_user_restore(self, testapp, client):
        assert 0 == len(self._get_all_synclogs())

        user = dummy_user(self.user_id)
        restore_payload = generate_restore_response(client, DOMAIN, user, self._get_restore_url_snippet())
        synclogs = self._get_all_synclogs()
        assert len(synclogs) == 1
        synclog = synclogs[0]
        check_xml_line_by_line(
            dummy_restore_xml(user, synclog.id, items=3),
            restore_payload,
        )
Exemplo n.º 5
0
    def test_mismatch(self, config):
        user_id = str(uuid4())
        synclog_id = synclog_id_from_restore_payload(
            get_restore_payload(config.restore_url, config.domain, dummy_user(user_id))
        )

        c1 = CaseBlock(case_id="690ad3bd49ba4eef9ca839681dbd86b9", create=True, user_id=user_id,
                       owner_id=user_id)
        c2 = CaseBlock(case_id="e66c36c673a24319832c62ed806096e4", create=True, user_id=user_id,
                       owner_id=user_id)
        result = post_case_blocks(config.receiver_url, [c1, c2], domain=config.domain,
                         form_extras={'headers': {"last_sync_token": synclog_id}})
        assert result.status_code == 201

        # check correct hash
        response = get_restore_response(config.restore_url, config.domain, dummy_user(user_id),
                                        since=synclog_id, state_hash="ccsh:21f8a4fb891affbcf51eaa9ba7da22ef")
        assert response.status_code == 200

        # check incorrect hash
        response = get_restore_response(config.restore_url, config.domain, dummy_user(user_id),
                                        since=synclog_id, state_hash="ccsh:badhash")
        assert response.status_code == 412
Exemplo n.º 6
0
    def test_sync_token(self, testapp, client):
        """
        Tests sync token / sync mode support
        """
        self.test_user_restore_with_case(testapp, client)
        user = dummy_user(self.user_id)

        synclog = self._get_one_synclog()
        synclog_id = synclog.id

        restore_payload = generate_restore_response(client, DOMAIN, user, self._get_restore_url_snippet(), since=synclog_id)
        new_synclog = self._get_synclog_by_previous_id(synclog_id)
        new_synclog_id = new_synclog.id

        # should no longer have a case block in the restore XML
        check_xml_line_by_line(
            dummy_restore_xml(user, new_synclog_id, items=3),
            restore_payload,
        )

        time.sleep(1)  # current jsonobject doesn't support microseconds in datetime fields
        # update the case
        with testapp.app_context():
            factory = CaseFactory(
                self._get_backend(),
                client,
                domain=DOMAIN,
                case_defaults={
                    'user_id': str(uuid4()),
                    'owner_id': self.user_id,
                    'case_type': 'duck',
                }
            )
            updated_case, = factory.create_or_update_case(
                CaseStructure(self.case_id, attrs={'update': {'occupation': 'restaurant owner'}}),
            )

        new_restore_payload = generate_restore_response(client, DOMAIN, user, self._get_restore_url_snippet(), since=new_synclog_id)

        new_new_synclog = self._get_synclog_by_previous_id(new_synclog_id)

        case_xml = xml.get_case_xml(updated_case, [const.CASE_ACTION_UPDATE], version=V2)
        # case block should come back
        check_xml_line_by_line(
            dummy_restore_xml(user, new_new_synclog.id, case_xml=case_xml, items=4),
            new_restore_payload,
        )
Exemplo n.º 7
0
    def test_basic_workflow(self, config):
        """
        Sync, create a case
        Verify sync doesn't contain case
        Update case by another user
        Verify sync contains updated case
        """
        user_id = str(uuid4())
        case_id = str(uuid4())
        user = dummy_user(user_id)

        initial_payload = get_restore_payload(config.restore_url, config.domain, user)
        synclog_id = synclog_id_from_restore_payload(
            initial_payload
        )

        # payload should not contain any cases
        check_xml_line_by_line(
            dummy_restore_xml(user, synclog_id, items=3),
            initial_payload,
        )

        factory = CaseFactory(
            config.receiver_url,
            domain=config.domain,
            form_extras={
                'user_id': user_id,
            }
        )
        case_attrs = {
            'create': True,
            'user_id': user_id,
            'owner_id': user_id,
            'case_type': 'gangster',
            'case_name': 'Fish',
            'update': {'last_name': 'Mooney'}
        }
        factory.create_or_update_case(
            CaseStructure(case_id, attrs=case_attrs),
            form_extras={'headers': {'last_sync_token': synclog_id}}
        )

        restore_payload = get_restore_payload(config.restore_url, config.domain, user, since=synclog_id)
        new_synclog_id = synclog_id_from_restore_payload(restore_payload)
        # restore still does not contain case
        check_xml_line_by_line(
            dummy_restore_xml(user, new_synclog_id, items=3),
            restore_payload,
        )

        # update the case
        case_updates = {'cover_job': 'restaurant owner'}
        date_modified = datetime.utcnow()
        factory.create_or_update_case(
            CaseStructure(case_id, attrs={'update': case_updates, 'date_modified': date_modified}),
            form_extras={
                'user_id': user_id,
                # 'headers': {
                #     'last_sync_token': new_synclog_id
                }#}
        )

        restore_payload = get_restore_payload(config.restore_url, config.domain, user, since=new_synclog_id)
        new_new_synclog_id = synclog_id_from_restore_payload(restore_payload)

        case_attrs['create'] = False
        case_attrs['update'].update(case_updates)
        case_block = CaseBlock(case_id, date_modified=date_modified, **case_attrs)
        # restore contain case
        check_xml_line_by_line(
            dummy_restore_xml(user, new_new_synclog_id, case_xml=case_block.as_string(), items=4),
            restore_payload,
        )