Exemple #1
0
    def test_update_responses(self):
        formxml = FormSubmissionBuilder(form_id='123',
                                        form_properties={
                                            'breakfast': 'toast',
                                            'lunch': 'sandwich'
                                        }).as_xml_string()
        pic = UploadedFile(BytesIO(b"fake"),
                           'pic.jpg',
                           content_type='image/jpeg')
        xform = submit_form_locally(formxml,
                                    DOMAIN,
                                    attachments={
                                        "image": pic
                                    }).xform

        updates = {'breakfast': 'fruit'}
        errors = FormProcessorInterface(DOMAIN).update_responses(
            xform, updates, 'user1')
        form = FormAccessors(DOMAIN).get_form(xform.form_id)
        self.assertEqual(0, len(errors))
        self.assertEqual('fruit', form.form_data['breakfast'])
        self.assertEqual('sandwich', form.form_data['lunch'])
        self.assertIn("image", form.attachments)
        self.assertEqual(form.get_attachment("image"), b"fake")
        self.assertXmlEqual(
            form.get_attachment("form.xml").decode('utf-8'),
            formxml.replace("toast", "fruit"),
        )
    def test_update_responses(self):
        formxml = FormSubmissionBuilder(
            form_id='123',
            form_properties={'breakfast': 'toast', 'lunch': 'sandwich'}
        ).as_xml_string()
        pic = UploadedFile(BytesIO(b"fake"), 'pic.jpg', content_type='image/jpeg')
        xform = submit_form_locally(formxml, DOMAIN, attachments={"image": pic}).xform

        updates = {'breakfast': 'fruit'}
        errors = FormProcessorInterface(DOMAIN).update_responses(xform, updates, 'user1')
        form = FormAccessors(DOMAIN).get_form(xform.form_id)
        self.assertEqual(0, len(errors))
        self.assertEqual('fruit', form.form_data['breakfast'])
        self.assertEqual('sandwich', form.form_data['lunch'])
        self.assertIn("image", form.attachments)
        self.assertEqual(form.get_attachment("image"), b"fake")
        self.assertXmlEqual(
            form.get_attachment("form.xml").decode('utf-8'),
            formxml.replace("toast", "fruit"),
        )
    def test_modify_attachment_xml_and_metadata_couch(self):
        form = get_simple_wrapped_form(
            uuid.uuid4().hex,
            metadata=TestFormMetadata(domain=DOMAIN),
            simple_form=GDPR_SIMPLE_FORM)
        new_form_xml = Command().update_form_data(form, NEW_USERNAME)
        FormAccessors(DOMAIN).modify_attachment_xml_and_metadata(
            form, new_form_xml, NEW_USERNAME)

        # Test that the metadata changed in the database
        form = FormAccessors(DOMAIN).get_form(form.form_id)
        actual_form_xml = form.get_attachment("form.xml").decode('utf-8')
        self.assertXMLEqual(EXPECTED_FORM_XML, actual_form_xml)

        # Test that the operations history is updated in this form
        refetched_form = FormAccessors(DOMAIN).get_form(form.form_id)
        self.assertEqual(len(refetched_form.history), 1)
        self.assertEqual(refetched_form.history[0].operation, "gdpr_scrub")
        self.assertEqual(refetched_form.metadata.username, NEW_USERNAME)
Exemple #4
0
    def test_update_responses(self):
        formxml = FormSubmissionBuilder(
            form_id='123',
            form_properties={
                'breakfast': 'toast',   # Simple questions
                'lunch': 'sandwich',
                'cell': {               # Simple group
                    'cytoplasm': 'squishy',
                    'organelles': 'grainy',
                },
                'shelves': [            # Simple repeat group
                    {'position': 'top'},
                    {'position': 'middle'},
                    {'position': 'bottom'},
                ],
                'grandparent': [        # Repeat group with child group
                    {'name': 'Haruki'},
                    {'name': 'Sugako'},
                    {
                        'name': 'Emma',
                        'parent': {
                            'name': 'Haruki',
                            'child': {
                                'name': 'Nao',
                            },
                        }
                    },
                ],
                'body': [               # Repeat group with child repeat group
                    {'arm': [
                        {'elbow': '1'},
                        {'finger': '5'},
                    ]},
                    {'leg': [
                        {'knee': '1'},
                        {'toe': '5'},
                    ]},
                ],
            }
        ).as_xml_string()
        pic = UploadedFile(BytesIO(b"fake"),
                           'pic.jpg',
                           content_type='image/jpeg')
        xform = submit_form_locally(formxml,
                                    DOMAIN,
                                    attachments={
                                        "image": pic
                                    }).xform

        updates = {
            'breakfast': 'fruit',
            'cell/organelles': 'bulbous',
            'shelves[1]/position': 'third',
            'shelves[3]/position': 'first',
            'grandparent[1]/name': 'Haruki #1',
            'grandparent[3]/name': 'Ema',
            'grandparent[3]/parent/name': 'Haruki #2',
            'grandparent[3]/parent/child/name': 'Nao-chan',
            'body[1]/arm[1]/elbow': '2',
            'body[2]/leg[2]/toe': '10',
        }
        errors = FormProcessorInterface(DOMAIN).update_responses(
            xform, updates, 'user1')
        form = FormAccessors(DOMAIN).get_form(xform.form_id)
        self.assertEqual(0, len(errors))
        self.assertEqual('fruit', form.form_data['breakfast'])
        self.assertEqual('sandwich', form.form_data['lunch'])
        self.assertEqual('squishy', form.form_data['cell']['cytoplasm'])
        self.assertEqual('bulbous', form.form_data['cell']['organelles'])
        self.assertEqual('third', form.form_data['shelves'][0]['position'])
        self.assertEqual('middle', form.form_data['shelves'][1]['position'])
        self.assertEqual('first', form.form_data['shelves'][2]['position'])
        self.assertEqual('Haruki #1', form.form_data['grandparent'][0]['name'])
        self.assertEqual('Sugako', form.form_data['grandparent'][1]['name'])
        self.assertEqual('Ema', form.form_data['grandparent'][2]['name'])
        self.assertEqual('Haruki #2',
                         form.form_data['grandparent'][2]['parent']['name'])
        self.assertEqual(
            'Nao-chan',
            form.form_data['grandparent'][2]['parent']['child']['name'])
        self.assertEqual('2', form.form_data['body'][0]['arm'][0]['elbow'])
        self.assertEqual('5', form.form_data['body'][0]['arm'][1]['finger'])
        self.assertEqual('1', form.form_data['body'][1]['leg'][0]['knee'])
        self.assertEqual('10', form.form_data['body'][1]['leg'][1]['toe'])
        self.assertIn("image", form.attachments)
        self.assertEqual(form.get_attachment("image"), b"fake")