From f1d1c694c17e35175912ad488e881d65ad1f1be1 Mon Sep 17 00:00:00 2001 From: Marcos Prieto Date: Thu, 28 Nov 2024 14:24:44 +0100 Subject: [PATCH] Store custom_canvas_user_id as LMSUser.lms_api_user_id --- lms/services/user.py | 5 +++++ tests/unit/lms/services/user_test.py | 3 +++ 2 files changed, 8 insertions(+) diff --git a/lms/services/user.py b/lms/services/user.py index 6a7dbca286..cbb97b2ef3 100644 --- a/lms/services/user.py +++ b/lms/services/user.py @@ -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, @@ -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"], @@ -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( diff --git a/tests/unit/lms/services/user_test.py b/tests/unit/lms/services/user_test.py index 3813ccd80c..f669d113fd 100644 --- a/tests/unit/lms/services/user_test.py +++ b/tests/unit/lms/services/user_test.py @@ -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( @@ -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