Skip to content

Commit a54f50b

Browse files
authored
[FIX/ENH] usernames (#570)
* wip: add user information * wip: fix usernames * add new user profile * update workflow to run relevant tests
1 parent c61934c commit a54f50b

File tree

16 files changed

+169
-72
lines changed

16 files changed

+169
-72
lines changed

.github/workflows/workflow.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ jobs:
182182
-e "AUTH0_AUTH_URL=${AUTH0_AUTH_URL}" \
183183
--rm -w /neurostore \
184184
neurostore \
185-
python -m pytest neurostore/tests
185+
bash -c "python -m pytest neurostore/tests && python -m pytest --auth neurostore/tests/test_auth.py"
186186
187187
neurosynth_compose_backend_tests:
188188
runs-on: ubuntu-latest

store/neurostore/resources/base.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,24 @@
3232
from . import data as viewdata
3333

3434

35+
def create_user():
36+
from auth0.v3.authentication.users import Users
37+
auth = request.headers.get("Authorization", None)
38+
token = auth.split()[1]
39+
profile_info = Users(
40+
current_app.config["AUTH0_BASE_URL"].removeprefix("https://")
41+
).userinfo(access_token=token)
42+
43+
# user signed up with auth0, but has not made any queries yet...
44+
# should have endpoint to "create user" after sign on with auth0
45+
current_user = User(
46+
external_id=connexion.context["user"],
47+
name=profile_info.get("name", "Unknown")
48+
)
49+
50+
return current_user
51+
52+
3553
class BaseView(MethodView):
3654
_model = None
3755
_nested = {}
@@ -64,9 +82,8 @@ def update_or_create(cls, data, id=None, commit=True):
6482

6583
current_user = get_current_user()
6684
if not current_user:
67-
# user signed up with auth0, but has not made any queries yet...
68-
# should have endpoint to "create user" after sign on with auth0
69-
current_user = User(external_id=connexion.context["user"])
85+
current_user = create_user()
86+
7087
db.session.add(current_user)
7188
db.session.commit()
7289

store/neurostore/tests/api/test_analyses.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from ...schemas import AnalysisSchema
44

55

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

1414

15-
def test_get_analyses(auth_client, ingest_neurosynth):
15+
def test_get_analyses(auth_client, ingest_neurosynth, session):
1616
# List of analyses
1717
resp = auth_client.get("/api/analyses/")
1818
assert resp.status_code == 200

store/neurostore/tests/api/test_annotations.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from ...models import Studyset, User
44

55

6-
def test_post_blank_annotation(auth_client, ingest_neurosynth):
6+
def test_post_blank_annotation(auth_client, ingest_neurosynth, session):
77
dset = Studyset.query.first()
88
payload = {
99
"studyset": dset.id,
@@ -17,7 +17,7 @@ def test_post_blank_annotation(auth_client, ingest_neurosynth):
1717
)
1818

1919

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

3838
# for some reason output is no longer valid
3939
@pytest.mark.xfail
40-
def test_get_annotations(auth_client, ingest_neurosynth):
40+
def test_get_annotations(auth_client, ingest_neurosynth, session):
4141
import pandas as pd
4242
from io import StringIO
4343

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

6161

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

7373

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

217217

218-
def test_mismatched_notes(auth_client, ingest_neurosynth):
218+
def test_mismatched_notes(auth_client, ingest_neurosynth, session):
219219
dset = Studyset.query.first()
220220
# y for x in non_flat for y in x
221221
data = [
@@ -249,7 +249,7 @@ def test_mismatched_notes(auth_client, ingest_neurosynth):
249249

250250
# test push analysis id that does not exist
251251
# Handle error better
252-
def test_put_nonexistent_analysis(auth_client, ingest_neurosynth):
252+
def test_put_nonexistent_analysis(auth_client, ingest_neurosynth, session):
253253
dset = Studyset.query.first()
254254
# y for x in non_flat for y in x
255255
data = [
@@ -281,7 +281,7 @@ def test_put_nonexistent_analysis(auth_client, ingest_neurosynth):
281281
)
282282

283283

284-
def test_correct_note_overwrite(auth_client, ingest_neurosynth):
284+
def test_correct_note_overwrite(auth_client, ingest_neurosynth, session):
285285
dset = Studyset.query.first()
286286
# y for x in non_flat for y in x
287287
data = [

store/neurostore/tests/api/test_base_studies.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Test Base Study Endpoint"""
22

33

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

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

1313

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

1717
assert info_resp.status_code == 200

store/neurostore/tests/api/test_conditions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
def test_get_conditions(auth_client, ingest_neurovault):
1+
def test_get_conditions(auth_client, ingest_neurovault, session):
22
resp = auth_client.get("/api/conditions/")
33
assert resp.status_code == 200
44
assert len(resp.json["results"]) > 1
55

66

7-
def test_post_conditions(auth_client, ingest_neurovault):
7+
def test_post_conditions(auth_client, ingest_neurovault, session):
88
my_condition = {"name": "ice cream", "description": "surprise, it's rocky road!"}
99
post_resp = auth_client.post("/api/conditions/", data=my_condition)
1010
assert post_resp.status_code == 200

store/neurostore/tests/api/test_crud.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
("points", Point, PointSchema),
3838
],
3939
)
40-
def test_create(auth_client, user_data, endpoint, model, schema):
40+
def test_create(auth_client, user_data, endpoint, model, schema, session):
4141
user = User.query.filter_by(name="user1").first()
4242

4343
rows = model.query.filter_by(user=user).all()
@@ -77,7 +77,7 @@ def test_create(auth_client, user_data, endpoint, model, schema):
7777
("points", Point, PointSchema),
7878
],
7979
)
80-
def test_read(auth_client, user_data, endpoint, model, schema):
80+
def test_read(auth_client, user_data, endpoint, model, schema, session):
8181
user = User.query.filter_by(name="user1").first()
8282
query = True
8383
if hasattr(model, "public"):
@@ -110,7 +110,7 @@ def test_read(auth_client, user_data, endpoint, model, schema):
110110
("points", Point, PointSchema, {"space": "MNI"}),
111111
],
112112
)
113-
def test_update(auth_client, user_data, endpoint, model, schema, update):
113+
def test_update(auth_client, user_data, endpoint, model, schema, update, session):
114114
user = User.query.filter_by(name="user1").first()
115115
record = model.query.filter_by(user=user).first()
116116

store/neurostore/tests/api/test_images.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from ...models import Study, Analysis, User, Image
33

44

5-
def test_get_images(auth_client, ingest_neurovault):
5+
def test_get_images(auth_client, ingest_neurovault, session):
66
# List of studysets
77
resp = auth_client.get("/api/images/")
88
assert resp.status_code == 200

store/neurostore/tests/api/test_points.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from ...models import User, Analysis, Study
55

66

7-
def test_get_points(auth_client, ingest_neurosynth):
7+
def test_get_points(auth_client, ingest_neurosynth, session):
88
# Get an analysis
99
resp = auth_client.get("/api/analyses/")
1010
analysis = decode_json(resp)["results"][0]

store/neurostore/tests/api/test_query_params.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
("analyses", AnalysisSchema()),
1313
],
1414
)
15-
def test_nested(auth_client, ingest_neurosynth, nested, resource_schema):
15+
def test_nested(auth_client, ingest_neurosynth, nested, resource_schema, session):
1616
resource, schema = resource_schema
1717
resp = auth_client.get(f"/api/{resource}/?nested={nested}")
1818
fields = [
@@ -33,7 +33,7 @@ def test_nested(auth_client, ingest_neurosynth, nested, resource_schema):
3333
continue
3434

3535

36-
def test_user_id(auth_client, user_data):
36+
def test_user_id(auth_client, user_data, session):
3737
from ...resources.users import User
3838

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

4545

46-
def test_source_id(auth_client, ingest_neurosynth):
46+
def test_source_id(auth_client, ingest_neurosynth, session):
4747
from ...resources.data import Study
4848

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

5555

56-
def test_data_type(auth_client, ingest_neurosynth, ingest_neurovault):
56+
def test_data_type(auth_client, ingest_neurosynth, ingest_neurovault, session):
5757
get_coord = auth_client.get("/api/studies/?data_type=coordinate")
5858
assert get_coord.status_code == 200
5959
get_img = auth_client.get("/api/studies/?data_type=image")
@@ -67,12 +67,12 @@ def test_data_type(auth_client, ingest_neurosynth, ingest_neurovault):
6767
)
6868

6969

70-
def test_page_size(auth_client, ingest_neurosynth):
70+
def test_page_size(auth_client, ingest_neurosynth, session):
7171
get_page_size = auth_client.get("/api/studies/?page_size=5")
7272
assert get_page_size.status_code == 200
7373

7474

75-
def test_common_queries(auth_client, ingest_neurosynth):
75+
def test_common_queries(auth_client, ingest_neurosynth, session):
7676
study = Study.query.filter(Study.pmid.isnot(None)).first()
7777

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

8585

86-
def test_multiword_queries(auth_client, ingest_neurosynth):
86+
def test_multiword_queries(auth_client, ingest_neurosynth, session):
8787
study = Study.query.first()
8888
name = study.name
8989
word_list = name.split(" ")

0 commit comments

Comments
 (0)