def create_and_log_in_user(testapp=None, department=None, rolename=None, username="******"): # set up a user user = User.create(username=username, email="{}@example.com".format(username), password="******") # associate a department if a department is passed if department: user.departments.append(department) user.active = True user.save() # associate a role if a name is passed if rolename: user_role = Role(name=rolename) user_role.save() user.roles.append(user_role) # login response = testapp.get("/login/") form = response.forms['loginForm'] form['username'] = user.username form['password'] = '******' response = form.submit().follow() return user
def make_admin_user(): password = prompt_pass("Password") user = User.create(username="******", email="*****@*****.**", password=password, active=True) admin_role = Role(name='admin') admin_role.save() user.roles.append(admin_role) user.save()
def log_in_user(testapp, department): # set up a user user = User.create(username="******", email="*****@*****.**", password="******") user.departments.append(department) user.active = True user.save() # login response = testapp.get("/login/") form = response.forms['loginForm'] form['username'] = user.username form['password'] = '******' response = form.submit().follow() return user
def test_client(): delete_everything() department = Department.create(name="Busy Town Police Department", short_name="BTPD", load_defaults=True) user = User.create(username="******", email="*****@*****.**", password="******", active=True, is_admin=True) user.departments.append(department) user.save() test_client = JSONTestClient() # missing_data_mutator = MissingDataMutator() # fuzzed_data_mutator = FuzzedDataMutator() # known_bad_data_mutator = KnownBadDataMutator() # empty_data_mutator = EmptyDataMutator() # casing_mutator = CasingMutator() # condenisng_date_mutator = CondenisngDateMutator() # gap_date_mutator = GapDateMutator() test_client.run(department, [])
def register(): form = RegisterForm(request.form, csrf_enabled=False) if form.validate_on_submit(): invite_code = Invite_Code.query.filter_by(code=form.invite_code.data).first() invite_code.used = True invite_code.save() new_user = User.create( username=form.username.data, email=form.email.data, password=form.password.data, active=True, department_id=invite_code.department_id, ) flash("Thank you for registering. You can now log in.", "success") return redirect(url_for("public.home")) else: flash_errors(form) return render_template("public/register.html", form=form)
def register(): form = RegisterForm(request.form, csrf_enabled=False) if form.validate_on_submit(): invite_code = Invite_Code.query.filter_by( code=form.invite_code.data).first() invite_code.used = True invite_code.save() new_user = User.create(username=form.username.data, email=form.email.data, password=form.password.data, active=True) new_user.departments.append( Department.get_by_id(invite_code.department_id)) flash("Thank you for registering. You can now log in.", 'success') return redirect(url_for('public.home')) else: flash_errors(form) return render_template('public/register.html', form=form)
def test_client(): ''' Erase the database and load in a full suite of test data ''' if not prompt_bool( "Are you sure you want to destroy and recreate Comport's database?" ): return delete_everything() # create a fake PD and admin user department = Department.create( name="Izquierda Metropolitan Police Department", short_name="IMPD", load_defaults=True) user = User.create(username="******", email="*****@*****.**", password="******", active=True, is_admin=True) user.departments.append(department) user.save() # create some fake officer out on service data date_now = datetime.datetime.now() date_step = date_now - relativedelta(months=30) while date_step.year < date_now.year or date_step.month < date_now.month: DenominatorValue.create( department_id=department.id, month=date_step.month, year=date_step.year, officers_out_on_service=(100000 + (randint(0, 46000) - 23000))) date_step = date_step + relativedelta(months=1) # create some fake demographic data demo_template = [ dict(race="Asian", city_factor=0.0194, dept_factor=0.0013), dict(race="Black", city_factor=0.2452, dept_factor=0.1402), dict(race="Hispanic", city_factor=0.0861, dept_factor=0.0253), dict(race="Other", city_factor=0.0699, dept_factor=0.0101), dict(race="White", city_factor=0.5794, dept_factor=0.8231) ] # for the city city_population = 100000 + round(100000 * ((randint(0, 16) / 100) - .08)) for value in demo_template: DemographicValue.create(department_id=department.id, race=value["race"], count=round(city_population * value["city_factor"]), department_value=False) # for the department dept_population = 1500 + round(1500 * ((randint(0, 16) / 100) - .08)) for value in demo_template: DemographicValue.create(department_id=department.id, race=value["race"], count=round(dept_population * value["dept_factor"]), department_value=True) # create a JSON test client and run it test_client = JSONTestClient() mutations = [] # mutations.append(MissingDataMutator()) # mutations.append(FuzzedDataMutator()) # mutations.append(KnownBadDataMutator()) # mutations.append(EmptyDataMutator()) # mutations.append(CasingMutator()) # mutations.append(CondenisngDateMutator()) # mutations.append(GapDateMutator()) test_client.run(department, mutations)
def load_test_data(): department = Department.query.filter_by( name="Indianapolis Metropolitan Police Department", short_name="IMPD").first() if not department: department = Department.create( name="Indianapolis Metropolitan Police Department", short_name="IMPD") if not User.query.filter_by(username="******").first(): User.create(username="******", email="*****@*****.**", password="******", active=True, department_id=department.id) for filename in glob.glob('data/testdata/complaints/complaints.csv'): with open(filename, 'rt') as f: reader = csv.DictReader(f) for complaint in reader: officer_identifier = hashlib.md5( (complaint.get("OFFNUM", None) + Config.SECRET_KEY).encode('UTF-8')).hexdigest() opaque_id = hashlib.md5( (complaint.get("INCNUM", None) + Config.SECRET_KEY).encode('UTF-8')).hexdigest() CitizenComplaint.create( opaque_id=opaque_id, department_id=department.id, occured_date=parse_csv_date( complaint.get("OCCURRED_DT", None)), division=complaint.get("UDTEXT24A", None), precinct=complaint.get("UDTEXT24B", None), shift=complaint.get("UDTEXT24C", None), beat=complaint.get("UDTEXT24D", None), disposition=complaint.get("FINDING", None), allegation_type=complaint.get("ALG_CLASS", None), allegation=complaint.get("ALLEGATION", None), census_tract=None, resident_race=complaint.get("RACE", None), officer_race=complaint.get("OFF_RACE", None), resident_sex=complaint.get("SEX", None), officer_sex=complaint.get("OFF_SEX", None), officer_identifier=officer_identifier, officer_years_of_service=complaint.get( "OFF_YR_EMPLOY", None), officer_age=complaint.get("OFF_AGE", None), resident_age=complaint.get("CIT_AGE", None)) for filename in glob.glob('data/testdata/uof/uof.csv'): with open(filename, 'rt') as f: reader = csv.DictReader(f) for incident in reader: officer_identifier = hashlib.md5( (incident.get("OFFNUM", None) + Config.SECRET_KEY).encode('UTF-8')).hexdigest() opaque_id = hashlib.md5( (incident.get("INCNUM", None) + Config.SECRET_KEY).encode('UTF-8')).hexdigest() UseOfForceIncident.create( department_id=department.id, opaque_id=opaque_id, occured_date=parse_csv_date( incident.get("OCCURRED_DT", None)), division=incident.get("UDTEXT24A", None), precinct=incident.get("UDTEXT24B", None), shift=incident.get("UDTEXT24C", None), beat=incident.get("UDTEXT24D", None), disposition=incident.get("DISPOSITION", None), census_tract=None, officer_force_type=incident.get("UOF_FORCE_TYPE", None), resident_resist_type=None, officer_weapon_used=None, resident_weapon_used=None, service_type=incident.get("SERVICE_TYPE", None), arrest_made=incident.get("CIT_ARRESTED", None), arrest_charges=incident.get("CITCHARGE_TYPE", None), resident_injured=incident.get("CIT_INJURED", None), resident_hospitalized=incident.get("CIT_HOSPITAL", None), officer_injured=incident.get("OFF_INJURED", None), officer_hospitalized=incident.get("OFF_HOSPITAL", None), use_of_force_reason=incident.get("UOF_REASON", None), resident_race=incident.get("RACE", None), officer_race=incident.get("OFF_RACE", None), resident_sex=incident.get("SEX", None), officer_sex=incident.get("OFF_SEX", None), officer_identifier=officer_identifier, officer_years_of_service=incident.get( "OFF_YR_EMPLOY", None), officer_age=incident.get("OFF_AGE", None), resident_age=incident.get("CIT_AGE", None), officer_condition=incident.get("OFF_COND_TYPE", None), resident_condition=incident.get("CIT_COND_TYPE", None)) for filename in glob.glob('data/testdata/ois/ois.csv'): with open(filename, 'rt') as f: reader = csv.DictReader(f) for incident in reader: officer_identifier = hashlib.md5( (incident.get("OFFNUM", None) + Config.SECRET_KEY).encode('UTF-8')).hexdigest() opaque_id = hashlib.md5( (incident.get("INCNUM", None) + Config.SECRET_KEY).encode('UTF-8')).hexdigest() OfficerInvolvedShooting.create( department_id=department.id, opaque_id=opaque_id, occured_date=parse_csv_date( incident.get("OCCURRED_DT", None)), division=incident.get("UDTEXT24A", None), precinct=incident.get("UDTEXT24B", None), shift=incident.get("UDTEXT24C", None), beat=incident.get("UDTEXT24D", None), disposition=incident.get("DISPOSITION", None), census_tract=None, officer_weapon_used=incident.get("WEAPON_TYPE", None), resident_weapon_used=incident.get("CIT_WEAPON_TYPE", None), service_type=incident.get("SERVICE_TYPE", None), resident_race=incident.get("RACE", None), officer_race=incident.get("OFF_RACE", None), resident_sex=incident.get("SEX", None), officer_sex=incident.get("OFF_SEX", None), officer_identifier=officer_identifier, officer_years_of_service=incident.get( "OFF_YR_EMPLOY", None), officer_age=incident.get("OFF_AGE", None), resident_age=incident.get("CIT_AGE", None), officer_condition=incident.get("OFF_COND_TYPE", None), resident_condition=incident.get("CIT_COND_TYPE", None)) for filename in glob.glob('data/testdata/denominators/denominators.csv'): with open(filename, 'rt') as f: reader = csv.DictReader(f) for month in reader: DenominatorValue.create(department_id=department.id, month=month.get("month", None), year=month.get("year", None), officers_out_on_service=month.get( "officers out on service", None)) for filename in glob.glob('data/testdata/demographics/demographics.csv'): with open(filename, 'rt') as f: reader = csv.DictReader(f) for value in reader: DemographicValue.create(department_id=department.id, race=value.get("race", None), count=value.get("count", None), department_value=value.get( "cityOrDepartment", None) == "department")
def test_check_password(self): user = User.create(username="******", email="*****@*****.**", password="******") assert user.check_password('foobarbaz123') is True assert user.check_password("barfoobaz") is False
def load_test_data(): department = Department.query.filter_by(name="Indianapolis Metropolitan Police Department", short_name="IMPD").first() if not department: department = Department.create(name="Indianapolis Metropolitan Police Department", short_name="IMPD") if not User.query.filter_by(username="******").first(): User.create(username="******", email="*****@*****.**", password="******", active=True, department_id=department.id) for filename in glob.glob('data/testdata/complaints/complaints.csv'): with open(filename, 'rt') as f: reader = csv.DictReader(f) for complaint in reader: officer_identifier = hashlib.md5((complaint.get("OFFNUM", None) + Config.SECRET_KEY).encode('UTF-8')).hexdigest() opaque_id = hashlib.md5((complaint.get("INCNUM", None) + Config.SECRET_KEY).encode('UTF-8')).hexdigest() CitizenComplaint.create( opaque_id=opaque_id, department_id=department.id, occured_date=parse_csv_date(complaint.get("OCCURRED_DT", None)), division=complaint.get("UDTEXT24A", None), precinct=complaint.get("UDTEXT24B", None), shift=complaint.get("UDTEXT24C", None), beat=complaint.get("UDTEXT24D", None), disposition=complaint.get("FINDING", None), allegation_type=complaint.get("ALG_CLASS", None), allegation=complaint.get("ALLEGATION", None), census_tract=None, resident_race=complaint.get("RACE", None), officer_race=complaint.get("OFF_RACE", None), resident_sex=complaint.get("SEX", None), officer_sex=complaint.get("OFF_SEX", None), officer_identifier=officer_identifier, officer_years_of_service=complaint.get("OFF_YR_EMPLOY", None), officer_age=complaint.get("OFF_AGE", None), resident_age=complaint.get("CIT_AGE", None) ) for filename in glob.glob('data/testdata/uof/uof.csv'): with open(filename, 'rt') as f: reader = csv.DictReader(f) for incident in reader: officer_identifier = hashlib.md5((incident.get("OFFNUM", None) + Config.SECRET_KEY).encode('UTF-8')).hexdigest() opaque_id = hashlib.md5((incident.get("INCNUM", None) + Config.SECRET_KEY).encode('UTF-8')).hexdigest() UseOfForceIncident.create( department_id=department.id, opaque_id=opaque_id, occured_date=parse_csv_date(incident.get("OCCURRED_DT", None)), division=incident.get("UDTEXT24A", None), precinct=incident.get("UDTEXT24B", None), shift=incident.get("UDTEXT24C", None), beat=incident.get("UDTEXT24D", None), disposition=incident.get("DISPOSITION", None), census_tract=None, officer_force_type=incident.get("UOF_FORCE_TYPE", None), resident_resist_type=None, officer_weapon_used=None, resident_weapon_used=None, service_type=incident.get("SERVICE_TYPE", None), arrest_made=incident.get("CIT_ARRESTED", None), arrest_charges=incident.get("CITCHARGE_TYPE", None), resident_injured=incident.get("CIT_INJURED", None), resident_hospitalized=incident.get("CIT_HOSPITAL", None), officer_injured=incident.get("OFF_INJURED", None), officer_hospitalized=incident.get("OFF_HOSPITAL", None), use_of_force_reason=incident.get("UOF_REASON", None), resident_race=incident.get("RACE", None), officer_race=incident.get("OFF_RACE", None), resident_sex=incident.get("SEX", None), officer_sex=incident.get("OFF_SEX", None), officer_identifier=officer_identifier, officer_years_of_service=incident.get("OFF_YR_EMPLOY", None), officer_age=incident.get("OFF_AGE", None), resident_age=incident.get("CIT_AGE", None), officer_condition=incident.get("OFF_COND_TYPE", None), resident_condition=incident.get("CIT_COND_TYPE", None) ) for filename in glob.glob('data/testdata/ois/ois.csv'): with open(filename, 'rt') as f: reader = csv.DictReader(f) for incident in reader: officer_identifier = hashlib.md5((incident.get("OFFNUM", None) + Config.SECRET_KEY).encode('UTF-8')).hexdigest() opaque_id = hashlib.md5((incident.get("INCNUM", None) + Config.SECRET_KEY).encode('UTF-8')).hexdigest() OfficerInvolvedShooting.create( department_id=department.id, opaque_id=opaque_id, occured_date=parse_csv_date(incident.get("OCCURRED_DT", None)), division=incident.get("UDTEXT24A", None), precinct=incident.get("UDTEXT24B", None), shift=incident.get("UDTEXT24C", None), beat=incident.get("UDTEXT24D", None), disposition=incident.get("DISPOSITION", None), census_tract=None, officer_weapon_used=incident.get("WEAPON_TYPE", None), resident_weapon_used=incident.get("CIT_WEAPON_TYPE", None), service_type=incident.get("SERVICE_TYPE", None), resident_race=incident.get("RACE", None), officer_race=incident.get("OFF_RACE", None), resident_sex=incident.get("SEX", None), officer_sex=incident.get("OFF_SEX", None), officer_identifier=officer_identifier, officer_years_of_service=incident.get("OFF_YR_EMPLOY", None), officer_age=incident.get("OFF_AGE", None), resident_age=incident.get("CIT_AGE", None), officer_condition=incident.get("OFF_COND_TYPE", None), resident_condition=incident.get("CIT_COND_TYPE", None) ) for filename in glob.glob('data/testdata/denominators/denominators.csv'): with open(filename, 'rt') as f: reader = csv.DictReader(f) for month in reader: DenominatorValue.create( department_id=department.id, month=month.get("month", None), year=month.get("year", None), officers_out_on_service=month.get("officers out on service", None) ) for filename in glob.glob('data/testdata/demographics/demographics.csv'): with open(filename, 'rt') as f: reader = csv.DictReader(f) for value in reader: DemographicValue.create( department_id=department.id, race=value.get("race", None), count=value.get("count", None), department_value=value.get("cityOrDepartment", None) == "department" )
def test_client(): ''' Erase the database and load in a full suite of test data ''' if not prompt_bool("Are you sure you want to destroy and recreate Comport's database?"): return delete_everything() # create a fake PD and admin user department = Department.create(name="Izquierda Metropolitan Police Department", short_name="IMPD", load_defaults=True) user = User.create(username="******", email="*****@*****.**", password="******", active=True, is_admin=True) user.departments.append(department) user.save() # create some fake officer out on service data date_now = datetime.datetime.now() date_step = date_now - relativedelta(months=30) while date_step.year < date_now.year or date_step.month < date_now.month: DenominatorValue.create( department_id=department.id, month=date_step.month, year=date_step.year, officers_out_on_service=(100000 + (randint(0, 46000) - 23000)) ) date_step = date_step + relativedelta(months=1) # create some fake demographic data demo_template = [ dict(race="Asian", city_factor=0.0194, dept_factor=0.0013), dict(race="Black", city_factor=0.2452, dept_factor=0.1402), dict(race="Hispanic", city_factor=0.0861, dept_factor=0.0253), dict(race="Other", city_factor=0.0699, dept_factor=0.0101), dict(race="White", city_factor=0.5794, dept_factor=0.8231) ] # for the city city_population = 100000 + round(100000 * ((randint(0, 16) / 100) - .08)) for value in demo_template: DemographicValue.create( department_id=department.id, race=value["race"], count=round(city_population * value["city_factor"]), department_value=False ) # for the department dept_population = 1500 + round(1500 * ((randint(0, 16) / 100) - .08)) for value in demo_template: DemographicValue.create( department_id=department.id, race=value["race"], count=round(dept_population * value["dept_factor"]), department_value=True ) # create a JSON test client and run it test_client = JSONTestClient() mutations = [] # mutations.append(MissingDataMutator()) # mutations.append(FuzzedDataMutator()) # mutations.append(KnownBadDataMutator()) # mutations.append(EmptyDataMutator()) # mutations.append(CasingMutator()) # mutations.append(CondenisngDateMutator()) # mutations.append(GapDateMutator()) test_client.run(department, mutations)