Skip to content

Commit

Permalink
[ENH] add usernames to compose (#588)
Browse files Browse the repository at this point in the history
* run test in ci

* run black

* undo changes to test_auth in neurostore

* add new test file
  • Loading branch information
jdkent authored Sep 8, 2023
1 parent 4826e79 commit 9cd54a5
Show file tree
Hide file tree
Showing 18 changed files with 156 additions and 48 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
31 changes: 27 additions & 4 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 Expand Up @@ -446,7 +467,9 @@ def put(self, id):
access_token = request.headers.get("Authorization")
neurostore_analysis_upload = create_or_update_neurostore_analysis.si(
ns_analysis_id=ns_analysis.id,
cluster_table=str(cluster_table_fnames[0]) if cluster_table_fnames else None,
cluster_table=str(cluster_table_fnames[0])
if cluster_table_fnames
else None,
nv_collection_id=result.neurovault_collection.id,
access_token=access_token,
)
Expand Down
3 changes: 2 additions & 1 deletion compose/neurosynth_compose/resources/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ def create_or_update_neurostore_analysis(
from auth0.v3.authentication.get_token import GetToken
import pandas as pd
from .neurostore import neurostore_session

ns_analysis = NeurostoreAnalysis.query.filter_by(id=ns_analysis_id).one()
nv_collection = NeurovaultCollection.query.filter_by(id=nv_collection_id).one()

Expand Down Expand Up @@ -126,7 +127,7 @@ def create_or_update_neurostore_analysis(
"value": row["Peak Stat"],
}
],
"order": point_idx
"order": point_idx,
}
if not pd.isna(row["Cluster Size (mm3)"]):
point["subpeak"] = True
Expand Down
1 change: 1 addition & 0 deletions compose/neurosynth_compose/schemas/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ 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 @@ -7,14 +7,17 @@
NeurostoreStudy,
NeurostoreAnalysis,
)
from ....resources.tasks import file_upload_neurovault, create_or_update_neurostore_analysis
from ....resources.tasks import (
file_upload_neurovault,
create_or_update_neurostore_analysis,
)
from ....resources.analysis import (
create_or_update_neurostore_study,
)


@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 +38,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 +81,9 @@ 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
30 changes: 30 additions & 0 deletions compose/neurosynth_compose/tests/api/test_auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from ..conftest import auth_test
import pytest


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

with pytest.raises(AuthError):
decode_token("improper_token")

for user in add_users.values():
decode_token(user["token"])


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

token_info = add_users
user_name = "user1" # user1 was not entered into database

client = Client(
token=token_info[user_name]["token"],
username=token_info[user_name]["external_id"],
)

resp = client.post("/api/projects", data={"name": "my project"})

assert resp.status_code == 200
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,9 @@ 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
Loading

0 comments on commit 9cd54a5

Please sign in to comment.