def test_submission_to_require_auth_without_perm(self): """ test submission to a private form by non-owner without perm is forbidden. """ view = XFormViewSet.as_view({ 'patch': 'partial_update' }) data = {'require_auth': True} self.assertFalse(self.xform.require_auth) request = self.factory.patch('/', data=data, **{ 'HTTP_AUTHORIZATION': 'Token %s' % self.user.auth_token}) view(request, pk=self.xform.id) self.xform.reload() self.assertTrue(self.xform.require_auth) xml_submission_file_path = os.path.join( os.path.dirname(os.path.abspath(__file__)), "../fixtures/tutorial/instances/tutorial_2012-06-27_11-27-53.xml" ) # create a new user username = '******' self._create_user(username, username) client = DigestClient() client.set_authorization(username, username, 'Digest') self._make_submission(xml_submission_file_path, client=client) self.assertEqual(self.response.status_code, 403)
def _download_xform(self): client = DigestClient() client.set_authorization('bob', 'bob') response = client.get(self.download_url) response_doc = minidom.parseString(response.content) xml_path = os.path.join(self.this_directory, "fixtures", "transportation", "transportation.xml") with open(xml_path) as xml_file: expected_doc = minidom.parse(xml_file) model_node = [ n for n in response_doc.getElementsByTagName("h:head")[0].childNodes if n.nodeType == Node.ELEMENT_NODE and n.tagName == "model"][0] # check for UUID and remove uuid_nodes = [node for node in model_node.childNodes if node.nodeType == Node.ELEMENT_NODE and node.getAttribute("nodeset") == "/transportation/formhub/uuid"] self.assertEqual(len(uuid_nodes), 1) uuid_node = uuid_nodes[0] uuid_node.setAttribute("calculate", "''") response_xml = response_doc.toxml().replace( self.xform.version, u"201411120717") # check content without UUID self.assertEqual(response_xml, expected_doc.toxml())
def _get_authenticated_client(self, url, username="******", password="******", extra={}): client = DigestClient() # request with no credentials req = client.get(url, {}, **extra) self.assertEqual(req.status_code, 401) # apply credentials client.set_authorization(username, password, "Digest") return client
def testOtaRestore(self, password=None): client = Client() client.set_authorization(self.couch_user.username, password if password else self.password, method='Digest') resp = client.get('/a/%s/phone/restore' % self.domain, follow=True) self.assertEqual(resp.status_code, 200) self.assertTrue(resp.content.count("Successfully restored account %s!" % self.username) > 0)
def _get_digest_client(self): self.user.profile.require_auth = True self.user.profile.save() client = DigestClient() client.set_authorization(self.profile_data['username'], self.profile_data['password1'], 'Digest') return client
def test_edited_submission(self): """ Test submissions that have been edited """ xml_submission_file_path = os.path.join( os.path.dirname(os.path.abspath(__file__)), "..", "fixtures", "tutorial", "instances", "tutorial_2012-06-27_11-27-53_w_uuid.xml" ) num_instances_history = InstanceHistory.objects.count() num_instances = Instance.objects.count() query_args = { 'username': self.user.username, 'id_string': self.xform.id_string, 'query': '{}', 'fields': '[]', 'sort': '[]', 'count': True } cursor = ParsedInstance.query_mongo(**query_args) num_mongo_instances = cursor[0]['count'] # make first submission self._make_submission(xml_submission_file_path) self.assertEqual(self.response.status_code, 201) self.assertEqual(Instance.objects.count(), num_instances + 1) # no new record in instances history self.assertEqual( InstanceHistory.objects.count(), num_instances_history) # check count of mongo instances after first submission cursor = ParsedInstance.query_mongo(**query_args) self.assertEqual(cursor[0]['count'], num_mongo_instances + 1) # edited submission xml_submission_file_path = os.path.join( os.path.dirname(os.path.abspath(__file__)), "..", "fixtures", "tutorial", "instances", "tutorial_2012-06-27_11-27-53_w_uuid_edited.xml" ) client = DigestClient() client.set_authorization('bob', 'bob', 'Digest') self._make_submission(xml_submission_file_path, client=client) self.assertEqual(self.response.status_code, 201) # we must have the same number of instances self.assertEqual(Instance.objects.count(), num_instances + 1) # should be a new record in instances history self.assertEqual( InstanceHistory.objects.count(), num_instances_history + 1) cursor = ParsedInstance.query_mongo(**query_args) self.assertEqual(cursor[0]['count'], num_mongo_instances + 1) # make sure we edited the mongo db record and NOT added a new row query_args['count'] = False cursor = ParsedInstance.query_mongo(**query_args) record = cursor[0] with open(xml_submission_file_path, "r") as f: xml_str = f.read() xml_str = clean_and_parse_xml(xml_str).toxml() edited_name = re.match(ur"^.+?<name>(.+?)</name>", xml_str).groups()[0] self.assertEqual(record['name'], edited_name)
def _authenticated_client( self, url, username='******', password='******', extra={}): client = DigestClient() # request with no credentials req = client.get(url, {}, **extra) self.assertEqual(req.status_code, 401) # apply credentials client.set_authorization(username, password, 'Digest') req = client.get(url, {}, **extra) # if 204 authorization successfull, proceed self.assertEqual(req.status_code, 204) # submissions should use this authenticated client return client
def instances_xml(url, request, **kwargs): response = requests.Response() client = DigestClient() client.set_authorization('bob', 'bob', 'Digest') res = client.get('%s?%s' % (url.path, url.query)) if res.status_code == 302: res = client.get(res['Location']) response.encoding = res.get('content-type') response._content = get_streaming_content(res) else: response._content = res.content response.status_code = 200 return response
def test_retrieve_xform_manifest_linked_form(self): # for linked forms check if manifest media download url for csv # has a group_delimiter param data_type = 'media' data_value = 'xform {} transportation'.format(self.xform.pk) media = self._add_form_metadata(self.xform, data_type, data_value) self.view = XFormListViewSet.as_view( { "get": "manifest", "head": "manifest" } ) # sign in bob request = self.factory.head('/') auth_response = self.view(request, pk=self.xform.pk) auth = DigestAuth('bob', 'bobbob') # set up bob's request request = self.factory.get('/xformsManifest') request.META.update(auth(request.META, auth_response)) # make request response = self.view(request, pk=self.xform.pk, format='csv') # test manifest_media_url = '{}{}'.format( media.data['media_url'], '?group_delimiter=.&repeat_index_tags=_,_') download_url = response.data[0]['downloadUrl'] self.assertEqual(manifest_media_url, download_url) url = '/bob/xformsMedia/{}/{}.csv?group_delimiter=.'\ .format(self.xform.pk, self.metadata.pk) username = '******' password = '******' client = DigestClient() client.set_authorization(username, password, 'Digest') req = client.get(url) self.assertEqual(req.status_code, 200) # enable meta perms data_value = "editor-minor|dataentry" MetaData.xform_meta_permission(self.xform, data_value=data_value) req = client.get(url) self.assertEqual(req.status_code, 401)
class FormExportTest(TestCase): def setUp(self): self.app_id = 'kasdlfkjsldfkjsdlkjf' self.domain_name = 'form-export-test' self.domain = create_domain(self.domain_name) self.username = '******' self.couch_user = CommCareUser.create(self.domain_name, self.username, password='******') self.couch_user.save() self.client = Client() self.client.login(username=self.couch_user.username, password='******') self.url = reverse("receiver_post_with_app_id", args=[self.domain_name, self.app_id]) def post_it(): f = StringIO(XML_DATA) f.name = 'form.xml' response = self.client.post(self.url, {'xml_submission_file': f}) self.form1 = post_it() self.form2 = post_it() self.custom_export = FormExportSchema.wrap({ 'type': 'form', 'app_id': self.app_id, 'default_format': Format.JSON, 'index': json.dumps([self.domain_name, XMLNS]), 'tables': [{ 'index': '#', 'display': 'Export', 'columns': [{'index': 'form.name', 'display': 'Name'}], }] }) def test_include_duplicates(self): self.custom_export.include_errors = True tmp, _ = self.custom_export.get_export_files() data = tmp.getvalue() data = json.loads(data) self.assertEqual(data['Export']['headers'], ['Name']) self.assertEqual(len(data['Export']['rows']), 2) self.custom_export.include_errors = False tmp, _ = self.custom_export.get_export_files() data = tmp.getvalue() data = json.loads(data) self.assertEqual(data['Export']['headers'], ['Name']) self.assertEqual(len(data['Export']['rows']), 1)
def setUp(self): self.app_id = 'kasdlfkjsldfkjsdlkjf' self.domain_name = 'form-export-test' self.domain = create_domain(self.domain_name) self.username = '******' self.couch_user = CommCareUser.create(self.domain_name, self.username, password='******') self.couch_user.save() self.client = Client() self.client.login(username=self.couch_user.username, password='******') self.url = reverse("receiver_post_with_app_id", args=[self.domain_name, self.app_id]) def post_it(): f = StringIO(XML_DATA) f.name = 'form.xml' response = self.client.post(self.url, {'xml_submission_file': f}) self.form1 = post_it() self.form2 = post_it() self.custom_export = FormExportSchema.wrap({ 'type': 'form', 'app_id': self.app_id, 'default_format': Format.JSON, 'index': json.dumps([self.domain_name, XMLNS]), 'tables': [{ 'index': '#', 'display': 'Export', 'columns': [{'index': 'form.name', 'display': 'Name'}], }] })
def test_data_entry_role_submission_when_requires_auth(self): self._publish_xls_form_to_project() self.user.profile.require_auth = True self.user.profile.save() alice_data = {'username': '******', 'email': '*****@*****.**', 'password1': 'alice', 'password2': 'alice'} self._login_user_and_profile(extra_post_data=alice_data) role.DataEntryRole.add(self.user, self.xform) paths = [os.path.join( self.main_directory, 'fixtures', 'transportation', 'instances', s, s + '.xml') for s in self.surveys] client = DigestClient() client.set_authorization('alice', 'alice', 'Digest') self._make_submission(paths[0], username='******', client=client) self.assertEqual(self.response.status_code, 201)
def _check_formlist(self): url = '/%s/formList' % self.user.username client = DigestClient() client.set_authorization('bob', 'bob') response = client.get(url) self.download_url = \ 'http://testserver/%s/forms/%s/form.xml'\ % (self.user.username, self.xform.pk) md5_hash = md5(self.xform.xml.encode('utf-8')).hexdigest() expected_content = """<?xml version="1.0" encoding="utf-8"?> <xforms xmlns="http://openrosa.org/xforms/xformsList"><xform><formID>transportation_2011_07_25</formID><name>transportation_2011_07_25</name><version>2014111</version><hash>md5:%(hash)s</hash><descriptionText></descriptionText><downloadUrl>%(download_url)s</downloadUrl></xform></xforms>""" # noqa expected_content = expected_content % { 'download_url': self.download_url, 'hash': md5_hash } self.assertEqual(response.content.decode('utf-8'), expected_content) self.assertTrue(response.has_header('X-OpenRosa-Version')) self.assertTrue(response.has_header('Date'))
def test_submission_when_requires_auth(self): self.user.profile.require_auth = True self.user.profile.save() # create a new user alice = self._create_user('alice', 'alice') # assign report perms to user assign_perm('report_xform', alice, self.xform) xml_submission_file_path = os.path.join( os.path.dirname(os.path.abspath(__file__)), "../fixtures/tutorial/instances/tutorial_2012-06-27_11-27-53.xml" ) client = DigestClient() client.set_authorization('alice', 'alice', 'Digest') self._make_submission( xml_submission_file_path, client=client) self.assertEqual(self.response.status_code, 201)
def test_unicode_submission(self): """Test xml submissions that contain unicode characters """ xml_submission_file_path = os.path.join( os.path.dirname(os.path.abspath(__file__)), "..", "fixtures", "tutorial", "instances", "tutorial_unicode_submission.xml") self.user.profile.require_auth = True self.user.profile.save() # create a new user alice = self._create_user('alice', 'alice') # assign report perms to user assign_perm('report_xform', alice, self.xform) client = DigestClient() client.set_authorization('alice', 'alice', 'Digest') self._make_submission(xml_submission_file_path) self.assertEqual(self.response.status_code, 201)
def test_pages(self): """ Confirm that all the groups/locations/users appear on the correct pages """ client = Client() client.login(username=self.username, password=self.password) # expected_id_sets is a list of sets. # expected_id_sets is constructed such that # For option with index x yielded by the view: # the option's id should be in expected_ids[x] expected_id_sets = [{"user_location"}, {"user_parent_location"}] for i in self.groups: expected_id_sets.append(self.group_ids) for i in self.locations: expected_id_sets.append(self.location_ids) for i in self.users: expected_id_sets.append(self.user_ids) page_size = 3 # using a small number because more pages will hopefully be more likely to reveal bugs expected_num_pages = int( math.ceil(len(expected_id_sets) / float(page_size))) for i in range(expected_num_pages): page = i + 1 response = client.get(reverse(CallCenterOwnerOptionsView.url_name, args=[self.domain.name]), data={ "page": page, "page_limit": page_size, "q": "" }) response_json = json.loads(response.content) self.assertEqual(response_json['total'], len(expected_id_sets)) for item_index, item in enumerate(response_json['results']): id_ = item['id'] option_index = ((page - 1) * page_size) + item_index self.assertTrue( id_ in expected_id_sets[option_index], "Unexpected item {} at index {}.".format( item, option_index))
def test_SMS_API_Users_not_shown_on_user_list_page(self): client = Client() client.login(username=DEFAULT_TEST_USER, password=DEFAULT_TEST_PASSWORD) request = HttpRequest() request.method = 'GET' request.user = User.objects.get(username="******") with patch('datawinners.accountmanagement.views.User') as user_class: with patch('datawinners.accountmanagement.views.RequestContext' ) as context: with patch( "datawinners.accountmanagement.views.render_to_response" ) as render_response_patch: objects = Mock() type(user_class).objects = PropertyMock( return_value=objects) users(request) objects.exclude.assert_called_once_with( groups__name__in=['Data Senders', 'SMS API Users'])
def setUpClass(cls): cls.client = Client() cls.client.login(username="******", password="******") cls.unique_id = random_string() cls.project_id = create_multiple_unique_id_project( cls.client, cls.unique_id) cls.client.set_authorization('*****@*****.**', 'tester150411', method="Digest") cls.register_people_one(cls.client)
def test_unicode_submission(self): """Test xml submissions that contain unicode characters """ xml_submission_file_path = os.path.join( os.path.dirname(os.path.abspath(__file__)), "..", "fixtures", "tutorial", "instances", "tutorial_unicode_submission.xml" ) self.user.profile.require_auth = True self.user.profile.save() # create a new user alice = self._create_user('alice', 'alice') # assign report perms to user assign_perm('report_xform', alice, self.xform) client = DigestClient() client.set_authorization('alice', 'alice', 'Digest') self._make_submission(xml_submission_file_path) self.assertEqual(self.response.status_code, 201)
class TestFormList(TestBase): def setUp(self): super(TestFormList, self).setUp() self.profile = UserProfile.objects.create( user=self.user, require_auth=True) self.profile.save() self.digest_client = DigestClient() def test_returns_200_for_owner(self): self.digest_client.set_authorization('bob', 'bob') response = self.digest_client.get(reverse(formList, kwargs={ 'username': '******' })) self.assertEqual(response.status_code, 200) def test_returns_401_for_anon(self): response = self.anon.get(reverse(formList, kwargs={ 'username': '******' })) self.assertEqual(response.status_code, 401) def test_returns_200_for_authenticated_non_owner(self): credentials = ('alice', 'alice',) self._create_user(*credentials) self.digest_client.set_authorization(*credentials) response = self.digest_client.get(reverse(formList, kwargs={ 'username': '******' })) self.assertEqual(response.status_code, 200)
class TestFormList(TestBase): def setUp(self): super(TestFormList, self).setUp() self.profile = UserProfile.objects.create(user=self.user, require_auth=True) self.profile.save() self.digest_client = DigestClient() def test_returns_200_for_owner(self): self.digest_client.set_authorization('bob', 'bob') response = self.digest_client.get( reverse(formList, kwargs={'username': '******'})) self.assertEqual(response.status_code, 200) def test_returns_401_for_anon(self): response = self.anon.get(reverse(formList, kwargs={'username': '******'})) self.assertEqual(response.status_code, 401) def test_returns_200_for_authenticated_non_owner(self): credentials = ( 'alice', 'alice', ) self._create_user(*credentials) self.digest_client.set_authorization(*credentials) response = self.digest_client.get( reverse(formList, kwargs={'username': '******'})) self.assertEqual(response.status_code, 200)
def test_data_entry_role_submission_when_requires_auth(self): self._publish_xls_form_to_project() self.user.profile.require_auth = True self.user.profile.save() alice_data = { 'username': '******', 'email': '*****@*****.**', 'password1': 'alice', 'password2': 'alice' } self._login_user_and_profile(extra_post_data=alice_data) role.DataEntryRole.add(self.user, self.xform) paths = [ os.path.join(self.main_directory, 'fixtures', 'transportation', 'instances', s, s + '.xml') for s in self.surveys ] client = DigestClient() client.set_authorization('alice', 'alice', 'Digest') self._make_submission(paths[0], username='******', client=client) self.assertEqual(self.response.status_code, 201)
def _check_formList(self): url = '/%s/formList' % self.user.username client = DigestClient() client.set_authorization('bob', 'bob') response = client.get(url) self.download_url = \ 'http://testserver/%s/forms/%s/form.xml'\ % (self.user.username, self.xform.pk) self.manifest_url = \ 'http://testserver/%s/xformsManifest/%s'\ % (self.user.username, self.xform.pk) md5_hash = md5(self.xform.xml).hexdigest() expected_content = """<?xml version="1.0" encoding="utf-8"?> <xforms xmlns="http://openrosa.org/xforms/xformsList"><xform><formID>transportation_2011_07_25</formID><name>transportation_2011_07_25</name><majorMinorVersion></majorMinorVersion><version></version><hash>md5:%(hash)s</hash><descriptionText>transportation_2011_07_25</descriptionText><downloadUrl>%(download_url)s</downloadUrl><manifestUrl>%(manifest_url)s</manifestUrl></xform></xforms>""" # noqa expected_content = expected_content % { 'download_url': self.download_url, 'manifest_url': self.manifest_url, 'hash': md5_hash } self.assertEqual(response.content, expected_content) self.assertTrue(response.has_header('X-OpenRosa-Version')) self.assertTrue(response.has_header('Date'))
def test_pages(self): """ Confirm that all the groups/locations/users appear on the correct pages """ client = Client() client.login(username=self.username, password=self.password) # expected_id_sets is a list of sets. # expected_id_sets is constructed such that # For option with index x yielded by the view: # the option's id should be in expected_ids[x] expected_id_sets = [{"user_location"}, {"user_parent_location"}] for i in self.groups: expected_id_sets.append(self.group_ids) for i in self.locations: expected_id_sets.append(self.location_ids) for i in self.users: expected_id_sets.append(self.user_ids) page_size = 3 # using a small number because more pages will hopefully be more likely to reveal bugs expected_num_pages = int(math.ceil(len(expected_id_sets) / float(page_size))) for i in range(expected_num_pages): page = i + 1 response = client.get(reverse( CallCenterOwnerOptionsView.url_name, args=[self.domain.name]), data={"page": page, "page_limit": page_size, "q": ""} ) response_json = json.loads(response.content) self.assertEqual(response_json['total'], len(expected_id_sets)) for item_index, item in enumerate(response_json['results']): id_ = item['id'] option_index = ((page - 1) * page_size) + item_index self.assertTrue( id_ in expected_id_sets[option_index], "Unexpected item {} at index {}.".format(item, option_index) )
def test_submission_to_require_auth_with_perm(self): """ test submission to a private form by non-owner is forbidden. TODO send authentication challenge when xform.require_auth is set. This is non-trivial because we do not know the xform until we have parsed the XML. """ raise SkipTest view = XFormViewSet.as_view({ 'patch': 'partial_update' }) data = {'require_auth': True} self.assertFalse(self.xform.require_auth) request = self.factory.patch('/', data=data, **{ 'HTTP_AUTHORIZATION': 'Token %s' % self.user.auth_token}) view(request, pk=self.xform.id) self.xform.reload() self.assertTrue(self.xform.require_auth) # create a new user username = '******' alice = self._create_user(username, username) # assign report perms to user assign_perm('report_xform', alice, self.xform) client = DigestClient() client.set_authorization(username, username, 'Digest') xml_submission_file_path = os.path.join( os.path.dirname(os.path.abspath(__file__)), "../fixtures/tutorial/instances/tutorial_2012-06-27_11-27-53.xml" ) self._make_submission(xml_submission_file_path, client=client) self.assertEqual(self.response.status_code, 201)
def setUp(self): super(FormExportTest, self).setUp() self.app_id = 'kasdlfkjsldfkjsdlkjf' self.domain_name = 'form-export-test' self.domain = create_domain(self.domain_name) self.username = '******' self.couch_user = CommCareUser.create(self.domain_name, self.username, password='******') self.couch_user.save() self.client = Client() self.client.login(username=self.couch_user.username, password='******') self.url = reverse("receiver_post_with_app_id", args=[self.domain_name, self.app_id]) self.custom_export = FormExportSchema.wrap({ 'type': 'form', 'app_id': self.app_id, 'default_format': Format.JSON, 'index': json.dumps([self.domain_name, XMLNS]), 'tables': [{ 'index': '#', 'display': 'Export', 'columns': [{'index': 'form.name', 'display': 'Name'}], }] })
def setUp(self): self.app_id = "kasdlfkjsldfkjsdlkjf" self.domain_name = "form-export-test" self.domain = create_domain(self.domain_name) self.username = "******" self.couch_user = CommCareUser.create(self.domain_name, self.username, password="******") self.couch_user.save() self.client = Client() self.client.login(username=self.couch_user.username, password="******") self.url = reverse("receiver_post_with_app_id", args=[self.domain_name, self.app_id]) self.custom_export = FormExportSchema.wrap( { "type": "form", "app_id": self.app_id, "default_format": Format.JSON, "index": json.dumps([self.domain_name, XMLNS]), "tables": [{"index": "#", "display": "Export", "columns": [{"index": "form.name", "display": "Name"}]}], } )
class TestXFromClientSubmission(unittest.TestCase): def setUp(self): self.client = DigestClient() self.client.set_authorization('*****@*****.**','tester150411', 'Digest') self.test_data = os.path.join(DIR, 'testdata') self.XFORM_XML = os.path.join(self.test_data,'xform-024.xml') self.XFORM_XML_ALL_FIELDS = os.path.join(self.test_data,'xform_all_fields.xml') #todo Test data is hardcoded currently. Need to fix this ex by creating required project. #integration # def test_should_download_xform_with_repeat_field(self): # project_id = '0528ba5a835a11e3bbaa001c42af7554' # r = self.client.get(path='/xforms/%s' %project_id) # self.assertEquals(r.status_code, 200) #integration # def test_should_update_xform_submission_with_reporter_id(self): # # todo same as test_should_update_xform_submission_with_reporter_id # submission_xml = os.path.join(self.test_data, 'repeat-submission.xml') # # r = self._do_submission(submission_xml) # # self.assertEquals(r.status_code, 201) # submission_id = r.get('submission_id', None) # self.assertIsNotNone(submission_id) # #todo verify the rep_id is present in submission doc def _do_submission(self, submission_xml): with tempfile.NamedTemporaryFile(suffix='.txt') as temp_file: temp_file.write(open(submission_xml, 'r').read()) temp_file.seek(0) r = self.client.post( '/xforms/submission', {'xml_submission_file': temp_file}, ) return r #integration # def test_should_update_xform_submission_with_reporter_id(self): # # todo Project needs to be created and the eid and form_code need to be updated the in the repeat-submission.xml # submission_xml = os.path.join(self.test_data, 'repeat-submission.xml') # # r = self._do_submission(submission_xml) # # self.assertEquals(r.status_code, 201) # submission_id = r.get('submission_id', None) # self.assertIsNotNone(submission_id) # todo fetch submission doc and verify; append something unique to submission to make it specific def create_expected_dict(self): expected_code_val_dict = [{'meta': [{'instanceID': ''}]}, {'form_code': '023'}, {'other': 'Samosa'}, {'name': 'Santa'}, {'location': '4.9158 11.9531'}, {'pizza_type': ''}, {'age': '30'}, {'education': [{'completed_on': '2014-02-10'}, {'degree': 'SantaSSC'}]}, {'fav_color': 'red blue'}, {'pizza_fan': 'yes'}] return expected_code_val_dict def create_test_fields_and_survey_for_all_fields_type(self): name = TextField('name', 'name' ,'What is your name?') degree = TextField('degree', 'degree' ,'Degree name') completed_on = DateField('completed_on', 'completed_on','Degree completion year', 'dd.mm.yyyy') education = FieldSet('education', 'education', 'Education', field_set=[degree,completed_on]) age = IntegerField('age', 'age' ,'What is your age?') opt_fav_col = [('red','Red'), ('blue','Blue'),('c','Green')] fav_col = SelectField('fav_color', 'fav_color', 'Which colors you like?', opt_fav_col) opt_pizza_col = [('yes', 'Yes'),('no','No')] pizza_fan = SelectField('pizza_fan', 'pizza_fan', 'Do you like pizza?', opt_pizza_col) other = TextField('other', 'other' ,'What else you like?') pizza_type = TextField('pizza_type', 'pizza_type' ,'Which pizza type you like?') location = GeoCodeField('location', 'location' ,'Your location?') form_fields = [name, education, age, fav_col, pizza_fan, other, pizza_type, location] # todo how required will be handled survey_response_values = {'name': 'Santa', 'pizza_type': None, 'age': '30', 'other': 'Samosa', 'location': '4.9158,11.9531', 'education': [{'completed_on': u'10.02.2014', 'degree': 'SantaSSC'}], 'pizza_fan': 'yes', 'fav_color': 'red blue'} return form_fields, survey_response_values def create_test_fields_and_survey(self): #change this to reporter #entity_field = TextField('clinic', 'ID', 'clinic label', entity_question_flag=True) city_field = TextField('city', 'city', 'What is the City name?') name_field = TextField('centername', 'centername', 'Center Name?') area_field = TextField('area', 'area', 'Area?') center_field_set = FieldSet('center', 'center', 'Center Information', field_set=[name_field, area_field]) form_fields = [#entity_field, city_field, center_field_set] survey_response_values = {'city': 'Bhopal', 'center': [{'centername': 'Boot', 'area': 'New Market'}, {'centername': 'Weene', 'area': 'Bgh'}], 'eid': 'rep276'} return form_fields, survey_response_values @attr('dcs') def test_should_create_xform_model_str(self): form_fields, survey_response_values = self.create_test_fields_and_survey() submissionProcessor = XFormSubmissionProcessor() expected_xml = '<project-name-01><city>Bhopal</city><center><centername>Boot</centername><area>New Market</area></center><center><centername>Weene</centername><area>Bgh</area></center><form_code>form_code-01</form_code></project-name-01>' instance_node_xml = submissionProcessor.get_model_edit_str(form_fields, survey_response_values, 'project-name-01', 'form_code-01') self.assertEqual(expected_xml, instance_node_xml) def test_should_create_xform_model_for_datetime_field(self): form_fields = [DateTimeField('date_time', 'date_time', 'label')] survey_response_values = {'date_time': '12.12.2212 12:12:12'} submissionProcessor = XFormSubmissionProcessor() expected_xml = '<project-name-01><date_time>2212-12-12T12:12:12</date_time><form_code>form_code-01</form_code></project-name-01>' instance_node_xml = submissionProcessor.get_model_edit_str(form_fields, survey_response_values, 'project-name-01', 'form_code-01') self.assertEqual(expected_xml, instance_node_xml) def test_should_create_xform_for_datetime_field_inside_repeat(self): field_set_fields = [DateTimeField('date_time', 'date_time', 'label', parent_field_code='field_set'), TextField('text','text','label', parent_field_code='field_set')] field_set = FieldSet('field_set', 'field_set', 'field_set', field_set=field_set_fields, fieldset_type='repeat') survey_response_values = {'field_set': [{'date_time': '12.12.2212 12:12:12', 'text': 'text1'}, {'date_time': '12.12.3212 12:12:12', 'text': 'text2'}]} submissionProcessor = XFormSubmissionProcessor() expected_xml = '<project-name-01><field_set><date_time>2212-12-12T12:12:12</date_time><text>text1</text></field_set>' \ '<field_set><date_time>3212-12-12T12:12:12</date_time><text>text2</text></field_set>' \ '<form_code>form_code-01</form_code></project-name-01>' instance_node_xml = submissionProcessor.get_model_edit_str([field_set], survey_response_values, 'project-name-01', 'form_code-01') self.assertEqual(expected_xml, instance_node_xml) @attr('dcs') def test_should_create_image_file_names_string(self): img_field = PhotoField('img1', 'img1', 'land image') land_address = TextField('address','address', 'Address') owner_name = TextField('name','name', 'Name') owner_img_field = PhotoField('owner_img', 'owner_img', 'owner image') owners_field_set = FieldSet('owners_info', 'owners_info', 'Owners Information', field_set=[owner_name, owner_img_field], fieldset_type='repeat') pet_breed = TextField('breed', 'breed', 'Breed') pet_field_set = FieldSet('pets', 'pets', 'Pet details', field_set=[pet_breed], fieldset_type='repeat') form_fields = [land_address, img_field, owners_field_set, pet_field_set] survey_response_values = {'address': 'some address', 'img1': 'img1.jgp', 'owners_info': [{'name':'name a', 'owner_img':'owner1.jpg'}, {'name': 'name b', 'owner_img':'owner2.jpg'}], 'pets' :[{'breed':'German goat'}]} expected = 'img1.jgp,owner1.jpg,owner2.jpg' imageProcessor = XFormImageProcessor() image_file_names_string = imageProcessor.get_media_files_str(form_fields, survey_response_values) self.assertEqual(expected, image_file_names_string)
def setUp(self): super(TestFormList, self).setUp() self.profile = UserProfile.objects.create(user=self.user, require_auth=True) self.profile.save() self.digest_client = DigestClient()
def test_edited_submission(self): """ Test submissions that have been edited """ # Delete all previous instance history objects InstanceHistory.objects.all().delete() xml_submission_file_path = os.path.join( os.path.dirname(os.path.abspath(__file__)), "..", "fixtures", "tutorial", "instances", "tutorial_2012-06-27_11-27-53_w_uuid.xml") num_instances_history = InstanceHistory.objects.count() num_instances = Instance.objects.count() query_args = { 'xform': self.xform, 'query': '{}', 'fields': '[]', 'count': True } cursor = [r for r in query_data(**query_args)] num_data_instances = cursor[0]['count'] # make first submission self._make_submission(xml_submission_file_path) self.assertEqual(self.response.status_code, 201) self.assertEqual(Instance.objects.count(), num_instances + 1) # Take initial instance from DB initial_instance = self.xform.instances.first() # check that '_last_edited' key is not in the json self.assertIsNone(initial_instance.json.get(LAST_EDITED)) # no new record in instances history self.assertEqual(InstanceHistory.objects.count(), num_instances_history) # check count of mongo instances after first submission cursor = query_data(**query_args) self.assertEqual(cursor[0]['count'], num_data_instances + 1) # edited submission xml_edit_submission_file_path = os.path.join( os.path.dirname(os.path.abspath(__file__)), "..", "fixtures", "tutorial", "instances", "tutorial_2012-06-27_11-27-53_w_uuid_edited.xml") client = DigestClient() client.set_authorization('bob', 'bob', 'Digest') with catch_signal(process_submission) as handler: self._make_submission(xml_edit_submission_file_path, client=client) self.assertEqual(self.response.status_code, 201) # we must have the same number of instances self.assertEqual(Instance.objects.count(), num_instances + 1) # should be a new record in instances history self.assertEqual(InstanceHistory.objects.count(), num_instances_history + 1) instance_history_1 = InstanceHistory.objects.first() edited_instance = self.xform.instances.first() self.assertDictEqual(initial_instance.get_dict(), instance_history_1.get_dict()) handler.assert_called_once_with(instance=edited_instance, sender=Instance, signal=ANY) self.assertNotEqual(edited_instance.uuid, instance_history_1.uuid) # check that instance history's submission_date is equal to instance's # date_created - last_edited by default is null for an instance self.assertEquals(edited_instance.date_created, instance_history_1.submission_date) # check that '_last_edited' key is not in the json self.assertIn(LAST_EDITED, edited_instance.json) cursor = query_data(**query_args) self.assertEqual(cursor[0]['count'], num_data_instances + 1) # make sure we edited the mongo db record and NOT added a new row query_args['count'] = False cursor = query_data(**query_args) record = cursor[0] with open(xml_edit_submission_file_path, "r") as f: xml_str = f.read() xml_str = clean_and_parse_xml(xml_str).toxml() edited_name = re.match(ur"^.+?<name>(.+?)</name>", xml_str).groups()[0] self.assertEqual(record['name'], edited_name) instance_before_second_edit = edited_instance xml_edit_submission_file_path = os.path.join( os.path.dirname(os.path.abspath(__file__)), "..", "fixtures", "tutorial", "instances", "tutorial_2012-06-27_11-27-53_w_uuid_edited_again.xml") self._make_submission(xml_edit_submission_file_path) cursor = query_data(**query_args) record = cursor[0] edited_instance = self.xform.instances.first() instance_history_2 = InstanceHistory.objects.last() self.assertEquals(instance_before_second_edit.last_edited, instance_history_2.submission_date) # check that '_last_edited' key is not in the json self.assertIn(LAST_EDITED, edited_instance.json) self.assertEqual(record['name'], 'Tom and Jerry') self.assertEqual(InstanceHistory.objects.count(), num_instances_history + 2) # submitting original submission is treated as a duplicate # does not add a new record # does not change data self._make_submission(xml_submission_file_path) self.assertEqual(self.response.status_code, 202) self.assertEqual(Instance.objects.count(), num_instances + 1) self.assertEqual(InstanceHistory.objects.count(), num_instances_history + 2)
def test_edited_submission_require_auth(self): """ Test submissions that have been edited """ xml_submission_file_path = os.path.join( os.path.dirname(os.path.abspath(__file__)), "..", "fixtures", "tutorial", "instances", "tutorial_2012-06-27_11-27-53_w_uuid.xml" ) num_instances_history = InstanceHistory.objects.count() num_instances = Instance.objects.count() query_args = { 'username': self.user.username, 'id_string': self.xform.id_string, 'query': '{}', 'fields': '[]', 'sort': '[]', 'count': True } cursor = ParsedInstance.query_mongo(**query_args) num_mongo_instances = cursor[0]['count'] # make first submission self._make_submission(xml_submission_file_path) self.assertEqual(self.response.status_code, 201) self.assertEqual(Instance.objects.count(), num_instances + 1) # no new record in instances history self.assertEqual( InstanceHistory.objects.count(), num_instances_history) # check count of mongo instances after first submission cursor = ParsedInstance.query_mongo(**query_args) self.assertEqual(cursor[0]['count'], num_mongo_instances + 1) # require authentication self.user.profile.require_auth = True self.user.profile.save() # create a new user alice = self._create_user('alice', 'alice') UserProfile.objects.create(user=alice) client = DigestClient() client.set_authorization('alice', 'alice', 'Digest') # edited submission xml_submission_file_path = os.path.join( os.path.dirname(os.path.abspath(__file__)), "..", "fixtures", "tutorial", "instances", "tutorial_2012-06-27_11-27-53_w_uuid_edited.xml" ) self._make_submission(xml_submission_file_path, client=client) self.assertEqual(self.response.status_code, 403) # assign report perms to user assign_perm('report_xform', alice, self.xform) assign_perm('logger.change_xform', alice, self.xform) self._make_submission(xml_submission_file_path, client=client) self.assertEqual(self.response.status_code, 201) # we must have the same number of instances self.assertEqual(Instance.objects.count(), num_instances + 1) # should be a new record in instances history self.assertEqual( InstanceHistory.objects.count(), num_instances_history + 1) cursor = ParsedInstance.query_mongo(**query_args) self.assertEqual(cursor[0]['count'], num_mongo_instances + 1) # make sure we edited the mongo db record and NOT added a new row query_args['count'] = False cursor = ParsedInstance.query_mongo(**query_args) record = cursor[0] with open(xml_submission_file_path, "r") as f: xml_str = f.read() xml_str = clean_and_parse_xml(xml_str).toxml() edited_name = re.match(ur"^.+?<name>(.+?)</name>", xml_str).groups()[0] self.assertEqual(record['name'], edited_name)
def _get_digest_client(self): self._set_require_auth(True) client = DigestClient() client.set_authorization('bob', 'bob', 'Digest') return client
class FormExportTest(TestCase): def setUp(self): super(FormExportTest, self).setUp() self.app_id = 'kasdlfkjsldfkjsdlkjf' self.domain_name = 'form-export-test' self.domain = create_domain(self.domain_name) self.username = '******' self.couch_user = CommCareUser.create(self.domain_name, self.username, password='******') self.couch_user.save() self.client = Client() self.client.login(username=self.couch_user.username, password='******') self.url = reverse("receiver_post_with_app_id", args=[self.domain_name, self.app_id]) self.custom_export = FormExportSchema.wrap({ 'type': 'form', 'app_id': self.app_id, 'default_format': Format.JSON, 'index': json.dumps([self.domain_name, XMLNS]), 'tables': [{ 'index': '#', 'display': 'Export', 'columns': [{ 'index': 'form.name', 'display': 'Name' }], }] }) def tearDown(self): self.couch_user.delete() super(FormExportTest, self).tearDown() def post_it(self, user_id=None, form_id=XFORM_ID): user_id = user_id or self.couch_user._id f = StringIO( XML_DATA.format( user_id=user_id, xmlns=XMLNS, xform_id=form_id, )) f.name = 'form.xml' return self.client.post(self.url, {'xml_submission_file': f}) def test_include_duplicates(self): self.post_it() self.post_it() self.custom_export.include_errors = True files = self.custom_export.get_export_files() data = json.loads(files.file.payload) self.assertEqual(data['Export']['headers'], ['Name']) self.assertEqual(len(data['Export']['rows']), 2) self.custom_export.include_errors = False files = self.custom_export.get_export_files() data = json.loads(files.file.payload) self.assertEqual(data['Export']['headers'], ['Name']) self.assertEqual(len(data['Export']['rows']), 1) def test_exclude_unknown_users(self): self.post_it(form_id='good', user_id=self.couch_user._id) files = self.custom_export.get_export_files() data = json.loads(files.file.payload) self.assertEqual(len(data['Export']['rows']), 1) # posting from a non-real user shouldn't update self.post_it(form_id='bad', user_id='notarealuser') files = self.custom_export.get_export_files() data = json.loads(files.file.payload) self.assertEqual(len(data['Export']['rows']), 1) # posting from the real user should update self.post_it(form_id='stillgood', user_id=self.couch_user._id) files = self.custom_export.get_export_files() data = json.loads(files.file.payload) self.assertEqual(len(data['Export']['rows']), 2)
def setUp(self): super(TestFormList, self).setUp() self.profile = UserProfile.objects.create( user=self.user, require_auth=True) self.profile.save() self.digest_client = DigestClient()
class FormExportTest(TestCase): def setUp(self): self.app_id = "kasdlfkjsldfkjsdlkjf" self.domain_name = "form-export-test" self.domain = create_domain(self.domain_name) self.username = "******" self.couch_user = CommCareUser.create(self.domain_name, self.username, password="******") self.couch_user.save() self.client = Client() self.client.login(username=self.couch_user.username, password="******") self.url = reverse("receiver_post_with_app_id", args=[self.domain_name, self.app_id]) self.custom_export = FormExportSchema.wrap( { "type": "form", "app_id": self.app_id, "default_format": Format.JSON, "index": json.dumps([self.domain_name, XMLNS]), "tables": [{"index": "#", "display": "Export", "columns": [{"index": "form.name", "display": "Name"}]}], } ) def tearDown(self): self.couch_user.delete() def post_it(self, user_id=None, form_id=XFORM_ID): user_id = user_id or self.couch_user._id f = StringIO(XML_DATA.format(user_id=user_id, xmlns=XMLNS, xform_id=form_id)) f.name = "form.xml" return self.client.post(self.url, {"xml_submission_file": f}) def test_include_duplicates(self): self.post_it() self.post_it() self.custom_export.include_errors = True tmp, _ = self.custom_export.get_export_files() data = json.loads(tmp.getvalue()) self.assertEqual(data["Export"]["headers"], ["Name"]) self.assertEqual(len(data["Export"]["rows"]), 2) self.custom_export.include_errors = False tmp, _ = self.custom_export.get_export_files() data = json.loads(tmp.getvalue()) self.assertEqual(data["Export"]["headers"], ["Name"]) self.assertEqual(len(data["Export"]["rows"]), 1) def test_exclude_unknown_users(self): self.post_it(form_id="good", user_id=self.couch_user._id) tmp, _ = self.custom_export.get_export_files() data = json.loads(tmp.getvalue()) self.assertEqual(len(data["Export"]["rows"]), 1) # posting from a non-real user shouldn't update self.post_it(form_id="bad", user_id="notarealuser") tmp, _ = self.custom_export.get_export_files() data = json.loads(tmp.getvalue()) self.assertEqual(len(data["Export"]["rows"]), 1) # posting from the real user should update self.post_it(form_id="stillgood", user_id=self.couch_user._id) tmp, _ = self.custom_export.get_export_files() data = json.loads(tmp.getvalue()) self.assertEqual(len(data["Export"]["rows"]), 2)
def test_edited_submission(self): """ Test submissions that have been edited """ # Delete all previous instance history objects InstanceHistory.objects.all().delete() xml_submission_file_path = os.path.join( os.path.dirname(os.path.abspath(__file__)), "..", "fixtures", "tutorial", "instances", "tutorial_2012-06-27_11-27-53_w_uuid.xml" ) num_instances_history = InstanceHistory.objects.count() num_instances = Instance.objects.count() query_args = { 'xform': self.xform, 'query': '{}', 'fields': '[]', 'count': True } cursor = [r for r in query_data(**query_args)] num_data_instances = cursor[0]['count'] # make first submission self._make_submission(xml_submission_file_path) self.assertEqual(self.response.status_code, 201) self.assertEqual(Instance.objects.count(), num_instances + 1) # Take initial instance from DB initial_instance = self.xform.instances.first() # check that '_last_edited' key is not in the json self.assertIsNone(initial_instance.json.get(LAST_EDITED)) # no new record in instances history self.assertEqual( InstanceHistory.objects.count(), num_instances_history) # check count of mongo instances after first submission cursor = query_data(**query_args) self.assertEqual(cursor[0]['count'], num_data_instances + 1) # edited submission xml_edit_submission_file_path = os.path.join( os.path.dirname(os.path.abspath(__file__)), "..", "fixtures", "tutorial", "instances", "tutorial_2012-06-27_11-27-53_w_uuid_edited.xml" ) client = DigestClient() client.set_authorization('bob', 'bob', 'Digest') with catch_signal(process_submission) as handler: self._make_submission(xml_edit_submission_file_path, client=client) self.assertEqual(self.response.status_code, 201) # we must have the same number of instances self.assertEqual(Instance.objects.count(), num_instances + 1) # should be a new record in instances history self.assertEqual( InstanceHistory.objects.count(), num_instances_history + 1) instance_history_1 = InstanceHistory.objects.first() edited_instance = self.xform.instances.first() self.assertDictEqual(initial_instance.get_dict(), instance_history_1.get_dict()) handler.assert_called_once_with(instance=edited_instance, sender=Instance, signal=ANY) self.assertNotEqual(edited_instance.uuid, instance_history_1.uuid) # check that instance history's submission_date is equal to instance's # date_created - last_edited by default is null for an instance self.assertEquals(edited_instance.date_created, instance_history_1.submission_date) # check that '_last_edited' key is not in the json self.assertIn(LAST_EDITED, edited_instance.json) cursor = query_data(**query_args) self.assertEqual(cursor[0]['count'], num_data_instances + 1) # make sure we edited the mongo db record and NOT added a new row query_args['count'] = False cursor = query_data(**query_args) record = cursor[0] with open(xml_edit_submission_file_path, "r") as f: xml_str = f.read() xml_str = clean_and_parse_xml(xml_str).toxml() edited_name = re.match(r"^.+?<name>(.+?)</name>", xml_str).groups()[0] self.assertEqual(record['name'], edited_name) instance_before_second_edit = edited_instance xml_edit_submission_file_path = os.path.join( os.path.dirname(os.path.abspath(__file__)), "..", "fixtures", "tutorial", "instances", "tutorial_2012-06-27_11-27-53_w_uuid_edited_again.xml" ) self._make_submission(xml_edit_submission_file_path) cursor = query_data(**query_args) record = cursor[0] edited_instance = self.xform.instances.first() instance_history_2 = InstanceHistory.objects.last() self.assertEquals(instance_before_second_edit.last_edited, instance_history_2.submission_date) # check that '_last_edited' key is not in the json self.assertIn(LAST_EDITED, edited_instance.json) self.assertEqual(record['name'], 'Tom and Jerry') self.assertEqual( InstanceHistory.objects.count(), num_instances_history + 2) # submitting original submission is treated as a duplicate # does not add a new record # does not change data self._make_submission(xml_submission_file_path) self.assertEqual(self.response.status_code, 202) self.assertEqual(Instance.objects.count(), num_instances + 1) self.assertEqual( InstanceHistory.objects.count(), num_instances_history + 2)
def setUp(self): self.client = DigestClient() self.client.set_authorization('*****@*****.**','tester150411', 'Digest') self.test_data = os.path.join(DIR, 'testdata') self.XFORM_XML = os.path.join(self.test_data,'xform-024.xml') self.XFORM_XML_ALL_FIELDS = os.path.join(self.test_data,'xform_all_fields.xml')
class FormExportTest(TestCase): def setUp(self): self.app_id = 'kasdlfkjsldfkjsdlkjf' self.domain_name = 'form-export-test' self.domain = create_domain(self.domain_name) self.username = '******' self.couch_user = CommCareUser.create(self.domain_name, self.username, password='******') self.couch_user.save() self.client = Client() self.client.login(username=self.couch_user.username, password='******') self.url = reverse("receiver_post_with_app_id", args=[self.domain_name, self.app_id]) self.custom_export = FormExportSchema.wrap({ 'type': 'form', 'app_id': self.app_id, 'default_format': Format.JSON, 'index': json.dumps([self.domain_name, XMLNS]), 'tables': [{ 'index': '#', 'display': 'Export', 'columns': [{'index': 'form.name', 'display': 'Name'}], }] }) def tearDown(self): self.couch_user.delete() def post_it(self, user_id=None, form_id=XFORM_ID): user_id = user_id or self.couch_user._id f = StringIO(XML_DATA.format( user_id=user_id, xmlns=XMLNS, xform_id=form_id, )) f.name = 'form.xml' return self.client.post(self.url, {'xml_submission_file': f}) def test_include_duplicates(self): self.post_it() self.post_it() self.custom_export.include_errors = True files = self.custom_export.get_export_files() data = json.loads(files.file.payload) self.assertEqual(data['Export']['headers'], ['Name']) self.assertEqual(len(data['Export']['rows']), 2) self.custom_export.include_errors = False files = self.custom_export.get_export_files() data = json.loads(files.file.payload) self.assertEqual(data['Export']['headers'], ['Name']) self.assertEqual(len(data['Export']['rows']), 1) def test_exclude_unknown_users(self): self.post_it(form_id='good', user_id=self.couch_user._id) files = self.custom_export.get_export_files() data = json.loads(files.file.payload) self.assertEqual(len(data['Export']['rows']), 1) # posting from a non-real user shouldn't update self.post_it(form_id='bad', user_id='notarealuser') files = self.custom_export.get_export_files() data = json.loads(files.file.payload) self.assertEqual(len(data['Export']['rows']), 1) # posting from the real user should update self.post_it(form_id='stillgood', user_id=self.couch_user._id) files = self.custom_export.get_export_files() data = json.loads(files.file.payload) self.assertEqual(len(data['Export']['rows']), 2)