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")
示例#2
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")
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")
示例#4
0
def test_get_all_card_sets(client, init_database):
    """
    GIVEN a Flask application, logged in admin and a card set created
    WHEN '/api/get/card_set/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)

        card_set_1 = create_card_set(client, creator=admin)
        card_set_2 = create_card_set(client, creator=admin)

        response = client.get(
            url_for("api.get_all_card_sets"),
            headers={
                "Authorization":
                "Bearer {}".format(access_token["access_token"])
            },
        )

        assert response.status_code == 200

        json = response.json
        assert len(json) == 2
        assert json[0]["id"] == card_set_1.id
        assert json[0]["name"] == card_set_1.name
        assert json[0]["measure"] == card_set_1.measure
        assert json[1]["id"] == card_set_2.id
        assert json[1]["name"] == card_set_2.name
        assert json[1]["measure"] == card_set_2.measure
示例#5
0
def test_get_index(client, init_database):
    """
    GIVEN a Flask application, admin, study
    WHEN '/admin' is requested (GET)
    THEN check response content, status code
    """
    with client.application.test_request_context():
        admin = create_admin(client, username="******", password="******")

        study = create_study(client, creator=admin)
        login(client, username="******", password="******")

        response = client.get(url_for("admin.index"), follow_redirects=True)

        assert response.status_code == 200
        assert bytes(study.name, "utf-8") in response.data
        assert bytes(study.card_set_x.name, "utf-8") in response.data
        assert bytes(study.card_set_y.name, "utf-8") in response.data
        assert bytes(study.user_group.name, "utf-8") in response.data
        assert bytes(study.description, "utf-8") in response.data
        assert bytes(study.image, "utf-8") in response.data
        assert bytes(str(study.number_of_columns), "utf-8") in response.data
        assert bytes(str(study.number_of_rows), "utf-8") in response.data
        assert bytes(str(study.start_date), "utf-8") in response.data
        assert bytes(str(study.end_date), "utf-8") in response.data
        assert bytes(study.name, "utf-8") in response.data
        client.get(url_for("auth.logout"))
示例#6
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
示例#7
0
def test_get_individual_cards(client, init_database):
    """
    GIVEN a Flask application, logged in admin and a card set created
    WHEN '/api/get/card/1' 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)

        card = create_card(client, creator=admin)

        response = client.get(
            url_for("api.get_card", id=1),
            headers={
                "Authorization":
                "Bearer {}".format(access_token["access_token"])
            },
        )

        assert response.status_code == 200

        json = response.json
        assert json["id"] == card.id
        assert json["name"] == card.name
        assert json["description"] == card.description
        assert json["image"] == card.image
示例#8
0
def test_get_all_data_value_label(client, init_database):
    """
    GIVEN a Flask application, logged in admin and a data value label created
    WHEN '/api/get/data_value_label/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)

        data_value_label_1 = create_data_value_label(client, creator=admin)
        data_value_label_2 = create_data_value_label(client, creator=admin)

        response = client.get(
            url_for("api.get_all_data_value_labels"),
            headers={
                "Authorization":
                "Bearer {}".format(access_token["access_token"])
            },
        )

        json = response.json

        assert response.status_code == 200
        assert json[0]["id"] == data_value_label_1.id
        assert json[0]["label"] == data_value_label_1.label
        assert json[1]["id"] == data_value_label_2.id
        assert json[1]["label"] == data_value_label_2.label
示例#9
0
def test_get_individual_study(client, init_database):
    """
    GIVEN a Flask application, logged in admin and a study created
    WHEN '/api/get/study/1' 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)

        study = create_study(client, creator=admin)
        response = client.get(
            url_for("api.get_study", id=1),
            headers={
                "Authorization":
                "Bearer {}".format(access_token["access_token"])
            },
        )

        json = response.json

        assert response.status_code == 200
        assert json["id"] == study.id
        assert json["name"] == study.name
        assert json["description"] == study.description
        assert json["image"] == study.image
        assert json["card_set_x"] == create_card_set_json(study.card_set_x)
        assert json["card_set_y"] == create_card_set_json(study.card_set_y)
        assert json["data_value_labels"] == [
            create_data_value_label_json(data_value_label)
            for data_value_label in study.data_value_labels
        ]
        assert json["number_of_columns"] == study.number_of_columns
        assert json["number_of_rows"] == study.number_of_rows
        assert json["user_group_id"] == study.user_group.id
示例#10
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
示例#11
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
示例#12
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
示例#13
0
def test_check_edit(client, init_database):
    """
    GIVEN a Flask application, admin
    WHEN all routes with @check_edit decorator are requested (GET)
    THEN check response location
    """

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

        study = create_study(client, creator=admin, start_date=date.today())

        response = client.get(
            url_for("admin.user_group", id=study.user_group.id),
            follow_redirects=False,
        )
        assert urlparse(response.location).path == url_for("admin.index")
        response = client.get(
            url_for("admin.card_set", id=study.card_set_x.id),
            follow_redirects=False,
        )
        assert urlparse(response.location).path == url_for("admin.index")
        response = client.get(url_for("admin.study", id=study.id),
                              follow_redirects=False)
        assert urlparse(response.location).path == url_for("admin.index")
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
示例#15
0
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
示例#16
0
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_create_invalid_card_set(client, init_database):
    """
    GIVEN a Flask application, logged in admin
    WHEN '/admin/create_study/1' is requested (GET)
    WHEN '/admin/card_set/1' is posted with INVALID data (POST)
    THEN check response is valid and card set is not created with the data.
    """
    with client.application.test_request_context():

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

        card_data = dict(card_name="Card 1")
        card_set_data = dict(card_set_name="Card Set 1",
                             cards=card_data,
                             measure="Sensitivity")

        response = client.get(url_for("admin.new_card_set"))

        assert urlparse(response.location).path == url_for("admin.card_set",
                                                           id=1)

        response = client.post(url_for("admin.card_set", id=1),
                               data=card_set_data)

        assert response.status_code == 200
        assert b"[This field is required.]" in response.data

        response = client.get(url_for("admin.index"))

        assert b"Card Set 1" not in response.data
示例#18
0
def test_create_invalid_user_group(client, init_database):
    """
    GIVEN a Flask application, logged in admin
    WHEN '/admin/new_user_group' is requested (GET)
    WHEN '/admin/study/1' is posted with invalid data (POST).
    THEN check response is valid and the user group has not been created.
    """
    user_group_form = UserGroupForm()

    user_group_form.name.data = "User Group 1"
    user_group_form.users.pop_entry()
    user_group_form.users.append_entry("test.com")
    user_group_form.submit.data = True
    with client.application.test_request_context():

        admin = create_admin(client, username="******", password="******")
        login(client, username="******", password="******")
        client.get(url_for("admin.new_user_group"))
        response = client.post(
            url_for("admin.user_group", id=1),
            data=user_group_form.data,
            follow_redirects=True,
        )

        assert response.status_code == 200
        assert b"[email]" in response.data
def test_edit_current_card_set(client, init_database):
    """
    GIVEN a Flask application, logged in admin, and card set which is added to a current study
    WHEN '/admin/card_set/1' is requested (GET)
    THEN check response is valid and card set cannot be edited.
    """
    with client.application.test_request_context():

        admin = create_admin(client, username="******", password="******")
        login(client, username="******", password="******")
        card_set = create_card_set(client, creator=admin)
        study = create_study(client, creator=admin, card_set_y=card_set)

        response = client.get(url_for("admin.card_set", id=card_set.id),
                              follow_redirects=True)

        assert response.status_code == 200
        assert (
            b"You cannot edit this Card Set as there are Studies associated with it which are currently in progress."
            in response.data)

        card_set_db = CardSet.query.first()

        assert card_set_db.name == card_set.name
        assert card_set_db.measure == card_set.measure
        assert card_set_db.cards == card_set.cards
        assert card_set_db.creator == admin

        client.get(url_for("auth.logout"))
示例#20
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
示例#21
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"]
示例#22
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
示例#23
0
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)
示例#24
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"))
示例#25
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"))
示例#26
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
        )
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")
示例#28
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
示例#29
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
def test_edit_future_card_set(client, init_database):
    """
    GIVEN a Flask application, logged in admin and card set which is added to a future study
    WHEN '/admin/card_set/1' is requested (GET)
    THEN check response is valid and card set can be edited.
    """
    with client.application.test_request_context():

        admin = create_admin(client, username="******", password="******")
        login(client, username="******", password="******")
        card_set = create_card_set(client, creator=admin)
        study = create_study(
            client,
            creator=admin,
            card_set_x=card_set,
            start_date=date.today() + timedelta(days=3),
        )

        response = client.get(
            url_for("admin.card_set", id=study.card_set_x.id),
            follow_redirects=True,
        )

        assert response.status_code == 200
        for card in card_set.cards:
            assert bytes(card.name, "utf-8") in response.data
            assert bytes(card.description, "utf-8") in response.data

        form_card = CardForm(card_name="Updated Card",
                             image=None,
                             desc="Updated Description")
        form_card_set = CardSetForm()
        form_card_set.card_set_name.data = "Updated Card Set"
        form_card_set.cards.pop_entry()
        form_card_set.cards.append_entry({
            "card_name": "Updated Card",
            "image": None,
            "desc": "Updated Description",
        })
        # form_card_set.cards.append_entry(form_card)
        form_card_set.measure.data = "Updated Measure"

        response = client.post(
            url_for("admin.card_set", id=study.card_set_x.id),
            data=form_card_set.data,
            follow_redirects=True,
        )

        assert study.card_set_x.name == "Updated Card Set"
        assert study.card_set_x.measure == "Updated Measure"
        assert study.card_set_x.cards[0].name == "Updated Card"
        assert study.card_set_x.cards[0].image == "updatedimage.jpg"
        assert study.card_set_x.cards[0].description == "Updated Description"
        assert len(study.heat_maps) == (len(study.card_set_x.cards) *
                                        len(study.card_set_y.cards) *
                                        len(study.data_value_labels) * 2)