Skip to content

Commit

Permalink
Store custom_canvas_user_id as LMSUser.lms_api_user_id
Browse files Browse the repository at this point in the history
  • Loading branch information
marcospri committed Dec 3, 2024
1 parent be5e873 commit f1d1c69
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lms/services/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ def upsert_user(self, lti_user: LTIUser) -> User:
def upsert_lms_user(self, user: User, lti_params: LTIParams) -> LMSUser:
"""Upsert LMSUser based on a User object."""
self._db.flush() # Make sure User has hit the DB on the current transaction

# API ID, only Canvas for now
lms_api_user_id = lti_params.get("custom_canvas_user_id")
lms_user = bulk_upsert(
self._db,
LMSUser,
Expand All @@ -86,6 +89,7 @@ def upsert_lms_user(self, user: User, lti_params: LTIParams) -> LMSUser:
"h_userid": user.h_userid,
"email": user.email,
"display_name": user.display_name,
"lms_api_user_id": lms_api_user_id,
}
],
index_elements=["h_userid"],
Expand All @@ -100,6 +104,7 @@ def upsert_lms_user(self, user: User, lti_params: LTIParams) -> LMSUser:
text('"lms_user"."lti_v13_user_id"'),
),
),
"lms_api_user_id",
],
).one()
bulk_upsert(
Expand Down
3 changes: 3 additions & 0 deletions tests/unit/lms/services/user_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ def test_upsert_user_doesnt_save_email_for_existing_students(

def test_upsert_lms_user(self, service, lti_user, pyramid_request, db_session):
user = service.upsert_user(lti_user)
pyramid_request.lti_params["custom_canvas_user_id"] = "lms_api_user_id"

lms_user = service.upsert_lms_user(user, pyramid_request.lti_params)

lms_user = db_session.scalars(
Expand All @@ -75,6 +77,7 @@ def test_upsert_lms_user(self, service, lti_user, pyramid_request, db_session):
assert lms_user.email == user.email
assert lms_user.updated == user.updated
assert lms_user.lti_v13_user_id == pyramid_request.lti_params.v13.get("sub")
assert lms_user.lms_api_user_id == "lms_api_user_id"

def test_upsert_lms_user_doesnt_clear_lti_v13_user_id(
self, service, lti_user, pyramid_request, db_session
Expand Down

0 comments on commit f1d1c69

Please sign in to comment.