Exemple #1
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)
Exemple #2
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"))
def test_location_api(search_location, client):
    login(client)
    resp = client.get(f"/admin/api/location?q={QUERY_TERM}")
    assert resp.status_code == 200
    # alter restaurants fixture to make datetime like jsonify output
    assert resp.json == _jsonify(QUERY_RESULT)
    search_location.assert_called_once_with(QUERY_TERM)
Exemple #4
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"))
Exemple #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"))
Exemple #6
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_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
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"))
Exemple #9
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
Exemple #10
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
Exemple #12
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
Exemple #13
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 == []
Exemple #14
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
Exemple #15
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
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")
Exemple #17
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)
Exemple #19
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")
Exemple #21
0
def test_edit_future_user_group(client, init_database):
    """
    GIVEN a Flask application, logged in admin, and a user group added to a future study
    WHEN '/admin/study/1' is requested (GET)
    THEN check response is valid and the user group can be edited.
    """
    with client.application.test_request_context():

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

        user_group = create_user_group(client, creator=admin)
        study = create_study(
            client,
            creator=admin,
            user_group=user_group,
            start_date=date.today() + timedelta(days=1),
        )

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

        assert response.status_code == 200
        for user in study.user_group.users:
            assert bytes(user.email, "utf-8") in response.data

        form = UserGroupForm()
        assert bytes(form.name.label.text, "utf-8") in response.data
        assert bytes(form.users.label.text, "utf-8") in response.data

        user_group_form = UserGroupForm()

        user_group_form.name.data = "Edited User Group 1"
        user_group_form.users.pop_entry()
        user_group_form.users.append_entry("*****@*****.**")
        user_group_form.submit.data = True

        response = client.post(
            url_for("admin.user_group", id=1),
            data=user_group_form.data,
            follow_redirects=True,
        )

        assert response.status_code == 200

        user_group = UserGroup.query.filter_by(id=1).first()

        assert user_group.name == "Edited User Group 1"
        assert user_group.users[0].email == "*****@*****.**"
Exemple #22
0
def test_create_update_delete_taxonomy_term(permission_client, test_users,
                                            taxonomy):
    client = permission_client
    content = json_.dumps({"title": "Test term"}, ensure_ascii=False)

    # create without login
    term = client.put('/2.0/taxonomies/test_taxonomy/term',
                      data=content,
                      content_type='application/json')
    assert term.status_code == 403

    # login
    login(client, test_users[0])

    # create
    term = client.put('/2.0/taxonomies/test_taxonomy/term',
                      data=content,
                      content_type='application/json')

    assert term.status_code == 201

    res_term = client.get('/2.0/taxonomies/test_taxonomy/term',
                          content_type='application/json')

    assert res_term.json["title"] == "Test term"

    # update
    content = json_.dumps({"title": "Test term updated"}, ensure_ascii=False)
    term = client.put('/2.0/taxonomies/test_taxonomy/term',
                      data=content,
                      content_type='application/json')

    assert term.status_code == 200

    res_term = client.get('/2.0/taxonomies/test_taxonomy/term',
                          content_type='application/json')

    assert res_term.json["title"] == "Test term updated"

    # delete
    term = client.delete('/2.0/taxonomies/test_taxonomy/term',
                         content_type='application/json')

    assert term.status_code == 200

    res_term = client.get('/2.0/taxonomies/test_taxonomy/term',
                          content_type='application/json')

    assert res_term.status_code == 410
Exemple #23
0
def test_create_update_delete_taxonomy(permission_client, test_users):
    client = permission_client
    content = json_.dumps({"title": "Test taxonomy"}, ensure_ascii=False)
    taxonomy = client.put('/2.0/taxonomies/test',
                          data=content,
                          content_type='application/json')
    assert taxonomy.status_code == 403

    # login
    login(client, test_users[0])

    # create
    taxonomy = client.put('/2.0/taxonomies/test',
                          data=content,
                          content_type='application/json')

    assert taxonomy.status_code == 201

    taxonomy_list = client.get('/2.0/taxonomies/',
                               content_type='application/json')

    assert taxonomy_list.json[0]["code"] == "test"
    assert taxonomy_list.json[0]["title"] == "Test taxonomy"

    # update
    content = json_.dumps({"title": "Test taxonomy updated"},
                          ensure_ascii=False)
    taxonomy = client.put('/2.0/taxonomies/test',
                          data=content,
                          content_type='application/json')

    assert taxonomy.status_code == 200

    taxonomy_list = client.get('/2.0/taxonomies/',
                               content_type='application/json')

    assert taxonomy_list.json[0]["code"] == "test"
    assert taxonomy_list.json[0]["title"] == "Test taxonomy updated"

    # delete
    taxonomy = client.delete('/2.0/taxonomies/test',
                             content_type='application/json')

    assert taxonomy.status_code == 204

    taxonomy_list = client.get('/2.0/taxonomies/',
                               content_type='application/json')

    assert taxonomy_list.json == []
def test_get_general_no_study(client, init_database):
    """
    GIVEN a Flask application, admin
    WHEN '/responses/1' is requested (GET)
    THEN check response content, status code
    """
    with client.application.test_request_context():
        admin = create_admin(client)

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

        response = client.get(url_for("responses.general", id=1),
                              follow_redirects=True)

        assert response.status_code == 404
Exemple #25
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"))
Exemple #26
0
def test_advance_age(client, rebuild_after):
    rv_login = login(client, '*****@*****.**', 'brunner')
    assert "Undying Kingdoms" in rv_login.data.decode()
    assert rv_login.status_code == 200

    rv_token = client.get('/game_clock/token')
    data_token = json.loads(rv_token.data.decode())
    assert data_token['status'] == 'success'
    assert data_token['auth_token']
    assert rv_token.status_code == 200

    # I might need to logout here just to be safe?

    world = World.query.first()
    age, day = world.age, world.day
    rv = client.get(
        '/game_clock/advance_age',
        headers=dict(
            Authorization='Bearer ' + data_token['auth_token']
        )
    )

    data = json.loads(rv.data.decode())
    assert data['status'] == 'success'
    assert data['message']
    world2 = World.query.first()
    assert County.query.all() == []
    assert world2.age == age + 1
    assert world2.day <= 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")
def test_delete_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/delete_card_set/1' is requested (GET)
    THEN check response is valid and card set is deleted.
    """
    with client.application.test_request_context():

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

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

        assert response.status_code == 200
        assert (
            b"You cannot delete this Card Set as it is currently associated with &#39;Test Study&#39; Studies, remove these associations before deleting this Card Set."
            in response.data)

        card_set_db = CardSet.query.filter_by(id=1).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

        study.card_set_x = None
        db.session.commit()

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

        card_set_db = CardSet.query.filter_by(id=1).first()

        assert response.status_code == 200
        assert card_set_db is None
        assert b"Card Set Test Card Set succesfully deleted" in response.data
Exemple #29
0
def test_wrong_old_password(client):
    access_token = login(client, TEST_USERNAME, NEW_PASSWORD)
    data = {'old_password': '******', 'new_password': NEW_PASSWORD}
    data, status_code = change_password(client, access_token, data)
    assert status_code == 401
    assert data.get('message') == 'Wrong password'
    user = UserModel.query.filter_by(username=TEST_USERNAME).first()
    assert verify_password(NEW_PASSWORD, user.hashed_password,
                           user.salt) is True
def test_get_token(client):
    rv_login = login(client, '*****@*****.**', 'brunner')
    assert "Undying Kingdoms" in rv_login.data.decode()
    assert rv_login.status_code == 200

    rv = client.get('/game_clock/token')
    data = json.loads(rv.data.decode())
    assert data['status'] == 'success'
    assert data['message']