Exemple #1
0
def test__admin__is_admin__is_accessible(client, faker):
    user = faker.get_test_user(is_admin=True)
    login(client, faker, user)

    out = AdminCustomView(User, db.session)

    assert out.is_accessible()
def test__ui_demographics_define_columns_post__not_owner(client, faker):
    user = login(client, faker)
    headers = faker.column_headers(10)

    dr = do_create_request(client, faker, user, headers=headers)

    logout(client)

    user2 = login(client, faker)

    response = client.post(
        url_for('ui.demographics_define_columns', id=dr.id, _external=True),
        data={
            'uhl_system_number_column_id': 0,
            'nhs_number_column_id': 0,
            'family_name_column_id': 0,
            'given_name_column_id': 0,
            'gender_column_id': 0,
            'dob_column_id': 0,
            'postcode_column_id': 0,
        },
    )

    assert response.status_code == 403

    _remove_files(dr)
Exemple #3
0
def test__get_value_from_all_arguments__get(client, faker):
    login(client, faker)

    field_name = 'hello'
    value = 'jim'

    resp = client.get(
        url_for('json_requests', field_name=field_name, hello=value))

    assert resp.get_json()['result'] == value
Exemple #4
0
def test__get_value_from_all_arguments__post_json(client, faker):
    login(client, faker)

    field_name = 'bread'
    value = 'cheese'

    resp = client.post_json(url_for('json_requests', field_name=field_name),
                            data={field_name: value})

    assert resp.get_json()['result'] == value
def test__ui_demographics_submit_post__not_owner(client, faker):
    user = login(client, faker)

    dr = do_create_request(client, faker, user)
    do_define_columns_post(client, dr.id, dr.columns[0], dr.columns[1],
                           dr.columns[2], dr.columns[3], dr.columns[4],
                           dr.columns[5], dr.columns[6])

    logout(client)

    user2 = login(client, faker)
    response = do_submit(client, dr.id)
    assert response.status_code == 403

    _remove_files(dr)
def test__ui_demographics_define_columns_get__not_owner(client, faker):
    user = login(client, faker)
    headers = faker.column_headers(10)

    dr = do_create_request(client, faker, user, headers=headers)

    logout(client)

    user2 = login(client, faker)
    response = client.get(
        url_for('ui.demographics_define_columns', id=dr.id, _external=True))

    assert response.status_code == 403

    _remove_files(dr)
Exemple #7
0
def test__boilerplate__pages(client, faker, item_count):
    user = login(client, faker)

    the_fields = [faker.get_test_field() for _ in range(item_count)]

    assert__page_navigation(client, url_for('pages_of_fields',
                                            _external=False), item_count)
Exemple #8
0
def test__extract_data__normal(client, faker, extension, row_count, mock_schedule_lookup_tasks, mock_log_exception):
    u = login(client, faker)
    dth = DemographicsTestHelper(faker=faker, user=u, row_count=row_count, extension=extension)
    dr = dth.get_demographics_request__data_extraction()

    extract_data(dr.id)

    actual = DemographicsRequest.query.get(dr.id)

    mock_schedule_lookup_tasks.assert_called_once_with(dr.id)
    mock_log_exception.assert_not_called()

    assert len(actual.data) == row_count
    assert actual.data_extracted == True
    assert actual.data_extracted_datetime is not None

    for e, a in zip(dth._person_details, actual.data):
        assert e['uhl_system_number'] == a.uhl_system_number
        assert e['nhs_number'] == a.nhs_number
        assert e['family_name'] == a.family_name
        assert e['given_name'] == a.given_name
        assert e['gender'] == a.gender
        assert parse_date(e['date_of_birth']) == parse_date(a.dob)
        assert e['postcode'] == a.postcode
    
    _remove_files(dr)
Exemple #9
0
def test__extract_pre_pmi_details__next_processed(client, faker,
                                                  mock_pmi_details,
                                                  mock_schedule_lookup_tasks):
    u = login(client, faker)
    dth = DemographicsTestHelper(faker=faker, user=u, row_count=2)
    dr = dth.get_demographics_request__pre_pmi_lookup()

    dr.data[0].pmi_pre_processed_datetime = datetime.datetime.utcnow()
    db.session.add(dr.data[0])
    db.session.commit()

    expected = PmiData(**faker.pmi_details(1))
    mock_pmi_details.return_value = expected

    extract_pre_pmi_details(dr.id)

    actual = DemographicsRequest.query.get(dr.id)

    assert actual.pmi_data_pre_completed_datetime is None
    assert sum(1 for d in actual.data
               if d.pmi_pre_processed_datetime is not None) == 2
    assert expected == actual.data[1].pmi_data
    assert len(actual.data[0].messages) == 0
    mock_schedule_lookup_tasks.assert_called_once_with(dr.id)

    _remove_files(dr)
Exemple #10
0
def test__extract_post_pmi_details__pmi_exception(client, faker,
                                                  mock_pmi_details,
                                                  mock_schedule_lookup_tasks):
    ERROR_MESSAGE = 'An Exception'

    u = login(client, faker)
    dth = DemographicsTestHelper(faker=faker,
                                 user=u,
                                 row_count=1,
                                 find_pre_pmi_details=False)
    dr = dth.get_demographics_request__post_pmi_lookup()

    mock_pmi_details.side_effect = PmiException(ERROR_MESSAGE)

    extract_post_pmi_details(dr.id)

    actual = DemographicsRequest.query.get(dr.id)

    assert actual.pmi_data_post_completed_datetime is None
    assert sum(1 for d in actual.data
               if d.pmi_post_processed_datetime is not None) == 1
    assert actual.data[0].pmi_data is None
    assert len(actual.data[0].messages) == 1

    m = actual.data[0].messages[0]
    assert m.type == 'error'
    assert m.source == 'pmi_details'
    assert m.scope == 'pmi_details'
    assert m.message == ERROR_MESSAGE
    mock_schedule_lookup_tasks.assert_called_once_with(dr.id)

    _remove_files(dr)
Exemple #11
0
def test__extract_post_pmi_details__invalid_uhl_system_number(
        client, faker, mock_pmi_details, mock_schedule_lookup_tasks):
    u = login(client, faker)
    dth = DemographicsTestHelper(faker=faker,
                                 user=u,
                                 row_count=1,
                                 find_pre_pmi_details=False)
    dr = dth.get_demographics_request__post_pmi_lookup()

    dr.data[0].uhl_system_number = faker.invalid_uhl_system_number()
    db.session.add(dr.data[0])
    db.session.commit()

    expected = PmiData(**faker.pmi_details(1))
    mock_pmi_details.return_value = expected

    extract_post_pmi_details(dr.id)

    actual = DemographicsRequest.query.get(dr.id)

    assert actual.pmi_data_post_completed_datetime is None
    assert sum(1 for d in actual.data
               if d.pmi_post_processed_datetime is not None) == 1
    assert expected == actual.data[0].pmi_data
    assert len(actual.data[0].messages) == 1

    m = actual.data[0].messages[0]
    assert m.type == 'warning'
    assert m.source == 'pmi_details'
    assert m.scope == 'uhl_system_number'
    assert m.message == f'Invalid format {faker.invalid_uhl_system_number()}'
    mock_schedule_lookup_tasks.assert_called_once_with(dr.id)

    _remove_files(dr)
def test__process_demographics_request_data__data_has_error(client, faker, mock_schedule_lookup_tasks, mock_spine_lookup, mock_log_exception):
    u = login(client, faker)
    dth = DemographicsTestHelper(faker=faker, user=u, row_count=1)
    dr = dth.get_demographics_request__spine_lookup()

    db.session.add(DemographicsRequestDataMessage(
        demographics_request_data=dr.data[0],
        type='error',
        source='TEST',
        scope='DATA',
        message='A message',
    ))
    db.session.commit()

    process_demographics_request_data(dr.id)

    mock_spine_lookup.assert_called_once_with(dr.data[0])
    mock_schedule_lookup_tasks.assert_called_once_with(dr.id)
    mock_log_exception.assert_not_called()

    actual = DemographicsRequest.query.get(dr.id)

    assert actual.lookup_completed_datetime is None
    assert actual.data[0].processed_datetime is not None

    _remove_files(dr)
Exemple #13
0
def test__spine_lookup__spine_exception(client, faker,
                                        mock_get_spine_parameters,
                                        mock_get_demographics_from_nhs_number,
                                        mock_get_demographics_from_search,
                                        exception_class, message_type):
    u = login(client, faker)
    dth = DemographicsTestHelper(faker=faker, user=u)
    dr = dth.get_demographics_request__spine_lookup()
    drd = dr.data[0]

    params = SpineParameters()
    params.nhs_number = drd.nhs_number
    params.dob = drd.dob

    mock_get_spine_parameters.return_value = params
    mock_get_demographics_from_nhs_number.side_effect = exception_class

    spine_lookup(drd)

    assert drd.response is None

    assert len(drd.messages) == 1

    assert drd.messages[0].type == message_type
    assert drd.messages[0].source == 'spine'
    assert drd.messages[0].scope == 'request'

    _remove_files(dr)
Exemple #14
0
def test__spine_lookup__no_parameters(client, faker, mock_get_spine_parameters,
                                      mock_get_demographics_from_nhs_number,
                                      mock_get_demographics_from_search):
    u = login(client, faker)
    dth = DemographicsTestHelper(faker=faker, user=u)
    dr = dth.get_demographics_request__spine_lookup()
    drd = dr.data[0]

    params = SpineParameters()

    mock_get_spine_parameters.return_value = params
    mock_get_demographics_from_search.return_value = spine_response_full

    spine_lookup(drd)

    mock_get_demographics_from_search.assert_not_called()
    mock_get_demographics_from_nhs_number.assert_not_called()

    assert drd.response is None

    assert len(drd.messages) == 1

    assert drd.messages[0].type == 'error'
    assert drd.messages[0].source == 'validation'
    assert drd.messages[0].scope == 'request'
    assert drd.messages[
        0].message == 'Not enough values to perform Spine lookup'

    _remove_files(dr)
Exemple #15
0
def test__spine_lookup__search_no_gender(client, faker,
                                         mock_get_spine_parameters,
                                         mock_get_demographics_from_nhs_number,
                                         mock_get_demographics_from_search):
    u = login(client, faker)
    dth = DemographicsTestHelper(faker=faker, user=u)
    dr = dth.get_demographics_request__spine_lookup()
    drd = dr.data[0]

    params = SpineParameters()
    params.nhs_number = ''
    params.dob = drd.dob
    params.family_name = drd.family_name
    params.given_name = drd.given_name
    params.gender = None
    params.postcode = drd.postcode

    mock_get_spine_parameters.return_value = params
    mock_get_demographics_from_search.return_value = spine_response_full

    spine_lookup(drd)

    mock_get_demographics_from_search.assert_not_called()
    mock_get_demographics_from_nhs_number.assert_not_called()

    _remove_files(dr)
def test__label_print__requires_id_entry(client, faker):
    user = login(client, faker)
    faker.add_all_studies(user)

    pack = LabelPack.query.filter_by(type='GoDcmPack').one()

    resp = client.get(
        _url(
            referrer='study',
            study_id=pack.study_id,
            pack_name=pack.type,
            count=1,
        ))

    assert__redirect(
        resp,
        'ui.label_print_definition',
        referrer='study',
        study_id=pack.study_id,
        pack_name=pack.type,
        count=1,
    )

    assert PseudoRandomId.query.join(PseudoRandomIdProvider).filter_by(
        prefix=GO_DCM_ID_TYPE_PARTICIPANT).count() == 0
Exemple #17
0
def test__extract_data__missing_columns(client, faker, column_headings, extension, mock_schedule_lookup_tasks, mock_log_exception):
    u = login(client, faker)
    dth = DemographicsTestHelper(faker=faker, user=u, extension=extension, column_headings=column_headings)
    dr = dth.get_demographics_request__data_extraction()

    extract_data(dr.id)

    actual = DemographicsRequest.query.get(dr.id)

    mock_schedule_lookup_tasks.assert_called_once_with(dr.id)
    mock_log_exception.assert_not_called()

    assert len(actual.data) == 1
    assert actual.data_extracted == True
    assert actual.data_extracted_datetime is not None

    for e, a in zip(dth.get_input_details(), actual.data):
        assert e.get('uhl_system_number', '') == a.uhl_system_number
        assert e.get('nhs_number', '') == a.nhs_number
        assert e.get('family_name', '') == a.family_name
        assert e.get('given_name', '') == a.given_name
        assert e.get('gender', '') == a.gender
        assert parse_date(e.get('date_of_birth', '')) == parse_date(a.dob)
        assert e.get('postcode', '') == a.postcode

    _remove_files(dr)
def test__study_details_uploads_and_complete(client, faker, outstanding,
                                             completed, deleted):
    user = login(client, faker)
    user2 = faker.get_test_user()
    study = faker.get_test_study(owner=user)

    # Cycle is used to alternately allocate
    # the uploads to a different user
    # thus we can test that we can see
    # uploads by users other than ourselves
    users = cycle([user, user2])

    uploads = []

    for _ in range(outstanding):
        u = faker.get_test_upload(study=study, uploader=next(users))
        uploads.append(u)

    for _ in range(completed):
        u = faker.get_test_upload(study=study,
                                  completed=True,
                                  uploader=next(users))
        uploads.append(u)

    for _ in range(deleted):
        u = faker.get_test_upload(study=study,
                                  deleted=True,
                                  uploader=next(users))

    resp = _get(client, _url(study_id=study.id, showCompleted='y'), user,
                study)
    _assert_response(study, uploads, resp)
Exemple #19
0
def test__boilerplate__search(client, faker):
    user = login(client, faker)
    resp = get_and_assert_standards(client,
                                    url_for('search'),
                                    user,
                                    has_form=True)
    assert__search_html(resp.soup, clear_url=url_for('search'))
def test__get_spine_parameters__no_pmi__valid_except_names(
        client, faker, mock_convert_name):
    u = login(client, faker)
    dth = DemographicsTestHelper(faker=faker,
                                 user=u,
                                 find_pre_pmi_details=False)
    dr = dth.get_demographics_request__spine_lookup()
    drd = dr.data[0]

    error_message = 'ABCDEFG'

    mock_convert_name.return_value = (error_message, '')

    actual = get_spine_parameters(drd)

    mock_convert_name.call_count = 2

    assert parse_date(actual.dob) == parse_date(drd.dob)
    assert actual.family_name == None
    assert actual.gender == convert_gender(drd.gender)[1]
    assert actual.given_name == None
    assert actual.nhs_number == drd.nhs_number
    assert actual.postcode == drd.postcode

    assert len(actual.warnings) == 2
    assert actual.warnings[0].scope == 'family_name'
    assert actual.warnings[0].message == error_message
    assert actual.warnings[1].scope == 'given_name'
    assert actual.warnings[1].message == error_message

    _remove_files(dr)
def test__get__requires_login(client, faker):
    user = login(client, faker)

    dr = do_create_request(client, faker, user)
    logout(client)

    assert__requires_login(client, _url(id=dr.id, external=False))
def test__get_spine_parameters__with_pmi__valid_except_pmi_postcode(
        client, faker, mock_convert_postcode):
    u = login(client, faker)
    dth = DemographicsTestHelper(faker=faker, user=u)
    dr = dth.get_demographics_request__spine_lookup()
    drd = dr.data[0]

    error_message = 'ABCDEFG'

    mock_convert_postcode.side_effect = [
        (None, drd.postcode),
        (error_message, ''),
    ]

    actual = get_spine_parameters(drd)

    mock_convert_postcode.call_count == 2

    assert parse_date(actual.dob) == parse_date(drd.dob)
    assert actual.family_name == drd.family_name
    assert actual.gender == convert_gender(drd.gender)[1]
    assert actual.given_name == drd.given_name
    assert actual.nhs_number == drd.nhs_number
    assert actual.postcode == drd.postcode

    assert len(actual.warnings) == 1
    assert actual.warnings[0].scope == 'pmi_postcode'
    assert actual.warnings[0].message == error_message

    _remove_files(dr)
Exemple #23
0
def test__schedule_lookup_tasks__got_post_pmi(
    client,
    faker,
    mock_process_demographics_request_data,
    mock_extract_data,
    mock_produce_demographics_result,
    mock_extract_pre_pmi_details,
    mock_extract_post_pmi_details,
    mock_log_exception,
):

    u = login(client, faker)
    dth = DemographicsTestHelper(faker=faker, user=u)
    dr = dth.get_demographics_request__create_results()

    do_lookup_tasks(dr.id)

    mock_extract_data.delay.assert_not_called()
    mock_extract_pre_pmi_details.delay.assert_not_called()
    mock_process_demographics_request_data.delay.assert_not_called()
    mock_extract_post_pmi_details.delay.assert_not_called()
    mock_produce_demographics_result.delay.assert_called_once_with(dr.id)
    mock_log_exception.assert_not_called()

    _remove_files(dr)
Exemple #24
0
def test__schedule_lookup_tasks__deleted(
    client,
    faker,
    mock_process_demographics_request_data,
    mock_extract_data,
    mock_produce_demographics_result,
    mock_extract_pre_pmi_details,
    mock_extract_post_pmi_details,
    mock_log_exception,
):

    u = login(client, faker)
    dth = DemographicsTestHelper(faker=faker, user=u)
    dr = dth.get_demographics_request__uploaded()
    dr.deleted_datetime = datetime.utcnow()
    db.session.add(dr)
    db.session.commit()

    do_lookup_tasks(dr.id)

    mock_extract_data.delay.assert_not_called()
    mock_extract_pre_pmi_details.delay.assert_not_called()
    mock_process_demographics_request_data.delay.assert_not_called()
    mock_extract_post_pmi_details.delay.assert_not_called()
    mock_produce_demographics_result.delay.assert_not_called()
    mock_log_exception.assert_not_called()

    _remove_files(dr)
def test__ui_demographics_define_columns_post_404(client, faker):
    user = login(client, faker)

    response = do_define_columns_post(client, 999, None, None, None, None,
                                      None, None, None)

    assert response.status_code == 404
def test__ui_demographics_define_columns_get_404(client, faker):
    user = login(client, faker)

    response = client.get(
        url_for('ui.demographics_define_columns', id=999, _external=True))

    assert response.status_code == 404
def test__ui_demographics_define_columns_update(client, faker):
    user = login(client, faker)

    dr = do_create_request(client, faker, user)
    response = do_define_columns_post(client, dr.id, dr.columns[0],
                                      dr.columns[1], dr.columns[2],
                                      dr.columns[3], dr.columns[4],
                                      dr.columns[5], dr.columns[6])

    assert response.status_code == 302
    assert response.location == url_for('ui.demographics_submit',
                                        id=dr.id,
                                        _external=True)

    response = client.get(
        url_for('ui.demographics_define_columns', id=dr.id, _external=True))

    for i, name in enumerate([
            'uhl_system_number_column_id', 'nhs_number_column_id',
            'family_name_column_id', 'given_name_column_id',
            'gender_column_id', 'dob_column_id', 'postcode_column_id'
    ]):
        select = response.soup.find('select', {'name': name})
        assert select is not None
        option = select.find('option', {"value": dr.columns[i].id})
        assert option is not None
        assert 'selected' in option.attrs

    _assert_uploaded_file_on_index(client, dr.filename, dr.id,
                                   AWAITING_SUBMISSION)

    _remove_files(dr)
Exemple #28
0
def test__spine_lookup__nhs_number(client, faker, mock_get_spine_parameters,
                                   mock_get_demographics_from_nhs_number,
                                   mock_get_demographics_from_search,
                                   warning_count):
    u = login(client, faker)
    dth = DemographicsTestHelper(faker=faker, user=u)
    dr = dth.get_demographics_request__spine_lookup()
    drd = dr.data[0]

    params = SpineParameters()
    params.nhs_number = drd.nhs_number
    params.dob = drd.dob
    params.family_name = drd.family_name
    params.given_name = drd.given_name
    params.gender = drd.gender
    params.postcode = drd.postcode

    for i in range(warning_count):
        params.add_warning(
            scope=f'scope {i}',
            message=f'message {i}',
        )

    mock_get_spine_parameters.return_value = params
    mock_get_demographics_from_nhs_number.return_value = spine_response_full

    spine_lookup(drd)

    mock_get_demographics_from_nhs_number.assert_called_once_with(
        nhs_number=params.nhs_number,
        dob=params.dob,
    )
    mock_get_demographics_from_search.assert_not_called()

    assert drd.response is not None
    assert drd.response.nhs_number == spine_response_full.nhs_number
    assert drd.response.title == spine_response_full.title
    assert drd.response.forename == spine_response_full.forename
    assert drd.response.middlenames == spine_response_full.middlenames
    assert drd.response.lastname == spine_response_full.lastname
    assert drd.response.sex == spine_response_full.sex
    assert drd.response.postcode == spine_response_full.postcode
    assert drd.response.address == spine_response_full.address
    assert parse_date(drd.response.date_of_birth) == parse_date(
        spine_response_full.date_of_birth)
    assert parse_date(drd.response.date_of_death) == parse_date(
        spine_response_full.date_of_death)
    assert drd.response.is_deceased == spine_response_full.is_deceased
    assert drd.response.current_gp_practice_code == spine_response_full.current_gp_practice_code

    assert len(drd.messages) == warning_count

    for i, w in enumerate(drd.messages):
        assert w.type == 'warning'
        assert w.source == 'validation'
        assert w.scope == f'scope {i}'
        assert w.message == f'message {i}'

    _remove_files(dr)
def test__study_details_uploads__with_files(client, faker):
    user = login(client, faker)
    study = faker.get_test_study(owner=user)

    uf = faker.get_test_upload_file(study=study)

    resp = _get(client, _url(study_id=study.id), user, study)
    _assert_response(study, [uf.upload], resp)
Exemple #30
0
def _test_blinding(client, faker, study, expected_blinding_types):
    u = login(client, faker)

    ids = study.get_blind_ids('hello', u)

    db.session.commit()

    _assert_ids_created_correctly('hello', ids, expected_blinding_types)