def test_get_all_studies(client, init_database): """ GIVEN a Flask application, logged in admin and a study created WHEN '/api/get/study/all' is requested with a valid access token (GET) THEN check response is valid, and content is correct. """ with client.application.test_request_context(): admin = create_admin(client) access_token = api_login(client) p1 = create_participant(client, username="******") p2 = create_participant(client, username="******") ug1 = create_user_group(client, participants=[p1], creator=admin) ug2 = create_user_group(client, participants=[p2], creator=admin) study_1 = create_study(client, creator=admin, user_group=ug1) study_2 = create_study(client, creator=admin, user_group=ug2) response = client.get( url_for("api.get_all_studies"), headers={ "Authorization": "Bearer {}".format(access_token["access_token"]) }, ) json = response.json assert response.status_code == 200 assert len(json) == 2
def test_get_all_user_groups(client, init_database): """ GIVEN a Flask application and a logged in admin WHEN '/api/get/user_group/all' is requested with a valid access token (GET) THEN check response code and content """ with client.application.test_request_context(): admin = create_admin(client) access_token = api_login(client) participant_1 = create_participant(client, username="******") participant_2 = create_participant(client, username="******") user_group = create_user_group( client, creator=admin, participants=[participant_1, participant_2]) participant_1 = create_participant(client, username="******") participant_2 = create_participant(client, username="******") user_group = create_user_group( client, creator=admin, participants=[participant_1, participant_2]) response = client.get( url_for("api.get_all_user_groups"), headers={ "Authorization": "Bearer {}".format(access_token["access_token"]) }, ) json = response.json assert response.status_code == 200 assert len(json) == 2
def test_participant_required(client, init_database): """ GIVEN a Flask application, participant in study WHEN routes with @participant_required is requested (GET) THEN check redirect location """ with client.application.test_request_context(): admin = create_admin(client, username="******", password="******") participant = create_participant(client, completed_form=True) user_group = create_user_group( client, creator=admin, participants=[participant] ) study = create_study(client, creator=admin, user_group=user_group) # with admin login(client, username="******", password="******") response = client.get( url_for("study.index", id=study.id), follow_redirects=False ) assert urlparse(response.location).path == url_for("auth.login") response = client.get( url_for("study.study", id=study.id), follow_redirects=False ) assert urlparse(response.location).path == url_for("auth.login") response = client.get( url_for("study.user_info", id=study.id), follow_redirects=False ) assert urlparse(response.location).path == url_for("auth.login") response = client.get( url_for("study.change_info", id=study.id), follow_redirects=False ) assert urlparse(response.location).path == url_for("auth.login") response = client.get( url_for("study.complete", id=study.id), follow_redirects=False ) assert urlparse(response.location).path == url_for("auth.login") client.get(url_for("auth.logout")) # with anon response = client.get( url_for("study.index", id=study.id), follow_redirects=False ) assert urlparse(response.location).path == url_for("auth.login") response = client.get( url_for("study.study", id=study.id), follow_redirects=False ) assert urlparse(response.location).path == url_for("auth.login") response = client.get( url_for("study.user_info", id=study.id), follow_redirects=False ) assert urlparse(response.location).path == url_for("auth.login") response = client.get( url_for("study.change_info", id=study.id), follow_redirects=False ) assert urlparse(response.location).path == url_for("auth.login") response = client.get( url_for("study.complete", id=study.id), follow_redirects=False ) assert urlparse(response.location).path == url_for("auth.login") client.get(url_for("auth.logout"))
def test_get_all_responses(client, init_database): """ GIVEN a Flask application and logged in user WHEN '/api/get/response/all' is requested with valid access token (GET) THEN check status code and response content and length """ with client.application.test_request_context(): admin = create_admin(client) access_token = api_login(client) participant_1 = create_participant(client, username="******") participant_2 = create_participant(client, username="******") user_group = create_user_group( client, participants=[participant_1, participant_2], creator=admin) study = create_study(client, creator=admin, user_group=user_group) study_response_1 = create_response(client, creator=admin, participant=participant_1, study=study) study_response_2 = create_response(client, creator=admin, participant=participant_2, study=study) response = client.get( url_for("api.get_all_responses"), headers={ "Authorization": "Bearer {}".format(access_token["access_token"]) }, ) assert response.status_code == 200 assert len(response.json) == 2
def test_calculate_count(client, init_database): """ GIVEN a Flask application, study, responses WHEN CreateHeatMap.calculate_count is called THEN check returned data """ admin = create_admin(client) participant_1 = create_participant(client) participant_2 = create_participant(client, username='******') user_group = create_user_group(client, participants=[participant_1, participant_2], creator=admin) study = create_study(client, creator=admin, user_group=user_group, data_value_labels=[]) response_1 = create_response(client, study=study, creator=admin, participant=participant_1) response_2 = create_response(client, study=study, creator=admin, participant=participant_2) c_1 = Card.query.filter_by(name='Entertainment').first() c_2 = Card.query.filter_by(name='Health Service').first() c = CreateOneHeatMapCount(study) data = c.calculate_count(c_1, c_2) assert 2 in data["values"]
def test_get_study(client, init_database): """ GIVEN a Flask application, participant, study WHEN '/study/<int:id>' is requested (GET) THEN check response content, status code """ with client.application.test_request_context(): admin = create_admin(client) participant = create_participant( client, username="******", password="******", gender="Male", age_group="20-29", country_of_birth="AF", latest_country="AX", education_level="L6", occupation="Almoner", completed_form=True, ) user_group = create_user_group(client, creator=admin, participants=[participant]) study = create_study(client, creator=admin, user_group=user_group) login(client, username="******", password="******") response = client.get(url_for("study.study", id=study.id), follow_redirects=True) no_rows = str(study.number_of_rows - 1) no_cols = str(study.number_of_columns - 1) assert response.status_code == 200 assert bytes(study.card_set_x.name, "utf-8") in response.data assert bytes(study.card_set_x.cards[0].name, "utf-8") in response.data assert (bytes(study.card_set_x.cards[0].description, "utf-8") in response.data) assert bytes(study.card_set_x.cards[0].image, "utf-8") in response.data assert (bytes("Highest " + study.card_set_x.measure, "utf-8") in response.data) assert (bytes("Lowest " + study.card_set_x.measure, "utf-8") in response.data) assert bytes(study.card_set_y.name, "utf-8") in response.data assert bytes(study.card_set_y.cards[0].name, "utf-8") in response.data assert (bytes(study.card_set_y.cards[0].description, "utf-8") in response.data) assert bytes(study.card_set_y.cards[0].image, "utf-8") in response.data assert (bytes("Highest " + study.card_set_y.measure, "utf-8") in response.data) assert (bytes("Lowest " + study.card_set_y.measure, "utf-8") in response.data) assert (bytes(study.data_value_labels[0].label, "utf-8") in response.data) assert bytes("row_" + no_rows, "utf-8") in response.data assert bytes("col_" + no_cols, "utf-8") in response.data
def test_CreateOneHeatMapCount(client, init_database): """ GIVEN a Flask application, study, response WHEN CreateOneHeatMapCount.add is called THEN check no error and content """ admin = create_admin(client) participant = create_participant(client) user_group = create_user_group(client, participants=[participant], creator=admin) study = create_study(client, creator=admin, user_group=user_group, data_value_labels=[]) response = create_response(client, study=study, creator=admin, participant=participant) c = CreateOneHeatMapCount(study) c.add(study.card_set_x.cards[0], study.card_set_y.cards[0], None) plot = c.plots assert isinstance(plot, list) assert isinstance(plot[0], tuple) assert len(plot) == 1
def test_delete_current_user_group(client, init_database): """ GIVEN a Flask application, logged in admin, and a user group added to a current study WHEN '/admin/delete_study/1' is requested (GET) THEN check response is valid and the user group has not been deleted. """ with client.application.test_request_context(): admin = create_admin(client, username="******", password="******") login(client, username="******", password="******") user_group = create_user_group(client, creator=admin) study = create_study(client, name="Test Study", creator=admin, user_group=user_group) response = client.get( url_for("admin.delete_user_group", id=user_group.id), follow_redirects=True, ) assert response.status_code == 200 assert ( b"You cannot delete this User Group as it is currently associated with Test Study Study, remove this association before deleting this User Group." in response.data) user_group_db = UserGroup.query.filter_by(id=user_group.id).first() assert user_group_db.name == user_group.name assert user_group_db.creator == user_group.creator assert user_group_db.users == user_group.users assert user_group_db.study == user_group.study
def test_edit_current_user_group(client, init_database): """ GIVEN a Flask application, logged in admin, and a user group added to a current study WHEN '/admin/study/1' is requested (GET) THEN check response is valid and the user group cannot be edited. """ with client.application.test_request_context(): admin = create_admin(client, username="******", password="******") login(client, username="******", password="******") user_group = create_user_group(client, creator=admin) study = create_study( client, creator=admin, user_group=user_group, start_date=date.today(), ) response = client.get( url_for("admin.user_group", id=user_group.id), follow_redirects=True, ) assert response.status_code == 200 assert ( b"You cannot edit this User Group as the Study associated with it is currently in progress." in response.data)
def test_get_individual_participant(client, init_database): """ GIVEN a Flask application and a logged in admin WHEN '/api/get/participant/1' is requested with valid access token (GET) THEN check response code, and content """ with client.application.test_request_context(): admin = create_admin(client) access_token = api_login(client) participant = create_participant(client) user_group = create_user_group(client, creator=admin, participants=[participant]) response = client.get( url_for("api.get_participant", id=2), headers={ "Authorization": "Bearer {}".format(access_token["access_token"]) }, ) json = response.json assert response.status_code == 200 assert json["id"] == participant.id assert json["username"] == participant.username assert json["type"] == participant.type
def test_valid_participant_required_valid(client, init_database): """ GIVEN a Flask application, participant in study WHEN routes with @valid_participant_required is requested (GET) THEN check redirect location """ with client.application.test_request_context(): admin = create_admin(client) participant = create_participant( client, username="******", password="******", completed_form=True, ) user_group = create_user_group( client, creator=admin, participants=[participant] ) study = create_study(client, creator=admin, user_group=user_group) login(client, username="******", password="******") response = client.get( url_for("study.study", id=study.id), follow_redirects=False ) assert bytes(study.card_set_x.cards[0].name, "utf-8") in response.data client.get(url_for("auth.logout"))
def test_check_not_completed_study(client, init_database): """ GIVEN a Flask application, participant in study, incomplete study & compelete study. WHEN routes with @check_not_completed_study is requested (GET) THEN check redirect location """ with client.application.test_request_context(): participant = create_participant( client, username="******", password="******", completed_form=True ) admin = create_admin(client, username="******", password="******") user_group = create_user_group( client, creator=admin, participants=[participant] ) study = create_study(client, creator=admin, user_group=user_group) login(client, username="******", password="******") response = client.get( url_for("study.complete", id=study.id), follow_redirects=False ) assert urlparse(response.location).path == url_for("auth.login") participant.completed_study = True db.session.commit() response = client.get( url_for("study.complete", id=study.id), follow_redirects=False ) assert b"<h3>Thank You!</h3>" in response.data assert ( b"<p>Thanks for participating in the study, you can now log out of the system.</p>" in response.data )
def test_delete_user_group(client, init_database): """ GIVEN a Flask application, logged in admin, and a user group WHEN '/admin/delete_study/1' is requested (GET) THEN check response is valid and the user group has been deleted. """ with client.application.test_request_context(): admin = create_admin(client, username="******", password="******") login(client, username="******", password="******") user_group = create_user_group(client, name="Test User Group", creator=admin) response = client.get( url_for("admin.delete_user_group", id=user_group.id), follow_redirects=True, ) assert response.status_code == 200 assert (b"User Group Test User Group succesfully deleted." in response.data) user_group_db = UserGroup.query.filter_by(id=user_group.id).first() participants = Participant.query.all() assert user_group_db is None assert participants == []
def test_post_change_user_info(client, init_database): """ GIVEN a Flask application, participant, completed user_info form WHEN '/study/change_user_info' is requested (GET) THEN check status code and response content. """ with client.application.test_request_context(): admin = create_admin(client) participant = create_participant( client, username="******", password="******", gender="Male", age_group="20-29", country_of_birth="AF", latest_country="AX", education_level="L6", occupation="Almoner", completed_form=True, ) user_group = create_user_group( client, creator=admin, participants=[participant] ) study = create_study(client, creator=admin, user_group=user_group) login(client, username="******", password="******") form = UserInfoForm() form.gender.data = "Prefer not to say" form.age_group.data = "60-69" form.nationality.data = "GB" form.latest_country.data = "GB" form.education_level.data = "L5" form.occupation.data = "Advertising Agent" form.income.data = "10000-20000" form.submit.data = True response = client.post( url_for("study.change_info"), data=form.data, follow_redirects=False, ) assert urlparse(response.location).path == url_for("study.index") response = client.get(response.location, follow_redirects=True) assert response.status_code == 200 participant_db = Participant.query.filter_by(id=participant.id).first() assert participant_db.gender == form.gender.data assert participant_db.age_group == form.age_group.data assert participant_db.country_of_birth == form.nationality.data assert participant_db.latest_country == form.latest_country.data assert participant_db.education_level == form.education_level.data assert participant_db.occupation == form.occupation.data assert participant_db.completed_form == True assert participant_db.completed_study == False
def test_get_heat_maps(client, init_database): """ GIVEN a Flask application, admin, study, responses WHEN '/responses/heat_maps/<int:id>' is requested (GET) THEN check response content, status code """ with client.application.test_request_context(): admin = create_admin(client) participant_1 = create_participant(client, username="******") participant_2 = create_participant(client, username="******") user_group = create_user_group( client, creator=admin, participants=[participant_1, participant_2] ) study = create_study(client, creator=admin, user_group=user_group) study_response_1 = create_response( client, participant=participant_1, creator=admin, study=study ) study_response_2 = create_response( client, participant=participant_2, creator=admin, study=study ) login(client, username="******", password="******") response = client.get( url_for("responses.heat_maps", id=study.id), follow_redirects=True ) assert response.status_code == 200 assert b"<h3>Heat Maps</h3>" in response.data if len(study.data_value_labels) > 0: assert b"<label>Data Value Label</label>" in response.data assert b"<label>Type</label>" in response.data assert b'<option type="true">Count</option>' in response.data assert b'<option type="false">Not Count</option>' in response.data assert bytes(study.card_set_x.name, "utf-8") in response.data for card in study.card_set_x.cards: assert bytes(card.name, "utf-8") in response.data assert bytes(study.card_set_y.name, "utf-8") in response.data for card in study.card_set_y.cards: assert bytes(card.name, "utf-8") in response.data for data_value_label in study.data_value_labels: assert bytes(data_value_label.label, "utf-8") in response.data assert bytes(str(participant_1.id), "utf-8") in response.data assert bytes(str(participant_2.id), "utf-8") in response.data client.get(url_for("auth.logout")) admin2 = create_admin(client, username="******", password="******") login(client, username="******", password="******") response = client.get( url_for("responses.heat_maps", id=study.id), follow_redirects=False ) assert urlparse(response.location).path == url_for("auth.login")
def test_post_compare_responses(client, init_database): """ GIVEN a Flask application, admin, study, responses WHEN '/responses/compare_responses/<int:id>' is posted (POST) THEN check response content, status code """ with client.application.test_request_context(): admin = create_admin(client) participant_1 = create_participant( client, username="******", password="******", gender="Male", age_group="20-29", country_of_birth="AF", latest_country="AX", education_level="L6", occupation="Almoner", completed_form=True, ) participant_2 = create_participant(client, username="******") user_group = create_user_group( client, creator=admin, participants=[participant_1, participant_2]) study = create_study(client, creator=admin, user_group=user_group) study_response_1 = create_response(client, participant=participant_2, creator=admin, study=study) study_response_2 = create_response(client, participant=participant_2, creator=admin, study=study) login(client, username="******", password="******") data = { "response_id_1": study_response_1.id, "response_id_2": study_response_2.id, } response = client.post( url_for("responses.compare_responses", id=study.id), json=data, follow_redirects=True, ) for data_value in study_response_1.data_values: assert bytes(data_value.data_value_label.label, 'utf-8') in response.data assert bytes(str(data_value.value), 'utf-8') in response.data for data_value in study_response_2.data_values: assert bytes(data_value.data_value_label.label, 'utf-8') in response.data assert bytes(str(data_value.value), 'utf-8') in response.data
def test_get_compare_responses(client, init_database): """ GIVEN a Flask application, admin, study, responses WHEN '/responses/compare_responses/<int:id>' is requested (GET) THEN check response content, status code """ with client.application.test_request_context(): admin = create_admin(client) participant_1 = create_participant(client, username="******") participant_2 = create_participant(client, username="******") user_group = create_user_group( client, creator=admin, participants=[participant_1, participant_2]) study = create_study(client, creator=admin, user_group=user_group) study_response_1 = create_response(client, participant=participant_1, creator=admin, study=study) study_response_2 = create_response(client, participant=participant_2, creator=admin, study=study) login(client, username="******", password="******") response = client.get( url_for("responses.compare_responses", id=study.id), follow_redirects=True, ) assert response.status_code == 200 assert b"Compare Responses" in response.data assert (bytes( "Here you can compare responses to study <b>{}</b> from pairs of users." .format(study.name), "utf-8", ) in response.data) assert b"Response 1" in response.data assert b"Response 2" in response.data assert b"Average" in response.data assert bytes(str(participant_1.username), "utf-8") in response.data assert bytes(str(participant_2.username), "utf-8") in response.data client.get(url_for("auth.logout")) admin2 = create_admin(client, username="******", password="******") login(client, username="******", password="******") response = client.get( url_for("responses.compare_responses", id=study.id), follow_redirects=False, ) assert urlparse(response.location).path == url_for("auth.login")
def test_edit_future_user_group(client, init_database): """ GIVEN a Flask application, logged in admin, and a user group added to a future study WHEN '/admin/study/1' is requested (GET) THEN check response is valid and the user group can be edited. """ with client.application.test_request_context(): admin = create_admin(client, username="******", password="******") login(client, username="******", password="******") user_group = create_user_group(client, creator=admin) study = create_study( client, creator=admin, user_group=user_group, start_date=date.today() + timedelta(days=1), ) response = client.get( url_for("admin.user_group", id=user_group.id), follow_redirects=True, ) assert response.status_code == 200 for user in study.user_group.users: assert bytes(user.email, "utf-8") in response.data form = UserGroupForm() assert bytes(form.name.label.text, "utf-8") in response.data assert bytes(form.users.label.text, "utf-8") in response.data user_group_form = UserGroupForm() user_group_form.name.data = "Edited User Group 1" user_group_form.users.pop_entry() user_group_form.users.append_entry("*****@*****.**") user_group_form.submit.data = True response = client.post( url_for("admin.user_group", id=1), data=user_group_form.data, follow_redirects=True, ) assert response.status_code == 200 user_group = UserGroup.query.filter_by(id=1).first() assert user_group.name == "Edited User Group 1" assert user_group.users[0].email == "*****@*****.**"
def test_get_create_pdf(client, init_database): """ GIVEN a Flask application, admin, study, responses WHEN '/responses/create_pdf/<int:id>' is requested (GET) THEN check response content, status code """ with client.application.test_request_context(): admin = create_admin(client) participant_1 = create_participant(client, username="******") participant_2 = create_participant(client, username="******") user_group = create_user_group( client, creator=admin, participants=[participant_1, participant_2] ) study = create_study(client, creator=admin, user_group=user_group) study_response_1 = create_response( client, participant=participant_1, creator=admin, study=study ) study_response_2 = create_response( client, participant=participant_2, creator=admin, study=study ) login(client, username="******", password="******") response = client.get( url_for("responses.create_pdf", id=study.id), follow_redirects=True ) assert response.status_code == 200 assert b"Create PDF" in response.data assert ( b"<p>Here you can generate a PDF and select what responses you want to include in the pdf</p>" in response.data ) assert b"All Responses" in response.data assert b"Average Response" in response.data assert b"Specific Responses" in response.data assert bytes(str(participant_1.username), "utf-8") in response.data assert bytes(str(participant_2.username), "utf-8") in response.data client.get(url_for("auth.logout")) admin2 = create_admin(client, username="******", password="******") login(client, username="******", password="******") response = client.get( url_for("responses.create_pdf", id=study.id), follow_redirects=False, ) assert urlparse(response.location).path == url_for("auth.login")
def test_get_general_study(client, init_database): """ GIVEN a Flask application, admin, study, responses WHEN '/responses/<int:id>' is requested (GET) THEN check response content, status code """ with client.application.test_request_context(): admin = create_admin(client) participant = create_participant( client, username="******", password="******", gender="Male", age_group="20-29", country_of_birth="AF", latest_country="AX", education_level="L6", occupation="Almoner", completed_form=True, ) user_group = create_user_group(client, creator=admin, participants=[participant]) study = create_study(client, creator=admin, user_group=user_group) study_response = create_response(client, participant=participant, creator=admin, study=study) login(client, username="******", password="******") response = client.get(url_for("responses.general", id=study.id), follow_redirects=True) assert response.status_code == 200 assert (bytes(str(len(study.responses)) + " Responses", "utf-8") in response.data) client.get(url_for("auth.logout")) admin2 = create_admin(client, username="******", password="******") login(client, username="******", password="******") response = client.get(url_for("responses.general", id=study.id), follow_redirects=False) assert urlparse(response.location).path == url_for("auth.login")
def test_get_card_y_responses(client, init_database): """ GIVEN a Flask application, study, response WHEN get_card_y_responses is called THEN check no error and return type """ admin = create_admin(client) participant = create_participant(client) user_group = create_user_group(client, participants=[participant], creator=admin) study = create_study(client, creator=admin, user_group=user_group) response = create_response(client, study=study, creator=admin, participant=participant) updated_heat_maps = get_card_y_responses(study) assert isinstance(updated_heat_maps, tuple)
def test_average_response(client, init_database): """ GIVEN a Flask application, study, responses WHEN average_response is called THEN check no error and return type """ with client.application.test_request_context(): admin = create_admin(client, username="******", password="******") login(client, username="******", password="******") p1 = create_participant(client, username="******") p2 = create_participant(client, username="******") p3 = create_participant(client, username="******") p4 = create_participant(client, username="******") user_group = create_user_group(client, creator=admin, participants=[p1, p2, p3, p4]) study = create_study(client, user_group=user_group, creator=admin) response_1 = create_response(client, study=study, participant=p1, creator=admin) response_2 = create_response(client, study=study, participant=p2, creator=admin) response_3 = create_response(client, study=study, participant=p3, creator=admin) response_4 = create_response(client, study=study, participant=p4, creator=admin) avg_response = average_response(study) for card_position_avg in avg_response.card_positions: for card_position in response_1.card_positions: if card_position_avg.card == card_position.card: assert card_position_avg.position == card_position.position
def test_get_index(client, init_database): """ GIVEN a Flask application, participant WHEN '/study' is requested (GET) THEN check status code and response content. """ with client.application.test_request_context(): admin = create_admin(client) participant = create_participant( client, username="******", password="******", gender="Male", age_group="20-29", country_of_birth="AF", latest_country="AX", education_level="L6", occupation="Almoner", completed_form=True, ) login(client, username="******", password="******") user_group = create_user_group(client, creator=admin, participants=[participant]) study = create_study(client, creator=admin, user_group=user_group) response = client.get(url_for("study.index"), follow_redirects=True) assert response.status_code == 200 assert bytes(study.name, "utf-8") in response.data assert bytes(study.description, "utf-8") in response.data if study.image: assert bytes(study.image, "utf-8") in response.data else: assert b"study_images/no_image.jpg" in response.data
def test_create_pdf(client, init_database): """ GIVEN a Flask application, study WHEN create_pdf is called THEN check no error and return type """ with client.application.test_request_context(): admin = create_admin(client, username="******", password="******") login(client, username="******", password="******") p1 = create_participant(client, username="******") p2 = create_participant(client, username="******") p3 = create_participant(client, username="******") p4 = create_participant(client, username="******") user_group = create_user_group(client, creator=admin, participants=[p1, p2, p3, p4]) study = create_study(client, user_group=user_group, creator=admin) response_1 = create_response(client, study=study, participant=p1, creator=admin) response_2 = create_response(client, study=study, participant=p2, creator=admin) response_3 = create_response(client, study=study, participant=p3, creator=admin) response_4 = create_response(client, study=study, participant=p4, creator=admin) file_path_1 = create_pdf(study, all_responses=True) assert isinstance(file_path_1, str) path = "app" + file_path_1 assert os.path.exists(path) is True file_path_2 = create_pdf(study, average_response2=True) assert isinstance(file_path_2, str) path = "app" + file_path_2 assert os.path.exists(path) == True assert file_path_1 != file_path_2 file_path_3 = create_pdf(study, response_ids=[response_1.id, response_3.id]) assert isinstance(file_path_3, str) path = "app" + file_path_3 assert os.path.exists(path) == True assert file_path_2 != file_path_3 assert file_path_1 != file_path_3 file_path_4 = create_pdf( study, average_response2=True, response_ids=[response_1.id, response_3.id], ) assert isinstance(file_path_4, str) path = "app" + file_path_4 assert os.path.exists(path) == True assert file_path_3 != file_path_4 assert file_path_2 != file_path_4 assert file_path_1 != file_path_4 file_path_5 = create_pdf(study, average_response2=True, all_responses=True) assert isinstance(file_path_1, str) path = "app" + file_path_5 assert os.path.exists(path) == True assert file_path_4 != file_path_5 assert file_path_3 != file_path_5 assert file_path_2 != file_path_5 assert file_path_1 != file_path_5
def test_get_average_response(client, init_database): """ GIVEN a Flask application, admin, study, responses WHEN '/responses/average_response/<int:id>' is requested (GET) THEN check response content, status code """ with client.application.test_request_context(): admin = create_admin(client) participant_1 = create_participant(client, username="******") participant_2 = create_participant(client, username="******") user_group = create_user_group( client, creator=admin, participants=[participant_1, participant_2]) study = create_study(client, creator=admin, user_group=user_group) study_response_1 = create_response(client, participant=participant_1, creator=admin, study=study) study_response_2 = create_response(client, participant=participant_2, creator=admin, study=study) login(client, username="******", password="******") response = client.get( url_for("responses.average_response", id=study.id), follow_redirects=True, ) assert response.status_code == 200 for card in study.card_set_x.cards: assert bytes(card.name, "utf-8") in response.data assert bytes(card.description, "utf-8") in response.data assert bytes(card.image, "utf-8") in response.data assert (bytes("Highest " + study.card_set_x.measure, "utf-8") in response.data) assert (bytes("Lowest " + study.card_set_x.measure, "utf-8") in response.data) for card in study.card_set_y.cards: assert bytes(card.name, "utf-8") in response.data assert bytes(card.description, "utf-8") in response.data assert bytes(card.image, "utf-8") in response.data assert (bytes("Highest " + study.card_set_y.measure, "utf-8") in response.data) assert (bytes("Lowest " + study.card_set_y.measure, "utf-8") in response.data) assert (bytes(study.data_value_labels[0].label, "utf-8") in response.data) client.get(url_for("auth.logout")) admin2 = create_admin(client, username="******", password="******") login(client, username="******", password="******") response = client.get( url_for("responses.average_response", id=study.id), follow_redirects=False, ) assert urlparse(response.location).path == url_for("auth.login")
def test_calculate_price(client, init_database): """ GIVEN a Flask application, study, responses WHEN CreateHeatMap.calculate_price is called THEN check returned data """ admin = create_admin(client) participant_1 = create_participant(client) participant_2 = create_participant(client, username='******') user_group = create_user_group(client, participants=[participant_1, participant_2], creator=admin) study = create_study(client, creator=admin, user_group=user_group, data_value_labels=[]) response_1 = create_response(client, study=study, creator=admin, participant=participant_1) response_2 = create_response(client, study=study, creator=admin, participant=participant_2) dv1 = DataValue.query.filter(DataValue.response == response_1).filter( DataValue.column == 0).filter(DataValue.row == 1).filter( DataValue.data_value_label == study.data_value_labels[0]).first() dv2 = DataValue.query.filter(DataValue.response == response_2).filter( DataValue.column == 0).filter(DataValue.row == 1).filter( DataValue.data_value_label == study.data_value_labels[0]).first() val_1 = 100 val_2 = 50 dv1.value = val_1 dv2.value = val_2 c_1 = Card.query.filter_by(name='Entertainment').first() c_2 = Card.query.filter_by(name='Health Service').first() for pos in c_1.positions: pos.position = 0 for pos in c_2.positions: pos.position = 1 db.session.commit() max_val = float('-inf') min_val = float('inf') for data_value in response_1.data_values: if data_value.data_value_label == study.data_value_labels[0]: if data_value.value > max_val: max_val = data_value.value if data_value.value < min_val: min_val = data_value.value normalised_1 = (val_1 - min_val) / (max_val - min_val) max_val = float('-inf') min_val = float('inf') for data_value in response_2.data_values: if data_value.data_value_label == study.data_value_labels[0]: if data_value.value > max_val: max_val = data_value.value if data_value.value < min_val: min_val = data_value.value normalised_2 = (val_2 - min_val) / (max_val - min_val) avg_normalised = (normalised_1 + normalised_2) / 2 c = CreateOneHeatMapCount(study) data = c.calculate_price(card_x=c_1, card_y=c_2, data_value_label=study.data_value_labels[0]) assert float("{:.2f}".format(avg_normalised)) in data["values"]
def test_post_study(client, init_database): """ GIVEN a Flask application, participant, study WHEN '/study/<int:id>' is posted (POST) THEN check database content, status code, response content """ with client.application.test_request_context(): admin = create_admin(client) participant = create_participant( client, username="******", password="******", gender="Male", age_group="20-29", country_of_birth="AF", latest_country="AX", education_level="L6", occupation="Almoner", completed_form=True, ) user_group = create_user_group(client, creator=admin, participants=[participant]) study = create_study(client, creator=admin, user_group=user_group) cards_x = { "col_0": [ { "id": 10, "name": "Financial", "image": "yay.jpg", "description": "", }, { "id": 8, "name": "Fashion", "image": "yay.jpg", "description": "", }, { "id": 11, "name": "Food and Beverage", "image": "yay.jpg", "description": "", }, { "id": 7, "name": "GP Health Records", "image": "yay.jpg", "description": "", }, ], "col_1": [ { "id": 9, "name": "Communications", "image": "yay.jpg", "description": "", }, { "id": 6, "name": "Service Contracts", "image": "yay.jpg", "description": "", }, { "id": 12, "name": "Location", "image": "yay.jpg", "description": "", }, ], "col_2": [ { "id": 4, "name": "Demographics Data", "image": "yay.jpg", "description": "", }, { "id": 3, "name": "Activity and Sleep", "image": "yay.jpg", "description": "", }, { "id": 5, "name": "Household", "image": "yay.jpg", "description": "", }, ], "col_3": [ { "id": 1, "name": "Entertainment", "image": "yay.jpg", "description": "", }, { "id": 2, "name": "Sensitive Health Report", "image": "yay.jpg", "description": "", }, ], } cards_y = { "row_3": [ { "id": 18, "name": "Government Regulated", "image": "hi.jpg", "description": "", }, { "id": 13, "name": "Mortgage", "image": "hi.jpg", "description": "", }, { "id": 25, "name": "Banks", "image": "hi.jpg", "description": "", }, { "id": 22, "name": "Supermarket", "image": "hi.jpg", "description": "", }, { "id": 15, "name": "Products and Services", "image": "hi.jpg", "description": "", }, ], "row_2": [ { "id": 16, "name": "Insurance", "image": "hi.jpg", "description": "", }, { "id": 14, "name": "Research Institute", "image": "hi.jpg", "description": "", }, { "id": 27, "name": "Electricity", "image": "hi.jpg", "description": "", }, { "id": 26, "name": "Government", "image": "hi.jpg", "description": "", }, { "id": 24, "name": "Council", "image": "hi.jpg", "description": "", }, ], "row_1": [ { "id": 20, "name": "Health Service", "image": "hi.jpg", "description": "", }, { "id": 21, "name": "Religious Organisation", "image": "hi.jpg", "description": "", }, { "id": 17, "name": "Education Institute", "image": "hi.jpg", "description": "", }, ], "row_0": [ { "id": 19, "name": "Social Media", "image": "hi.jpg", "description": "", }, { "id": 23, "name": "Authorities", "image": "hi.jpg", "description": "", }, ], } data_values = { "col_0_row_3": [ { "id": 1, "label": "You get the benefit (e.g. Personalised Service)", "value": 43, }, { "id": 2, "label": "Data Consumer gets the benefit (e.g. Supply Chain gets the benefit)", "value": 63, }, ], "col_1_row_3": [ { "id": 1, "label": "You get the benefit (e.g. Personalised Service)", "value": 82, }, { "id": 2, "label": "Data Consumer gets the benefit (e.g. Supply Chain gets the benefit)", "value": 72, }, ], "col_2_row_3": [ { "id": 1, "label": "You get the benefit (e.g. Personalised Service)", "value": 82, }, { "id": 2, "label": "Data Consumer gets the benefit (e.g. Supply Chain gets the benefit)", "value": 99, }, ], "col_3_row_3": [ { "id": 1, "label": "You get the benefit (e.g. Personalised Service)", "value": 71, }, { "id": 2, "label": "Data Consumer gets the benefit (e.g. Supply Chain gets the benefit)", "value": 72, }, ], "col_0_row_2": [ { "id": 1, "label": "You get the benefit (e.g. Personalised Service)", "value": 42, }, { "id": 2, "label": "Data Consumer gets the benefit (e.g. Supply Chain gets the benefit)", "value": 52, }, ], "col_1_row_2": [ { "id": 1, "label": "You get the benefit (e.g. Personalised Service)", "value": 67, }, { "id": 2, "label": "Data Consumer gets the benefit (e.g. Supply Chain gets the benefit)", "value": 89, }, ], "col_2_row_2": [ { "id": 1, "label": "You get the benefit (e.g. Personalised Service)", "value": 52, }, { "id": 2, "label": "Data Consumer gets the benefit (e.g. Supply Chain gets the benefit)", "value": 62, }, ], "col_3_row_2": [ { "id": 1, "label": "You get the benefit (e.g. Personalised Service)", "value": 66, }, { "id": 2, "label": "Data Consumer gets the benefit (e.g. Supply Chain gets the benefit)", "value": 72, }, ], "col_0_row_1": [ { "id": 1, "label": "You get the benefit (e.g. Personalised Service)", "value": 82, }, { "id": 2, "label": "Data Consumer gets the benefit (e.g. Supply Chain gets the benefit)", "value": 65, }, ], "col_1_row_1": [ { "id": 1, "label": "You get the benefit (e.g. Personalised Service)", "value": 82, }, { "id": 2, "label": "Data Consumer gets the benefit (e.g. Supply Chain gets the benefit)", "value": 32, }, ], "col_2_row_1": [ { "id": 1, "label": "You get the benefit (e.g. Personalised Service)", "value": 42, }, { "id": 2, "label": "Data Consumer gets the benefit (e.g. Supply Chain gets the benefit)", "value": 82, }, ], "col_3_row_1": [ { "id": 1, "label": "You get the benefit (e.g. Personalised Service)", "value": 92, }, { "id": 2, "label": "Data Consumer gets the benefit (e.g. Supply Chain gets the benefit)", "value": 73, }, ], "col_0_row_0": [ { "id": 1, "label": "You get the benefit (e.g. Personalised Service)", "value": 43, }, { "id": 2, "label": "Data Consumer gets the benefit (e.g. Supply Chain gets the benefit)", "value": 77, }, ], "col_1_row_0": [ { "id": 1, "label": "You get the benefit (e.g. Personalised Service)", "value": 28, }, { "id": 2, "label": "Data Consumer gets the benefit (e.g. Supply Chain gets the benefit)", "value": 32, }, ], "col_2_row_0": [ { "id": 1, "label": "You get the benefit (e.g. Personalised Service)", "value": 12, }, { "id": 2, "label": "Data Consumer gets the benefit (e.g. Supply Chain gets the benefit)", "value": 44, }, ], "col_3_row_0": [ { "id": 1, "label": "You get the benefit (e.g. Personalised Service)", "value": 66, }, { "id": 2, "label": "Data Consumer gets the benefit (e.g. Supply Chain gets the benefit)", "value": 34, }, ], } data = { "cards_x": cards_x, "cards_y": cards_y, "data_values": data_values, } login(client, username="******", password="******") response = client.post( url_for("study.study", id=study.id), json=dict(data), follow_redirects=False, ) response_data_json = ast.literal_eval(response.data.decode("utf-8")) assert response_data_json["url"] == url_for("study.complete") response = client.get(response_data_json["url"], follow_redirects=True) assert response.status_code == 200 assert b"Thank You!" in response.data assert ( b"<p>Thanks for participating in the study, you can now log out of the system.</p>" in response.data) response = client.get(url_for("study.study", id=study.id), follow_redirects=False) assert urlparse(response.location).path == url_for("study.complete") response = client.get(url_for("study.index"), follow_redirects=False) assert urlparse(response.location).path == url_for("study.complete") response = client.get(url_for("study.change_info"), follow_redirects=False) assert urlparse(response.location).path == url_for("study.complete") response = client.get(url_for("study.user_info"), follow_redirects=False) assert urlparse(response.location).path == url_for("study.complete") response_db = Response.query.filter_by(participant=participant).first() assert response_db.study == study assert response_db.participant == participant cards_x_munch = munchify(cards_x) cards_y_munch = munchify(cards_y) data_values_munch = munchify(data_values) for col, cards in cards_x_munch.items(): col_num = int(col.split('_')[1]) for card in cards: for card_position in response_db.card_positions: if card == card_position.card: assert card_position.col == col_num for row, cards in cards_y_munch.items(): row_num = int(row.split('_')[1]) for card in cards: for card_position in response_db.card_positions: if card == card_position.card: assert card_position.row_num == row_num for col_row, data_values in data_values_munch.items(): col_num = int(col_row.split('_')[1]) row_num = int(col_row.split('_')[3]) for data_value in data_values: for data_value_db in response_db.data_values: if col_num == data_value_db.column and row_num == data_value_db.row and data_value_db.data_value_label.label == data_value.label: assert data_value_db.value == data_value.value
def test_send_mail(client, init_database): """ GIVEN a Flask application, admin, study WHEN study is today THEN check emails are sent with correct content """ with client.application.test_request_context(): admin = create_admin(client, username="******", password="******") participant_1 = create_participant(client, username="******") participant_2 = create_participant(client, username="******") participant_3 = create_participant(client, username="******") participant_4 = create_participant(client, username="******") participant_5 = create_participant(client, username="******") user_group = create_user_group( client, creator=admin, participants=[participant_1, participant_2]) user_group_2 = create_user_group( client, creator=admin, participants=[participant_3, participant_4, participant_5], ) study = create_study( client, start_date=date.today(), creator=admin, user_group=user_group, ) study2 = create_study(client, creator=admin, start_date=date.today() + timedelta(days=3)) study_3 = create_study( client, creator=admin, start_date=date.today(), user_group=user_group_2, ) studies = Study.query.filter( func.DATE(Study.start_date) == date.today()).all() assert study2 not in studies letters = string.ascii_letters strength = 8 for study in studies: if study.mail_sent == False: user_group = UserGroup.query.filter_by( id=study.user_group_id).first() if user_group: with mail.record_messages() as outbox: for user in user_group.users: password = "".join( random.choice(letters) for i in range(strength)) user.set_password(password) body = render_template( "email.html", study=study, username=user.username, password=password, ) msg = Message( "You Have Been Invited To A Study!", recipients=[user.email], sender=current_app.config["MAIL_USERNAME"], ) msg.html = body assert password in msg.html assert user.username in msg.html assert msg.recipients[0] == user.email mail.send(msg) user.email = None assert user.email is None assert len(outbox) == len(user_group.users) try: study.mail_sent = True db.session.commit() except: db.session.rollback()