def test_tally_list_data_view_valid_search_filter(self): """ Test that tally list data view returns the correct data when a valid search filter is applied. """ tally_1 = create_tally(name='example_1_tally') create_tally(name='example_2_tally') view = views.TallyListDataView.as_view() request = self.factory.get('/') request.user = self.user request.GET = request.GET.copy() request.GET['search[value]'] = tally_1.name response = view(request) data = json.loads(response.content.decode())['data'] tally_id, tally_name, created_date, modified_formatted_date,\ admin_view_link, edit_link = data[0] self.assertEquals(1, len(data)) self.assertEquals( admin_view_link, f'<a href="/super-administrator/{tally_1.id}/"' ' class ="btn btn-default btn-small">Admin View</a>') self.assertEquals( edit_link, f'<a href="/tally-manager/update-tally/{tally_1.id}/"' ' class ="btn btn-default btn-small">Edit</a>') self.assertEquals(tally_id, str(tally_1.id)) self.assertEquals(tally_name, tally_1.name) self.assertEquals(created_date, str(tally_1.created_date)) self.assertEquals( modified_formatted_date, tally_1.modified_date.strftime('%a, %d %b %Y %H:%M:%S %Z'))
def test_quality_control_post_quarantine(self): self._create_and_login_user() tally = create_tally() tally.users.add(self.user) center = create_center() create_station(center) create_quarantine_checks() result_form = create_result_form( form_state=FormState.QUALITY_CONTROL, tally=tally, center=center, station_number=1) create_reconciliation_form( result_form, self.user, number_unstamped_ballots=1000) create_quality_control(result_form, self.user) self._add_user_to_group(self.user, groups.QUALITY_CONTROL_CLERK) view = views.QualityControlDashboardView.as_view() data = { 'correct': 1, 'result_form': result_form.pk, 'tally_id': tally.pk, } request = self.factory.post('/', data=data) request.session = {'result_form': result_form.pk} request.user = self.user response = view(request, tally_id=tally.pk) result_form.reload() self.assertEqual(response.status_code, 302) self.assertTrue(result_form.form_state, FormState.AUDIT) self.assertTrue(result_form.audit) self.assertEqual(result_form.audit.quarantine_checks.count(), 2) self.assertEqual(result_form.audit.user, self.user) self.assertEqual(result_form.audited_count, 1) self.assertIn('quality-control/print', response['location'])
def test_dashboard_abort_post(self): self._create_and_login_user() tally = create_tally() tally.users.add(self.user) barcode = '123456789' create_result_form(barcode, tally=tally, form_state=FormState.QUALITY_CONTROL) self._add_user_to_group(self.user, groups.QUALITY_CONTROL_CLERK) result_form = ResultForm.objects.get(barcode=barcode) create_quality_control(result_form, self.user) view = views.QualityControlDashboardView.as_view() data = { 'result_form': result_form.pk, 'abort': 1, 'tally_id': tally.pk, } request = self.factory.post('/', data=data) request.user = self.user request.session = {'result_form': result_form.pk} response = view(request, tally_id=tally.pk) quality_control = result_form.qualitycontrol_set.all()[0] self.assertEqual(quality_control.active, False) self.assertEqual(response.status_code, 302) self.assertIn('quality-control/home', response['location']) self.assertEqual(request.session, {})
def test_women_post_incorrect_ballot_released(self): self._create_and_login_user() tally = create_tally() tally.users.add(self.user) barcode = '123456789' ballot = create_ballot(available_for_release=True) create_result_form(barcode, tally=tally, ballot=ballot, form_state=FormState.QUALITY_CONTROL) result_form = ResultForm.objects.get(barcode=barcode) create_quality_control(result_form, self.user) create_candidates(result_form, self.user) self._add_user_to_group(self.user, groups.QUALITY_CONTROL_CLERK) view = views.QualityControlDashboardView.as_view() data = { 'result_form': result_form.pk, 'incorrect': 1, 'tally_id': tally.pk, } request = self.factory.post('/', data=data) request.user = self.user request.session = {'result_form': result_form.pk} response = view(request, tally_id=tally.pk) self.assertEqual(response.status_code, 302) self.assertIn('quality-control/confirm-reject', response['location']) result_form = ResultForm.objects.get(pk=result_form.pk) quality_control = result_form.qualitycontrol_set.all()[0] self.assertTrue(quality_control.active) self.assertEqual(result_form.form_state, FormState.QUALITY_CONTROL) self.assertEqual(result_form.rejected_count, 0)
def test_women_get(self): self._create_and_login_user() tally = create_tally() tally.users.add(self.user) barcode = '123456789' center = create_center() station = create_station(center=center) create_result_form(barcode, center=center, station_number=station.station_number, tally=tally, form_state=FormState.QUALITY_CONTROL) result_form = ResultForm.objects.get(barcode=barcode) name = 'general candidate name' women_name = 'women candidate name' votes = 123 create_candidates(result_form, self.user, name, votes, women_name) self._add_user_to_group(self.user, groups.QUALITY_CONTROL_CLERK) view = views.QualityControlDashboardView.as_view() request = self.factory.get('/') request.user = self.user request.session = {'result_form': result_form.pk} response = view(request, tally_id=tally.pk) response.render() self.assertEqual(response.status_code, 200) self.assertContains(response, 'Women') self.assertContains(response, women_name) self.assertContains(response, name) self.assertContains(response, str(votes))
def test_quality_control_post(self): barcode = '123456789' self._create_and_login_user() tally = create_tally() tally.users.add(self.user) create_result_form(barcode, tally=tally, form_state=FormState.QUALITY_CONTROL) self._add_user_to_group(self.user, groups.QUALITY_CONTROL_CLERK) view = views.QualityControlView.as_view() data = { 'barcode': barcode, 'barcode_copy': barcode, 'tally_id': tally.pk, } request = self.factory.post('/', data=data) request.user = self.user request.session = {} response = view(request, tally_id=tally.pk) self.assertEqual(response.status_code, 302) self.assertIn('quality-control/dashboard', response['location']) result_form = ResultForm.objects.get(barcode=barcode) self.assertEqual(result_form.form_state, FormState.QUALITY_CONTROL) self.assertEqual(result_form.qualitycontrol.user, self.user)
def test_general_post_correct(self): self._create_and_login_user() tally = create_tally() tally.users.add(self.user) barcode = '123456789' result_form = create_result_form(barcode, tally=tally, form_state=FormState.QUALITY_CONTROL) create_quality_control(result_form, self.user) self._add_user_to_group(self.user, groups.QUALITY_CONTROL_CLERK) view = views.QualityControlDashboardView.as_view() data = { 'result_form': result_form.pk, 'correct': 1, 'tally_id': tally.id, } request = self.factory.post('/', data=data) request.user = self.user request.session = {'result_form': result_form.pk} response = view(request, tally_id=tally.pk) result_form.reload() self.assertEqual(response.status_code, 302) self.assertIn('quality-control/print', response['location']) quality_control = QualityControl.objects.get( pk=result_form.qualitycontrol.pk) self.assertEqual(result_form.form_state, FormState.QUALITY_CONTROL) self.assertTrue(quality_control.passed_general)
def test_confirm_form_reset_view_post(self): self._create_and_login_user() tally = create_tally() tally.users.add(self.user) barcode = '123456789' ballot = create_ballot(available_for_release=True) create_result_form(barcode, tally=tally, ballot=ballot, form_state=FormState.QUALITY_CONTROL) result_form = ResultForm.objects.get(barcode=barcode) create_quality_control(result_form, self.user) self._add_user_to_group(self.user, groups.QUALITY_CONTROL_CLERK) view = views.ConfirmFormResetView.as_view() reject_reason = 'Form Incorrect' data = { 'reject_reason': reject_reason } request = self.factory.post('/', data=data) request.user = self.user request.session = {'result_form': result_form.pk} response = view(request, tally_id=tally.pk) result_form = ResultForm.objects.get(pk=result_form.pk) quality_control = result_form.qualitycontrol_set.all()[0] self.assertEqual(response.status_code, 302) self.assertIn('quality-control/reject', response['location']) self.assertEqual(request.session, {}) self.assertEqual(result_form.form_state, FormState.DATA_ENTRY_1) self.assertEqual(result_form.rejected_count, 1) self.assertEqual(result_form.reject_reason, reject_reason) self.assertFalse(quality_control.active) self.assertFalse(quality_control.passed_reconciliation)
def test_dashboard_get(self): barcode = '123456789' self._create_and_login_user() tally = create_tally() tally.users.add(self.user) center = create_center(tally=tally) station = create_station(center=center) create_result_form(barcode, center=center, station_number=station.station_number, tally=tally, form_state=FormState.QUALITY_CONTROL) result_form = ResultForm.objects.get(barcode=barcode) create_candidates(result_form, self.user) create_quality_control(result_form, self.user) self._add_user_to_group(self.user, groups.QUALITY_CONTROL_CLERK) view = views.QualityControlDashboardView.as_view() request = self.factory.get('/') request.user = self.user request.session = {'result_form': result_form.pk} response = view(request, tally_id=tally.pk) self.assertEqual(response.status_code, 200) # gender has been removed # self.assertContains(response, str(result_form.gender_name)) self.assertContains(response, 'General Results Section') self.assertNotContains(response, 'Reconciliation') self.assertContains(response, 'Cancel')
def test_dashboard_get_double_recon_raise(self): self._create_and_login_user() tally = create_tally() tally.users.add(self.user) barcode = '123456789' center = create_center(tally=tally) station = create_station(center=center) result_form = create_result_form(barcode, center=center, station_number=station.station_number, tally=tally, form_state=FormState.QUALITY_CONTROL) create_reconciliation_form(result_form, self.user) create_reconciliation_form(result_form, self.user, ballot_number_from=2) create_candidates(result_form, self.user) create_quality_control(result_form, self.user) self._add_user_to_group(self.user, groups.QUALITY_CONTROL_CLERK) view = views.QualityControlDashboardView.as_view() request = self.factory.get('/') request.user = self.user request.session = {'result_form': result_form.pk} with self.assertRaises(SuspiciousOperation): view(request, tally_id=tally.pk)
def test_print_quarantine_get(self): self._create_and_login_user() tally = create_tally() tally.users.add(self.user) code = '12345' station_number = 1 center = create_center(code, tally=tally) create_station(center) result_form = create_result_form(form_state=FormState.QUALITY_CONTROL, center=center, tally=tally, station_number=station_number) quarantine_check = QuarantineCheck.objects.create( user=self.user, name='1', method='1', value=1) audit = create_audit(result_form, self.user) audit.quarantine_checks.add(quarantine_check) self._add_user_to_group(self.user, groups.QUALITY_CONTROL_CLERK) view = views.PrintView.as_view() request = self.factory.get('/') request.session = {'result_form': result_form.pk} request.user = self.user response = view(request, tally_id=tally.pk) self.assertEqual(response.status_code, 200) self.assertContains(response, 'Quarantined')
def test_session_expiry_logout_view_during_de_1(self): encoded_result_form_data_entry_start_time =\ json.loads(json.dumps(timezone.now(), cls=DjangoJSONEncoder)) tally = create_tally() tally.users.add(self.user) result_form = create_result_form(form_state=FormState.DATA_ENTRY_1, tally=tally) data_entry_url = 'enter-results/%s/' % tally.pk request = self.factory.get(data_entry_url) request.user = self.user # Adding session middleware = SessionMiddleware() middleware.process_request(request) request.session.save() request.session['encoded_result_form_data_entry_start_time'] =\ encoded_result_form_data_entry_start_time request.session['result_form'] =\ result_form.pk response = views.session_expiry_logout_view(request) self.assertEqual(response.status_code, 302) self.assertEquals( request.session['encoded_result_form_data_entry_start_time'], encoded_result_form_data_entry_start_time) self.assertEquals( request.session['result_form'], result_form.pk)
def test_print_quarantine_get(self): self._create_and_login_user() tally = create_tally() tally.users.add(self.user) code = '12345' station_number = 1 center = create_center(code, tally=tally) create_station(center) result_form = create_result_form(form_state=FormState.QUALITY_CONTROL, center=center, tally=tally, station_number=station_number) quarantine_check = QuarantineCheck.objects.create(user=self.user, name='1', method='1', value=1) audit = create_audit(result_form, self.user) audit.quarantine_checks.add(quarantine_check) self._add_user_to_group(self.user, groups.QUALITY_CONTROL_CLERK) view = views.PrintView.as_view() request = self.factory.get('/') request.session = {'result_form': result_form.pk} request.user = self.user response = view(request, tally_id=tally.pk) self.assertEqual(response.status_code, 200) self.assertContains(response, 'Quarantined')
def test_duplicate_forms_post(self): self._create_or_login_intake_clerk() tally = create_tally() tally.users.add(self.user) center = create_center(tally=tally) station = create_station(center=center, tally=tally) result_form = create_result_form(tally=tally, center=center, station_number=station.station_number, form_state=FormState.DATA_ENTRY_1) result_form2 = create_result_form( '123456289', tally=tally, ballot=result_form.ballot, center=result_form.center, station_number=result_form.station_number, serial_number=3) view = views.CenterDetailsView.as_view() data = { 'barcode': result_form2.barcode, 'barcode_copy': result_form2.barcode, 'tally_id': tally.pk, } request = self.factory.post('/', data=data) request.session = {} request.user = self.user response = view(request, tally_id=tally.pk) self.assertEqual(response.status_code, 302) self.assertEqual(request.session['intake-error'], INTAKE_DUPLICATE_ERROR_MESSAGE)
def test_intake_supervisor(self): self._create_and_login_user(username='******') self._create_and_login_user() tally = create_tally() tally.users.add(self.user) form_user = self.user barcode = '123456789' center = create_center() create_result_form(barcode, form_state=FormState.DATA_ENTRY_1, user=form_user, tally=tally, center=center) self._add_user_to_group(self.user, groups.INTAKE_SUPERVISOR) view = views.CenterDetailsView.as_view() barcode_data = { 'barcode': barcode, 'barcode_copy': barcode, 'tally_id': tally.pk, } request = self.factory.post('/', data=barcode_data) request.user = self.user request.session = {} response = view(request, tally_id=tally.pk) self.assertEqual(response.status_code, 302) self.assertIn('intake/printcover', response['location']) result_form = ResultForm.objects.get(barcode=barcode) self.assertEqual(result_form.form_state, FormState.DATA_ENTRY_1) self.assertEqual(result_form.user, form_user)
def test_when_more_than_one_replacement_form_redirects_no_center(self): self._create_and_login_user() tally = create_tally() tally.users.add(self.user) barcode = '123456789' create_result_form(barcode, tally=tally, form_state=FormState.UNSUBMITTED) create_result_form('123456289', tally=tally, form_state=FormState.UNSUBMITTED, serial_number=3) self._add_user_to_group(self.user, groups.INTAKE_CLERK) view = views.CenterDetailsView.as_view() barcode_data = { 'barcode': barcode, 'barcode_copy': barcode, 'tally_id': tally.pk, } request = self.factory.post('/', data=barcode_data) request.user = self.user request.session = {} response = view(request, tally_id=tally.pk) self.assertEqual(response.status_code, 302) self.assertIn('intake/enter-center', response['location']) result_form = ResultForm.objects.get(barcode=barcode) self.assertEqual(result_form.form_state, FormState.INTAKE) self.assertEqual(result_form.user, self.user)
def test_create_result_form_valid(self): tally = create_tally() tally.users.add(self.user) code = '12345' barcode = '12345' ballot = create_ballot(tally=tally) sc, _ = SubConstituency.objects.get_or_create(code=1, field_office='1') center = create_center(code, tally=tally, sub_constituency=sc) station = create_station(center) form_data = { 'center': center.pk, 'station_number': station.station_number, 'tally': tally.pk, 'form_state': 9, 'ballot': ballot.pk, 'barcode': barcode, 'created_user': self.request.user.userprofile, 'gender': 1 } form = CreateResultForm(form_data) self.assertTrue(form.is_valid()) self.assertEqual(form.instance.barcode, barcode) form.save() self.assertEqual( ResultForm.objects.get(id=form.instance.id).barcode, barcode)
def test_center_detail_redirects_to_check_center_details_zero_prefix(self): self._create_and_login_user() tally = create_tally() tally.users.add(self.user) barcode = '000000001' center = create_center(tally=tally) create_result_form(barcode, form_state=FormState.UNSUBMITTED, tally=tally, center=center) self._add_user_to_group(self.user, groups.INTAKE_CLERK) view = views.CenterDetailsView.as_view() barcode_data = { 'barcode': barcode, 'barcode_copy': barcode, 'tally_id': tally.pk, } request = self.factory.post('/', data=barcode_data) request.user = self.user request.session = {} response = view(request, tally_id=tally.pk) self.assertEqual(response.status_code, 302) self.assertIn('intake/check-center-details', response['location']) result_form = ResultForm.objects.get(barcode=barcode) self.assertEqual(result_form.form_state, FormState.INTAKE) self.assertEqual(result_form.user, self.user)
def test_create_race_invalid_document_size_error(self): tally = create_tally() tally.users.add(self.user) view = views.CreateRaceView.as_view() file_size = settings.MAX_FILE_UPLOAD_SIZE * 2 image = SimpleUploadedFile("image.jpg", bytes(file_size), content_type="image/jpeg") data = { 'number': 1, 'race_type': 0, 'tally_id': tally.pk, 'available_for_release': True, 'document': image, } request = self.factory.post('/', data) request.user = self.user request.session = data configure_messages(request) response = view(request, tally_id=tally.pk) self.assertFalse(response.context_data['form'].is_valid()) self.assertEqual( response.context_data['form'].errors['document'][0], str('File size must be under 10.0\xa0MB.' ' Current file size is 20.0\xa0MB.'))
def test_create_race_view(self): tally = create_tally() tally.users.add(self.user) view = views.CreateRaceView.as_view() file_size = settings.MAX_FILE_UPLOAD_SIZE image_file_name = "image.jpg" image_file = SimpleUploadedFile(image_file_name, bytes(file_size), content_type="image/jpeg") data = { 'number': 2, 'race_type': 0, 'tally_id': tally.pk, 'available_for_release': True, 'document': image_file, } request = self.factory.post('/', data) request.user = self.user request.session = data configure_messages(request) response = view(request, tally_id=tally.pk) self.assertEqual(response.status_code, 302) ballot = Ballot.objects.get(document__isnull=False) self.assertIn(image_file_name, ballot.document.path) shutil.rmtree(os.path.dirname(ballot.document.path))
def test_disable_entity_view_post_station(self): tally = create_tally() tally.users.add(self.user) center = create_center(tally=tally) station = create_station(center) comment_text = 'example comment text' view = views.DisableEntityView.as_view() data = { 'center_code_input': center.code, 'station_number_input': station.station_number, 'tally_id': tally.pk, 'comment_input': comment_text, 'disable_reason': '2', } request = self.factory.post('/', data) request.user = self.user response = view(request, center_code=center.code, station_number=station.station_number, tally_id=tally.pk) self.assertEqual(response.status_code, 302) self.assertIn('/data/center-list/%s/' % tally.pk, response['Location']) station.reload() self.assertEqual(station.disable_reason.value, 2) self.assertEqual(station.comments.all()[0].text, comment_text)
def test_create_race_invalid_document_extension_error(self): tally = create_tally() tally.users.add(self.user) view = views.CreateRaceView.as_view() file_size = settings.MAX_FILE_UPLOAD_SIZE video = SimpleUploadedFile("file.mp4", bytes(file_size), content_type="video/mp4") data = { 'number': 1, 'race_type': 0, 'tally_id': tally.pk, 'available_for_release': True, 'document': video, } request = self.factory.post('/', data) request.user = self.user request.session = data configure_messages(request) response = view(request, tally_id=tally.pk) self.assertFalse(response.context_data['form'].is_valid()) self.assertEqual( response.context_data['form'].errors['document'][0], str('File extention (.mp4) is not supported.' ' Allowed extensions are: .png, .jpg, .doc, .pdf.'))
def test_quality_control_post_quarantine(self): self._create_and_login_user() tally = create_tally() tally.users.add(self.user) center = create_center() create_station(center) create_quarantine_checks() result_form = create_result_form(form_state=FormState.QUALITY_CONTROL, tally=tally, center=center, station_number=1) create_reconciliation_form(result_form, self.user, number_unstamped_ballots=1000) create_quality_control(result_form, self.user) self._add_user_to_group(self.user, groups.QUALITY_CONTROL_CLERK) view = views.QualityControlDashboardView.as_view() data = { 'correct': 1, 'result_form': result_form.pk, 'tally_id': tally.pk, } request = self.factory.post('/', data=data) request.session = {'result_form': result_form.pk} request.user = self.user response = view(request, tally_id=tally.pk) result_form.reload() self.assertEqual(response.status_code, 302) self.assertTrue(result_form.form_state, FormState.AUDIT) self.assertTrue(result_form.audit) self.assertEqual(result_form.audit.quarantine_checks.count(), 2) self.assertEqual(result_form.audit.user, self.user) self.assertEqual(result_form.audited_count, 1) self.assertIn('quality-control/print', response['location'])
def test_new_form_post_invalid(self): # save clearance as clerk self._create_and_login_user() self._add_user_to_group(self.user, groups.CLEARANCE_CLERK) tally = create_tally() tally.users.add(self.user) result_form = create_result_form(form_state=FormState.CLEARANCE, force_ballot=False, tally=tally, gender=None) view = CreateResultFormView.as_view(clearance_result_form=True) data = { 'result_form': result_form.pk, 'gender': [u'0'], 'tally_id': tally.id, } request = self.factory.post('/', data=data) request.user = self.user request.session = {'result_form': result_form.pk} response = view(request, tally_id=tally.pk) result_form.reload() pk = request.session['result_form'] self.assertEqual(response.status_code, 200) self.assertIsNotNone(pk) result_form = ResultForm.objects.get(pk=pk) self.assertIsNotNone(result_form.barcode) self.assertEqual(result_form.form_state, FormState.CLEARANCE)
def test_new_form_post_invalid(self): # save clearance as clerk self._create_and_login_user() self._add_user_to_group(self.user, groups.CLEARANCE_CLERK) tally = create_tally() tally.users.add(self.user) result_form = create_result_form( form_state=FormState.CLEARANCE, force_ballot=False, tally=tally, gender=None) view = CreateResultFormView.as_view(clearance_result_form=True) data = { 'result_form': result_form.pk, 'gender': [u'0'], 'tally_id': tally.id, } request = self.factory.post('/', data=data) request.user = self.user request.session = {'result_form': result_form.pk} response = view(request, tally_id=tally.pk) result_form.reload() pk = request.session['result_form'] self.assertEqual(response.status_code, 200) self.assertIsNotNone(pk) result_form = ResultForm.objects.get(pk=pk) self.assertIsNotNone(result_form.barcode) self.assertEqual(result_form.form_state, FormState.CLEARANCE)
def test_new_form_post(self): # save clearance as clerk self._create_and_login_user() self._add_user_to_group(self.user, groups.CLEARANCE_CLERK) tally = create_tally() tally.users.add(self.user) center = create_center(tally=tally) station = create_station(center) office = create_office(tally=tally) result_form = create_result_form( form_state=FormState.CLEARANCE, force_ballot=False, tally=tally, gender=Gender.MALE) ballot = create_ballot(tally=tally) view = CreateResultFormView.as_view(clearance_result_form=True) data = { 'result_form': result_form.pk, 'gender': [u'0'], 'ballot': [ballot.pk], 'center': [center.pk], 'office': [office.pk], 'tally_id': tally.id, 'station_number': station.station_number, } request = self.factory.post('/', data=data) request.user = self.user request.session = {'result_form': result_form.pk} response = view(request, tally_id=tally.pk) result_form.reload() self.assertEqual(response.status_code, 200) self.assertEqual(result_form.gender, Gender.MALE)
def test_review_post_supervisor_implement(self): # save clearance as clerk self._create_and_login_user() self._add_user_to_group(self.user, groups.CLEARANCE_CLERK) tally = create_tally() tally.users.add(self.user) center = create_center(tally=tally) station_number = 2 result_form = create_result_form(form_state=FormState.CLEARANCE, center=center, tally=tally, station_number=station_number) self.assertEqual(result_form.center, center) self.assertEqual(result_form.station_number, station_number) view = views.ReviewView.as_view() data = { 'result_form': result_form.pk, 'action_prior_to_recommendation': 1, 'resolution_recommendation': 0, 'forward': 1, 'tally_id': tally.pk, } request = self.factory.post('/', data=data) request.user = self.user request.session = data response = view(request, tally_id=tally.pk) # save as supervisor self._create_and_login_user(username='******') self._add_user_to_group(self.user, groups.CLEARANCE_SUPERVISOR) tally.users.add(self.user) view = views.ReviewView.as_view() data = { 'result_form': result_form.pk, 'action_prior_to_recommendation': 1, 'resolution_recommendation': 3, 'implement': 1, 'tally_id': tally.pk, } request = self.factory.post('/', data=data) request.user = self.user request.session = data response = view(request, tally_id=tally.pk) clearance = result_form.clearances.all()[0] result_form.reload() self.assertEqual(clearance.supervisor, self.user) self.assertFalse(clearance.active) self.assertTrue(clearance.reviewed_supervisor) self.assertTrue(clearance.reviewed_team) self.assertEqual(clearance.action_prior_to_recommendation, ActionsPrior.REQUEST_AUDIT_ACTION_FROM_FIELD) self.assertEqual(response.status_code, 302) self.assertEqual(result_form.form_state, FormState.UNSUBMITTED) self.assertEqual(result_form.center, center) self.assertEqual(result_form.station_number, station_number)
def test_clearance(self): self._create_or_login_intake_clerk() tally = create_tally() tally.users.add(self.user) result_form = create_result_form(tally=tally) view = views.ClearanceView.as_view() self.request.user = self.user self.request.session = { 'result_form': result_form.pk, 'tally_id': tally.pk, } self.request.session['encoded_result_form_intake_start_time'] =\ self.encoded_result_form_intake_start_time with self.assertRaises(Exception): response = view(self.request) result_form.form_state = FormState.INTAKE result_form.save() result_form.form_state = FormState.CLEARANCE result_form.save() response = view(self.request, tally_id=tally.pk) self.assertIsNone(self.request.session.get('result_form')) self.assertContains(response, 'Form Sent to Clearance. Pass to Supervisor') result_form_stat = ResultFormStats.objects.get(result_form=result_form) self.assertEqual(result_form_stat.approved_by_supervisor, False) self.assertEqual(result_form_stat.reviewed_by_supervisor, False) self.assertEqual(result_form_stat.user, self.user) self.assertEqual(result_form_stat.result_form, result_form)
def test_center_detail_redirects_no_center(self): self._create_and_login_user() tally = create_tally() tally.users.add(self.user) barcode = '123456789' create_result_form(barcode, form_state=FormState.UNSUBMITTED, tally=tally) self._add_user_to_group(self.user, groups.INTAKE_CLERK) view = views.CenterDetailsView.as_view() barcode_data = { 'barcode': barcode, 'barcode_copy': barcode, 'tally_id': tally.id, } request = self.factory.post('/', data=barcode_data) request.user = self.user request.session = {} response = view(request, tally_id=tally.pk) self.assertEqual(response.status_code, 302) self.assertIn('intake/enter-center', response['location']) result_form = ResultForm.objects.get(barcode=barcode) self.assertEqual(result_form.form_state, FormState.INTAKE) self.assertEqual(result_form.user, self.user)
def test_set_user_timeout_invalid_post(self): tally = create_tally() tally.users.add(self.user) view = views.SetUserTimeOutView.as_view() user_idle_timeout = 'example' data = {'user_idle_timeout': user_idle_timeout} request = self.factory.post('/', data=data) request.user = self.user site_id = getattr(settings, "SITE_ID", None) site = None try: site = Site.objects.get(pk=site_id) except Site.DoesNotExist: site = Site.objects.create(name="HENC RMS") site_id = site.id create_site_info(site=site, user_idle_timeout=50) response = view(request, site_id=site_id) response.render() siteinfo = SiteInfo.objects.get(site__pk=site_id) self.assertNotEqual(response.status_code, 302) self.assertIn( str('Current user idle timeout: ' '{} minutes').format(siteinfo.user_idle_timeout), str(response.content)) self.assertContains( response, '<label for="id_user_idle_timeout">User idle timeout:</label>') self.assertContains( response, str('<input type="text" name="user_idle_timeout" value="example" ' 'size="50" required id="id_user_idle_timeout">'))
def test_surpervisors_approvals_get_with_no_data(self): tally = create_tally() tally.users.add(self.user) request = self._get_request() view = staff_performance_metrics.SupervisorsApprovalsView.as_view() request = self.factory.get('/') request.user = self.user response = view( request, tally_id=tally.pk, ) number_of_tables = 3 self.assertContains(response, "<h1>Supervisors Approvals Rate Report</h1>") self.assertContains(response, "<h3>Audit Supervisors Approval Rate</h3>") self.assertContains(response, "<h3>Tally Managers Approval Rate</h3>") self.assertContains(response, "<th>Number of Approved Forms</th>", number_of_tables) self.assertContains(response, "<th>Number of Forms Sent For Review</th>", number_of_tables) self.assertContains(response, "<th>Approval Rate</th>", number_of_tables) self.assertContains(response, '<td>No Data</td>', number_of_tables)
def test_center_detail_center_alpha_numeric(self): self._create_and_login_user() self._add_user_to_group(self.user, groups.DATA_ENTRY_1_CLERK) tally = create_tally() tally.users.add(self.user) code = '12345' station_number = 1 center = create_center(code, tally=tally) create_station(center) result_form = create_result_form(form_state=FormState.DATA_ENTRY_1, center=center, tally=tally, station_number=station_number) view = views.CenterDetailsView.as_view() data = center_data('12345', '12346') data['center_number'] = 'abcde' data['center_number_copy'] = 'abcde' session = {'result_form': result_form.pk} data.update(session) request = self.factory.post('/', data=data) request.user = self.user request.session = session response = view(request, tally_id=tally.pk) self.assertContains(response, u'Expecting only numbers for center number')
def test_center_detail_invalid_center(self): self._create_and_login_user() self._add_user_to_group(self.user, groups.DATA_ENTRY_1_CLERK) tally = create_tally() tally.users.add(self.user) code = '12345' other_code = '21345' station_number = 1 center = create_center(code, tally=tally) other_center = create_center(other_code, tally=tally) create_station(center) create_station(other_center) result_form = create_result_form(form_state=FormState.DATA_ENTRY_1, center=center, tally=tally, station_number=station_number) view = views.CenterDetailsView.as_view() session = {'result_form': result_form.pk} data = center_data(other_code, station_number=station_number, tally_id=tally.pk) data.update(session) request = self.factory.post('/', data=data) request.user = self.user request.session = session response = view(request, tally_id=tally.pk) self.assertEqual(response.status_code, 200) response.render() self.assertContains(response, 'Center and station numbers do not match')
def test_result_form_stats(self): tally = create_tally() tally.users.add(self.user) result_form =\ create_result_form( name="Example", tally=tally, form_state=FormState.AUDIT) start_time = timezone.now() minutes = 65.5 end_time = start_time + timezone.timedelta(minutes=minutes) form_processing_time_in_seconds =\ (end_time - start_time).total_seconds() # Audit supervisor audit review audit_supervisor_user =\ self._create_user('audit_supervisor', 'password') self._add_user_to_group(audit_supervisor_user, groups.AUDIT_SUPERVISOR) result_form_stats = create_result_form_stats( processing_time=form_processing_time_in_seconds, user=audit_supervisor_user, result_form=result_form, reviewed_by_supervisor=True, approved_by_supervisor=True) self.assertTrue(isinstance(result_form_stats, ResultFormStats)) self.assertEqual(1, ResultFormStats.objects.count()) self.assertEqual(result_form_stats.processing_time, form_processing_time_in_seconds) self.assertEqual(result_form_stats.user, audit_supervisor_user) self.assertEqual(result_form_stats.result_form, result_form) self.assertEqual(result_form_stats.approved_by_supervisor, True) self.assertEqual(result_form_stats.reviewed_by_supervisor, True)
def test_station_progress_list_data_view(self): """ Test that station progress list data view returns correct data """ tally = create_tally() tally.users.add(self.user) center = create_center(tally=tally) station = create_station(center=center, tally=tally) create_result_form(form_state=FormState.ARCHIVED, tally=tally, station_number=station.station_number) view = views.StationProgressListDataView.as_view() request = self.factory.get('/') request.user = self.user response = view(request, tally_id=tally.pk) office_name, sub_constituency_code, center_name, center_code,\ station_number, gender, registrants, active = json.loads( response.content.decode())['data'][0] self.assertEquals(office_name, str(station.center.office.name)) self.assertEquals(sub_constituency_code, str(station.sub_constituency.code)) self.assertEquals(office_name, str(station.center.office.name)) self.assertEquals(center_name, str(station.center.name)) self.assertEquals(center_code, str(station.center.code)) self.assertEquals(station_number, str(station.station_number)) self.assertEquals(gender, str(station.gender.name.capitalize())) self.assertEquals(registrants, str(station.registrants)) self.assertEquals(active, str(station.active))
def test_review_post_forward(self): self._create_and_login_user() self._add_user_to_group(self.user, groups.CLEARANCE_CLERK) tally = create_tally() tally.users.add(self.user) result_form = create_result_form(form_state=FormState.CLEARANCE, tally=tally) view = views.ReviewView.as_view() data = { 'result_form': result_form.pk, 'action_prior_to_recommendation': 1, 'resolution_recommendation': 0, 'forward': 1, 'tally_id': tally.pk, } request = self.factory.post('/', data=data) request.user = self.user request.session = data response = view(request, tally_id=tally.pk) clearance = result_form.clearance self.assertEqual(clearance.user, self.user) self.assertEqual(clearance.reviewed_team, True) self.assertEqual(clearance.action_prior_to_recommendation, ActionsPrior.REQUEST_AUDIT_ACTION_FROM_FIELD) self.assertEqual(response.status_code, 302)
def test_intake_clerk_selects_matches(self): self._create_and_login_user() tally = create_tally() tally.users.add(self.user) barcode = '123456789' center = create_center(tally=tally) result_form = create_result_form(barcode, center=center, tally=tally) self._add_user_to_group(self.user, groups.INTAKE_CLERK) view = views.CheckCenterDetailsView.as_view() post_data = { 'result_form': result_form.pk, 'is_match': 'true', 'tally_id': tally.id, } request = self.factory.post('/', data=post_data) request.user = self.user request.session = {'result_form': result_form.pk} with self.assertRaises(Exception): response = view(request) result_form.form_state = FormState.INTAKE result_form.save() response = view(request, tally_id=tally.pk) self.assertEqual(response.status_code, 302) self.assertIn('/intake/printcover', response['location'])
def test_confirm_form_reset_view_post(self): self._create_and_login_user() tally = create_tally() tally.users.add(self.user) barcode = '123456789' ballot = create_ballot(available_for_release=True) create_result_form(barcode, tally=tally, ballot=ballot, form_state=FormState.QUALITY_CONTROL) result_form = ResultForm.objects.get(barcode=barcode) create_quality_control(result_form, self.user) self._add_user_to_group(self.user, groups.QUALITY_CONTROL_CLERK) view = views.ConfirmFormResetView.as_view() reject_reason = 'Form Incorrect' data = {'reject_reason': reject_reason} request = self.factory.post('/', data=data) request.user = self.user request.session = {'result_form': result_form.pk} response = view(request, tally_id=tally.pk) result_form = ResultForm.objects.get(pk=result_form.pk) quality_control = result_form.qualitycontrol_set.all()[0] self.assertEqual(response.status_code, 302) self.assertIn('quality-control/reject', response['location']) self.assertEqual(request.session, {}) self.assertEqual(result_form.form_state, FormState.DATA_ENTRY_1) self.assertEqual(result_form.rejected_count, 1) self.assertEqual(result_form.reject_reason, reject_reason) self.assertFalse(quality_control.active) self.assertFalse(quality_control.passed_reconciliation)
def test_center_detail_redirects_to_enter_results(self): self._create_and_login_user() self._add_user_to_group(self.user, groups.DATA_ENTRY_1_CLERK) tally = create_tally() tally.users.add(self.user) code = '12345' station_number = 1 center = create_center(code, tally=tally) create_station(center) result_form = create_result_form(form_state=FormState.DATA_ENTRY_1, center=center, tally=tally, station_number=station_number) view = views.CenterDetailsView.as_view() result_form_data = {'result_form': result_form.pk} data = center_data(code, station_number=station_number, tally_id=tally.pk) data.update(result_form_data) request = self.factory.post('/', data=data) request.user = self.user request.session = result_form_data response = view(request, tally_id=tally.pk) self.assertEqual(response.status_code, 302) self.assertIn('data-entry/enter-results', response['location'])
def test_confirmation_get_corrections(self): self._create_and_login_user() self._add_user_to_group(self.user, groups.DATA_ENTRY_2_CLERK) tally = create_tally() tally.users.add(self.user) result_form = create_result_form(form_state=FormState.CORRECTION, tally=tally) view = views.ConfirmationView.as_view() request = self.factory.get('/') request.user = self.user request.session = {'result_form': result_form.pk} request.session['encoded_result_form_data_entry_start_time'] =\ self.encoded_result_form_data_entry_start_time response = view(request, tally_id=tally.pk) self.assertIsNone(request.session.get('result_form')) self.assertEqual(response.status_code, 200) self.assertContains(response, 'Corrections') self.assertContains(response, reverse( 'data-entry', kwargs={'tally_id': tally.pk})) result_form_stat = ResultFormStats.objects.get(user=self.user) self.assertEqual(result_form_stat.approved_by_supervisor, False) self.assertEqual(result_form_stat.reviewed_by_supervisor, False) self.assertEqual(result_form_stat.user, self.user) self.assertEqual(result_form_stat.result_form, result_form)
def test_intake_clerk_is_redirected(self): self._create_and_login_user() tally = create_tally() tally.users.add(self.user) self._add_user_to_group(self.user, groups.INTAKE_CLERK) response = self.view(self.request, tally_id=tally.pk) self.assertEqual(response.status_code, 302) self.assertIn('/intake', response['location'])
def test_form_not_received_list_csv_view(self): tally = create_tally() tally.users.add(self.user) view = views.FormNotReceivedListView.as_view() request = self.factory.get('/') request.user = self.user response = view(request, format='csv', tally_id=tally.pk) self.assertContains(response, "barcode")
def setUp(self): self.factory = RequestFactory() self._create_permission_groups() self._create_and_login_user() self._add_user_to_group(self.user, groups.TALLY_MANAGER) self.tally = create_tally() self.tally.users.add(self.user) create_office(name=self.office_name, tally=self.tally)
def test_race_list_view(self): tally = create_tally() tally.users.add(self.user) view = views.RaceListView.as_view() request = self.factory.get('/') request.user = self.user response = view(request, tally_id=tally.pk) self.assertContains(response, "Races List") self.assertContains(response, "New Race")
def test_form_not_received_list_view(self): tally = create_tally() tally.users.add(self.user) view = views.FormNotReceivedListView.as_view() request = self.factory.get('/') request.user = self.user response = view(request, tally_id=tally.pk) self.assertContains(response, "Forms Not Received") self.assertNotContains(response, "New Form")
def test_dashboard_get_forms(self): tally = create_tally() create_result_form(form_state=FormState.CLEARANCE, station_number=42, tally=tally) response = self._common_view_tests( views.DashboardView.as_view(), tally=tally) self.assertContains(response, 'Clearance') self.assertContains(response, '42')
def test_print_cover_invalid_state(self): self._create_or_login_intake_clerk() tally = create_tally() tally.users.add(self.user) result_form = create_result_form(tally=tally) view = views.PrintCoverView.as_view() self.request.user = self.user self.request.session = {'result_form': result_form.pk} with self.assertRaises(SuspiciousOperation): view(self.request, tally_id=tally.pk)
def test_forms_for_race(self): tally = create_tally() tally.users.add(self.user) ballot = create_ballot(tally=tally) view = views.FormsForRaceView.as_view() request = self.factory.get('/') request.user = self.user response = view(request, ballot=ballot.number, tally_id=tally.pk) self.assertContains(response, "Forms for Race %s" % ballot.number) self.assertNotContains(response, "New Form")
def test_center_list_view(self): tally = create_tally() tally.users.add(self.user) view = views.CenterListView.as_view() request = self.factory.get('/') request.user = self.user response = view(request, tally_id=tally.pk) self.assertContains(response, "Center and Station List") self.assertContains(response, "Download") self.assertContains(response, "New Station") self.assertContains(response, "New Center")