def testBasic(self): """Test basic backup functionality - create a backup by faking a post and make sure everything was created properly""" path = os.path.dirname(__file__) data_path = os.path.join(path, "data") # Make sure there's nothing to start self.assertEqual(0, BackupUser.objects.count()) self.assertEqual(0, Backup.objects.count()) #submit the xml populate("backup.xml", path=data_path) # should create a user and a backup self.assertEqual(1, BackupUser.objects.count()) self.assertEqual(1, Backup.objects.count()) # and they should be mapped correctly with the data matching # the (hard-coded) data in the xml user = BackupUser.objects.all()[0] backup = Backup.objects.all()[0] self.assertEqual("siwema", user.username) self.assertEqual(1, backup.users.count()) self.assertEqual(user, backup.users.all()[0]) self.assertEqual("RKEBWRSWIAFQ5VGKRC93YBV2C", backup.device_id) # also, make sure we created an instance of the right handler way_handled = SubmissionHandlingOccurrence.objects.get\ (submission=backup.attachment.submission) self.assertEqual(APP_NAME, way_handled.handled.app) self.assertEqual(BACKUP_HANDLER, way_handled.handled.method)
def test_sync_some_submissions(self): """ Tests synchronizing some data from self (posts a few MD5s) """ manager = XFormManager() # populate some files schema_1 = create_xsd_and_populate("pf_followup.xsd", "pf_followup_1.xml", path = DATA_DIR) schema_2 = create_xsd_and_populate("pf_new_reg.xsd", "pf_new_reg_1.xml", path = DATA_DIR) schema_3 = create_xsd_and_populate("pf_ref_completed.xsd", "pf_ref_completed_1.xml", path = DATA_DIR) # get MD5 of all current submissions MD5_buffer = rest_util.get_field_as_bz2(Submission, 'checksum') # populate a few more files submit_1 = populate("pf_followup_2.xml", path = DATA_DIR) submit_2 = populate("pf_new_reg_2.xml", path = DATA_DIR) submit_3 = populate("pf_ref_completed_2.xml", path = DATA_DIR) submit_4 = populate("pf_ref_completed_3.xml", path = DATA_DIR) starting_submissions_count = Submission.objects.all().count() starting_schemata_count = FormDefModel.objects.all().count() # get the difference between the first 3 files and the current # set of files (i.e. the last 4 files) submissions_file = "submissions.tar" self._POST_MD5s(MD5_buffer, submissions_file) # save checksums and delete the ones just populated (d,e,f) checksums = [ submit_1.checksum, submit_2.checksum, submit_3.checksum, submit_3.checksum ] manager.remove_data(schema_1.id, Metadata.objects.get(attachment=submit_1.xform).raw_data, \ remove_submission = True) manager.remove_data(schema_2.id, Metadata.objects.get(attachment=submit_2.xform).raw_data, \ remove_submission = True) manager.remove_data(schema_3.id, Metadata.objects.get(attachment=submit_3.xform).raw_data, \ remove_submission = True) manager.remove_data(schema_3.id, Metadata.objects.get(attachment=submit_4.xform).raw_data, \ remove_submission = True) # load data from sync file (d,e,f) load_submissions(submissions_file, "127.0.0.1:8000") try: # verify that the submissions etc. count are correct (d,e,f) self.assertEqual( starting_submissions_count, Submission.objects.all().count()) submits = Submission.objects.all().order_by('-submit_time')[:4] # verify that the correct submissions were loaded Submission.objects.get(checksum=checksums[0]) Submission.objects.get(checksum=checksums[1]) Submission.objects.get(checksum=checksums[2]) Submission.objects.get(checksum=checksums[3]) except Submission.DoesNotExist: self.fail("Incorrect submission received") finally: # clean up manager = XFormManager() manager.remove_schema(schema_1.id, remove_submissions = True) manager.remove_schema(schema_2.id, remove_submissions = True) manager.remove_schema(schema_3.id, remove_submissions = True)
def test_sync_dupe_submissions(self): """ Tests synchronizing duplicate data from self""" manager = XFormManager() # populate some files schema_1 = create_xsd_and_populate("pf_followup.xsd", "pf_followup_1.xml", path = DATA_DIR) schema_2 = create_xsd_and_populate("pf_new_reg.xsd", "pf_new_reg_1.xml", path = DATA_DIR) schema_3 = create_xsd_and_populate("pf_ref_completed.xsd", "pf_ref_completed_1.xml", path = DATA_DIR) starting_submissions_count = Submission.objects.all().count() # <STATE 1/> # get MD5 of 3 populated files MD5_buffer = rest_util.get_field_as_bz2(Submission, 'checksum') # add 3 dupes and 1 new file submit_1 = populate("pf_followup_1.xml", path = DATA_DIR) submit_2 = populate("pf_new_reg_1.xml", path = DATA_DIR) submit_3 = populate("pf_ref_completed_1.xml", path = DATA_DIR) # <STATE 2/> submissions_file = "submissions.tar" self._POST_MD5s(MD5_buffer, submissions_file) self._assert_tar_count_equals(submissions_file, 0) submit_4 = populate("pf_ref_completed_3.xml", path = DATA_DIR) # <STATE 3/> # get the difference between state 1 and state 3 self._POST_MD5s(MD5_buffer, submissions_file) # save checksum and delete the ones just populated checksum_4 = submit_4.checksum submit_1.delete() submit_2.delete() submit_3.delete() submit_4.delete() # should get the same 3 schemas we registered above self._assert_tar_count_equals(submissions_file, 1) # load data from sync file (d,e,f) load_submissions(submissions_file, "127.0.0.1:8000") try: # verify that we only have 4 submissions self.assertEqual( starting_submissions_count+1, Submission.objects.all().count() ) Submission.objects.get(checksum=checksum_4) except Submission.DoesNotExist: self.fail("Incorrect submission received") finally: # clean up manager = XFormManager() manager.remove_schema(schema_1.id, remove_submissions = True) manager.remove_schema(schema_2.id, remove_submissions = True) manager.remove_schema(schema_3.id, remove_submissions = True)
def test_sync_no_submissions(self): """ Tests synchronizing no data from self (posts all MD5s) """ manager = XFormManager() # load data schema_1 = create_xsd_and_populate("pf_followup.xsd", \ "pf_followup_1.xml", path = DATA_DIR) populate("pf_followup_2.xml", path = DATA_DIR) schema_2 = create_xsd_and_populate("pf_new_reg.xsd", \ "pf_new_reg_1.xml", path = DATA_DIR) populate("pf_new_reg_2.xml", path = DATA_DIR) schema_3 = create_xsd_and_populate("pf_ref_completed.xsd", \ "pf_ref_completed_1.xml", path = DATA_DIR) populate("pf_ref_completed_2.xml", path = DATA_DIR) populate("pf_ref_completed_3.xml", path = DATA_DIR) starting_submissions_count = Submission.objects.all().count() # get sync file from self submissions_file = "submissions.tar" generate_submissions(serverhost, 'brian', 'test', download=True, to=submissions_file) # test that the received submissions file is empty self._assert_tar_count_equals(submissions_file, 0) load_submissions(submissions_file, "127.0.0.1:8000") try: # verify that no new submissions were loaded self.assertEqual( starting_submissions_count, Submission.objects.all().count()) finally: # clean up manager.remove_schema(schema_1.id, remove_submissions = True) manager.remove_schema(schema_2.id, remove_submissions = True) manager.remove_schema(schema_3.id, remove_submissions = True)
def testBasicRegistration(self): path = os.path.dirname(__file__) data_path = os.path.join(path, "data") # Make sure there's nothing to start self.assertEqual(0, Phone.objects.count()) # submit the xml submission = populate("reg.xml", domain=self.domain, path=data_path) # should create a phone and user object self.assertEqual(1, Phone.objects.count()) self.assertEqual(1, PhoneUserInfo.objects.count()) # sanity check the data phone = Phone.objects.all()[0] user_info = PhoneUserInfo.objects.all()[0] django_user = user_info.user # phone self.assertEqual("67QQ86GVH8CCDNSCL0VQVKF7A", phone.device_id) # phone user info self.assertEqual(phone, user_info.phone) self.assertEqual(submission.attachments.all()[0], user_info.attachment) self.assertEqual("phone_registered", user_info.status) self.assertEqual("test_registration", user_info.username) self.assertEqual("1982", user_info.password) self.assertEqual("BXPKZLP49P3DDTJH3W0BRM2HV", user_info.uuid) self.assertEqual(date(2010,03,23), user_info.registered_on) expected_data = {"sid": "18", "hcbpid": "29", "district": "district 9", "region": "ally", "ward": "collins"} additional_data = user_info.additional_data for key in expected_data: self.assertTrue(key in additional_data, "Key %s should be set in the additional data" % key) self.assertEqual(expected_data[key], additional_data[key], "Value for %s was %s but should be %s" % \ (key, additional_data[key], expected_data[key])) for key in additional_data: self.assertTrue(key in expected_data, "Extra key %s was found in submitted data" % key) # django user self.assertNotEqual(None, django_user) self.assertEqual("test_registration", django_user.username) user_domains = Domain.active_for_user(django_user) self.assertEqual(1, user_domains.count()) self.assertEqual(self.domain, user_domains.all()[0]) # also, make sure we created an instance of the right handler way_handled = SubmissionHandlingOccurrence.objects.get\ (submission=user_info.attachment.submission) self.assertEqual(APP_NAME, way_handled.handled.app) self.assertEqual(REGISTRATION_HANDLER, way_handled.handled.method)
def test_generate_debug_submissions(self): """ Tests downloading some submissions from self This is only useful to make sure that the test_load_diff_submissions test below is working properly. """ #setup schema_1 = create_xsd_and_populate("pf_followup.xsd", \ "pf_followup_1.xml", path = DATA_DIR) populate("pf_followup_2.xml", path = DATA_DIR) schema_2 = create_xsd_and_populate("pf_new_reg.xsd", \ "pf_new_reg_1.xml", path = DATA_DIR) populate("pf_new_reg_2.xml", path = DATA_DIR) schema_3 = create_xsd_and_populate("pf_ref_completed.xsd", \ "pf_ref_completed_1.xml", path = DATA_DIR) populate("pf_ref_completed_2.xml", path = DATA_DIR) # the 'debug' flag limits the generated MD5s to a count of 5 submissions_file = "submissions.tar" # debug means we only post 5 submissions (instead of all) generate_submissions(serverhost, 'brian', 'test', debug=True, download=True, to=submissions_file) try: self._assert_tar_count_equals(submissions_file, Submission.objects.all().count()-5) # cleanup finally: # delete all data on self manager = XFormManager() manager.remove_schema(schema_1.id, remove_submissions = True) manager.remove_schema(schema_2.id, remove_submissions = True) manager.remove_schema(schema_3.id, remove_submissions = True)
def test_generate_all_submissions(self): """ Tests downloading all submissions from self """ # setup schema_1 = create_xsd_and_populate("pf_followup.xsd", path = DATA_DIR) submit_1 = populate("pf_followup_1.xml", path = DATA_DIR) submit_2 = populate("pf_followup_2.xml", path = DATA_DIR) schema_2 = create_xsd_and_populate("pf_new_reg.xsd", path = DATA_DIR) submit_3 = populate("pf_new_reg_1.xml", path = DATA_DIR) submit_4 = populate("pf_new_reg_2.xml", path = DATA_DIR) schema_3 = create_xsd_and_populate("pf_ref_completed.xsd", path = DATA_DIR) submit_5 = populate("pf_ref_completed_1.xml", path = DATA_DIR) submit_6 = populate("pf_ref_completed_2.xml", path = DATA_DIR) # download and check submissions_file = "submissions.tar" generate_submissions(serverhost, 'brian', 'test', latest=False, download=True, to=submissions_file) try: self._assert_tar_count_equals(submissions_file, Submission.objects.all().count()) # cleanup finally: # delete all data on self manager = XFormManager() submit_1.delete() submit_2.delete() submit_3.delete() submit_4.delete() submit_5.delete() submit_6.delete() manager.remove_schema(schema_1.id, remove_submissions = True) manager.remove_schema(schema_2.id, remove_submissions = True) manager.remove_schema(schema_3.id, remove_submissions = True)
def testBasicViews(self): domain = Domain.objects.get(name='mockdomain') formdef = create_xsd_and_populate("data/pf_followup.xsd", "data/pf_followup_1.xml", domain) instance = populate("data/pf_followup_2.xml") response = self.client.get('/xforms/') self.assertNotContains(response,"Error", status_code=200) self.assertNotContains(response,"Exception", status_code=200) response = self.client.get('/xforms/register/') self.assertNotContains(response,"Error", status_code=200) self.assertNotContains(response,"Exception", status_code=200) response = self.client.get('/xforms/reregister/mockdomain/') self.assertNotContains(response,"Error", status_code=200) self.assertNotContains(response,"Exception", status_code=200) response = self.client.get('/xforms/remove/%s/' % formdef.id) self.assertNotContains(response,"Error", status_code=200) self.assertNotContains(response,"Exception", status_code=200) response = self.client.get('/xforms/%s/submit/' % formdef.id) self.assertNotContains(response,"Error", status_code=200) self.assertNotContains(response,"Exception", status_code=200) urls = [ '/xforms/show/%s/' % formdef.id, '/xforms/show/%s/%s/' % (formdef.id, instance.id), '/xforms/show/%s/%s/csv/' % (formdef.id, instance.id), '/xforms/data/%s/delete/' % formdef.id, '/xforms/data/%s/' % formdef.id, '/xforms/data/%s/csv/' % formdef.id, '/xforms/data/%s/xml/' % formdef.id, ] for url in urls: self._test_valid_and_permissions(url, self.authuser, self.unauthuser) # format url variables like so: # response = self.client.get('/api/xforms/',{'format':'json'}) manager = XFormManager() manager.remove_schema(formdef.id)