def test_citizen_created(self, get_records, open): citizen = CitizenFactory() data = model_to_dict(citizen) citizen.delete() get_records.return_value = [data] self.assertFalse(Citizen.objects.exists()) stats = import_citizen_dump(input_filename=None) self.assertEqual(1, stats.records_read) new_cit = Citizen.objects.get() self.assertEqual(data, model_to_dict(new_cit))
def test_get_citizens_to_change_by_upload(self): changeset = ChangesetFactory( how_to_select=Changeset.SELECT_UPLOADED_NIDS) citizen1 = CitizenFactory() citizen2 = CitizenFactory() citizen3 = CitizenFactory() changeset.selected_citizens.add(citizen1, citizen2) self.assertIn(citizen1, changeset.get_citizens_to_change()) self.assertIn(citizen2, changeset.get_citizens_to_change()) self.assertNotIn(citizen3, changeset.get_citizens_to_change())
def test_upload_file_invalid_line(self): # We notice a file with too short a line data = self.data data['how_to_select'] = str(Changeset.SELECT_UPLOADED_NIDS) citizen1 = CitizenFactory() citizen2 = CitizenFactory() filetext = "1{nid1}\n{nid2}\n".format(nid1=citizen1.national_id, nid2=citizen2.national_id) upload_file = SimpleUploadedFile('my_filename', filetext) form = ChangesetForm(data=data, files={'upload_file': upload_file}, request=MagicMock()) self.assertFalse(form.is_valid()) self.assertIn('upload_file', form.errors)
def test_record_gone_do_not_delete_with_existing(self): existing = CitizenFactory(pk=1) TempCitizenFactory(pk=1, **model_to_dict(existing)) cit = CitizenFactory(pk=2) existing2 = CitizenFactory(pk=3) TempCitizenFactory(pk=3, **model_to_dict(existing2)) stats = mirror_database(from_model=TempCitizen, to_model=Citizen) self.assertEqual(2, stats.unchanged_count) self.assertEqual(0, stats.modified_record_count) self.assertEqual(0, stats.new_record_count) self.assertEqual(1, stats.not_there_anymore_count) # NOT deleted self.assertTrue(Citizen.objects.filter(pk=cit.pk).exists())
def test_upload_file_invalid_nid(self): # We catch invalid NIDs in the upload file data = self.data data['how_to_select'] = str(Changeset.SELECT_UPLOADED_NIDS) citizen1 = CitizenFactory() citizen2 = CitizenFactory() nid1 = str(citizen1.national_id) nid1 = '3' + nid1[1:] filetext = "{nid1}\n{nid2}\n".format(nid1=nid1, nid2=citizen2.national_id) upload_file = SimpleUploadedFile('my_filename', filetext) form = ChangesetForm(data=data, files={'upload_file': upload_file}, request=MagicMock()) self.assertFalse(form.is_valid()) self.assertIn('upload_file', form.errors)
def setUp(self): # these should be blocked during the changeset self.citizen1 = CitizenFactory() self.citizen2 = CitizenFactory() # citizen3 already blocked self.citizen3 = CitizenFactory() self.citizen3.block() # citizen4 won't be included self.citizen4 = CitizenFactory() self.changeset = ChangesetFactory( status=Changeset.STATUS_APPROVED, change=Changeset.CHANGE_BLOCK, )
def test_upload_file_no_such_citizen(self): # We catch non-existent citizens in the upload file data = self.data data['how_to_select'] = str(Changeset.SELECT_UPLOADED_NIDS) citizen1 = CitizenFactory() citizen2 = CitizenFactory() filetext = "{nid1}\n{nid2}\n".format(nid1=citizen1.national_id + 27, nid2=citizen2.national_id) upload_file = SimpleUploadedFile('my_filename', filetext) with patch('changesets.forms.get_citizen_by_national_id') as mock_get_citizen: mock_get_citizen.return_value = None # No such citizen form = ChangesetForm(data=data, files={'upload_file': upload_file}, request=MagicMock()) self.assertFalse(form.is_valid()) self.assertIn('upload_file', form.errors)
def test_is_eligible_blocked(self): # Blocked citizen is not eligible TODAY = datetime.datetime(2000, 6, 2) citizen = CitizenFactory(birth_date=datetime.date(1982, 6, 1)) self.assertTrue(citizen.is_eligible(as_of=TODAY)) citizen.block() citizen.save() self.assertFalse(citizen.is_eligible(as_of=TODAY))
def test_upload_file(self): # We can successfully upload a file of NIDs and look up the citizens # and blank lines are okay data = self.data data['how_to_select'] = str(Changeset.SELECT_UPLOADED_NIDS) citizen1 = CitizenFactory() citizen2 = CitizenFactory() filetext = "{nid1}\n\n{nid2}\n".format(nid1=citizen1.national_id, nid2=citizen2.national_id) upload_file = SimpleUploadedFile('my_filename', filetext) mock_request = MagicMock(user=UserFactory()) form = ChangesetForm(data=data, files={'upload_file': upload_file}, request=mock_request) self.assertTrue(form.is_valid(), msg=str(form.errors)) changeset = form.save() self.assertIn(citizen1, changeset.selected_citizens.all()) self.assertIn(citizen2, changeset.selected_citizens.all())
def test_new_record_with_existing(self): nid = 100000000000 existing = CitizenFactory(pk=1, national_id=nid) TempCitizenFactory(pk=1, **model_to_dict(existing)) nid += 1 cit = TempCitizenFactory(pk=2, national_id=nid) nid += 1 existing2 = CitizenFactory(pk=3, national_id=nid) TempCitizenFactory(pk=3, **model_to_dict(existing2)) stats = mirror_database(from_model=TempCitizen, to_model=Citizen) self.assertEqual(2, stats.unchanged_count) self.assertEqual(0, stats.modified_record_count) self.assertEqual(1, stats.new_record_count) self.assertEqual(0, stats.not_there_anymore_count) self.assertTrue(Citizen.objects.filter(pk=cit.pk).exists())
def test_citizen_updated(self, get_records, open): cit1 = CitizenFactory() cit2 = CitizenFactory() citizen = CitizenFactory() data = model_to_dict(citizen) new_name = 'Jim Bob' data['first_name'] = new_name get_records.return_value = [ model_to_dict(cit1), model_to_dict(cit2), data ] stats = import_citizen_dump(input_filename=None, max_change_percent=34) self.assertEqual(3, stats.records_read) updated_cit = Citizen.objects.get(pk=citizen.pk) self.assertEqual(new_name, updated_cit.first_name)
def setUp(self): self.number = "919-999-9999" self.center = RegistrationCenterFactory() self.conn = self.create_connection(data={'identity': self.number}) self.citizen = CitizenFactory() self.good_nid = self.citizen.national_id self.bad_nid = get_random_number_string(length=constants.NID_LENGTH) self.short_nid = get_random_number_string(length=constants.NID_LENGTH - 1) self.good_center_id = self.center.center_id self.bad_center_id = get_random_number_string( length=constants.CENTER_ID_LENGTH) self.long_center_id = get_random_number_string( length=constants.CENTER_ID_LENGTH + 1) self.fields = {'to_addr': settings.REGISTRATION_SHORT_CODE} RegistrationPeriodFactory(start_time=PAST_DAY, end_time=FUTURE_DAY)
def test_can_repeat_reg(self): # User can send multiple messages from the same phone number for the # same voting center. citizen = CitizenFactory() center1 = RegistrationCenterFactory() # Message #1 - original registration self.receive("%s %s" % (citizen.national_id, center1.center_id), self.conn, fields=self.fields) # Citizen should be registered reg = models.Registration.objects.get(citizen=citizen) self.assertEqual(center1, reg.registration_center) self.assertEqual(constants.RESPONSE_VALID_REGISTRATION, self.get_last_response_code()) # Repeat - message 2 self.receive("%s %s" % (citizen.national_id, center1.center_id), self.conn, fields=self.fields) # Citizen should be registered reg = models.Registration.objects.get(citizen=citizen) self.assertEqual(center1, reg.registration_center) # Repeated reg self.assertEqual(constants.MESSAGE_1, self.get_last_response_code()) self.receive("%s %s" % (citizen.national_id, center1.center_id), self.conn, fields=self.fields) self.assertEqual(constants.MESSAGE_1, self.get_last_response_code())
def test_case_detail_view(self): citizen = CitizenFactory(family_name='Doe', first_name='John') # factory case = CaseFactory(citizen=citizen) rsp = self.client.get(reverse('case_detail', kwargs={'pk': case.pk}), follow=False) if rsp.status_code == 302: self.fail("Redirected to %s" % rsp['Location']) self.assertContains(rsp, 'John')
def test_view_view(self): self.center2 = RegistrationCenterFactory(name='Centra Dua') changeset = ChangesetFactory( name='My Changeset', change=Changeset.CHANGE_CENTER, how_to_select=Changeset.SELECT_UPLOADED_NIDS, target_center=self.center2, justification='Just because', ) per_page = UploadedCitizenBrowse.paginate_by self.assertIsNotNone(per_page) citizens = [CitizenFactory() for i in range(per_page + 2)] changeset.selected_citizens.add(*citizens) citizens = sorted(citizens, key=attrgetter('national_id')) # Get citizens affected by this changeset, sorted ascending by national ID url = reverse( 'browse_uploadedcitizens') + "?changeset=%s&o=0" % changeset.pk rsp = self.client.get(url) self.assertEqual(OK, rsp.status_code) context = rsp.context object_list = context['object_list'] self.assertLessEqual(len(object_list), per_page) # Should be on first page self.assertContains(rsp, citizens[0].national_id) self.assertContains(rsp, str(citizens[0])) # Last citizen ought to be on the next page self.assertNotContains(rsp, citizens[-1].national_id) self.assertNotContains(rsp, str(citizens[-1]))
def test_not_born_yet(self): tomorrow = (now() + timedelta(hours=24)).date() citizen = CitizenFactory(birth_date=tomorrow) data = model_to_dict(citizen) data = stringize(data) form = CitizenRecordForm(data) self.assertFalse(form.is_valid()) self.assertIn('birth_date', form.errors)
def test_too_many_changes(self, get_records, open): cit1 = CitizenFactory() cit2 = CitizenFactory() citizen = CitizenFactory() data = model_to_dict(citizen) new_name = 'Jim Bob' data['first_name'] = new_name get_records.return_value = [ model_to_dict(cit1), model_to_dict(cit2), data ] # Import raises an exception: with self.assertRaises(TooManyChanges): import_citizen_dump(input_filename=None, max_change_percent=10) # Citizen is unchanged: new_citizen = refresh_model(citizen) self.assertEqual(citizen.first_name, new_citizen.first_name)
def test_missing_captcha_causes_failure(self): CitizenFactory(national_id=123456789012, fbr_number=1234) data = {'national_id': '123456789012', 'fbr_number': '1234'} # no captcha, so skip the call -> data.update(self.captcha) rsp = self.client.post(self.url, data=data) errors = rsp.context['form'].errors self.assertTrue(errors) self.assertTrue('captcha' in errors)
def setUp(self): self.citizen = CitizenFactory() self.good_nid = self.citizen.national_id self.bad_nid = long( get_random_number_string(length=constants.NID_LENGTH)) self.nid_without_citizen = long( get_random_number_string(length=constants.NID_LENGTH)) self.sms = SMSFactory()
def setUp(self): self.sms = SMSFactory() self.citizen = CitizenFactory(national_id=219782058018) self.center = RegistrationCenterFactory() RegistrationCenterFactory(center_id=self.center.center_id, deleted=True) self.mock_response = Mock() self.reg_period = RegistrationPeriodFactory(start_time=PAST_DAY, end_time=FUTURE_DAY)
def test_new_citizen(self): CitizenFactory(national_id=123456789012, fbr_number=1234) data = {'national_id': '123456789012', 'fbr_number': '1234'} data.update(self.captcha) rsp = self.client.post(self.url, data=data) self.assertEqual(200, rsp.status_code) context = rsp.context self.assertIn('citizen', context) self.assertNotIn('center', context)
def test_unchanged_record(self): cit = CitizenFactory() TempCitizenFactory(**model_to_dict(cit)) stats = mirror_database(from_model=TempCitizen, to_model=Citizen) self.assertEqual(1, stats.unchanged_count) self.assertEqual(0, stats.modified_record_count) self.assertEqual(0, stats.new_record_count) self.assertEqual(0, stats.not_there_anymore_count)
def test_record_gone_do_not_delete(self): cit = CitizenFactory() stats = mirror_database(from_model=TempCitizen, to_model=Citizen) self.assertEqual(0, stats.unchanged_count) self.assertEqual(0, stats.modified_record_count) self.assertEqual(0, stats.new_record_count) self.assertEqual(1, stats.not_there_anymore_count) # NOT deleted self.assertTrue(Citizen.objects.filter(pk=cit.pk).exists())
def test_unique_registration(self): # Undeleted, confirmed registrations must be unique on citizen citizen = CitizenFactory() RegistrationFactory(citizen=citizen, deleted=False, archive_time=None) RegistrationFactory(citizen=citizen, deleted=True) RegistrationFactory(citizen=citizen, archive_time=now()) with self.assertRaises(IntegrityError): RegistrationFactory(citizen=citizen, deleted=False, archive_time=None)
def test_case_list_number(self): citizen = CitizenFactory(family_name='Doe', first_name='John') # factory target = CaseFactory(citizen=citizen) for i in range(4): # 4 more CaseFactory() rsp = self.client.get(reverse('report_cases') + "?q=%d" % target.citizen.national_id) self.assertEqual(1, len(rsp.context['object_list'])) obj = rsp.context['object_list'][0] self.assertEqual(target, obj)
def test_wrong_gender_for_national_id(self): citizen = CitizenFactory( gender=FEMALE, national_id=123456789012, # expecting male gender ) data = model_to_dict(citizen) data = stringize(data) form = CitizenRecordForm(data) self.assertFalse(form.is_valid()) self.assertIn("does not match gender", str(form.errors))
def test_temp_citizen_created(self, get_records, open): citizen = CitizenFactory() data = model_to_dict(citizen) citizen.delete() get_records.return_value = [data] self.assertFalse(Citizen.objects.exists()) self.assertFalse(TempCitizen.objects.exists()) with patch('civil_registry.utils.delete_all'): with patch('civil_registry.utils.mirror_database') as mirror_database: mirror_database.return_value = MirrorStats() stats = import_citizen_dump(input_filename=None) # We mocked mirror_database, so nothing actually got copied to Citizen # but it should have "read" the data into TempCitizen, and we # mocked delete_all, so it should still be there. self.assertFalse(Citizen.objects.exists()) self.assertEqual(1, stats.records_read) mirror_database.assert_called() temp_cit = TempCitizen.objects.get() self.assertEqual(data, model_to_dict(temp_cit))
def test_changed_record_with_existing(self): nid = 100000000000 existing = CitizenFactory(pk=1, national_id=nid) nid += 1 TempCitizenFactory(pk=existing.pk, **model_to_dict(existing)) nid += 1 cit = CitizenFactory(pk=2, national_id=nid) temp_cit = TempCitizenFactory(pk=cit.pk, national_id=nid, first_name=cit.first_name + '_CHANGED') existing2 = CitizenFactory(pk=3) TempCitizenFactory(pk=existing2.pk, **model_to_dict(existing2)) stats = mirror_database(from_model=TempCitizen, to_model=Citizen) self.assertEqual(2, stats.unchanged_count) self.assertEqual(1, stats.modified_record_count) self.assertEqual(0, stats.new_record_count) self.assertEqual(0, stats.not_there_anymore_count) c = Citizen.objects.get(pk=cit.pk) self.assertEqual(temp_cit.first_name, c.first_name)
def test_changed_record(self): cit = CitizenFactory() temp_cit = TempCitizenFactory(pk=cit.pk, first_name=cit.first_name + '_CHANGED') stats = mirror_database(from_model=TempCitizen, to_model=Citizen) self.assertEqual(0, stats.unchanged_count) self.assertEqual(1, stats.modified_record_count) self.assertEqual(0, stats.new_record_count) self.assertEqual(0, stats.not_there_anymore_count) c = Citizen.objects.get(pk=cit.pk) self.assertEqual(temp_cit.first_name, c.first_name)
def test_eastern_arabic_in_fbrn(self): """ Query site should accept Eastern arabic numerals for FBRN. """ CitizenFactory(fbr_number=1234, national_id=123456789012) data = {'national_id': '١٢٣٤٥٦٧٨٩٠12', 'fbr_number': '١٢34'} data.update(self.captcha) rsp = self.client.post(self.url, data=data) self.assertEqual(200, rsp.status_code) context = rsp.context self.assertIn('citizen', context)
def test_citizen_not_registered(self): # let's query for the registration citizen2 = CitizenFactory() # unregistered msg = u"{nid}".format(nid=citizen2.national_id) self.receive(msg, self.conn, fields=self.fields) self.assertEqual(self.get_last_response_code(), constants.VOTER_QUERY_NOT_REGISTERED) context = {'person': unicode(citizen2)} expected = self.translate(constants.VOTER_QUERY_NOT_REGISTERED, context) # Arabic self.assertEqual(self.get_last_response_message(), expected)
def test_temp_citizen_created(self, get_records, open): citizen = CitizenFactory() data = model_to_dict(citizen) citizen.delete() get_records.return_value = [data] self.assertFalse(Citizen.objects.exists()) self.assertFalse(TempCitizen.objects.exists()) with patch('civil_registry.utils.delete_all'): with patch( 'civil_registry.utils.mirror_database') as mirror_database: mirror_database.return_value = MirrorStats() stats = import_citizen_dump(input_filename=None) # We mocked mirror_database, so nothing actually got copied to Citizen # but it should have "read" the data into TempCitizen, and we # mocked delete_all, so it should still be there. self.assertFalse(Citizen.objects.exists()) self.assertEqual(1, stats.records_read) mirror_database.assert_called() temp_cit = TempCitizen.objects.get() self.assertEqual(data, model_to_dict(temp_cit))
def setUp(self): self.number = "919-999-9999" self.center = RegistrationCenterFactory() self.conn = self.create_connection(data={'identity': self.number}) self.citizen = CitizenFactory() self.good_nid = self.citizen.national_id self.bad_nid = get_random_number_string(length=constants.NID_LENGTH) self.short_nid = get_random_number_string(length=constants.NID_LENGTH - 1) self.good_center_id = self.center.center_id self.bad_center_id = get_random_number_string(length=constants.CENTER_ID_LENGTH) self.long_center_id = get_random_number_string(length=constants.CENTER_ID_LENGTH + 1) self.fields = {'to_addr': settings.REGISTRATION_SHORT_CODE} RegistrationPeriodFactory(start_time=PAST_DAY, end_time=FUTURE_DAY)
class ResponseTest(TranslationTest, LibyaRapidTest): # Test response messages (Arabic) def setUp(self): self.number = "919-999-9999" self.center = RegistrationCenterFactory() self.conn = self.create_connection(data={'identity': self.number}) self.citizen = CitizenFactory() self.good_nid = self.citizen.national_id self.bad_nid = get_random_number_string(length=constants.NID_LENGTH) self.short_nid = get_random_number_string(length=constants.NID_LENGTH - 1) self.good_center_id = self.center.center_id self.bad_center_id = get_random_number_string(length=constants.CENTER_ID_LENGTH) self.long_center_id = get_random_number_string(length=constants.CENTER_ID_LENGTH + 1) self.fields = {'to_addr': settings.REGISTRATION_SHORT_CODE} RegistrationPeriodFactory(start_time=PAST_DAY, end_time=FUTURE_DAY) def test_garbage(self, registration_open): self.receive("PING", self.conn, fields=self.fields) expected = self.translate(constants.MESSAGE_INCORRECT) # arabic self.assertEqual(self.get_last_response_message(), expected) def test_garbage_enhanced(self, registration_open): for i in range(1, 5): # last iteration should get an enhanced message self.receive("PING", self.conn, fields=self.fields) expected = self.translate(constants.MESSAGE_INCORRECT, enhanced=True) # arabic self.assertEqual(self.get_last_response_message(), expected) def test_wrong_length_nid(self, registration_open): msg = "{nid}#{center}".format(nid=self.short_nid, center=self.good_center_id) self.receive(msg, self.conn, fields=self.fields) expected = self.translate(constants.RESPONSE_NID_WRONG_LENGTH) # arabic self.assertEqual(self.get_last_response_message(), expected) def test_wrong_length_nid_enhanced(self, registration_open): msg = "{nid}#{center}".format(nid=self.short_nid, center=self.good_center_id) for i in range(1, 5): # last iteration should get an enhanced message self.receive(msg, self.conn, fields=self.fields) expected = self.translate(constants.RESPONSE_NID_WRONG_LENGTH, enhanced=True) # arabic self.assertEqual(self.get_last_response_message(), expected) def test_wrong_length_center_id(self, registration_open): msg = "{nid}#{center}".format(nid=self.good_nid, center=self.long_center_id) self.receive(msg, self.conn, fields=self.fields) expected = self.translate(constants.RESPONSE_CENTER_ID_WRONG_LENGTH) # arabic self.assertEqual(self.get_last_response_message(), expected) def test_wrong_length_center_id_enhanced(self, registration_open): msg = "{nid}#{center}".format(nid=self.good_nid, center=self.long_center_id) for i in range(1, 5): # last iteration should get an enhanced message self.receive(msg, self.conn, fields=self.fields) expected = self.translate(constants.RESPONSE_CENTER_ID_WRONG_LENGTH, enhanced=True) self.assertEqual(self.get_last_response_message(), expected) def test_wrong_length_nid_query(self, registration_open): msg = "{nid}".format(nid=self.short_nid) self.receive(msg, self.conn, fields=self.fields) expected = self.translate(constants.VOTER_QUERY_NID_WRONG_LENGTH) self.assertEqual(self.get_last_response_message(), expected) def test_citizen_under_18(self, registration_open): self.citizen.birth_date = datetime.datetime.today() self.citizen.save() msg = "{nid}#{center}".format(nid=self.good_nid, center=self.good_center_id) self.receive(msg, self.conn, fields=self.fields) expected = self.translate(constants.RESPONSE_NID_INVALID) # arabic self.assertEqual(self.get_last_response_message(), expected) def test_center_does_not_exist(self, registration_open): msg = "{nid}#{center}".format(nid=self.good_nid, center=self.bad_center_id) self.receive(msg, self.conn, fields=self.fields) expected = self.translate(constants.RESPONSE_CENTER_ID_INVALID) # arabic self.assertEqual(self.get_last_response_message(), expected) def test_nid_does_not_exist(self, registration_open): msg = "{nid}#{center}".format(nid=self.bad_nid, center=self.good_center_id) self.receive(msg, self.conn, fields=self.fields) expected = self.translate(constants.RESPONSE_NID_INVALID) # arabic self.assertEqual(self.get_last_response_message(), expected) def test_good_registration(self, registration_open): msg = "{nid}#{center}".format(nid=self.good_nid, center=self.good_center_id) self.receive(msg, self.conn, fields=self.fields) context = {'person': unicode(self.citizen), 'centre': self.center.name, 'code': self.center.center_id} expected = self.translate(constants.MESSAGE_1, context=context) # arabic self.assertEqual(self.get_last_response_message(), expected) def test_good_registration_enhanced(self, registration_open): msg = "{nid}#{center}".format(nid=self.good_nid, center=self.good_center_id) for i in range(1, 5): # last iteration should get an enhanced message self.receive(msg, self.conn, fields=self.fields) context = {'person': unicode(self.citizen), 'centre': self.center.name, 'code': self.center.center_id} expected = self.translate(constants.MESSAGE_1, context=context, enhanced=True) # arabic self.assertEqual(self.get_last_response_code(), constants.MESSAGE_1) self.assertEqual(self.get_last_response_message(), expected) def test_good_update(self, registration_open): new_center = RegistrationCenterFactory() msg = "{nid}#{center}".format(nid=self.good_nid, center=self.good_center_id) self.receive(msg, self.conn, fields=self.fields) # registers msg = "{nid}#{center}".format(nid=self.good_nid, center=new_center.center_id) self.receive(msg, self.conn, fields=self.fields) # updates context = {'person': unicode(self.citizen), 'centre': new_center.name, 'code': new_center.center_id} # 1st update - message 1 expected = self.translate(constants.MESSAGE_1, context=context) # arabic self.assertEqual(self.get_last_response_message(), expected) # 2nd update - message 4 msg = "{nid}#{center}".format(nid=self.good_nid, center=self.good_center_id) self.receive(msg, self.conn, fields=self.fields) # updates again context = {'person': unicode(self.citizen), 'centre': new_center.name, 'code': self.good_center_id} expected = self.translate(constants.MESSAGE_4, context=context) # arabic # 3rd and final update - message 5 msg = "{nid}#{center}".format(nid=self.good_nid, center=new_center.center_id) self.receive(msg, self.conn, fields=self.fields) # updates context = {'person': unicode(self.citizen), 'centre': new_center.name, 'code': new_center.center_id} expected = self.translate(constants.MESSAGE_5, context=context) # arabic def test_attempt_update_wrong_from_number(self, registration_open): # create a valid registration sms = SMSFactory(from_number=self.number, citizen=self.citizen) RegistrationFactory( citizen=self.citizen, registration_center=self.center, archive_time=None, sms=sms) # try to register at a new center with a new number new_center = RegistrationCenterFactory() new_number = '919-888-8888' msg = "{nid}#{center}".format(nid=self.good_nid, center=new_center.center_id) new_conn = self.create_connection(data={'identity': new_number}) self.receive(msg, new_conn, fields=self.fields) # message should have the existing number in it (not new_number) context = {'centre': self.center.name, 'number': self.number[-4:]} expected = self.translate(constants.MESSAGE_2, context=context) # arabic self.assertEqual(self.get_last_response_message(), expected) def test_attempt_update_wrong_from_number_same_center(self, registration_open): # create a valid registration sms = SMSFactory(from_number=self.number, citizen=self.citizen) RegistrationFactory( citizen=self.citizen, registration_center=self.center, archive_time=None, sms=sms) # try to register at same center with a new number new_number = '919-888-8888' msg = "{nid}#{center}".format(nid=self.good_nid, center=self.center.center_id) new_conn = self.create_connection(data={'identity': new_number}) self.receive(msg, new_conn, fields=self.fields) # message should have the existing number in it (not new_number) context = {'centre': self.center.name, 'number': self.number[-4:]} expected = self.translate(constants.MESSAGE_2, context=context) # arabic self.assertEqual(self.get_last_response_message(), expected)
class ExecuteUnblockTest(TestCase): """ Test executeing unblocking changesets. Each test will set up a change in a different way but that ought to have the same results. Then we'll try to roll back the change, after changing one voter, and check those results. """ def setUp(self): # these should be unblocked during the changeset self.citizen1 = CitizenFactory() self.citizen1.block() self.citizen2 = CitizenFactory() self.citizen2.block() # citizen3 already unblocked self.citizen3 = CitizenFactory() # citizen4 won't be included self.citizen4 = CitizenFactory() self.citizen4.block() self.changeset = ChangesetFactory( status=Changeset.STATUS_APPROVED, change=Changeset.CHANGE_UNBLOCK, ) def doit(self): self.changeset.full_clean() citizens = list(self.changeset.get_citizens_to_change()) self.assertIn(self.citizen1, citizens) self.assertIn(self.citizen2, citizens) self.assertIn(self.citizen3, citizens) # In the list, but will not be changed self.assertNotIn(self.citizen4, citizens) self.changeset.execute() self.citizen1 = refresh_model(self.citizen1) self.citizen2 = refresh_model(self.citizen2) self.citizen3 = refresh_model(self.citizen3) self.citizen4 = refresh_model(self.citizen4) self.assertFalse(self.citizen1.blocked) self.assertFalse(self.citizen2.blocked) self.assertFalse(self.citizen3.blocked) self.assertTrue(self.citizen4.blocked) changes = self.changeset.change_records.all() self.assertTrue(changes.filter(citizen=self.citizen1, changed=True).exists()) self.assertTrue(changes.filter(citizen=self.citizen2, changed=True).exists()) self.assertTrue(changes.filter(citizen=self.citizen3, changed=False).exists()) self.assertFalse(changes.filter(citizen=self.citizen4).exists()) self.assertEqual(Changeset.STATUS_PARTIALLY_SUCCESSFUL, self.changeset.status) # Now block citizen 2 self.citizen2.block() rollback = ChangesetFactory( status=Changeset.STATUS_APPROVED, change=Changeset.CHANGE_ROLLBACK, how_to_select=Changeset.SELECT_OTHER_CHANGESET, other_changeset=self.changeset, ) citizens = list(rollback.get_citizens_to_change()) self.assertIn(self.citizen1, citizens) self.assertIn(self.citizen2, citizens) self.assertNotIn(self.citizen3, citizens) # not changed before, so not in list this time self.assertNotIn(self.citizen4, citizens) rollback.execute() self.changeset = refresh_model(self.changeset) self.assertEqual(Changeset.STATUS_ROLLED_BACK, self.changeset.status) self.citizen1 = refresh_model(self.citizen1) self.citizen2 = refresh_model(self.citizen2) self.citizen3 = refresh_model(self.citizen3) self.citizen4 = refresh_model(self.citizen4) self.assertTrue(self.citizen1.blocked) self.assertTrue(self.citizen2.blocked) self.assertFalse(self.citizen3.blocked) self.assertTrue(self.citizen4.blocked) changes = rollback.change_records.all() self.assertTrue(changes.filter(citizen=self.citizen1, changed=True).exists()) self.assertTrue(changes.filter(citizen=self.citizen2, changed=False).exists()) # reg 3 not in the list because they were changed previously self.assertFalse(changes.filter(citizen=self.citizen3).exists()) self.assertFalse(changes.filter(citizen=self.citizen4).exists()) self.assertEqual(Changeset.STATUS_PARTIALLY_SUCCESSFUL, rollback.status) def test_unblock_by_uploaded_citizens(self): self.changeset.how_to_select = Changeset.SELECT_UPLOADED_NIDS self.changeset.save() self.changeset.selected_citizens.add(self.citizen1, self.citizen2, self.citizen3) self.doit() def test_unblock_by_selected_centers(self): center1 = RegistrationCenterFactory() center2 = RegistrationCenterFactory() RegistrationCenterFactory() RegistrationFactory(citizen=self.citizen1, registration_center=center1, archive_time=None) RegistrationFactory(citizen=self.citizen2, registration_center=center1, archive_time=None) RegistrationFactory(citizen=self.citizen3, registration_center=center2, archive_time=None) self.changeset.how_to_select = Changeset.SELECT_CENTERS self.changeset.save() self.changeset.selected_centers.add(center1, center2) self.doit() def test_unblock_by_other_changeset(self): other = ChangesetFactory(status=Changeset.STATUS_SUCCESSFUL) self.changeset.how_to_select = Changeset.SELECT_OTHER_CHANGESET self.changeset.other_changeset = other self.changeset.save() ChangeRecordFactory(citizen=self.citizen1, changeset=other, changed=True) ChangeRecordFactory(citizen=self.citizen2, changeset=other, changed=True) ChangeRecordFactory(citizen=self.citizen3, changeset=other, changed=True) ChangeRecordFactory(citizen=self.citizen4, changeset=other, changed=False) self.doit()
def test_block_citizen(self): citizen = CitizenFactory() RegistrationFactory(citizen=citizen, archive_time=None) self.assertIsNotNone(citizen.registration) citizen.block() self.assertIsNone(citizen.registration)