コード例 #1
0
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
コード例 #2
0
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
コード例 #3
0
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
コード例 #4
0
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"]
コード例 #5
0
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")
コード例 #6
0
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
コード例 #7
0
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")
コード例 #8
0
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")
コード例 #9
0
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
コード例 #10
0
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
コード例 #11
0
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"))
コード例 #12
0
def test_get_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,
        )

        login(client, username="******", password="******")

        response = client.get(
            url_for("study.change_info"), follow_redirects=True
        )
        assert response.status_code == 200
        assert b"Male" in response.data
        assert b"20-29" in response.data
        assert b"Afghanistan" in response.data
        assert b"Aland Islands" in response.data
        assert b"Level 6" in response.data
        assert b"Almoner" in response.data
コード例 #13
0
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
コード例 #14
0
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"))
コード例 #15
0
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
        )
コード例 #16
0
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
コード例 #17
0
def test_participant_error_handler_404(client, init_database):
    """
    GIVEN a Flask application, logged in participant
    WHEN '/<path:unkown_path>' is requested (GET)
    THEN check response content, status code
    """

    with client.application.test_request_context():
        create_participant(client, username="******", password="******")
        login(client, username="******", password="******")

        response = client.get("/no_resource_found_here", follow_redirects=True)

        assert response.status_code == 404
        assert b"Error :(" in response.data
        assert b"404 Not Found" in response.data
        assert b"Go Back" in response.data
        assert bytes(url_for("study.index"), "utf-8") in response.data
        client.get(url_for("auth.logout"))
コード例 #18
0
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
コード例 #19
0
def test_participant_error_handler_405(client, init_database):
    """
    GIVEN a Flask application, logged in participant
    WHEN '/study/user_info' is posted (POST)
    THEN check response content, status code
    """

    with client.application.test_request_context():
        create_participant(
            client,
            username="******",
            password="******",
            completed_form=True,
        )
        login(client, username="******", password="******")

        response = client.post(url_for("study.index"), follow_redirects=True)

        assert response.status_code == 405
        assert b"Error :(" in response.data
        assert b"405 Method Not Allowed" in response.data
        assert b"Go Back" in response.data
        assert bytes(url_for("study.index"), "utf-8") in response.data
        client.get(url_for("auth.logout"))
コード例 #20
0
def test_valid_participant_required_invalid(client, init_database):
    """
    GIVEN a Flask application, participant not in study
    WHEN routes with @valid_participant_required is requested (GET)
    THEN check redirect location
    """
    with client.application.test_request_context():
        participant = create_participant(client, username="******", password="******")
        study = create_study(client)
        login(client, username="******", password="******")
        response = client.get(
            url_for("study.study", id=study.id), follow_redirects=False
        )
        assert urlparse(response.location).path == url_for("auth.login")
        client.get(url_for("auth.logout"))
コード例 #21
0
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")
コード例 #22
0
def test_valid_login(client, init_database):
    """
    GIVEN a Flask application
    WHEN '/auth/login' is posted with VALID credentials (POST)
    THEN check response is valid and user is logged in
    """
    with client.application.test_request_context():

        participant = create_participant(client,
                                         username="******",
                                         password="******")
        response = client.post(
            url_for("auth.login"),
            data=dict(username="******", password="******"),
            follow_redirects=True,
        )

        form = UserInfoForm()
        assert response.status_code == 200
        assert bytes(form.gender.label.text, "utf-8") in response.data
        for choice in form.gender.iter_choices():
            assert bytes(choice[1], "utf-8") in response.data
        assert bytes(form.age_group.label.text, "utf-8") in response.data
        for choice in form.age_group.iter_choices():
            assert bytes(choice[1], "utf-8") in response.data
        assert bytes(form.nationality.label.text, "utf-8") in response.data
        for choice in form.nationality.iter_choices():
            assert bytes(choice[1], "utf-8") in response.data
        assert bytes(form.latest_country.label.text, "utf-8") in response.data
        for choice in form.latest_country.iter_choices():
            assert bytes(choice[1], "utf-8") in response.data
        assert bytes(form.education_level.label.text, "utf-8") in response.data
        for choice in form.education_level.iter_choices():
            assert bytes(choice[1], "utf-8") in response.data
        assert bytes(form.occupation.label.text, "utf-8") in response.data
        for choice in form.occupation.iter_choices():
            assert bytes(choice[1], "utf-8") in response.data
        assert bytes(form.income.label.text, "utf-8") in response.data
        for choice in form.income.iter_choices():
            assert bytes(choice[1], "utf-8") in response.data
        assert bytes(form.submit.label.text, "utf-8") in response.data

        client.get("auth.logout")
コード例 #23
0
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)
コード例 #24
0
def test_logout_logged_in(client, init_database):
    """
    GIVEN a Flask application
    WHEN '/auth/logout' is requested when logged in (GET)
    THEN check response is valid and user is logged out
    """
    # login
    with client.application.test_request_context():
        participant = create_participant(client,
                                         username="******",
                                         password="******")
        response = client.post(
            url_for("auth.login"),
            data=dict(username="******", password="******"),
            follow_redirects=True,
        )
        assert response.status_code == 200
        # logout
        response = client.get(url_for("auth.logout"), follow_redirects=True)
        assert response.status_code == 200
        assert b"Sign In" in response.data
        assert b"Username" in response.data
        assert b"Password" in response.data
コード例 #25
0
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
コード例 #26
0
def test_admin_required(client, init_database):
    """
    GIVEN a Flask application, participant
    WHEN all routes with @admin_required decorator are requested (GET)
    THEN check response location
    """
    with client.application.test_request_context():
        create_participant(client, username="******", password="******")
        login(client, username="******", password="******")

        study = create_study(client)

        response = client.get(url_for("responses.general", id=study.id),
                              follow_redirects=False)
        assert urlparse(response.location).path == url_for("auth.login")
        response = client.get(url_for("responses.heat_maps", id=study.id),
                              follow_redirects=False)
        assert urlparse(response.location).path == url_for("auth.login")
        response = client.get(
            url_for("responses.compare_responses", id=study.id),
            follow_redirects=False,
        )
        assert urlparse(response.location).path == url_for("auth.login")
        response = client.get(
            url_for("responses.average_response", id=study.id),
            follow_redirects=False,
        )
        assert urlparse(response.location).path == url_for("auth.login")
        response = client.get(
            url_for("responses.create_pdf", id=study.id),
            follow_redirects=False,
        )
        assert urlparse(response.location).path == url_for("auth.login")

        response = client.get(url_for("admin.index"), follow_redirects=False)
        assert urlparse(response.location).path == url_for("auth.login")
        response = client.get(url_for("admin.study", id=study.id),
                              follow_redirects=False)
        assert urlparse(response.location).path == url_for("auth.login")
        response = client.get(
            url_for("admin.card_set", id=study.card_set_x.id),
            follow_redirects=False,
        )
        assert urlparse(response.location).path == url_for("auth.login")
        response = client.get(
            url_for("admin.user_group", id=study.user_group.id),
            follow_redirects=False,
        )
        assert urlparse(response.location).path == url_for("auth.login")
        response = client.get(url_for("admin.new_study"),
                              follow_redirects=False)
        assert urlparse(response.location).path == url_for("auth.login")
        response = client.get(url_for("admin.new_card_set"),
                              follow_redirects=False)
        assert urlparse(response.location).path == url_for("auth.login")
        response = client.get(url_for("admin.new_user_group"),
                              follow_redirects=False)
        assert urlparse(response.location).path == url_for("auth.login")
        response = client.get(
            url_for("admin.delete_user_group", id=study.user_group.id),
            follow_redirects=False,
        )
        assert urlparse(response.location).path == url_for("auth.login")
        response = client.get(
            url_for("admin.delete_card_set", id=study.card_set_x.id),
            follow_redirects=False,
        )
        assert urlparse(response.location).path == url_for("auth.login")
        response = client.get(url_for("admin.delete_study", id=study.id),
                              follow_redirects=False)
        assert urlparse(response.location).path == url_for("auth.login")

        client.get(url_for("auth.logout"))
コード例 #27
0
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()
コード例 #28
0
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")
コード例 #29
0
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"]
コード例 #30
0
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