-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Issue 2676] Create a GET /users/:userID endpoint (#3041)
## Summary Fixes #{[2676](#2676)} ### Time to review: __15 mins__ ## Changes proposed Created GET `v1/users/:user_id` endpoint ( expected user_id type UUID) Created `UserGetResponseSchema` Created `get_user` service method to fetch user object Created 2 tests: 1. Test correct user object returned 2. Test 401 error when auth user request for another user_id ## Context for reviewers This endpoint should have authentication configured The endpoint should validate that the user ID matches the user ID being queried and raise a 401 Unauthorized if not --------- Co-authored-by: nava-platform-bot <[email protected]>
- Loading branch information
1 parent
0df81b5
commit e504342
Showing
7 changed files
with
140 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from uuid import UUID | ||
|
||
from sqlalchemy import select | ||
|
||
from src.adapters import db | ||
from src.db.models.user_models import LinkExternalUser | ||
|
||
|
||
def _fetch_user(db_session: db.Session, user_id: UUID) -> LinkExternalUser | None: | ||
stmt = select(LinkExternalUser).where(LinkExternalUser.user_id == user_id) | ||
|
||
user = db_session.execute(stmt).scalar_one_or_none() | ||
|
||
return user | ||
|
||
|
||
def get_user(db_session: db.Session, user_id: UUID) -> LinkExternalUser | None: | ||
return _fetch_user(db_session, user_id) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import uuid | ||
|
||
from src.auth.api_jwt_auth import create_jwt_for_user | ||
from tests.src.db.models.factories import LinkExternalUserFactory | ||
|
||
################ | ||
# GET user tests | ||
################ | ||
|
||
|
||
def test_get_user_200(enable_factory_create, client, db_session, api_auth_token): | ||
external_user = LinkExternalUserFactory.create() | ||
token, _ = create_jwt_for_user(external_user.user, db_session) | ||
db_session.commit() | ||
|
||
resp = client.get(f"/v1/users/{external_user.user_id}", headers={"X-SGG-Token": token}) | ||
|
||
assert resp.status_code == 200 | ||
assert resp.get_json()["data"]["user_id"] == str(external_user.user_id) | ||
|
||
|
||
def test_get_user_401(enable_factory_create, client, db_session, api_auth_token): | ||
external_user = LinkExternalUserFactory.create() | ||
token, _ = create_jwt_for_user(external_user.user, db_session) | ||
db_session.commit() | ||
|
||
random_uuid = str(uuid.uuid4()) | ||
resp = client.get(f"/v1/users/{random_uuid}", headers={"X-SGG-Token": token}) | ||
|
||
assert resp.status_code == 401 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters