@@ -445,6 +445,9 @@ void deleteUser() throws JsonProcessingException {
445445 super .remoteProvisionedGroupRepository .save (remoteProvisionedGroup );
446446
447447 User user = userRepository .findBySubIgnoreCase (KB_USER_SUB ).get ();
448+ user .getUserRoles ().removeIf (userRole -> !StringUtils .hasText (userRole .getRole ().getCrmRoleId ()));
449+ userRepository .save (user );
450+
448451 RemoteProvisionedUser remoteProvisionedUser = new RemoteProvisionedUser (user , UUID .randomUUID ().toString (), "7" );
449452 super .remoteProvisionedUserRepository .save (remoteProvisionedUser );
450453 //Because of the PUT request of the change in the group, all users are fetched and checked if they exists in the remote SCIM
@@ -473,6 +476,47 @@ void deleteUser() throws JsonProcessingException {
473476 assertTrue (optionalUser .isEmpty ());
474477 }
475478
479+ @ Test
480+ void deleteUserWithNativeInviteRoles () throws JsonProcessingException {
481+ CRMContact crmContact = new CRMContact ();
482+ crmContact .setContactId (CRM_CONTACT_ID );
483+ crmContact .setOrganisation (new CRMOrganisation (CRM_ORGANIZATION_ID , "abbr" , "name" ));
484+
485+ stubForManageProvisioning (List .of ("5" ));
486+ Role role = roleRepository .findByName ("Research" ).get ();
487+ RemoteProvisionedGroup remoteProvisionedGroup = new RemoteProvisionedGroup (role , UUID .randomUUID ().toString (), "7" );
488+ super .remoteProvisionedGroupRepository .save (remoteProvisionedGroup );
489+
490+ User user = userRepository .findBySubIgnoreCase (KB_USER_SUB ).get ();
491+ RemoteProvisionedUser remoteProvisionedUser = new RemoteProvisionedUser (user , UUID .randomUUID ().toString (), "7" );
492+ super .remoteProvisionedUserRepository .save (remoteProvisionedUser );
493+ //Because of the PUT request of the change in the group, all users are fetched and checked if they exists in the remote SCIM
494+ User guestUser = userRepository .findBySubIgnoreCase (GUEST_SUB ).get ();
495+ RemoteProvisionedUser remoteProvisionedUserGuest = new RemoteProvisionedUser (guestUser , UUID .randomUUID ().toString (), "7" );
496+ super .remoteProvisionedUserRepository .save (remoteProvisionedUserGuest );
497+
498+ stubForUpdateScimRole ();
499+ stubForDeleteScimUser ();
500+
501+ String response = given ()
502+ .when ()
503+ .accept (ContentType .JSON )
504+ .header (API_KEY_HEADER , "secret" )
505+ .contentType (ContentType .JSON )
506+ .body (crmContact )
507+ .delete ("/crm/profile" )
508+ .then ()
509+ .extract ()
510+ .asString ();
511+ assertEquals ("deleted" , response );
512+
513+ Organisation organisation = organisationRepository .findByCrmOrganisationId (CRM_ORGANIZATION_ID )
514+ .orElseThrow (() -> new NotFoundException ("Organisation not found: " + CRM_ORGANIZATION_ID ));
515+ Optional <User > optionalUser = userRepository .findByCrmContactIdAndOrganisation (CRM_CONTACT_ID , organisation );
516+ assertFalse (optionalUser .isEmpty ());
517+ assertEquals (1 , optionalUser .get ().getUserRoles ().size ());
518+ }
519+
476520 @ Test
477521 void scopeInviteRoleToUniqueCRMRoleIdAndOrganizationId () throws JsonProcessingException {
478522 String converRoleId = "92b2b379-07e4-e811-8100-005056956c1a" ;
0 commit comments