def test_basic_devicelog(self): xml = self.get_xml('devicelog') submit_form_locally(xml, 'test-domain') # Assert Device Report Entries self.assertEqual(DeviceReportEntry.objects.count(), 7) first = DeviceReportEntry.objects.first() self.assertEqual(first.type, 'resources') self.assertEqual(first.domain, 'test-domain') self.assertEqual(first.msg, 'Staging Sandbox: 54cfe6515fc24e3fafa1170b9a7c2a00') self.assertEqual(first.device_id, '351780060530179') self.assertEqual( first.app_version, 'CommCare ODK, version "2.15.0"(335344). App v182. CommCare Version 2.15. Build 335344, built on: October-02-2014' ) self.assertEqual(first.i, 0) self.assertIsNotNone(first.server_date) self.assertIsNotNone(first.date) second = DeviceReportEntry.objects.all()[1] self.assertEqual(second.type, 'user') self.assertEqual(second.username, 'ricatla') self.assertEqual(second.user_id, '428d454aa9abc74e1964e16d3565d6b6') # Assert UserEntries self.assertEqual(UserEntry.objects.count(), 1) first = UserEntry.objects.first() self.assertIsNotNone(first.xform_id) self.assertEqual(first.i, 0) self.assertEqual(first.user_id, '428d454aa9abc74e1964e16d3565d6b6') self.assertEqual(first.username, 'ricatla') self.assertEqual(first.sync_token, '848609ceef09fa567e98ca61e3b0514d')
def test_uses_referrals(self): """ submit form with a case block that uses referrals check that - it errors - the form is not saved under its original id - an XFormError is saved with the original id as orig_id """ submit_form_locally( """<data xmlns="example.com/foo"> <meta> <instanceID>abc-easy-as-456</instanceID> </meta> <case case_id="123" xmlns="http://commcarehq.org/case/transaction/v2"> <referral> <referral_id>456</referral_id> <open> <referral_types>t1 t2</referral_types> </open> </referral> </case> </data>""", 'my_very_special_domain', ) xform_errors = FormProcessorInterface.get_by_doc_type('my_very_special_domain', 'XFormError') related_errors = [xform_error for xform_error in xform_errors if xform_error.id == 'abc-easy-as-456'] self.assertEqual(len(related_errors), 1) related_error = related_errors[0] self.assertEqual(related_error.problem, 'UsesReferrals: Sorry, referrals are no longer supported!')
def test_basic_devicelog(self): xml = self.get_xml('devicelog') submit_form_locally(xml, 'test-domain') self.assert_device_report_entries() self.assert_user_entries() self.assert_user_error_entries() self.assert_force_close_entries()
def test_device_report_submissions_ignored(self): """ submit a device report with a stock block and make sure it doesn't get processed """ def _assert_no_stock_transactions(): if should_use_sql_backend(self.domain.name): self.assertEqual(0, LedgerTransaction.objects.count()) else: self.assertEqual(0, StockTransaction.objects.count()) _assert_no_stock_transactions() fpath = os.path.join(os.path.dirname(__file__), 'data', 'xml', 'device_log.xml') with open(fpath) as f: form = f.read() amounts = [(p._id, 10) for p in self.products] product_block = products_xml(amounts) form = form.format( form_id=uuid.uuid4().hex, user_id=self.user._id, date=json_format_datetime(datetime.utcnow()), sp_id=self.sp.case_id, product_block=product_block ) submit_form_locally( instance=form, domain=self.domain.name, ) _assert_no_stock_transactions()
def test_device_report_submissions_ignored(self): """ submit a device report with a stock block and make sure it doesn't get processed """ def _assert_no_stock_transactions(): if should_use_sql_backend(self.domain): self.assertEqual(0, LedgerTransaction.objects.count()) else: self.assertEqual(0, StockTransaction.objects.count()) _assert_no_stock_transactions() fpath = os.path.join(os.path.dirname(__file__), 'data', 'xml', 'device_log.xml') with open(fpath) as f: form = f.read() amounts = [(p._id, 10) for p in self.products] product_block = products_xml(amounts) form = form.format(form_id=uuid.uuid4().hex, user_id=self.user._id, date=json_format_datetime(datetime.utcnow()), sp_id=self.sp.case_id, product_block=product_block) submit_form_locally( instance=form, domain=self.domain.name, ) _assert_no_stock_transactions()
def test_no_case_id(self): """ submit form with a case block that has no case_id check that - it errors - the form is not saved under its original id - an XFormError is saved with the original id as orig_id - the error was logged (<-- is this hard to test?) <data xmlns="example.com/foo"> <case case_id=""> <update><foo>bar</foo></update> </case> </data> """ submit_form_locally( """<data xmlns="example.com/foo"> <meta> <instanceID>abc-easy-as-123</instanceID> </meta> <case case_id="" xmlns="http://commcarehq.org/case/transaction/v2"> <update><foo>bar</foo></update> </case> </data>""", 'my_very_special_domain', ) xform_errors = FormProcessorInterface.get_by_doc_type('my_very_special_domain', 'XFormError') related_errors = [xform_error for xform_error in xform_errors if xform_error.id == 'abc-easy-as-123'] self.assertEqual(len(related_errors), 1) related_error = related_errors[0] self.assertEqual(related_error.problem, 'IllegalCaseId: case_id must not be empty')
def create_case(case_id, files, patient_case_id=None): """ Handle case submission for the sonosite endpoint """ # we already parsed what we need from this, so can just remove it # without worrying we will need it later files.pop('PT_PPS.XML', '') xform = render_sonosite_xform(files, case_id, patient_case_id) file_dict = {} for f in files: file_dict[f] = UploadedFile(files[f], f) submit_form_locally( instance=xform, attachments=file_dict, domain=UTH_DOMAIN, ) # this is a bit of a hack / abstraction violation # would be nice if submit_form_locally returned info about cases updated case_ids = { case_update.id for case_update in get_case_updates(convert_xform_to_json(xform)) } return [CommCareCase.get(case_id) for case_id in case_ids]
def test_get_ledger_values_for_case_as_of(self): case_id = uuid.uuid4().hex form_xml = get_simple_form_xml(uuid.uuid4().hex, case_id) submit_form_locally(form_xml, domain=self.domain)[1] # submit ledger data balances = ( (self.product_a._id, 100), (self.product_b._id, 50), ) ledger_blocks = [ get_single_balance_block(case_id, prod_id, balance) for prod_id, balance in balances ] submit_case_blocks(ledger_blocks, self.domain) # check results results = get_ledger_values_for_case_as_of( domain=self.domain, case_id=case_id, section_id='stock', as_of=datetime.utcnow()) self.assertEqual(2, len(results)) self.assertEqual(100, results[self.product_a._id]) self.assertEqual(50, results[self.product_b._id]) # check the date filter works before_data = datetime.utcnow() - timedelta(days=2) self.assertEqual({}, get_ledger_values_for_case_as_of( domain=self.domain, case_id=case_id, section_id='stock', as_of=before_data))
def test_conflicting_ids(self): """ If a form and a case share an ID it's a conflict """ xml_data = self.get_xml('id_conflicts') with self.assertRaises(BulkSaveError): submit_form_locally(xml_data, 'test-domain')
def test_get_ledger_values_for_case_as_of(self): case_id = uuid.uuid4().hex form_xml = get_simple_form_xml(uuid.uuid4().hex, case_id) submit_form_locally(form_xml, domain=self.domain)[1] # submit ledger data balances = ( (self.product_a._id, 100), (self.product_b._id, 50), ) ledger_blocks = [ get_single_balance_block(case_id, prod_id, balance) for prod_id, balance in balances ] submit_case_blocks(ledger_blocks, self.domain) # check results results = get_ledger_values_for_case_as_of(domain=self.domain, case_id=case_id, section_id='stock', as_of=datetime.utcnow()) self.assertEqual(2, len(results)) self.assertEqual(100, results[self.product_a._id]) self.assertEqual(50, results[self.product_b._id]) # check the date filter works before_data = datetime.utcnow() - timedelta(days=2) self.assertEqual({}, get_ledger_values_for_case_as_of(domain=self.domain, case_id=case_id, section_id='stock', as_of=before_data))
def submit_stock_update(user, site_code, product_code, balance): """For local testing only.""" case, location = get_supply_point_and_location(user.domain, site_code) product = SQLProduct.objects.get(domain=user.domain, code=product_code) tx = StockTransactionHelper( product_id=product.product_id, action=StockActions.STOCKONHAND, domain=user.domain, quantity=balance, location_id=location.location_id, timestamp=datetime.utcnow(), case_id=case.case_id, section_id=SECTION_TYPE_STOCK, ) xml = to_instance({ 'timestamp': datetime.utcnow(), 'user': user, 'phone': user.phone_number or '8675309', 'location': location, 'transactions': [tx], }) submit_form_locally( instance=xml, domain=user.domain, )
def test_migration(self): xform = self.get_xml("form") form_bad_tz = self.get_json("form") case_bad_tz = self.get_json("case") form_good_tz = self.get_json("form-tz") case_good_tz = self.get_json("case-tz") with override_settings(PHONE_TIMEZONES_HAVE_BEEN_PROCESSED=False, PHONE_TIMEZONES_SHOULD_BE_PROCESSED=False): submit_form_locally(xform, self.domain) # Form before xform_instance, = get_forms_by_type(self.domain, "XFormInstance", limit=10) xform_json = xform_instance.to_json() self._compare_forms(xform_json, form_bad_tz, "Form before migration does not match") # Case before case, = get_cases_in_domain(self.domain) self._compare_cases(case.to_json(), case_bad_tz, "Case before migration does not match") run_timezone_migration_for_domain(self.domain) # Form after xform_instance, = get_forms_by_type(self.domain, "XFormInstance", limit=10) xform_json = xform_instance.to_json() self._compare_forms(xform_json, form_good_tz, "Form after migration does not match") # Case after case, = get_cases_in_domain(self.domain) self._compare_cases(case.to_json(), case_good_tz, "Case after migration does not match")
def submit_case_blocks( case_blocks, domain, username="******", user_id="", xmlns="http://commcarehq.org/case", attachments=None, form_id=None, ): """ Submits casexml in a manner similar to how they would be submitted from a phone. returns the UID of the resulting form. """ attachments = attachments or {} now = json_format_datetime(datetime.datetime.utcnow()) if not isinstance(case_blocks, basestring): case_blocks = "".join(case_blocks) form_id = form_id or uuid.uuid4().hex form_xml = render_to_string( "hqcase/xml/case_block.xml", { "xmlns": xmlns, "case_block": case_blocks, "time": now, "uid": form_id, "username": username, "user_id": user_id, }, ) submit_form_locally(instance=form_xml, domain=domain, attachments=attachments) return form_id
def testUserRestoreWithCase(self): file_path = os.path.join(os.path.dirname(__file__), "data", "create_short.xml") with open(file_path, "rb") as f: xml_data = f.read() submit_form_locally(xml_data, self.domain) expected_case_block = """ <case case_id="asdf" date_modified="2010-06-29T13:42:50.000000Z" user_id="foo" xmlns="http://commcarehq.org/case/transaction/v2"> <create> <case_type>test_case_type</case_type> <case_name>test case name</case_name> <owner_id>foo</owner_id> </create> <update> <external_id>someexternal</external_id> </update> </case>""" restore_payload = generate_restore_payload( project=Domain(name=self.domain), user=dummy_user(), items=True, version=V3 ) sync_log_id = SyncLog.view( "phone/sync_logs_by_user", include_docs=True, reduce=False, ).one().get_id check_xml_line_by_line( self, dummy_restore_xml(sync_log_id, expected_case_block, items=4), restore_payload )
def submit_case_blocks(case_blocks, domain, username="******", user_id="", xmlns='http://commcarehq.org/case', attachments=None, form_id=None): """ Submits casexml in a manner similar to how they would be submitted from a phone. returns the UID of the resulting form. """ attachments = attachments or {} now = json_format_datetime(datetime.datetime.utcnow()) if not isinstance(case_blocks, basestring): case_blocks = ''.join(case_blocks) form_id = form_id or uuid.uuid4().hex form_xml = render_to_string( 'hqcase/xml/case_block.xml', { 'xmlns': xmlns, 'case_block': case_blocks, 'time': now, 'uid': form_id, 'username': username, 'user_id': user_id, }) submit_form_locally( instance=form_xml, domain=domain, attachments=attachments, ) return form_id
def submit_case_blocks(case_blocks, domain, username="******", user_id="", xmlns='http://commcarehq.org/case', attachments=None, form_id=None): """ Submits casexml in a manner similar to how they would be submitted from a phone. returns the UID of the resulting form. """ attachments = attachments or {} now = json_format_datetime(datetime.datetime.utcnow()) if not isinstance(case_blocks, basestring): case_blocks = ''.join(case_blocks) form_id = form_id or uuid.uuid4().hex form_xml = render_to_string('hqcase/xml/case_block.xml', { 'xmlns': xmlns, 'case_block': case_blocks, 'time': now, 'uid': form_id, 'username': username, 'user_id': user_id, }) submit_form_locally( instance=form_xml, domain=domain, attachments=attachments, ) return form_id
def test_pause(self): xform = self.get_xml('form') set_migration_started(self.domain) with self.assertRaisesRegexp(LocalSubmissionError, 'status code 503'): submit_form_locally(xform, self.domain) _run_timezone_migration_for_domain(self.domain) set_migration_complete(self.domain) # no issue submit_form_locally(xform, self.domain)
def test_cant_own_case(self): _, _, [case] = submit_form_locally(ALICE_XML, ALICE_DOMAIN) response, form, cases = submit_form_locally(EVE_XML, EVE_DOMAIN) self.assertIn('IllegalCaseId', response.content) self.assertNotIn('plan_to_buy_gun', case.dynamic_case_properties()) _, _, [case] = submit_form_locally(ALICE_UPDATE_XML, ALICE_DOMAIN) self.assertEqual(case.dynamic_case_properties()['plan_to_buy_gun'], 'no')
def test_pause(self): xform = self.get_xml("form") set_migration_started(self.domain) with self.assertRaisesRegexp(LocalSubmissionError, "status code 503"): submit_form_locally(xform, self.domain) _run_timezone_migration_for_domain(self.domain) set_migration_complete(self.domain) # no issue submit_form_locally(xform, self.domain)
def _create_form(app_id, xmlns, with_attachment): xml = FORM_XML.format(doc_id=random_hex(), xmlns=xmlns) attachments = {} if with_attachment: attachment = open('./corehq/ex-submodules/casexml/apps/case/tests/data/attachments/fruity.jpg', 'rb') attachments = { 'pic.jpg': UploadedFile(attachment, 'pic.jpg') } submit_form_locally(xml, domain=DOMAIN, app_id=app_id, attachments=attachments)
def test_broken_save(self): """ Test that if the second form submission terminates unexpectedly and the main form isn't saved, then there are no side effects such as the original having been marked as deprecated. """ class BorkDB(object): """context manager for making a db's bulk_save temporarily fail""" def __init__(self, db): self.old = {} self.db = db def __enter__(self): self.old['bulk_save'] = self.db.bulk_save self.db.bulk_save = MagicMock(name='bulk_save', side_effect=RequestFailed()) def __exit__(self, exc_type, exc_val, exc_tb): self.db.bulk_save = self.old['bulk_save'] xforms = FormProcessorInterface.get_by_doc_type(self.domain, 'XFormInstance') self.assertEqual(len(xforms), 0) xml_data1, xml_data2 = self._get_files() submit_form_locally(xml_data1, self.domain) xform = FormProcessorInterface.get_xform(self.ID) self.assertEqual(self.ID, xform.id) self.assertEqual("XFormInstance", xform.doc_type) self.assertEqual(self.domain, xform.domain) self.assertEqual( UnfinishedSubmissionStub.objects.filter(xform_id=self.ID).count(), 0 ) # This seems like a couch specific test util. Will likely need postgres test utils with BorkDB(XFormInstance.get_db()): with self.assertRaises(RequestFailed): submit_form_locally(xml_data2, self.domain) # it didn't go through, so make sure there are no edits still xforms = FormProcessorInterface.get_by_doc_type(self.domain, 'XFormDeprecated') self.assertEqual(len(xforms), 0) xform = FormProcessorInterface.get_xform(self.ID) self.assertIsNotNone(xform) self.assertEqual( UnfinishedSubmissionStub.objects.filter(xform_id=self.ID, saved=False).count(), 1 ) self.assertEqual( UnfinishedSubmissionStub.objects.filter(xform_id=self.ID).count(), 1 )
def testTopLevelExclusion(self): """ Entire forms tagged as device logs should be excluded """ file_path = os.path.join(os.path.dirname(__file__), "data", "exclusion", "device_report.xml") with open(file_path, "rb") as f: xml_data = f.read() submit_form_locally(xml_data, TEST_DOMAIN) self.assertEqual(0, len(CaseAccessors(TEST_DOMAIN).get_case_ids_in_domain()))
def process(domain, data): import pprint logger.debug(pprint.pformat(data)) xml = to_instance(data) logger.debug(xml) submit_form_locally(instance=xml, domain=domain)
def test_broken_save(self): """ Test that if the second form submission terminates unexpectedly and the main form isn't saved, then there are no side effects such as the original having been marked as deprecated. """ class BorkDB(object): """context manager for making a db's bulk_save temporarily fail""" def __init__(self, db): self.old = {} self.db = db def __enter__(self): self.old['bulk_save'] = self.db.bulk_save self.db.bulk_save = MagicMock(name='bulk_save', side_effect=RequestFailed()) def __exit__(self, exc_type, exc_val, exc_tb): self.db.bulk_save = self.old['bulk_save'] self.assertEqual( XFormInstance.view('couchforms/edits', key=self.ID).count(), 0) self.assertFalse(XFormInstance.get_db().doc_exist(self.ID)) xml_data1, xml_data2 = self._get_files() submit_form_locally(xml_data1, self.domain) doc = XFormInstance.get(self.ID) self.assertEqual(self.ID, doc.get_id) self.assertEqual("XFormInstance", doc.doc_type) self.assertEqual(self.domain, doc.domain) self.assertEqual( UnfinishedSubmissionStub.objects.filter(xform_id=self.ID).count(), 0 ) with BorkDB(XFormInstance.get_db()): with self.assertRaises(RequestFailed): submit_form_locally(xml_data2, self.domain) # it didn't go through, so make sure there are no edits still self.assertEqual( XFormInstance.view('couchforms/edits', key=self.ID).count(), 0) self.assertTrue(XFormInstance.get_db().doc_exist(self.ID)) self.assertEqual( UnfinishedSubmissionStub.objects.filter(xform_id=self.ID, saved=False).count(), 1 ) self.assertEqual( UnfinishedSubmissionStub.objects.filter(xform_id=self.ID).count(), 1 )
def testDuplicateCasePropertiesBug(self): """ Submit multiple values for the same property in an update block """ xml_data = self.get_xml('duplicate_case_properties') response, form, [case] = submit_form_locally(xml_data, 'test-domain') self.assertEqual("", case.dynamic_case_properties()['foo']) xml_data = self.get_xml('duplicate_case_properties_2') response, form, [case] = submit_form_locally(xml_data, 'test-domain') self.assertEqual("2", case.dynamic_case_properties()['bar'])
def testOutOfOrderSubmissions(self): dir = os.path.join(os.path.dirname(__file__), "data", "ordering") for fname in ('update_oo.xml', 'create_oo.xml'): with open(os.path.join(dir, fname), "rb") as f: xml_data = f.read() submit_form_locally(xml_data, 'test-domain') case = CaseAccessors().get_case('30bc51f6-3247-4966-b4ae-994f572e85fe') self.assertEqual('from the update form', case.pupdate) self.assertEqual('from the create form', case.pcreate) self.assertEqual('overridden by the update form', case.pboth)
def attach_images_to_case(case_id, files): """ Handle case submission for the vscan endpoint """ xform = render_vscan_xform(case_id, files) file_dict = {} for f in files: identifier = os.path.split(f)[-1] file_dict[identifier] = UploadedFile(files[f], identifier) submit_form_locally(xform, attachments=file_dict, domain=UTH_DOMAIN)
def _create_form(app_id, xmlns, with_attachment): xml = FORM_XML.format(doc_id=random_hex(), xmlns=xmlns) attachments = {} if with_attachment: attachment = open( './corehq/ex-submodules/casexml/apps/case/tests/data/attachments/fruity.jpg', 'rb') attachments = {'pic.jpg': UploadedFile(attachment, 'pic.jpg')} submit_form_locally(xml, domain=DOMAIN, app_id=app_id, attachments=attachments)
def process(domain, data): import pprint logger.debug(pprint.pformat(data)) xml = to_instance(data) logger.debug(xml) submit_form_locally( instance=xml, domain=domain, )
def submit_error_case(case_id): """ Used if something went wrong creating the real vscan case update. """ xform = render_vscan_error(case_id) submit_form_locally( instance=xform, domain=UTH_DOMAIN, )
def test_broken_save(self): """ Test that if the second form submission terminates unexpectedly and the main form isn't saved, then there are no side effects such as the original having been marked as deprecated. """ class BorkDB(object): """context manager for making a db's bulk_save temporarily fail""" def __init__(self, db): self.old = {} self.db = db def __enter__(self): self.old['bulk_save'] = self.db.bulk_save self.db.bulk_save = MagicMock(name='bulk_save', side_effect=RequestFailed()) def __exit__(self, exc_type, exc_val, exc_tb): self.db.bulk_save = self.old['bulk_save'] self.assertEqual(access_edits(key=self.ID).count(), 0) self.assertFalse(XFormInstance.get_db().doc_exist(self.ID)) xml_data1, xml_data2 = self._get_files() submit_form_locally(xml_data1, self.domain) doc = XFormInstance.get(self.ID) self.assertEqual(self.ID, doc.get_id) self.assertEqual("XFormInstance", doc.doc_type) self.assertEqual(self.domain, doc.domain) self.assertEqual( UnfinishedSubmissionStub.objects.filter(xform_id=self.ID).count(), 0 ) with BorkDB(XFormInstance.get_db()): with self.assertRaises(RequestFailed): submit_form_locally(xml_data2, self.domain) # it didn't go through, so make sure there are no edits still self.assertEqual(access_edits(key=self.ID).count(), 0) self.assertTrue(XFormInstance.get_db().doc_exist(self.ID)) self.assertEqual( UnfinishedSubmissionStub.objects.filter(xform_id=self.ID, saved=False).count(), 1 ) self.assertEqual( UnfinishedSubmissionStub.objects.filter(xform_id=self.ID).count(), 1 )
def test_wrong_domain(self): domain = 'test-domain' instance = self.get_xml('duplicate') _, xform1, _ = submit_form_locally( instance, domain='wrong-domain', ) _, xform2, _ = submit_form_locally( instance, domain=domain, ) self.assertNotEqual(xform1.form_id, xform2.form_id)
def submit_xml_form(self, xml_method, **submit_extras): instance_id = uuid.uuid4().hex instance = submission_wrap( instance_id, self.products, self.user, self.sp, self.sp2, xml_method, ) submit_form_locally(instance=instance, domain=self.domain.name, **submit_extras) return instance_id
def submit_xml_form(self, xml_method, timestamp=None, date_formatter=json_format_datetime, **submit_extras): instance_id = uuid.uuid4().hex instance = submission_wrap( instance_id, self.products, self.user, self.sp.case_id, self.sp2.case_id, xml_method, timestamp=timestamp, date_formatter=date_formatter, ) submit_form_locally(instance=instance, domain=self.domain.name, **submit_extras) return instance_id
def test_duplicate_case_published(self): case_id = uuid.uuid4().hex form_xml = get_simple_form_xml(uuid.uuid4().hex, case_id) submit_form_locally(form_xml, domain=self.domain)[1] self.assertEqual(1, len(CaseAccessors(self.domain).get_case_ids_in_domain())) case_consumer = get_test_kafka_consumer(topics.CASE_SQL) dupe_form = submit_form_locally(form_xml, domain=self.domain)[1] self.assertTrue(dupe_form.is_duplicate) # check the case was republished case_meta = change_meta_from_kafka_message(case_consumer.next().value) self.assertEqual(case_id, case_meta.document_id) self.assertEqual(self.domain, case_meta.domain)
def test_case_ledger_form(self): form_xml = self.get_xml('case_ledger_form') _, xform, cases = submit_form_locally(form_xml, domain=self.domain) transaction = cases[0].get_transaction_by_form_id(xform.form_id) self.assertTrue(transaction.is_form_transaction) self.assertTrue(transaction.is_case_create) self.assertTrue(transaction.is_case_close) self.assertTrue(transaction.is_ledger_transaction) form_xml = self.get_xml('case_ledger_form_2') _, xform, cases = submit_form_locally(form_xml, domain=self.domain) transaction = cases[0].get_transaction_by_form_id(xform.form_id) self.assertTrue(transaction.is_form_transaction)
def submit_xml_form(self, xml_method, **submit_extras): from casexml.apps.case import settings settings.CASEXML_FORCE_DOMAIN_CHECK = False instance = submission_wrap( self.products, self.user, self.sp, self.sp2, xml_method, ) submit_form_locally( instance=instance, domain=self.domain.name, **submit_extras )
def submit_xml_form(self, xml_method, **submit_extras): from casexml.apps.case import settings settings.CASEXML_FORCE_DOMAIN_CHECK = False instance_id = uuid.uuid4().hex instance = submission_wrap( instance_id, self.products, self.user, self.sp, self.sp2, xml_method, ) submit_form_locally(instance=instance, domain=self.domain.name, **submit_extras) return instance_id
def test_empty_case_id(self): """ Ensure that form processor fails on empty id """ xml_data = self.get_xml('empty_id') response, form, cases = submit_form_locally(xml_data, 'test-domain') self.assertIn('IllegalCaseId', response.content)
def testLotsOfSubcases(self): """ How do we do when submitting a form with multiple blocks for the same case? """ xml_data = self.get_xml('lots_of_subcases') response, form, cases = submit_form_locally(xml_data, 'test-domain') self.assertEqual(11, len(cases))
def submit_xml_form(self, xml_method, **submit_extras): instance_id = uuid.uuid4().hex instance = submission_wrap( instance_id, self.products, self.user, self.sp, self.sp2, xml_method, ) submit_form_locally( instance=instance, domain=self.domain.name, **submit_extras ) return instance_id
def testSignal(self): global archive_counter, restore_counter archive_counter = 0 restore_counter = 0 def count_archive(**kwargs): global archive_counter archive_counter += 1 def count_unarchive(**kwargs): global restore_counter restore_counter += 1 xform_archived.connect(count_archive) xform_unarchived.connect(count_unarchive) xml_data = self.get_xml('basic') response, xform, cases = submit_form_locally( xml_data, 'test-domain', ) self.assertEqual(0, archive_counter) self.assertEqual(0, restore_counter) xform.archive() self.assertEqual(1, archive_counter) self.assertEqual(0, restore_counter) xform = self.formdb.get_form(xform.form_id) xform.unarchive() self.assertEqual(1, archive_counter) self.assertEqual(1, restore_counter)
def _make_form(self, build_id=None): metadata = TestFormMetadata(domain=self.domain) form_xml = get_simple_form_xml(uuid.uuid4().hex, metadata=metadata) _, form, _ = submit_form_locally(form_xml, self.domain, build_id=build_id or self.app._id) return form
def test_no_case_id(self): """ submit form with a case block that has no case_id check that - it errors - the form is not saved under its original id - an XFormError is saved with the original id as orig_id - the error was logged (<-- is this hard to test?) <data xmlns="example.com/foo"> <case case_id=""> <update><foo>bar</foo></update> </case> </data> """ domain = 'special_domain' _, xform, _ = submit_form_locally( """<data xmlns="example.com/foo"> <meta> <instanceID>abc-easy-as-123</instanceID> </meta> <case case_id="" xmlns="http://commcarehq.org/case/transaction/v2"> <update><foo>bar</foo></update> </case> </data>""", domain, ) self.assertTrue(xform.is_error) self.assertEqual(xform.problem, 'IllegalCaseId: case_id must not be empty')
def test_uses_referrals(self): """ submit form with a case block that uses referrals check that - it errors - the form is not saved under its original id - an XFormError is saved with the original id as orig_id """ domain = 'special_domain' _, xform, _ = submit_form_locally( """<data xmlns="example.com/foo"> <meta> <instanceID>abc-easy-as-456</instanceID> </meta> <case case_id="123" xmlns="http://commcarehq.org/case/transaction/v2"> <referral> <referral_id>456</referral_id> <open> <referral_types>t1 t2</referral_types> </open> </referral> </case> </data>""", domain, ) self.assertTrue(xform.is_error) self.assertEqual( xform.problem, 'UsesReferrals: Sorry, referrals are no longer supported!')
def submit_case_blocks(case_blocks, domain, username="******", user_id="", xmlns='http://commcarehq.org/case'): now = json_format_datetime(datetime.datetime.utcnow()) if not isinstance(case_blocks, basestring): case_blocks = ''.join(case_blocks) form_xml = render_to_string('hqcase/xml/case_block.xml', { 'xmlns': xmlns, 'case_block': case_blocks, 'time': now, 'uid': uuid.uuid4().hex, 'username': username, 'user_id': user_id, }) submit_form_locally( instance=form_xml, domain=domain, )
def process_sms_form_complete(session, form, completed=True): resp, xform, cases = submit_form_locally(form, session.domain, app_id=session.app_id, partial_submission=not completed) session.end(completed=completed) session.submission_id = xform.form_id session.save()
def submit_xml_form(self, xml_method, timestamp=None, date_formatter=json_format_datetime, **submit_extras): instance_id = uuid.uuid4().hex instance = submission_wrap( instance_id, self.products, self.user, self.sp._id, self.sp2._id, xml_method, timestamp=timestamp, date_formatter=date_formatter, ) submit_form_locally( instance=instance, domain=self.domain.name, **submit_extras ) return instance_id