Refactoring - Version stable

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
dahoud
2026-03-28 17:07:11 +00:00
parent 40a2dd9728
commit 9c66909eff
3 changed files with 118 additions and 29 deletions

View File

@@ -6,6 +6,7 @@ import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.*;
import dev.lions.unionflow.server.client.RoleServiceClient;
import dev.lions.unionflow.server.client.UserServiceClient;
import dev.lions.unionflow.server.entity.Membre;
import dev.lions.unionflow.server.repository.MembreRepository;
@@ -36,6 +37,10 @@ class MembreKeycloakSyncServiceTest {
@RestClient
UserServiceClient userServiceClient;
@InjectMock
@RestClient
RoleServiceClient roleServiceClient;
// =========================================================================
// provisionKeycloakUser
// =========================================================================
@@ -232,8 +237,8 @@ class MembreKeycloakSyncServiceTest {
}
@Test
@DisplayName("promouvoirAdminOrganisation assigne ADMIN_ORGANISATION et retire MEMBRE/MEMBRE_ACTIF")
void promouvoirAdminOrganisation_assignsAdminRoleAndRemovesMemberRoles() {
@DisplayName("promouvoirAdminOrganisation assigne ADMIN_ORGANISATION et révoque MEMBRE/MEMBRE_ACTIF")
void promouvoirAdminOrganisation_assignsAdminRoleAndRevokesMemberRoles() {
UUID membreId = UUID.randomUUID();
UUID keycloakId = UUID.randomUUID();
@@ -249,16 +254,15 @@ class MembreKeycloakSyncServiceTest {
UserDTO user = new UserDTO();
user.setId(keycloakId.toString());
user.setEnabled(true);
user.setRealmRoles(new java.util.ArrayList<>(java.util.List.of("MEMBRE", "MEMBRE_ACTIF")));
user.setRealmName("unionflow");
when(userServiceClient.getUserById(eq(keycloakId.toString()), eq("unionflow"))).thenReturn(user);
when(userServiceClient.updateUser(anyString(), any(UserDTO.class), anyString())).thenReturn(user);
doNothing().when(roleServiceClient).revokeRealmRoles(anyString(), anyString(), any(RoleServiceClient.RoleNamesRequest.class));
doNothing().when(roleServiceClient).assignRealmRoles(anyString(), anyString(), any(RoleServiceClient.RoleNamesRequest.class));
syncService.promouvoirAdminOrganisationDansKeycloak(membreId);
verify(userServiceClient).updateUser(eq(keycloakId.toString()), any(UserDTO.class), eq("unionflow"));
assertThat(user.getRealmRoles()).contains("ADMIN_ORGANISATION");
assertThat(user.getRealmRoles()).doesNotContain("MEMBRE", "MEMBRE_ACTIF");
verify(roleServiceClient).revokeRealmRoles(eq(keycloakId.toString()), eq("unionflow"), any(RoleServiceClient.RoleNamesRequest.class));
verify(roleServiceClient).assignRealmRoles(eq(keycloakId.toString()), eq("unionflow"), any(RoleServiceClient.RoleNamesRequest.class));
}
@Test
@@ -278,11 +282,12 @@ class MembreKeycloakSyncServiceTest {
UserDTO user = new UserDTO();
user.setId(keycloakId.toString());
user.setEnabled(false); // désactivé
user.setRealmRoles(new java.util.ArrayList<>());
user.setEnabled(false);
user.setRealmName("unionflow");
when(userServiceClient.getUserById(anyString(), anyString())).thenReturn(user);
when(userServiceClient.updateUser(anyString(), any(UserDTO.class), anyString())).thenReturn(user);
doNothing().when(roleServiceClient).revokeRealmRoles(anyString(), anyString(), any(RoleServiceClient.RoleNamesRequest.class));
doNothing().when(roleServiceClient).assignRealmRoles(anyString(), anyString(), any(RoleServiceClient.RoleNamesRequest.class));
syncService.promouvoirAdminOrganisationDansKeycloak(membreId);
@@ -330,11 +335,13 @@ class MembreKeycloakSyncServiceTest {
fetchedUser.setRealmName("unionflow");
when(userServiceClient.getUserById(eq(newKeycloakId.toString()), anyString())).thenReturn(fetchedUser);
when(userServiceClient.updateUser(anyString(), any(UserDTO.class), anyString())).thenReturn(fetchedUser);
doNothing().when(roleServiceClient).revokeRealmRoles(anyString(), anyString(), any(RoleServiceClient.RoleNamesRequest.class));
doNothing().when(roleServiceClient).assignRealmRoles(anyString(), anyString(), any(RoleServiceClient.RoleNamesRequest.class));
syncService.promouvoirAdminOrganisationDansKeycloak(membreId);
verify(userServiceClient).createUser(any(UserDTO.class), eq("unionflow"));
verify(userServiceClient).updateUser(eq(newKeycloakId.toString()), any(UserDTO.class), eq("unionflow"));
verify(roleServiceClient).assignRealmRoles(eq(newKeycloakId.toString()), eq("unionflow"), any(RoleServiceClient.RoleNamesRequest.class));
}
@Test
@@ -359,6 +366,78 @@ class MembreKeycloakSyncServiceTest {
.hasMessageContaining("Impossible de promouvoir le compte Keycloak");
}
// =========================================================================
// activerMembreDansKeycloak
// =========================================================================
@Test
@DisplayName("activerMembreDansKeycloak échoue si le membre n'existe pas")
void activerMembreDansKeycloak_failsIfMembreNotFound() {
UUID membreId = UUID.randomUUID();
when(membreRepository.findByIdOptional(membreId)).thenReturn(Optional.empty());
assertThatThrownBy(() -> syncService.activerMembreDansKeycloak(membreId))
.isInstanceOf(NotFoundException.class);
}
@Test
@DisplayName("activerMembreDansKeycloak assigne MEMBRE_ACTIF via roleServiceClient")
void activerMembreDansKeycloak_assignsMemberActifRole() {
UUID membreId = UUID.randomUUID();
UUID keycloakId = UUID.randomUUID();
Membre membre = new Membre();
membre.setId(membreId);
membre.setKeycloakId(keycloakId);
membre.setEmail("membre@unionflow.dev");
membre.setNom("Membre");
membre.setPrenom("Test");
when(membreRepository.findByIdOptional(membreId)).thenReturn(Optional.of(membre));
UserDTO user = new UserDTO();
user.setId(keycloakId.toString());
user.setEnabled(true);
user.setRealmName("unionflow");
when(userServiceClient.getUserById(eq(keycloakId.toString()), eq("unionflow"))).thenReturn(user);
doNothing().when(roleServiceClient).assignRealmRoles(anyString(), anyString(), any(RoleServiceClient.RoleNamesRequest.class));
syncService.activerMembreDansKeycloak(membreId);
verify(roleServiceClient).assignRealmRoles(
eq(keycloakId.toString()), eq("unionflow"), any(RoleServiceClient.RoleNamesRequest.class));
}
@Test
@DisplayName("activerMembreDansKeycloak active le compte s'il est désactivé")
void activerMembreDansKeycloak_enablesDisabledAccount() {
UUID membreId = UUID.randomUUID();
UUID keycloakId = UUID.randomUUID();
Membre membre = new Membre();
membre.setId(membreId);
membre.setKeycloakId(keycloakId);
membre.setEmail("disabled@unionflow.dev");
membre.setNom("Disabled");
membre.setPrenom("Membre");
when(membreRepository.findByIdOptional(membreId)).thenReturn(Optional.of(membre));
UserDTO user = new UserDTO();
user.setId(keycloakId.toString());
user.setEnabled(false);
user.setRealmName("unionflow");
when(userServiceClient.getUserById(anyString(), anyString())).thenReturn(user);
when(userServiceClient.updateUser(anyString(), any(UserDTO.class), anyString())).thenReturn(user);
doNothing().when(roleServiceClient).assignRealmRoles(anyString(), anyString(), any(RoleServiceClient.RoleNamesRequest.class));
syncService.activerMembreDansKeycloak(membreId);
assertThat(user.getEnabled()).isTrue();
verify(userServiceClient).updateUser(anyString(), any(UserDTO.class), anyString());
verify(roleServiceClient).assignRealmRoles(anyString(), anyString(), any(RoleServiceClient.RoleNamesRequest.class));
}
// =========================================================================
// syncMembreToKeycloak
// =========================================================================