def test_restore_in_progress_form_submitted_kills_old_jobs(self): """If the user submits a form somehow while a job is running, the job should be terminated """ task_cache_id = restore_cache_key(ASYNC_RESTORE_CACHE_KEY_PREFIX, self.user.user_id) initial_sync_cache_id = restore_cache_key(RESTORE_CACHE_KEY_PREFIX, self.user.user_id, version='2.0') fake_cached_thing = 'fake-cached-thing' restore_config = self._restore_config(async=True) # pretend we have a task running restore_config.cache.set(task_cache_id, fake_cached_thing) restore_config.cache.set(initial_sync_cache_id, fake_cached_thing) form = """ <data xmlns="http://openrosa.org/formdesigner/blah"> <meta> <userID>{user_id}</userID> </meta> </data> """ with mock.patch('corehq.form_processor.submission_post.revoke_celery_task') as revoke: # with a different user in the same domain, task doesn't get killed submit_form_locally(form.format(user_id="other_user"), self.domain) self.assertFalse(revoke.called) self.assertEqual(restore_config.cache.get(task_cache_id), fake_cached_thing) self.assertEqual(restore_config.cache.get(initial_sync_cache_id), fake_cached_thing) # task gets killed when the user submits a form submit_form_locally(form.format(user_id=self.user.user_id), self.domain) revoke.assert_called_with(fake_cached_thing) self.assertIsNone(restore_config.cache.get(task_cache_id)) self.assertIsNone(restore_config.cache.get(initial_sync_cache_id))
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_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_fire_signals(self): from corehq.apps.receiverwrapper.tests.test_submit_errors import failing_signal_handler case_id = uuid.uuid4().hex form_id = uuid.uuid4().hex with failing_signal_handler('signal death'): submit_case_blocks( CaseBlock(case_id=case_id, create=True, case_type='box').as_string().decode('utf-8'), self.domain, form_id=form_id ) form = self.formdb.get_form(form_id) with catch_signal(successful_form_received) as form_handler, catch_signal(case_post_save) as case_handler: submit_form_locally( instance=form.get_xml(), domain=self.domain, ) case = self.casedb.get_case(case_id) if should_use_sql_backend(self.domain): self.assertEqual(form, form_handler.call_args[1]['xform']) self.assertEqual(case, case_handler.call_args[1]['case']) else: signal_form = form_handler.call_args[1]['xform'] self.assertEqual(form.form_id, signal_form.form_id) self.assertEqual(form.get_rev, signal_form.get_rev) signal_case = case_handler.call_args[1]['case'] self.assertEqual(case.case_id, signal_case.case_id) self.assertEqual(case.get_rev, signal_case.get_rev)
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_device_logs(self): from corehq.apps.receiverwrapper.util import submit_form_locally from phonelog.models import DeviceReportEntry, ForceCloseEntry, UserEntry, UserErrorEntry from corehq.apps.users.models import CommCareUser from django.contrib.auth.models import User expected_object_counts = Counter({ User: 1, DeviceReportEntry: 7, UserEntry: 1, UserErrorEntry: 2, ForceCloseEntry: 1 }) user = CommCareUser.create( domain=self.domain_name, username='******', password='******', email='*****@*****.**', uuid='428d454aa9abc74e1964e16d3565d6b6' # match ID in devicelog.xml ) self.addCleanup(user.delete) with open('corehq/ex-submodules/couchforms/tests/data/devicelogs/devicelog.xml', 'rb') as f: xml = f.read() submit_form_locally(xml, self.domain_name) self._dump_and_load(expected_object_counts)
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_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 test_form_with_missing_xmlns(self): form_id = uuid.uuid4().hex form_template = """<?xml version='1.0' ?> <data uiVersion="1" version="1" name=""{xmlns}> <name>fgg</name> <n1:meta xmlns:n1="http://openrosa.org/jr/xforms"> <n1:deviceID>354957031935664</n1:deviceID> <n1:timeStart>2016-03-01T12:04:16Z</n1:timeStart> <n1:timeEnd>2016-03-01T12:04:16Z</n1:timeEnd> <n1:username>bcdemo</n1:username> <n1:userID>user-abc</n1:userID> <n1:instanceID>{form_id}</n1:instanceID> </n1:meta> </data>""" xml = form_template.format( form_id=form_id, xmlns=' xmlns="http://openrosa.org/formdesigner/456"' ) submit_form_locally(xml, self.domain_name) # hack the form to remove XMLNS since it's now validated during form submission form = FormAccessors(self.domain_name).get_form(form_id) form.xmlns = None del form.form_data['@xmlns'] xml_no_xmlns = form_template.format(form_id=form_id, xmlns="") form.delete_attachment('form.xml') form.put_attachment(xml_no_xmlns, 'form.xml') self._do_migration_and_assert_flags(self.domain_name) self.assertEqual(1, len(self._get_form_ids())) self._compare_diffs([])
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_dry_run(self): self.assertFalse(should_use_sql_backend(self.domain_name)) call_command( 'migrate_domain_from_couch_to_sql', self.domain_name, MIGRATE=True, no_input=True, dry_run=True ) clear_local_domain_sql_backend_override(self.domain_name) with self.assertRaises(CommandError): call_command('migrate_domain_from_couch_to_sql', self.domain_name, COMMIT=True, no_input=True) self.assertFalse(Domain.get_by_name(self.domain_name).use_sql_backend) xml = """<?xml version="1.0" ?> <n0:registration xmlns:n0="http://openrosa.org/user/registration"> <username>W4</username> <password>2</password> <uuid>P8DU7OLHVLZXU21JR10H3W8J2</uuid> <date>2013-11-19</date> <registering_phone_id>8H1N48EFPF6PA4UOO8YGZ2KFZ</registering_phone_id> <user_data> <data key="user_type">standard</data> </user_data> </n0:registration> """ submit_form_locally(xml, self.domain_name) couch_form_ids = self._get_form_ids() self.assertEqual(1, len(couch_form_ids)) call_command('migrate_domain_from_couch_to_sql', self.domain_name, blow_away=True, no_input=True) self.assertFalse(Domain.get_by_name(self.domain_name).use_sql_backend)
def test_get_ledger_values_for_case_as_of_same_date(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), ) ledger_blocks = [ get_single_balance_block(case_id, prod_id, balance) for prod_id, balance in balances ] submit_case_blocks(ledger_blocks, self.domain) # submit two transfers at the same time transfer_date = json_format_datetime(datetime.utcnow()) transfers = [ (self.product_a._id, 1, transfer_date), (self.product_a._id, 2, transfer_date), ] for prod_id, transfer, date in transfers: submit_case_blocks(get_single_transfer_block(case_id, None, prod_id, transfer, date), 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(1, len(results)) self.assertEqual(97, results[self.product_a._id])
def test_duplicate_form_published(self): form_id = uuid.uuid4().hex form_xml = get_simple_form_xml(form_id) orig_form = submit_form_locally(form_xml, domain=self.domain)[1] self.assertEqual(form_id, orig_form.form_id) self.assertEqual(1, len(self.form_accessors.get_all_form_ids_in_domain())) with process_kafka_changes(self.form_pillow): with process_couch_changes('DefaultChangeFeedPillow'): # post an exact duplicate dupe_form = submit_form_locally(form_xml, domain=self.domain)[1] self.assertTrue(dupe_form.is_duplicate) self.assertNotEqual(form_id, dupe_form.form_id) if should_use_sql_backend(self.domain): self.assertEqual(form_id, dupe_form.orig_id) # make sure changes made it to kafka dupe_form_meta = self.processor.changes_seen[0].metadata self.assertEqual(dupe_form.form_id, dupe_form_meta.document_id) self.assertEqual(dupe_form.domain, dupe_form.domain) if should_use_sql_backend(self.domain): # sql domains also republish the original form to ensure that if the server crashed # in the processing of the form the first time that it is still sent to kafka orig_form_meta = self.processor.changes_seen[1].metadata self.assertEqual(orig_form.form_id, orig_form_meta.document_id) self.assertEqual(self.domain, orig_form_meta.domain) self.assertEqual(dupe_form.domain, dupe_form.domain)
def submit_growth_form(domain, case_id, weight_prev, weight_child): xml = TEST_GROWTH_FORM_XML.format( xmlns=GROWTH_MONITORING_XMLNS, case_id=case_id, weight_prev=weight_prev, weight_child=weight_child, ) submit_form_locally(xml, domain)
def test_modified_on(self): form_id = uuid.uuid4().hex before = datetime.utcnow() xml = get_simple_form_xml(form_id) submit_form_locally(xml, DOMAIN) form = self.formdb.get_form(form_id) self.assertIsNotNone(form.server_modified_on) self.assertGreater(form.server_modified_on, before)
def test_v1_case(self): xml = """<?xml version="1.0" ?> <data name="pregnancy checklist" uiVersion="1" version="1" xmlns="http://openrosa.org/formdesigner/42461CD4-06D8-4FE5-BCEC-006130F7764F1" xmlns:jrm="http://dev.commcarehq.org/jr/xforms"> <name>RITA</name> <age>26</age> <number>918</number> <case> <case_id>P0YJ</case_id> <date_modified>2011-05-20T12:27:34.823+05:30</date_modified> <create> <case_type_id>pregnant_mother</case_type_id> <case_name>RITA</case_name> <user_id>XT3XPMS</user_id> <external_id>RITA</external_id> </create> <update> <name>RITA</name> </update> </case> <meta> <deviceID>8D24OUKK3AR4ZG7NF9CYSQFAT</deviceID> <timeStart>2011-05-20T12:25:17.882+05:30</timeStart> <timeEnd>2011-05-20T12:27:34.831+05:30</timeEnd> <username>adevi</username> <userID>XT3XPMS</userID> <uid>WXJYZ</uid> </meta> </data>""" submit_form_locally(xml, self.domain_name) update_xml = """<?xml version="1.0" ?> <data name="pregnancy checklist" uiVersion="1" version="1" xmlns="http://openrosa.org/formdesigner/42461CD4-06D8-4FE5-BCEC-006130F7764F" xmlns:jrm="http://dev.commcarehq.org/jr/xforms"> <case> <case_id>P0YJ</case_id> <date_modified>2012-02-24T00:51:07.836+05:30</date_modified> <close/> </case> <meta> <deviceID>44AV</deviceID> <timeStart>2012-02-24T00:46:43.007+05:30</timeStart> <timeEnd>2012-02-24T00:51:07.841+05:30</timeEnd> <username>rek</username> <userID>L53SD</userID> <uid>Z75H7</uid> </meta> </data>""" submit_form_locally(update_xml, self.domain_name) self.assertEqual(2, len(self._get_form_ids())) self.assertEqual(1, len(self._get_case_ids())) self._do_migration_and_assert_flags(self.domain_name) self.assertEqual(2, len(self._get_form_ids())) self.assertEqual(1, len(self._get_case_ids())) self._compare_diffs([])
def test_cant_own_case(self): result_alice = submit_form_locally(ALICE_XML, ALICE_DOMAIN) result_eve = submit_form_locally(EVE_XML, EVE_DOMAIN) self.assertIn('IllegalCaseId', result_eve.response.content.decode('utf-8')) self.assertNotIn('plan_to_buy_gun', result_alice.case.dynamic_case_properties()) result_alice_update = submit_form_locally(ALICE_UPDATE_XML, ALICE_DOMAIN) self.assertEqual(result_alice_update.case.dynamic_case_properties()['plan_to_buy_gun'], 'no')
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): xml = to_instance(data) logger.debug(xml) submit_form_locally( instance=xml, domain=domain, )
def test_pause(self): xform = self.get_xml('form') set_tz_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_tz_migration_complete(self.domain) # no issue submit_form_locally(xform, self.domain)
def test_submit_form_no_userid(self): form = """ <data xmlns="http://openrosa.org/formdesigner/blah"> <meta> <deviceID>test</deviceID> </meta> </data> """ submit_form_locally(form, self.domain)
def test_cant_own_case(self): result_alice = submit_form_locally(ALICE_XML, ALICE_DOMAIN) result_eve = submit_form_locally(EVE_XML, EVE_DOMAIN) self.assertIn('IllegalCaseId', result_eve.response.content) self.assertNotIn('plan_to_buy_gun', result_alice.case.dynamic_case_properties()) result_alice_update = submit_form_locally(ALICE_UPDATE_XML, ALICE_DOMAIN) self.assertEqual(result_alice_update.case.dynamic_case_properties()['plan_to_buy_gun'], 'no')
def test_submission_error_log_migration(self): try: submit_form_locally(b"To be an XForm or NOT to be an xform/>", self.domain_name) except LocalSubmissionError: pass self.assertEqual(1, len(self._get_form_ids(doc_type='SubmissionErrorLog'))) self._do_migration_and_assert_flags(self.domain_name) self.assertEqual(1, len(self._get_form_ids(doc_type='SubmissionErrorLog'))) self._compare_diffs([])
def test_submission_error_log_migration(self): try: submit_form_locally("To be an XForm or NOT to be an xform/>", self.domain_name) except LocalSubmissionError: pass self.assertEqual(1, len(self._get_form_ids(doc_type='SubmissionErrorLog'))) self._do_migration_and_assert_flags(self.domain_name) self.assertEqual(1, len(self._get_form_ids(doc_type='SubmissionErrorLog'))) self._compare_diffs([])
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_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 form_context(form_ids): for form_id in form_ids: builder = FormSubmissionBuilder( form_id=form_id, metadata=TestFormMetadata(domain=DOMAIN), ) submit_form_locally(builder.as_xml_string(), DOMAIN) try: yield finally: XFormInstance.objects.hard_delete_forms(DOMAIN, form_ids)
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 test_basic_form_migration_with_timezones(self): form_xml = self.get_xml('tz_form') with override_settings(PHONE_TIMEZONES_HAVE_BEEN_PROCESSED=False, PHONE_TIMEZONES_SHOULD_BE_PROCESSED=False): submit_form_locally(form_xml, self.domain_name) self.assertEqual(1, len(self._get_form_ids())) self.assertEqual(1, len(self._get_case_ids())) self._do_migration_and_assert_flags(self.domain_name) self.assertEqual(1, len(self._get_case_ids())) self.assertEqual(1, len(self._get_form_ids())) self._compare_diffs([])
def _submit_bp_form(self, form_date, case_id, using_ifa='no', num_ifa_consumed_last_seven_days=0, anemia=None, extra_meal='no', resting_during_pregnancy='no', counsel_immediate_bf='no', counsel_bp_vid='no', counsel_preparation='no', counsel_fp_vid='no', counsel_immediate_conception='no', counsel_accessible_postpartum_fp='no'): form = ElementTree.Element('data') form.attrib['xmlns'] = XMNLS_BP_FORM form.attrib['xmlns:jrm'] = 'http://openrosa.org/jr/xforms' meta = ElementTree.Element('meta') add_element(meta, 'timeEnd', form_date.isoformat()) form.append(meta) case = ElementTree.Element('case') case.attrib['date_modified'] = form_date.isoformat() case.attrib['case_id'] = case_id case.attrib['xmlns'] = 'http://commcarehq.org/case/transaction/v2' form.append(case) add_element(form, 'play_family_planning_vid', counsel_fp_vid) add_element(form, 'conceive', counsel_immediate_conception) bp1 = ElementTree.Element('bp1') add_element(bp1, 'using_ifa', using_ifa) if using_ifa == 'yes': add_element(bp1, 'ifa_last_seven_days', num_ifa_consumed_last_seven_days) add_element(bp1, 'anemia', anemia) add_element(bp1, 'eating_extra', extra_meal) add_element(bp1, 'resting', resting_during_pregnancy) form.append(bp1) bp2 = ElementTree.Element('bp2') add_element(bp2, 'immediate_breastfeeding', counsel_immediate_bf) add_element(bp2, 'play_birth_preparedness_vid', counsel_bp_vid) add_element(bp2, 'counsel_preparation', counsel_preparation) form.append(bp2) fp_group = ElementTree.Element('family_planning_group') add_element(fp_group, 'counsel_accessible_ppfp', counsel_accessible_postpartum_fp) form.append(fp_group) submit_form_locally(ElementTree.tostring(form), self.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 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 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 _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 test_basic_duplicate(self): xml_data = self.get_xml('duplicate') xform = submit_form_locally(xml_data, 'test-domain').xform self.assertEqual(self.ID, xform.form_id) self.assertTrue(xform.is_normal) self.assertEqual("test-domain", xform.domain) xform = submit_form_locally(xml_data, 'test-domain').xform self.assertNotEqual(self.ID, xform.form_id) self.assertTrue(xform.is_duplicate) self.assertTrue(self.ID in xform.problem) self.assertEqual(self.ID, xform.orig_id)
def setUp(self): delete_all_xforms() for item in FormData.objects.all(): item.delete() file_path = os.path.join(os.path.dirname(__file__), "data", "meta.xml") with open(file_path, "rb") as f: xml_data = f.read() submit_form_locally(xml_data, 'sofabed', app_id='12345', received_on=datetime.utcnow()) self.instance = XFormInstance.get('THIS_IS_THE_INSTANCEID')
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 test_basic_duplicate(self): xml_data = self.get_xml('duplicate') xform = submit_form_locally(xml_data, 'test-domain').xform self.assertEqual(self.ID, xform.form_id) self.assertTrue(xform.is_normal) self.assertEqual("test-domain", xform.domain) xform = submit_form_locally(xml_data, 'test-domain').xform self.assertNotEqual(self.ID, xform.form_id) self.assertTrue(xform.is_duplicate) self.assertTrue(self.ID in xform.problem) if getattr(settings, 'TESTS_SHOULD_USE_SQL_BACKEND', False): self.assertEqual(self.ID, xform.orig_id)
def test_copy_operations(self): original_xml = self.get_xml('original') edit_xml = self.get_xml('edit') xform = submit_form_locally(original_xml, self.domain).xform xform.archive(user_id='user1') xform.unarchive(user_id='user2') xform = submit_form_locally(edit_xml, self.domain).xform self.assertEqual(3, len(xform.history)) self.assertEqual('archive', xform.history[0].operation) self.assertEqual('unarchive', xform.history[1].operation) self.assertEqual('edit', xform.history[2].operation)
def test_duplicate_case_properties(self): """ Submit multiple values for the same property in an update block """ case_id = '061ecbae-d9be-4bb5-bdd4-e62abd5eaf7b' post_case_blocks([CaseBlock(create=True, case_id=case_id).as_xml()]) xml_data = self.get_xml('duplicate_case_properties') result = submit_form_locally(xml_data, 'test-domain') self.assertEqual("", result.case.dynamic_case_properties()['foo']) xml_data = self.get_xml('duplicate_case_properties_2') result = submit_form_locally(xml_data, 'test-domain') self.assertEqual("2", result.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 = CommCareCase.objects.get_case('30bc51f6-3247-4966-b4ae-994f572e85fe', 'test-domain') self.assertEqual('from the update form', case.case_json['pupdate']) self.assertEqual('from the create form', case.case_json['pcreate']) # NOTE the SQL form processor works differently than the Couch one did: # it processes submissions in the order they are received self.assertEqual('this should get overridden', case.case_json['pboth'])
def test_duplicate_form_migration(self): with open('corehq/ex-submodules/couchforms/tests/data/posts/duplicate.xml', encoding='utf-8') as f: duplicate_form_xml = f.read() submit_form_locally(duplicate_form_xml, self.domain_name) submit_form_locally(duplicate_form_xml, self.domain_name) self.assertEqual(1, len(self._get_form_ids())) self.assertEqual(1, len(self._get_form_ids('XFormDuplicate'))) self._do_migration_and_assert_flags(self.domain_name) self.assertEqual(1, len(self._get_form_ids())) self.assertEqual(1, len(self._get_form_ids('XFormDuplicate'))) self._compare_diffs([])
def test_wrong_domain(self): domain = 'test-domain' instance = self.get_xml('duplicate') result1 = submit_form_locally( instance, domain='wrong-domain', ) result2 = submit_form_locally( instance, domain=domain, ) self.assertNotEqual(result1.xform.form_id, result2.xform.form_id)
def submit_form(user_id, device_id, last_sync_token): form = """ <data xmlns="http://openrosa.org/formdesigner/blah"> <meta> <userID>{user_id}</userID> <deviceID>{device_id}</deviceID> </meta> </data> """ submit_form_locally( form.format(user_id=user_id, device_id=device_id), self.domain, last_sync_token=last_sync_token, )
def test_restore_caches_cleared(self): cache = get_redis_default_cache() cache_key = restore_cache_key(RESTORE_CACHE_KEY_PREFIX, 'user_id', version="2.0") cache.set(cache_key, 'test-thing') self.assertEqual(cache.get(cache_key), 'test-thing') form = """ <data xmlns="http://openrosa.org/formdesigner/blah"> <meta> <userID>{user_id}</userID> </meta> </data> """ submit_form_locally(form.format(user_id='user_id'), DOMAIN) self.assertIsNone(cache.get(cache_key))
def test_edit_form_with_attachments(self): attachment_source = './corehq/ex-submodules/casexml/apps/case/tests/data/attachments/fruity.jpg' attachment_file = open(attachment_source, 'rb') attachments = { 'fruity_file': UploadedFile(attachment_file, 'fruity_file', content_type='image/jpeg') } def _get_xml(date, form_id): return """<?xml version='1.0' ?> <data uiVersion="1" version="1" name="" xmlns="http://openrosa.org/formdesigner/123"> <name>fgg</name> <date>2011-06-07</date> <n1:meta xmlns:n1="http://openrosa.org/jr/xforms"> <n1:deviceID>354957031935664</n1:deviceID> <n1:timeStart>{date}</n1:timeStart> <n1:timeEnd>{date}</n1:timeEnd> <n1:username>bcdemo</n1:username> <n1:userID>user-abc</n1:userID> <n1:instanceID>{form_id}</n1:instanceID> </n1:meta> </data>""".format(date=date, attachment_source=attachment_source, form_id=form_id) form_id = uuid.uuid4().hex original_xml = _get_xml('2016-03-01T12:04:16Z', form_id) submit_form_locally( original_xml, self.domain, attachments=attachments, ) form = self.formdb.get_form(form_id) self.assertIn('fruity_file', form.attachments) self.assertIn(original_xml, form.get_xml().decode('utf-8')) # edit form edit_xml = _get_xml('2016-04-01T12:04:16Z', form_id) submit_form_locally( edit_xml, self.domain, ) form = self.formdb.get_form(form_id) self.assertIsNotNone(form.edited_on) self.assertIsNotNone(form.deprecated_form_id) self.assertIn('fruity_file', form.attachments) self.assertIn(edit_xml, form.get_xml().decode('utf-8'))
def test_modified_on_archive(self): form_id = uuid.uuid4().hex submit_form_locally(get_simple_form_xml(form_id), DOMAIN) before = datetime.utcnow() form = self.formdb.get_form(form_id) form.archive() form = self.formdb.get_form(form_id) self.assertGreater(form.server_modified_on, before) before = datetime.utcnow() form.unarchive() form = self.formdb.get_form(form_id) self.assertGreater(form.server_modified_on, before)
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 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') result = submit_form_locally( xml_data, 'test-domain', ) self.assertEqual(0, archive_counter) self.assertEqual(0, restore_counter) result.xform.archive() self.assertEqual(1, archive_counter) self.assertEqual(0, restore_counter) xform = self.formdb.get_form(result.xform.form_id) xform.unarchive() self.assertEqual(1, archive_counter) self.assertEqual(1, restore_counter)