Skip to content

Commit

Permalink
run test in ci
Browse files Browse the repository at this point in the history
  • Loading branch information
jdkent committed Sep 8, 2023
1 parent 4826e79 commit 9c5707c
Show file tree
Hide file tree
Showing 17 changed files with 117 additions and 47 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ jobs:
-e "NEUROVAULT_ACCESS_TOKEN=${NEUROVAULT_ACCESS_TOKEN}" \
--rm -w /compose \
compose \
bash -c "python -m pytest neurosynth_compose/tests && python -m pytest --celery neurosynth_compose/tests/api/celery_tests"
bash -c "python -m pytest neurosynth_compose/tests && python -m pytest --celery neurosynth_compose/tests/api/celery_tests && python -m pytest --auth neurosynth_compose/tests/api/test_auth.py"
style_check:
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion compose/neurosynth_compose/openapi
27 changes: 24 additions & 3 deletions compose/neurosynth_compose/resources/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,26 @@
from .singular import singularize


def create_user():
from auth0.v3.authentication.users import Users
auth = request.headers.get("Authorization", None)
if auth is None:
return None
token = auth.split()[1]
profile_info = Users(
current_app.config["AUTH0_BASE_URL"].removeprefix("https://")
).userinfo(access_token=token)

# user signed up with auth0, but has not made any queries yet...
# should have endpoint to "create user" after sign on with auth0
current_user = User(
external_id=connexion.context["user"],
name=profile_info.get("name", "Unknown")
)

return current_user


def get_current_user():
user = connexion.context.get("user")
if user:
Expand Down Expand Up @@ -81,9 +101,10 @@ def update_or_create(cls, data, id=None, commit=True):
if not current_user:
# user signed up with auth0, but has not made any queries yet...
# should have endpoint to "create user" after sign on with auth0
current_user = User(external_id=connexion.context["user"])
db.session.add(current_user)
db.session.commit()
current_user = create_user()
if current_user:
db.session.add(current_user)
db.session.commit()

id = id or data.get("id", None) # want to handle case of {"id": "asdfasf"}

Expand Down
3 changes: 3 additions & 0 deletions compose/neurosynth_compose/schemas/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ class BaseSchema(Schema):
created_at = fields.DateTime()
updated_at = fields.DateTime(allow_none=True)
user_id = fields.String(data_key="user")
username = fields.String(
attribute="user.name", dump_only=True
)


class EstimatorSchema(Schema):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@


@celery_test
def test_file_upload_neurovault(app, db, mock_pynv):
def test_file_upload_neurovault(session, app, db, mock_pynv):
import os
from pathlib import Path
import shutil
Expand All @@ -35,7 +35,7 @@ def test_file_upload_neurovault(app, db, mock_pynv):

@celery_test
def test_create_or_update_neurostore_analysis(
auth_client, app, db, mock_pynv, meta_analysis_cached_result_files
session, auth_client, app, db, mock_pynv, meta_analysis_cached_result_files
):
cluster_tables = [
f for f in meta_analysis_cached_result_files["tables"] if "clust.tsv" in f.name
Expand Down Expand Up @@ -78,7 +78,7 @@ def test_create_or_update_neurostore_analysis(


@celery_test
def test_result_upload(auth_client, app, db, meta_analysis_cached_result_files):
def test_result_upload(session, auth_client, app, db, meta_analysis_cached_result_files):
data = {}
data["statistical_maps"] = [
(open(m, "rb"), m.name) for m in meta_analysis_cached_result_files["maps"]
Expand Down
2 changes: 1 addition & 1 deletion compose/neurosynth_compose/tests/api/test_annotation.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
def test_get_annotations(auth_client, user_data):
def test_get_annotations(session, auth_client, user_data):
get_all = auth_client.get("/api/annotations")
assert get_all.status_code == 200
id_ = get_all.json["results"][0]["id"]
Expand Down
8 changes: 5 additions & 3 deletions compose/neurosynth_compose/tests/api/test_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
("projects", Project, ProjectSchema),
],
)
def test_create(auth_client, user_data, endpoint, model, schema):
def test_create(session, auth_client, user_data, endpoint, model, schema):
user = User.query.filter_by(name="user1").first()
examples = model.query.filter_by(user=user).all()
for example in examples:
Expand All @@ -42,6 +42,8 @@ def test_create(auth_client, user_data, endpoint, model, schema):
del payload["neurostore_url"]
if "neurostore_study" in payload:
del payload["neurostore_study"]
if "username" in payload:
del payload["username"]

if isinstance(example, MetaAnalysis):
del payload["neurostore_analysis"]
Expand Down Expand Up @@ -75,7 +77,7 @@ def test_create(auth_client, user_data, endpoint, model, schema):
("projects", Project, ProjectSchema),
],
)
def test_read(auth_client, user_data, endpoint, model, schema):
def test_read(session, auth_client, user_data, endpoint, model, schema):
user = User.query.filter_by(name="user1").first()
if hasattr(model, "public"):
query = (model.user == user) | (model.public == True) # noqa E712
Expand Down Expand Up @@ -107,7 +109,7 @@ def test_read(auth_client, user_data, endpoint, model, schema):
("projects", Project, ProjectSchema, {"name": "my project"}),
],
)
def test_update(auth_client, db, session, user_data, endpoint, model, schema, update):
def test_update(session, auth_client, db, user_data, endpoint, model, schema, update):
user = User.query.filter_by(name="user1").first()
record = model.query.filter_by(user=user).first()

Expand Down
4 changes: 2 additions & 2 deletions compose/neurosynth_compose/tests/api/test_meta_analysis.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
def test_get_meta_analyses(app, auth_client, user_data):
def test_get_meta_analyses(session, app, auth_client, user_data):
get_all = auth_client.get("/api/meta-analyses")
assert get_all.status_code == 200

Expand All @@ -15,5 +15,5 @@ def test_get_meta_analyses(app, auth_client, user_data):
assert data[key] is None or isinstance(data[key], dict)


def test_ingest_neurostore(neurostore_data):
def test_ingest_neurostore(session, neurostore_data):
pass
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from neurosynth_compose.models import MetaAnalysis


def test_create_meta_analysis_result(app, auth_client, user_data):
def test_create_meta_analysis_result(session, app, auth_client, user_data):
meta_analysis = MetaAnalysis.query.first()
headers = {"Compose-Upload-Key": meta_analysis.run_key}
data = {
Expand All @@ -20,7 +20,7 @@ def test_create_meta_analysis_result(app, auth_client, user_data):
assert meta_resp.status_code == 200


def test_create_meta_analysis_result_no_snapshots(app, db, auth_client, user_data):
def test_create_meta_analysis_result_no_snapshots(session, app, db, auth_client, user_data):
meta_analysis = MetaAnalysis.query.first()
meta_analysis.studyset.snapshot = None
meta_analysis.annotation.snapshot = None
Expand Down
2 changes: 1 addition & 1 deletion compose/neurosynth_compose/tests/api/test_specification.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
def test_get_specification(app, auth_client, user_data):
def test_get_specification(session, app, auth_client, user_data):
get = auth_client.get("/api/specifications")
assert get.status_code == 200
5 changes: 3 additions & 2 deletions compose/neurosynth_compose/tests/api/test_studyset.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@
from ...schemas import StudysetSchema


def test_get_studysets(auth_client, user_data):
def test_get_studysets(session, auth_client, user_data):
get = auth_client.get("/api/studysets")
assert get.status_code == 200


def test_post_studyset_with_new_neurostore_id(auth_client, user_data):
def test_post_studyset_with_new_neurostore_id(session, auth_client, user_data):
user = User.query.filter_by(name="user1").first()
example = Studyset.query.filter_by(user=user).first()
schema = StudysetSchema()
payload = schema.dump(example)
payload.pop("url")
payload.pop("username")
# payload["neurostore_id"] = ""

resp = auth_client.post("/api/studysets", data=payload)
Expand Down
87 changes: 65 additions & 22 deletions compose/neurosynth_compose/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ def pytest_addoption(parser):
default=False,
help="Run celery tests",
)
parser.addoption(
"--auth",
action="store_true",
default=False,
help="Run authentication tests",
)


celery_test = pytest.mark.skipif(
Expand All @@ -52,21 +58,56 @@ def pytest_addoption(parser):
"not config.getoption('--schemathesis')",
reason="Only run when --schemathesis is given",
)

auth_test = pytest.mark.skipif(
"not config.getoption('--auth')",
reason="Only run when --auth is given",
)

"""
Test fixtures for bypassing authentication
"""


# https://github.com/pytest-dev/pytest/issues/363#issuecomment-406536200
@pytest.fixture(scope="session")
def monkeysession(request):
from _pytest.monkeypatch import MonkeyPatch
def real_app():
"""Session-wide test `Flask` application."""
from ..core import app as _app

mpatch = MonkeyPatch()
yield mpatch
mpatch.undo()
if "APP_SETTINGS" not in environ:
config = "neurostore.config.TestingConfig"
else:
config = environ["APP_SETTINGS"]
if not getattr(_app, "config", None):
_app = _app._app
_app.config.from_object(config)
# _app.config["SQLALCHEMY_ECHO"] = True

# Establish an application context before running the tests.
ctx = _app.app_context()
ctx.push()

yield _app

ctx.pop()


@pytest.fixture(scope="session")
def real_db(real_app):
"""Session-wide test database."""
_db.create_all()

yield _db

_db.session.remove()
sa.orm.close_all_sessions()
_db.drop_all()


# https://github.com/pytest-dev/pytest/issues/363#issuecomment-406536200
@pytest.fixture(scope="session", autouse=False)
def monkeysession(request):
with pytest.MonkeyPatch.context() as mp:
yield mp


def mock_decode_token(token):
Expand Down Expand Up @@ -201,7 +242,7 @@ def celery_app(app, db):
return make_celery(app)


@pytest.fixture(scope="function", autouse=True)
@pytest.fixture(scope="function", autouse=False)
def session(db):
"""Creates a new db session for a test.
Changes in session are rolled back"""
Expand Down Expand Up @@ -295,11 +336,11 @@ def mock_add_users(app, db, mock_auth):


@pytest.fixture(scope="function")
def add_users(app, db):
def add_users(real_app, real_db):
"""Adds a test user to db"""
from neurosynth_compose.resources.auth import decode_token

domain = app.config["AUTH0_BASE_URL"].split("://")[1]
domain = real_app.config["AUTH0_BASE_URL"].split("://")[1]
token = GetToken(domain)

users = [
Expand All @@ -318,26 +359,28 @@ def add_users(app, db):
name = u["name"]
passw = u["password"]
payload = token.login(
client_id=app.config["AUTH0_CLIENT_ID"],
client_secret=app.config["AUTH0_CLIENT_SECRET"],
client_id=real_app.config["AUTH0_CLIENT_ID"],
client_secret=real_app.config["AUTH0_CLIENT_SECRET"],
username=name + "@email.com",
password=passw,
realm="Username-Password-Authentication",
audience=app.config["AUTH0_API_AUDIENCE"],
scope="openid",
audience=real_app.config["AUTH0_API_AUDIENCE"],
scope="openid profile email",
)
token_info = decode_token(payload["access_token"])
user = User(
name=name,
external_id=token_info["sub"],
)
if User.query.filter_by(name=token_info["sub"]).first() is None:
db.session.add(user)
db.session.commit()
# do not add user1 into database
if name != "user1":
user = User(
name=name,
external_id=token_info["sub"],
)
if User.query.filter_by(external_id=token_info["sub"]).first() is None:
real_db.session.add(user)
real_db.session.commit()

tokens[name] = {
"token": payload["access_token"],
"id": User.query.filter_by(external_id=token_info["sub"]).first().id,
"external_id": token_info["sub"],
}

yield tokens
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@

@schemathesis_test
@schema.parametrize(endpoint="^/api/annotations")
def test_annotation_endpoint(case, auth_client):
def test_annotation_endpoint(case, auth_client, session):
response = case.call_wsgi(headers=auth_client._get_headers())
case.validate_response(response)
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

@schemathesis_test
@schema.parametrize(endpoint="^/api/meta-analyses")
def test_meta_analysis_endpoint(case, auth_client):
def test_meta_analysis_endpoint(case, auth_client, session):
# The `session` argument must be supplied.
response = case.call_wsgi(headers=auth_client._get_headers())
case.validate_response(response)
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

@schemathesis_test
@schema.parametrize(endpoint="^/api/specifications")
def test_specification_endpoint(case, auth_client):
def test_specification_endpoint(case, auth_client, session):
# The `session` argument must be supplied.
response = case.call_wsgi(headers=auth_client._get_headers())
case.validate_response(response)
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

@schemathesis_test
@schema.parametrize(endpoint="^/api/studysets")
def test_studyset_endpoint(case, auth_client):
def test_studyset_endpoint(case, auth_client, session):
# The `session` argument must be supplied.
response = case.call_wsgi(headers=auth_client._get_headers())
case.validate_response(response)
4 changes: 2 additions & 2 deletions store/neurostore/tests/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import pytest


@auth_test
#@auth_test
def test_decode_token(add_users):
from ..resources.auth import decode_token, AuthError

Expand All @@ -13,7 +13,7 @@ def test_decode_token(add_users):
decode_token(user["token"])


@auth_test
#@auth_test
def test_creating_new_user_on_db(add_users):
from .request_utils import Client

Expand Down

0 comments on commit 9c5707c

Please sign in to comment.