def test_ac_can_edit_officer_in_their_dept(mockdata, client, session): with current_app.test_request_context(): login_ac(client) department = Department.query.filter_by(id=AC_DEPT).first() first_name = 'Testier' last_name = 'OTester' middle_initial = 'R' suffix = '' race = random.choice(RACE_CHOICES)[0] gender = random.choice(GENDER_CHOICES)[0] form = AddOfficerForm(first_name=first_name, last_name=last_name, middle_initial=middle_initial, suffix=suffix, race=race, gender=gender, star_no=666, rank='COMMANDER', department=department.id, birth_year=1990, # because of encoding error, link_type must be set for tests links=[LinkForm(link_type='link').data]) data = process_form_data(form.data) rv = client.post( url_for('main.add_officer'), data=data, follow_redirects=True ) officer = Officer.query.filter_by( last_name=last_name).one() new_last_name = 'Shiny' form = EditOfficerForm( first_name=first_name, last_name=new_last_name, suffix=suffix, race=race, gender=gender, department=department.id, # because of encoding error, link_type must be set for tests links=[LinkForm(link_type='link').data] ) data = process_form_data(form.data) rv = client.post( url_for('main.edit_officer', officer_id=officer.id), data=data, follow_redirects=True ) assert new_last_name in rv.data assert last_name not in rv.data # Check the changes were added to the database officer = Officer.query.filter_by( id=officer.id).one() assert officer.last_name == new_last_name
def test_admin_can_add_new_officer_with_suffix(mockdata, client, session): with current_app.test_request_context(): login_admin(client) department = random.choice(dept_choices()) links = [ LinkForm(url='http://www.pleasework.com', link_type='link').data, LinkForm(url='http://www.avideo/?v=2345jk', link_type='video').data ] form = AddOfficerForm(first_name='Testy', last_name='McTesty', middle_initial='T', suffix='Jr', race='WHITE', gender='M', star_no=666, rank='COMMANDER', department=department.id, birth_year=1990, links=links) data = process_form_data(form.data) rv = client.post(url_for('main.add_officer'), data=data, follow_redirects=True) assert 'New Officer McTesty added' in rv.data.decode('utf-8') # Check the officer was added to the database officer = Officer.query.filter_by(last_name='McTesty').one() assert officer.first_name == 'Testy' assert officer.race == 'WHITE' assert officer.gender == 'M' assert officer.suffix == 'Jr'
def test_browse_filtering_allows_good(client, mockdata, session): with current_app.test_request_context(): department_id = Department.query.first().id # Add a officer with a specific race, gender, rank and age to the first page login_admin(client) links = [ LinkForm(url='http://www.pleasework.com', link_type='link').data, LinkForm(url='http://www.avideo/?v=2345jk', link_type='video').data ] form = AddOfficerForm(first_name='A', last_name='A', middle_initial='A', race='WHITE', gender='M', star_no=666, rank='COMMANDER', department=department_id, birth_year=1990, links=links) data = process_form_data(form.data) rv = client.post(url_for('main.add_officer'), data=data, follow_redirects=True) assert 'A' in rv.data.decode('utf-8') # Check the officer was added to the database officer = Officer.query.filter_by(last_name='A').one() assert officer.first_name == 'A' assert officer.race == 'WHITE' assert officer.gender == 'M' # Check that added officer appears when filtering for this race, gender, rank and age form = BrowseForm(race='WHITE', gender='M', rank='COMMANDER', min_age=datetime.now().year - 1991, max_age=datetime.now().year - 1989) data = process_form_data(form.data) rv = client.get(url_for('main.list_officer', department_id=department_id), data=data, follow_redirects=True) filter_list = rv.data.decode('utf-8').split("<dt>Race</dt>")[1:] assert any("<dd>White</dd>" in token for token in filter_list) filter_list = rv.data.decode('utf-8').split("<dt>Rank</dt>")[1:] assert any("<dd>COMMANDER</dd>" in token for token in filter_list) filter_list = rv.data.decode('utf-8').split("<dt>Gender</dt>")[1:] assert any("<dd>M</dd>" in token for token in filter_list)
def test_admins_can_edit_incident_links_and_licenses(mockdata, client, session): with current_app.test_request_context(): login_admin(client) inc = Incident.query.first() address_form = LocationForm( street_name=inc.address.street_name, cross_street1=inc.address.cross_street1, cross_street2=inc.address.cross_street2, city=inc.address.city, state=inc.address.state, zip_code=inc.address.zip_code ) old_links = inc.links old_links_forms = [LinkForm(url=link.url, link_type=link.link_type).data for link in inc.links] new_url = 'http://rachel.com' link_form = LinkForm(url='http://rachel.com', link_type='video') old_license_plates = inc.license_plates new_number = '453893' license_plates_form = LicensePlateForm(number=new_number, state='IA') ooid_forms = [OOIdForm(ooid=officer.id) for officer in inc.officers] form = IncidentForm( date_field=str(inc.date.date()), time_field=str(inc.date.time()), report_number=inc.report_number, description=inc.description, department='1', address=address_form.data, links=old_links_forms + [link_form.data], license_plates=[license_plates_form.data], officers=ooid_forms ) data = process_form_data(form.data) rv = client.post( url_for('main.incident_api', obj_id=inc.id) + '/edit', data=data, follow_redirects=True ) assert rv.status_code == 200 assert 'successfully updated' in rv.data.decode('utf-8') # old links are still there for link in old_links: assert link in inc.links assert new_url in [link.url for link in inc.links] # old license plates are gone assert old_license_plates not in inc.license_plates assert len(inc.license_plates) == 1 assert new_number in [lp.number for lp in inc.license_plates]
def test_admins_can_create_basic_incidents(mockdata, client, session): with current_app.test_request_context(): login_admin(client) date = datetime(2000, 5, 25, 1, 45) report_number = '42' address_form = LocationForm(street_name='AAAAA', cross_street1='BBBBB', city='FFFFF', state='IA', zip_code='03435') # These have to have a dropdown selected because if not, an empty Unicode string is sent, which does not mach the '' selector. link_form = LinkForm(link_type='video') license_plates_form = LicensePlateForm(state='AZ') form = IncidentForm(date_field=str(date.date()), time_field=str(date.time()), report_number=report_number, description='Something happened', department='1', address=address_form.data, links=[link_form.data], license_plates=[license_plates_form.data], officers=[]) data = process_form_data(form.data) rv = client.post(url_for('main.incident_api') + 'new', data=data, follow_redirects=True) assert rv.status_code == 200 assert 'created' in rv.data inc = Incident.query.filter_by(date=date).first() assert inc is not None
def test_admin_adds_officer_with_letter_in_badge_no(mockdata, client, session): with current_app.test_request_context(): login_admin(client) department = random.choice(dept_choices()) form = AddOfficerForm(first_name='Test', last_name='Testersly', middle_initial='T', race='WHITE', gender='M', star_no='T666', rank='COMMANDER', department=department.id, birth_year=1990, # because of encoding error, link_type must be set for tests links=[LinkForm(link_type='link').data]) data = process_form_data(form.data) rv = client.post( url_for('main.add_officer'), data=data, follow_redirects=True ) assert 'Testersly' in rv.data # Check the officer was added to the database officer = Officer.query.filter_by( last_name='Testersly').one() assert officer.first_name == 'Test' assert officer.race == 'WHITE' assert officer.gender == 'M' assert officer.assignments[0].star_no == 'T666'
def test_ac_cannot_edit_officer_not_in_their_dept(mockdata, client, session): with current_app.test_request_context(): login_ac(client) officer = officer = Officer.query.except_(Officer.query.filter_by(department_id=AC_DEPT)).first() old_last_name = officer.last_name new_last_name = 'Shiny' form = EditOfficerForm( last_name=new_last_name, # because of encoding error, link_type must be set for tests links=[LinkForm(link_type='link').data]) data = process_form_data(form.data) rv = client.post( url_for('main.edit_officer', officer_id=officer.id), data=data, follow_redirects=True ) assert rv.status_code == 403 # Ensure changes were not made to database officer = Officer.query.filter_by( id=officer.id).one() assert officer.last_name == old_last_name
def test_ac_cannot_add_new_officer_not_in_their_dept(mockdata, client, session): with current_app.test_request_context(): login_ac(client) department = Department.query.except_(Department.query.filter_by(id=AC_DEPT)).first() first_name = 'Sam' last_name = 'Augustus' middle_initial = 'H' race = random.choice(RACE_CHOICES)[0] gender = random.choice(GENDER_CHOICES)[0] form = AddOfficerForm(first_name=first_name, last_name=last_name, middle_initial=middle_initial, race=race, gender=gender, star_no=666, rank='COMMANDER', department=department.id, birth_year=1990, # because of encoding error, link_type must be set for tests links=[LinkForm(link_type='link').data]) data = process_form_data(form.data) client.post( url_for('main.add_officer'), data=data, follow_redirects=True ) officer = Officer.query.filter_by(last_name=last_name).first() assert officer is None
def test_officer_csv(mockdata, client, session): with current_app.test_request_context(): login_admin(client) department = random.choice(dept_choices()) links = [ LinkForm(url='http://www.pleasework.com', link_type='link').data, ] form = AddOfficerForm(first_name='CKtVwe2gqhAIc', last_name='FVkcjigWUeUyA', middle_initial='T', suffix='Jr', race='WHITE', gender='M', star_no=90009, rank='PO', department=department.id, birth_year=1910, links=links) # add the officer rv = client.post( url_for('main.add_officer'), data=process_form_data(form.data), follow_redirects=True ) # dump officer csv rv = client.get( url_for('main.download_dept_csv', department_id=department.id), follow_redirects=True ) # get csv entry matching officer last n"createdame csv = list(filter(lambda row: form.last_name.data in row, rv.data.decode('utf-8').split("\n"))) assert len(csv) == 1 assert form.first_name.data in csv[0] assert form.last_name.data in csv[0] assert form.rank.data in csv[0]
def test_admin_can_edit_existing_officer(mockdata, client, session): with current_app.test_request_context(): login_admin(client) department = random.choice(dept_choices()) link_url0 = 'http://pleasework.com' link_url1 = 'http://avideo/?v=2345jk' links = [ LinkForm(url=link_url0, link_type='link').data, LinkForm(url=link_url0, link_type='video').data ] form = AddOfficerForm(first_name='Test', last_name='Testerinski', middle_initial='T', race='WHITE', gender='M', star_no=666, rank='COMMANDER', department=department.id, birth_year=1990, links=links) data = process_form_data(form.data) rv = client.post( url_for('main.add_officer'), data=data, follow_redirects=True ) officer = Officer.query.filter_by( last_name='Testerinski').one() form = EditOfficerForm(last_name='Changed', links=links[:1]) data = process_form_data(form.data) rv = client.post( url_for('main.edit_officer', officer_id=officer.id), data=data, follow_redirects=True ) assert 'Changed' in rv.data assert 'Testerinski' not in rv.data assert link_url0 in rv.data assert link_url1 not in rv.data
def test_admins_can_edit_incident_officers(mockdata, client, session): with current_app.test_request_context(): login_admin(client) inc = Incident.query.options(joinedload(Incident.links), joinedload(Incident.license_plates), joinedload(Incident.officers)).first() address_form = LocationForm(street_name=inc.address.street_name, cross_street1=inc.address.cross_street1, cross_street2=inc.address.cross_street2, city=inc.address.city, state=inc.address.state, zip_code=inc.address.zip_code) links_forms = [ LinkForm(url=link.url, link_type=link.link_type).data for link in inc.links ] license_plates_forms = [ LicensePlateForm(number=lp.number, state=lp.state).data for lp in inc.license_plates ] old_officers = inc.officers old_officer_ids = [officer.id for officer in inc.officers] old_ooid_forms = [OOIdForm(oo_id=the_id) for the_id in old_officer_ids] # get a new officer that is different from the old officers new_officer = Officer.query.except_( Officer.query.filter(Officer.id.in_(old_officer_ids))).first() new_ooid_form = OOIdForm(oo_id=new_officer.id) form = IncidentForm(date_field=str(inc.date), time_field=str(inc.time), report_number=inc.report_number, description=inc.description, department='1', address=address_form.data, links=links_forms, license_plates=license_plates_forms, officers=old_ooid_forms + [new_ooid_form]) data = process_form_data(form.data) rv = client.post(url_for('main.incident_api', obj_id=inc.id) + '/edit', data=data, follow_redirects=True) assert rv.status_code == 200 assert 'successfully updated' in rv.data.decode('utf-8') for officer in old_officers: assert officer in inc.officers assert new_officer.id in [off.id for off in inc.officers]
def test_admins_can_edit_incident_date_and_address(mockdata, client, session): with current_app.test_request_context(): login_admin(client) inc = Incident.query.options(joinedload(Incident.links), joinedload(Incident.license_plates), joinedload(Incident.officers)).first() inc_id = inc.id new_date = date(2017, 6, 25) new_time = time(1, 45) street_name = 'Newest St' address_form = LocationForm(street_name=street_name, cross_street1='Your St', city='Boston', state='NH', zip_code='03435') links_forms = [ LinkForm(url=link.url, link_type=link.link_type).data for link in inc.links ] license_plates_forms = [ LicensePlateForm(number=lp.number, state=lp.state).data for lp in inc.license_plates ] ooid_forms = [OOIdForm(ooid=officer.id) for officer in inc.officers] form = IncidentForm(date_field=str(new_date), time_field=str(new_time), report_number=inc.report_number, description=inc.description, department='1', address=address_form.data, links=links_forms, license_plates=license_plates_forms, officers=ooid_forms) data = process_form_data(form.data) rv = client.post(url_for('main.incident_api', obj_id=inc.id) + '/edit', data=data, follow_redirects=True) assert rv.status_code == 200 assert 'successfully updated' in rv.data.decode('utf-8') updated = Incident.query.get(inc_id) assert updated.date == new_date assert updated.time == new_time assert updated.address.street_name == street_name
def test_admins_cannot_edit_nonexisting_officers(mockdata, client, session): with current_app.test_request_context(): login_admin(client) inc = Incident.query.first() address_form = LocationForm( street_name=inc.address.street_name, cross_street1=inc.address.cross_street1, cross_street2=inc.address.cross_street2, city=inc.address.city, state=inc.address.state, zip_code=inc.address.zip_code ) links_forms = [LinkForm(url=link.url, link_type=link.link_type).data for link in inc.links] license_plates_forms = [LicensePlateForm(number=lp.number, state=lp.state).data for lp in inc.license_plates] old_officers = inc.officers old_officer_ids = [officer.id for officer in inc.officers] old_ooid_forms = [OOIdForm(oo_id=the_id) for the_id in old_officer_ids] # create an OOIdForm with an invalid officer ID new_ooid_form = OOIdForm(oo_id="99999999999999999") form = IncidentForm( date_field=str(inc.date.date()), time_field=str(inc.date.time()), report_number=inc.report_number, description=inc.description, department='1', address=address_form.data, links=links_forms, license_plates=license_plates_forms, officers=old_ooid_forms + [new_ooid_form] ) data = process_form_data(form.data) rv = client.post( url_for('main.incident_api', obj_id=inc.id) + '/edit', data=data, follow_redirects=True ) assert rv.status_code == 200 assert 'Not a valid officer id' in rv.data.decode('utf-8') for officer in old_officers: assert officer in inc.officers
def test_incidents_csv(mockdata, client, session): with current_app.test_request_context(): login_admin(client) department = random.choice(dept_choices()) # Delete existing incidents for chosen department Incident.query.filter_by(department_id=department.id).delete() incident_date = datetime(2000, 5, 25, 1, 45) report_number = '42' address_form = LocationForm(street_name='ABCDE', city='FGHI', state='IA') link_form = LinkForm(url='http://example.com', link_type='video') license_plates_form = LicensePlateForm(state='AZ') form = IncidentForm(date_field=str(incident_date.date()), time_field=str(incident_date.time()), report_number=report_number, description='Something happened', department=str(department.id), department_id=department.id, address=address_form.data, links=[link_form.data], license_plates=[license_plates_form.data], officers=[]) # add the incident rv = client.post(url_for('main.incident_api') + 'new', data=process_form_data(form.data), follow_redirects=True) assert "created" in rv.data.decode('utf-8') # dump incident csv rv = client.get(url_for('main.download_incidents_csv', department_id=department.id), follow_redirects=True) # get the csv entry with matching report number csv = list( filter(lambda row: report_number in row, rv.data.decode('utf-8').split("\n"))) print(csv) assert len(csv) == 1 assert form.description.data in csv[0]
def test_ac_can_edit_incidents_in_their_department(mockdata, client, session): with current_app.test_request_context(): login_ac(client) inc = Incident.query.filter_by(department_id=AC_DEPT).first() new_date = datetime(2017, 6, 25, 1, 45) street_name = 'Newest St' address_form = LocationForm(street_name=street_name, cross_street1='Your St', city='Boston', state='NH', zip_code='03435') links_forms = [ LinkForm(url=link.url, link_type=link.link_type).data for link in inc.links ] license_plates_forms = [ LicensePlateForm(number=lp.number, state=lp.state).data for lp in inc.license_plates ] ooid_forms = [OOIdForm(ooid=officer.id) for officer in inc.officers] form = IncidentForm(date_field=str(new_date.date()), time_field=str(new_date.time()), report_number=inc.report_number, description=inc.description, department=AC_DEPT, address=address_form.data, links=links_forms, license_plates=license_plates_forms, officers=ooid_forms) data = process_form_data(form.data) rv = client.post(url_for('main.incident_api', obj_id=inc.id) + '/edit', data=data, follow_redirects=True) assert rv.status_code == 200 assert 'successfully updated' in rv.data.decode('utf-8') assert inc.date == new_date.date() assert inc.time == new_date.time() assert inc.address.street_name == street_name
def test_ac_can_add_new_officer_in_their_dept(mockdata, client, session): with current_app.test_request_context(): login_ac(client) department = Department.query.filter_by(id=AC_DEPT).first() first_name = 'Testy' last_name = 'OTester' middle_initial = 'R' race = random.choice(RACE_CHOICES)[0] gender = random.choice(GENDER_CHOICES)[0] form = AddOfficerForm(first_name=first_name, last_name=last_name, middle_initial=middle_initial, race=race, gender=gender, star_no=666, rank='COMMANDER', department=department.id, birth_year=1990, # because of encoding error, link_type must be set for tests links=[LinkForm(link_type='link').data]) data = process_form_data(form.data) rv = client.post( url_for('main.add_officer'), data=data, follow_redirects=True ) assert rv.status_code == 200 assert last_name in rv.data.decode('utf-8') # Check the officer was added to the database officer = Officer.query.filter_by( last_name=last_name).one() assert officer.first_name == first_name assert officer.race == race assert officer.gender == gender