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__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)
def test__get__requires_login(client, faker):
    user = login(client, faker)
    dr = do_create_request(
        client,
        faker,
        user,
        headers=faker.column_headers(10),
    )
    logout(client)

    assert__requires_login(client, _url(id=dr.id, external=False))
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)
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_delete_get(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])
    do_submit(client, dr.id)

    response = client.get(
        url_for('ui.demographics_delete', id=dr.id, _external=True))
    assert response.status_code == 200

    # Do not allows others to delete
    logout(client)

    user = login(client, faker)
    response = client.get(
        url_for('ui.demographics_delete', id=dr.id, _external=True))
    assert response.status_code == 403

    _remove_files(dr)
def test__ui_demographics_result_created__download(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])

    response = do_submit(client, dr.id)

    dr.result_created_datetime = datetime.utcnow()

    db.session.add(dr)
    db.session.commit()

    contents = faker.text()

    with open(dr.result_filepath, "w") as f:
        f.write(contents)
        
    _assert_uploaded_file_on_index(client, dr.filename, dr.id, RESULT_CREATED)

    response = client.get(url_for('ui.demographics_download_result', id=dr.id, _external=True))
    assert response.status_code == 200
    assert response.get_data().decode("utf8") == contents

    dr = DemographicsRequest.query.get(dr.id)
    assert dr.result_downloaded_datetime is not None
    assert dr.result_downloaded == True

    # Do not allows others to download
    logout(client)

    user = login(client, faker)
    response = client.get(url_for('ui.demographics_download_result', id=dr.id, _external=True))
    assert response.status_code == 403

    _remove_files(dr)