Skip to content

Commit

Permalink
Merge pull request #867 from onaio/encrypted-form-multiple-media-atta…
Browse files Browse the repository at this point in the history
…chment-fix

put all media attachment to a list
  • Loading branch information
ukanga authored Jan 5, 2017
2 parents 0d872b6 + 1c412fb commit 79f95b2
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 3 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<data id="tutorial_encrypted" version="201701031234" encrypted="yes" xmlns="http://www.opendatakit.org/xforms/encrypted"><base64EncryptedKey>ZJTcMOmwgRAM0IQcYselpBy365bUQv3XKfdiS1wYHia5l6iH11v2abmMVkyErPPh7dzy3IWh0n85DJjX9WqPgY84ObtDEs1L7VGIesj4bfunRP8loJMJns5aUmmA/KPgTYPSJJT/JrGDfuLznxx0MAEYihRRETJe+3I0atfx2IOrgJgJ+T5+w9imC5/OMHFV9BknORlg/1xHzIi1Rq2Y1tEiNTFNNQiI4rqxKI/J4Ie72Dchvhke/baLt6Elm5kX+D7Hn5Efhm3/0G1SQXLjOHFJSUcUkPcode/4stRurwZfoC4UgCtN7wjfsrbPCasogvcxmgFDAkbWdFhZc6sIoQ==</base64EncryptedKey><orx:meta xmlns:orx="http://openrosa.org/xforms"><orx:instanceID>uuid:f8971231-f3b8-4b2b-8c35-d95fa207d937</orx:instanceID></orx:meta>
<media><file>1483528430996.jpg.enc</file></media>
<media><file>1483528445767.jpg.enc</file></media>
<encryptedXmlFile>submission.xml.enc</encryptedXmlFile><base64EncryptedElementSignature>UUR8Vwvp9Tbcb7/ORxQIXO/cerZpXJH/X5qa5aTtrZ8uhFUqnsAzG0j0RpVZE7/ukKEYOY5V+KaqgkHSCuWlFwHtOwhNVEt1MSkf9zIFGqfAs+fCV2XXDEfFuiDsR9/Iuk5bxX08OZSzWX9Jy/hkWh3wEE4Vgdd7dvkOLxX8Xoi4a0jShCWtrlhagsRqNoqrSipsqhE+9jOKMnmZcuq357jPPbcvyqstrzsLrtGf0h42YaSMtaDnUknbibVIIO4ezEdWauSiOINxkasDyqEYulLVQK52nlhhUvZglLkgrRWQ279x4TwhwQkwIcRxksUp3XCUL9rAzUx2RXiQNLrywg==</base64EncryptedElementSignature></data>
Binary file not shown.
38 changes: 38 additions & 0 deletions onadata/apps/logger/tests/test_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,41 @@ def test_parse_xform_nested_repeats_multiple_nodes(self):
self.assertContains(self.response,
"Multiple nodes with the same name",
status_code=400)

def test_multiple_media_files_on_encrypted_form(self):
self._create_user_and_login()
# publish our form which contains some some repeats
xls_file_path = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"../fixtures/tutorial_encrypted/tutorial_encrypted.xls"
)
count = XForm.objects.count()
self._publish_xls_file_and_set_xform(xls_file_path)
self.assertEqual(count + 1, XForm.objects.count())

# submit an instance
xml_submission_file_path = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"../fixtures/tutorial_encrypted/instances/tutorial_encrypted.xml"
)
self._make_submission(xml_submission_file_path)
self.assertNotContains(self.response,
"Multiple nodes with the same name",
status_code=201)

# load xml file to parse and compare
xml_file = open(xml_submission_file_path)
self.xml = xml_file.read()
xml_file.close()

parser = XFormInstanceParser(self.xml, self.xform)
dict = parser.to_dict()

expected_list = [{u'file': u'1483528430996.jpg.enc'},
{u'file': u'1483528445767.jpg.enc'}]
self.assertEqual(dict.get('data').get('media'), expected_list)

flat_dict = parser.to_flat_dict()
expected_flat_list = [{u'media/file': u'1483528430996.jpg.enc'},
{u'media/file': u'1483528445767.jpg.enc'}]
self.assertEqual(flat_dict.get('media'), expected_flat_list)
9 changes: 6 additions & 3 deletions onadata/apps/logger/xform_instance_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def clean_and_parse_xml(xml_string):
return xml_obj


def _xml_node_to_dict(node, repeats=[]):
def _xml_node_to_dict(node, repeats=[], encrypted=False):
assert isinstance(node, minidom.Node)
if len(node.childNodes) == 0:
# there's no data for this leaf node
Expand All @@ -162,7 +162,8 @@ def _xml_node_to_dict(node, repeats=[]):
assert d.keys() == [child_name]
node_type = dict
# check if name is in list of repeats and make it a list if so
if child_xpath in repeats:
# All the photo attachments in an encrypted form use name media
if child_xpath in repeats or (encrypted and child_name == 'media'):
node_type = list

if node_type == dict:
Expand Down Expand Up @@ -283,7 +284,9 @@ def parse(self, xml_str):
self._root_node = self._xml_obj.documentElement
repeats = [e.get_abbreviated_xpath()
for e in self.dd.get_survey_elements_of_type(u"repeat")]
self._dict = _xml_node_to_dict(self._root_node, repeats)

self._dict = _xml_node_to_dict(self._root_node, repeats,
self.dd.encrypted)
self._flat_dict = {}
if self._dict is None:
raise InstanceEmptyError
Expand Down

0 comments on commit 79f95b2

Please sign in to comment.