Skip to content

Commit

Permalink
[FIX/ENH] usernames (#570)
Browse files Browse the repository at this point in the history
* wip: add user information

* wip: fix usernames

* add new user profile

* update workflow to run relevant tests
  • Loading branch information
jdkent authored Aug 16, 2023
1 parent c61934c commit a54f50b
Show file tree
Hide file tree
Showing 16 changed files with 169 additions and 72 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ jobs:
-e "AUTH0_AUTH_URL=${AUTH0_AUTH_URL}" \
--rm -w /neurostore \
neurostore \
python -m pytest neurostore/tests
bash -c "python -m pytest neurostore/tests && python -m pytest --auth neurostore/tests/test_auth.py"
neurosynth_compose_backend_tests:
runs-on: ubuntu-latest
Expand Down
23 changes: 20 additions & 3 deletions store/neurostore/resources/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,24 @@
from . import data as viewdata


def create_user():
from auth0.v3.authentication.users import Users
auth = request.headers.get("Authorization", 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


class BaseView(MethodView):
_model = None
_nested = {}
Expand Down Expand Up @@ -64,9 +82,8 @@ def update_or_create(cls, data, id=None, commit=True):

current_user = get_current_user()
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"])
current_user = create_user()

db.session.add(current_user)
db.session.commit()

Expand Down
4 changes: 2 additions & 2 deletions store/neurostore/tests/api/test_analyses.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from ...schemas import AnalysisSchema


def test_get_nested_and_not_nested_analyses(auth_client, ingest_neurosynth):
def test_get_nested_and_not_nested_analyses(auth_client, ingest_neurosynth, session):
analysis_id = Analysis.query.first().id
non_nested = auth_client.get(f"/api/analyses/{analysis_id}?nested=false")
nested = auth_client.get(f"/api/analyses/{analysis_id}?nested=true")
Expand All @@ -12,7 +12,7 @@ def test_get_nested_and_not_nested_analyses(auth_client, ingest_neurosynth):
assert isinstance(nested.json["points"][0], dict)


def test_get_analyses(auth_client, ingest_neurosynth):
def test_get_analyses(auth_client, ingest_neurosynth, session):
# List of analyses
resp = auth_client.get("/api/analyses/")
assert resp.status_code == 200
Expand Down
16 changes: 8 additions & 8 deletions store/neurostore/tests/api/test_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from ...models import Studyset, User


def test_post_blank_annotation(auth_client, ingest_neurosynth):
def test_post_blank_annotation(auth_client, ingest_neurosynth, session):
dset = Studyset.query.first()
payload = {
"studyset": dset.id,
Expand All @@ -17,7 +17,7 @@ def test_post_blank_annotation(auth_client, ingest_neurosynth):
)


def test_post_annotation(auth_client, ingest_neurosynth):
def test_post_annotation(auth_client, ingest_neurosynth, session):
dset = Studyset.query.first()
# y for x in non_flat for y in x
data = [
Expand All @@ -37,7 +37,7 @@ def test_post_annotation(auth_client, ingest_neurosynth):

# for some reason output is no longer valid
@pytest.mark.xfail
def test_get_annotations(auth_client, ingest_neurosynth):
def test_get_annotations(auth_client, ingest_neurosynth, session):
import pandas as pd
from io import StringIO

Expand All @@ -59,7 +59,7 @@ def test_get_annotations(auth_client, ingest_neurosynth):
assert isinstance(df, pd.DataFrame)


def test_clone_annotation(auth_client, simple_neurosynth_annotation):
def test_clone_annotation(auth_client, simple_neurosynth_annotation, session):
annotation_entry = simple_neurosynth_annotation
resp = auth_client.post(
f"/api/annotations/?source_id={annotation_entry.id}", data={}
Expand All @@ -71,7 +71,7 @@ def test_clone_annotation(auth_client, simple_neurosynth_annotation):
assert data["source"] == "neurostore"


def test_single_analysis_delete(auth_client, user_data):
def test_single_analysis_delete(auth_client, user_data, session):
user = User.query.filter_by(name="user1").first()
# get relevant studyset
studysets = auth_client.get(f"/api/studysets/?user_id={user.external_id}")
Expand Down Expand Up @@ -215,7 +215,7 @@ def test_analysis_addition_to_studyset(auth_client, session, user_data):
)


def test_mismatched_notes(auth_client, ingest_neurosynth):
def test_mismatched_notes(auth_client, ingest_neurosynth, session):
dset = Studyset.query.first()
# y for x in non_flat for y in x
data = [
Expand Down Expand Up @@ -249,7 +249,7 @@ def test_mismatched_notes(auth_client, ingest_neurosynth):

# test push analysis id that does not exist
# Handle error better
def test_put_nonexistent_analysis(auth_client, ingest_neurosynth):
def test_put_nonexistent_analysis(auth_client, ingest_neurosynth, session):
dset = Studyset.query.first()
# y for x in non_flat for y in x
data = [
Expand Down Expand Up @@ -281,7 +281,7 @@ def test_put_nonexistent_analysis(auth_client, ingest_neurosynth):
)


def test_correct_note_overwrite(auth_client, ingest_neurosynth):
def test_correct_note_overwrite(auth_client, ingest_neurosynth, session):
dset = Studyset.query.first()
# y for x in non_flat for y in x
data = [
Expand Down
4 changes: 2 additions & 2 deletions store/neurostore/tests/api/test_base_studies.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Test Base Study Endpoint"""


def test_flat_base_study(auth_client, ingest_neurosynth):
def test_flat_base_study(auth_client, ingest_neurosynth, session):
flat_resp = auth_client.get("/api/base-studies/?flat=true")
reg_resp = auth_client.get("/api/base-studies/?flat=false")

Expand All @@ -11,7 +11,7 @@ def test_flat_base_study(auth_client, ingest_neurosynth):
assert "versions" in reg_resp.json["results"][0]


def test_info_base_study(auth_client, ingest_neurosynth):
def test_info_base_study(auth_client, ingest_neurosynth, session):
info_resp = auth_client.get("/api/base-studies/?info=true")

assert info_resp.status_code == 200
Expand Down
4 changes: 2 additions & 2 deletions store/neurostore/tests/api/test_conditions.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
def test_get_conditions(auth_client, ingest_neurovault):
def test_get_conditions(auth_client, ingest_neurovault, session):
resp = auth_client.get("/api/conditions/")
assert resp.status_code == 200
assert len(resp.json["results"]) > 1


def test_post_conditions(auth_client, ingest_neurovault):
def test_post_conditions(auth_client, ingest_neurovault, session):
my_condition = {"name": "ice cream", "description": "surprise, it's rocky road!"}
post_resp = auth_client.post("/api/conditions/", data=my_condition)
assert post_resp.status_code == 200
Expand Down
6 changes: 3 additions & 3 deletions store/neurostore/tests/api/test_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
("points", Point, PointSchema),
],
)
def test_create(auth_client, user_data, endpoint, model, schema):
def test_create(auth_client, user_data, endpoint, model, schema, session):
user = User.query.filter_by(name="user1").first()

rows = model.query.filter_by(user=user).all()
Expand Down Expand Up @@ -77,7 +77,7 @@ def test_create(auth_client, user_data, endpoint, model, schema):
("points", Point, PointSchema),
],
)
def test_read(auth_client, user_data, endpoint, model, schema):
def test_read(auth_client, user_data, endpoint, model, schema, session):
user = User.query.filter_by(name="user1").first()
query = True
if hasattr(model, "public"):
Expand Down Expand Up @@ -110,7 +110,7 @@ def test_read(auth_client, user_data, endpoint, model, schema):
("points", Point, PointSchema, {"space": "MNI"}),
],
)
def test_update(auth_client, user_data, endpoint, model, schema, update):
def test_update(auth_client, user_data, endpoint, model, schema, update, session):
user = User.query.filter_by(name="user1").first()
record = model.query.filter_by(user=user).first()

Expand Down
2 changes: 1 addition & 1 deletion store/neurostore/tests/api/test_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from ...models import Study, Analysis, User, Image


def test_get_images(auth_client, ingest_neurovault):
def test_get_images(auth_client, ingest_neurovault, session):
# List of studysets
resp = auth_client.get("/api/images/")
assert resp.status_code == 200
Expand Down
2 changes: 1 addition & 1 deletion store/neurostore/tests/api/test_points.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from ...models import User, Analysis, Study


def test_get_points(auth_client, ingest_neurosynth):
def test_get_points(auth_client, ingest_neurosynth, session):
# Get an analysis
resp = auth_client.get("/api/analyses/")
analysis = decode_json(resp)["results"][0]
Expand Down
14 changes: 7 additions & 7 deletions store/neurostore/tests/api/test_query_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
("analyses", AnalysisSchema()),
],
)
def test_nested(auth_client, ingest_neurosynth, nested, resource_schema):
def test_nested(auth_client, ingest_neurosynth, nested, resource_schema, session):
resource, schema = resource_schema
resp = auth_client.get(f"/api/{resource}/?nested={nested}")
fields = [
Expand All @@ -33,7 +33,7 @@ def test_nested(auth_client, ingest_neurosynth, nested, resource_schema):
continue


def test_user_id(auth_client, user_data):
def test_user_id(auth_client, user_data, session):
from ...resources.users import User

id_ = auth_client.username
Expand All @@ -43,7 +43,7 @@ def test_user_id(auth_client, user_data):
assert study["user"] == user.external_id


def test_source_id(auth_client, ingest_neurosynth):
def test_source_id(auth_client, ingest_neurosynth, session):
from ...resources.data import Study

study = Study.query.first()
Expand All @@ -53,7 +53,7 @@ def test_source_id(auth_client, ingest_neurosynth):
assert post.json == get.json["results"][0]


def test_data_type(auth_client, ingest_neurosynth, ingest_neurovault):
def test_data_type(auth_client, ingest_neurosynth, ingest_neurovault, session):
get_coord = auth_client.get("/api/studies/?data_type=coordinate")
assert get_coord.status_code == 200
get_img = auth_client.get("/api/studies/?data_type=image")
Expand All @@ -67,12 +67,12 @@ def test_data_type(auth_client, ingest_neurosynth, ingest_neurovault):
)


def test_page_size(auth_client, ingest_neurosynth):
def test_page_size(auth_client, ingest_neurosynth, session):
get_page_size = auth_client.get("/api/studies/?page_size=5")
assert get_page_size.status_code == 200


def test_common_queries(auth_client, ingest_neurosynth):
def test_common_queries(auth_client, ingest_neurosynth, session):
study = Study.query.filter(Study.pmid.isnot(None)).first()

pmid_search = auth_client.get(f"/api/studies/?pmid={study.pmid}")
Expand All @@ -83,7 +83,7 @@ def test_common_queries(auth_client, ingest_neurosynth):
assert len(pmid_search.json["results"]) == len(total_search.json["results"])


def test_multiword_queries(auth_client, ingest_neurosynth):
def test_multiword_queries(auth_client, ingest_neurosynth, session):
study = Study.query.first()
name = study.name
word_list = name.split(" ")
Expand Down
24 changes: 12 additions & 12 deletions store/neurostore/tests/api/test_studies.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from ...models import Studyset, Study, User, Analysis


def test_create_study_as_user_and_analysis_as_bot(auth_clients):
def test_create_study_as_user_and_analysis_as_bot(auth_clients, session):
# create study as user
user_auth_client = next(ac for ac in auth_clients if ac.username == "user1-id")

Expand All @@ -19,7 +19,7 @@ def test_create_study_as_user_and_analysis_as_bot(auth_clients):
assert analysis_resp.status_code == 200


def test_get_studies(auth_client, ingest_neurosynth, ingest_neuroquery):
def test_get_studies(auth_client, ingest_neurosynth, ingest_neuroquery, session):
# List of studies
resp = auth_client.get("/api/studies/?nested=true&level=group")
assert resp.status_code == 200
Expand Down Expand Up @@ -62,7 +62,7 @@ def test_get_studies(auth_client, ingest_neurosynth, ingest_neuroquery):
},
],
)
def test_put_studies(auth_client, ingest_neurosynth, data):
def test_put_studies(auth_client, ingest_neurosynth, data, session):
study_entry = Study.query.first()
study_clone = auth_client.post(
f"/api/studies/?source_id={study_entry.id}", data={}
Expand All @@ -87,7 +87,7 @@ def test_put_studies(auth_client, ingest_neurosynth, data):
assert put_resp.json["metadata"] == updated_study_entry.metadata_


def test_clone_studies(auth_client, ingest_neurosynth, ingest_neurovault):
def test_clone_studies(auth_client, ingest_neurosynth, ingest_neurovault, session):
study_entry = Study.query.filter(Study.metadata_.isnot(None)).first()
resp = auth_client.post(f"/api/studies/?source_id={study_entry.id}", data={})
data = resp.json
Expand All @@ -110,7 +110,7 @@ def test_clone_studies(auth_client, ingest_neurosynth, ingest_neurovault):
)


def test_private_studies(user_data, auth_clients):
def test_private_studies(user_data, auth_clients, session):
from ...resources.users import User

client1, client2 = auth_clients[0:2]
Expand All @@ -136,7 +136,7 @@ def test_private_studies(user_data, auth_clients):
assert user1_get.status_code == 200


def test_post_studies(auth_client, ingest_neurosynth):
def test_post_studies(auth_client, ingest_neurosynth, session):
payload = auth_client.get("/api/analyses/").json["results"]
analyses = [analysis["id"] for analysis in payload]
my_study = {
Expand Down Expand Up @@ -164,14 +164,14 @@ def test_delete_studies(auth_client, ingest_neurosynth, session):
assert Analysis.query.filter_by(id=analysis).first() is None


def test_production_study_query(auth_client, user_data):
def test_production_study_query(auth_client, user_data, session):
auth_client.get(
"/api/studies/?sort=name&page=1&desc=true&page_size=29999&nested=false&unique=true"
)


@pytest.mark.skip("not supporting this feature anymore")
def test_getting_studysets_by_owner(auth_clients, user_data):
def test_getting_studysets_by_owner(auth_clients, user_data, session):
client1 = auth_clients[0]
id1 = client1.username
user_studysets_db = Studyset.query.filter_by(user_id=id1).all()
Expand All @@ -191,15 +191,15 @@ def test_getting_studysets_by_owner(auth_clients, user_data):


@pytest.mark.parametrize("param", ["true", "false", "doi", "name", "pmid"])
def test_get_unique_studies(auth_client, user_data, param):
def test_get_unique_studies(auth_client, user_data, param, session):
# clone a study owned by the user
study_entry = Study.query.filter_by(user_id=auth_client.username).first()
auth_client.post(f"/api/studies/?source_id={study_entry.id}", data={})
resp = auth_client.get(f"/api/studies/?unique={param}")
assert resp.status_code == 200


def test_cache_update(auth_client, user_data):
def test_cache_update(auth_client, user_data, session):
study_entry = Study.query.filter_by(user_id=auth_client.username).first()
auth_client.get(f"/api/studies/{study_entry.id}")
auth_client.get(f"/api/studies/{study_entry.id}?nested=true")
Expand All @@ -208,7 +208,7 @@ def test_cache_update(auth_client, user_data):
auth_client.get(f"/api/studies/{study_entry.id}")


def test_post_meta_analysis(auth_client, user_data):
def test_post_meta_analysis(auth_client, user_data, session):
study_data = {
"name": "meta-analysis",
"analyses": [
Expand Down Expand Up @@ -244,7 +244,7 @@ def test_post_meta_analysis(auth_client, user_data):
assert resp.status_code == 200


def test_studies_flat(auth_client, ingest_neurosynth):
def test_studies_flat(auth_client, ingest_neurosynth, session):
flat_resp = auth_client.get("/api/studies/?flat=true")
reg_resp = auth_client.get("/api/studies/?flat=false")

Expand Down
Loading

0 comments on commit a54f50b

Please sign in to comment.