@@ -259,7 +259,44 @@ def test_sync_person_id_change(
259259 "users-sync" ,
260260 dict (action = "updating-existing-users" , msg = expected_log_msg ),
261261 log_uuid = mock .ANY ,
262- ),
262+ )
263+
264+
265+ @patch ("invenio_cern_sync.users.sync.KeycloakService" )
266+ @patch ("invenio_cern_sync.users.sync.AuthZService" )
267+ @patch ("invenio_cern_sync.users.sync.log_warning" )
268+ def test_sync_identity_missing (
269+ mock_log_warning ,
270+ MockAuthZService ,
271+ MockKeycloakService ,
272+ app ,
273+ cern_identities ,
274+ db ,
275+ ):
276+ """Test sync identity missing."""
277+ # prepare the db with the initial data
278+ client_id = app .config ["CERN_APP_CREDENTIALS" ]["consumer_key" ]
279+
280+ MockAuthZService .return_value .get_identities .return_value = cern_identities
281+ sync (method = "AuthZ" )
282+
283+ # delete the first identity from the DB
284+ first = cern_identities [0 ]
285+ previous_person_id = first ["personId" ]
286+ UserIdentity .query .filter_by (id = first ["personId" ]).delete ()
287+ db .session .commit ()
288+ MockAuthZService .return_value .get_identities .return_value = cern_identities
289+ sync (method = "AuthZ" )
290+
291+ # check that the user identity was updated, but the user was not duplicated
292+ assert UserIdentity .query .filter_by (id = first ["personId" ]).one ()
293+ user = User .query .filter_by (email = first ["primaryAccountEmail" ]).one ()
294+ expected_log_msg = f"Identity Id changed for User `{ user .username } ` `{ user .email } `. Previous UserIdentity.id in the local DB: `{ previous_person_id } ` - New Identity Id from CERN DB: `{ first ['personId' ]} `."
295+ mock_log_warning .assert_any_call (
296+ "users-sync" ,
297+ dict (action = "updating-existing-users" , msg = expected_log_msg ),
298+ log_uuid = mock .ANY ,
299+ )
263300
264301
265302@patch ("invenio_cern_sync.users.sync.KeycloakService" )
@@ -308,4 +345,4 @@ def test_sync_username_email_change(
308345 "users-sync" ,
309346 dict (action = "updating-existing-users" , msg = expected_log_msg ),
310347 log_uuid = mock .ANY ,
311- ),
348+ )
0 commit comments