diff --git a/coral/package.json b/coral/package.json index 086d6c2e88..2e521e1d97 100644 --- a/coral/package.json +++ b/coral/package.json @@ -59,7 +59,7 @@ "remark-parse": "^11.0.0", "remark-rehype": "^11.1.0", "remark-stringify": "^11.0.0", - "unified": "^11.0.4", + "unified": "^11.0.5", "zod": "^3.23.8" }, "devDependencies": { @@ -99,7 +99,7 @@ "react-aria": "^3.33.1", "react-test-renderer": "^18.3.1", "rollup-plugin-visualizer": "^5.12.0", - "ts-jest": "^29.1.3", + "ts-jest": "^29.1.5", "ts-node": "^10.9.2", "typescript": "^5.4.5", "vite": "^4.5.3", diff --git a/coral/pnpm-lock.yaml b/coral/pnpm-lock.yaml index fc974c2f6d..8664929c2f 100644 --- a/coral/pnpm-lock.yaml +++ b/coral/pnpm-lock.yaml @@ -66,8 +66,8 @@ dependencies: specifier: ^11.0.0 version: 11.0.0 unified: - specifier: ^11.0.4 - version: 11.0.4 + specifier: ^11.0.5 + version: 11.0.5 yaml: specifier: 2.2.2 version: 2.2.2 @@ -185,8 +185,8 @@ devDependencies: specifier: ^5.12.0 version: 5.12.0 ts-jest: - specifier: ^29.1.3 - version: 29.1.3(@babel/core@7.23.5)(esbuild@0.18.17)(jest@29.7.0)(typescript@5.4.5) + specifier: ^29.1.5 + version: 29.1.5(@babel/core@7.23.5)(esbuild@0.18.17)(jest@29.7.0)(typescript@5.4.5) ts-node: specifier: ^10.9.2 version: 10.9.2(@types/node@20.12.12)(typescript@5.4.5) @@ -8145,7 +8145,7 @@ packages: whatwg-encoding: 2.0.0 whatwg-mimetype: 3.0.0 whatwg-url: 11.0.0 - ws: 8.13.0 + ws: 8.17.1 xml-name-validator: 4.0.0 transitivePeerDependencies: - bufferutil @@ -9526,7 +9526,7 @@ packages: react: 18.3.1 remark-parse: 11.0.0 remark-rehype: 11.1.0 - unified: 11.0.4 + unified: 11.0.5 unist-util-visit: 5.0.0 vfile: 6.0.1 transitivePeerDependencies: @@ -9800,7 +9800,7 @@ packages: dependencies: '@types/hast': 3.0.2 hast-util-from-html: 2.0.1 - unified: 11.0.4 + unified: 11.0.5 dev: false /rehype-remark@10.0.0: @@ -9809,7 +9809,7 @@ packages: '@types/hast': 3.0.2 '@types/mdast': 4.0.3 hast-util-to-mdast: 10.1.0 - unified: 11.0.4 + unified: 11.0.5 vfile: 6.0.1 dev: false @@ -9825,7 +9825,7 @@ packages: dependencies: '@types/hast': 3.0.2 hast-util-to-html: 9.0.0 - unified: 11.0.4 + unified: 11.0.5 dev: false /remark-parse@11.0.0: @@ -9834,7 +9834,7 @@ packages: '@types/mdast': 4.0.3 mdast-util-from-markdown: 2.0.0 micromark-util-types: 2.0.0 - unified: 11.0.4 + unified: 11.0.5 transitivePeerDependencies: - supports-color dev: false @@ -9845,7 +9845,7 @@ packages: '@types/hast': 3.0.2 '@types/mdast': 4.0.3 mdast-util-to-hast: 13.0.2 - unified: 11.0.4 + unified: 11.0.5 vfile: 6.0.1 dev: false @@ -9854,7 +9854,7 @@ packages: dependencies: '@types/mdast': 4.0.3 mdast-util-to-markdown: 2.1.0 - unified: 11.0.4 + unified: 11.0.5 dev: false /remove-accents@0.4.2: @@ -10514,8 +10514,8 @@ packages: engines: {node: '>=6.10'} dev: false - /ts-jest@29.1.3(@babel/core@7.23.5)(esbuild@0.18.17)(jest@29.7.0)(typescript@5.4.5): - resolution: {integrity: sha512-6L9qz3ginTd1NKhOxmkP0qU3FyKjj5CPoY+anszfVn6Pmv/RIKzhiMCsH7Yb7UvJR9I2A64rm4zQl531s2F1iw==} + /ts-jest@29.1.5(@babel/core@7.23.5)(esbuild@0.18.17)(jest@29.7.0)(typescript@5.4.5): + resolution: {integrity: sha512-UuClSYxM7byvvYfyWdFI+/2UxMmwNyJb0NPkZPQE2hew3RurV7l7zURgOHAd/1I1ZdPpe3GUsXNXAcN8TFKSIg==} engines: {node: ^14.15.0 || ^16.10.0 || ^18.0.0 || >=20.0.0} hasBin: true peerDependencies: @@ -10723,8 +10723,8 @@ packages: '@fastify/busboy': 2.0.0 dev: true - /unified@11.0.4: - resolution: {integrity: sha512-apMPnyLjAX+ty4OrNap7yumyVAMlKx5IWU2wlzzUdYJO9A8f1p9m/gywF/GM2ZDFcjQPrx59Mc90KwmxsoklxQ==} + /unified@11.0.5: + resolution: {integrity: sha512-xKvGhPWw3k84Qjh8bI3ZeJjqnyadK+GEFtazSfZv/rKeTkTjOJho6mFqh2SM96iIcZokxiOpg78GazTSg8+KHA==} dependencies: '@types/unist': 3.0.1 bail: 2.0.2 @@ -11204,8 +11204,8 @@ packages: signal-exit: 3.0.7 dev: true - /ws@8.13.0: - resolution: {integrity: sha512-x9vcZYTrFPC7aSIbj7sRCYo7L/Xb8Iy+pW0ng0wt2vCJv7M9HOMy0UoN3rr+IFC7hb7vXoqS+P9ktyLLLhO+LA==} + /ws@8.17.1: + resolution: {integrity: sha512-6XQFvXTkbfUOZOKKILFG1PDK2NDQs4azKQl26T0YS5CxqWLgXajbPZ+h4gZekJyRqFU8pvnbAbbs/3TgRPy+GQ==} engines: {node: '>=10.0.0'} peerDependencies: bufferutil: ^4.0.1 diff --git a/core/src/main/resources/static/js/envs.js b/core/src/main/resources/static/js/envs.js index 6a1728f0d0..80e59ead3d 100644 --- a/core/src/main/resources/static/js/envs.js +++ b/core/src/main/resources/static/js/envs.js @@ -705,6 +705,11 @@ app.controller("envsCtrl", function($scope, $http, $location, $window) { $scope.addNewEnv = function() { + var defRepFactor = parseInt($scope.addNewEnv.defrepfctr,10) + var maxRepFactor = parseInt($scope.addNewEnv.maxrepfctr,10) + var defPartitions = parseInt($scope.addNewEnv.defparts,10) + var maxPartitions = parseInt($scope.addNewEnv.defmaxparts,10) + // Validation partitions if($scope.addNewEnv.defparts.length<=0 || $scope.addNewEnv.defparts<=0) { @@ -713,7 +718,7 @@ app.controller("envsCtrl", function($scope, $http, $location, $window) { return; } - if(isNaN($scope.addNewEnv.defparts)){ + if(isNaN(defPartitions)){ $scope.alertnote = "Default partitions should be a valid number"; $scope.showAlertToast(); return; @@ -726,13 +731,13 @@ app.controller("envsCtrl", function($scope, $http, $location, $window) { return; } - if(isNaN($scope.addNewEnv.defmaxparts)){ + if(isNaN(maxPartitions)){ $scope.alertnote = "Maximum partitions should be a valid number"; $scope.showAlertToast(); return; } - if($scope.addNewEnv.defparts > $scope.addNewEnv.defmaxparts){ + if(defPartitions > maxPartitions){ $scope.alertnote = "Default partitions should be less than Maximum partitions"; $scope.showAlertToast(); return; @@ -747,26 +752,26 @@ app.controller("envsCtrl", function($scope, $http, $location, $window) { return; } - if(isNaN($scope.addNewEnv.defrepfctr)){ + if(isNaN(defRepFactor)){ $scope.alertnote = "Default replication factor should be a valid number"; $scope.showAlertToast(); return; } - if($scope.addNewEnv.maxrepfctr.length<=0 || $scope.addNewEnv.maxrepfctr<=0) + if(defRepFactor<=0 || maxRepFactor<=0) { $scope.alertnote = "Maximum Replication factor should not be empty and should be greater than 0"; $scope.showAlertToast(); return; } - if(isNaN($scope.addNewEnv.maxrepfctr)){ + if(isNaN(maxRepFactor)){ $scope.alertnote = "Maximum Replication factor should be a valid number"; $scope.showAlertToast(); return; } - if($scope.addNewEnv.defrepfctr > $scope.addNewEnv.maxrepfctr){ + if(defRepFactor > maxRepFactor){ $scope.alertnote = "Default Replication factor should be less than Maximum Replication factor"; $scope.showAlertToast(); return; diff --git a/core/src/test/java/io/aiven/klaw/UtilMethods.java b/core/src/test/java/io/aiven/klaw/UtilMethods.java index 3ff154c421..566641faad 100644 --- a/core/src/test/java/io/aiven/klaw/UtilMethods.java +++ b/core/src/test/java/io/aiven/klaw/UtilMethods.java @@ -172,6 +172,20 @@ public List getUserInfoList(String username, String role) { return userInfoList; } + public List getUserInfoList(int count, String usernamePrefix) { + List userInfoList = new ArrayList<>(); + for (int i = 0; i < count; i++) { + UserInfo userInfo = new UserInfo(); + userInfo.setTeamId(i % 2); + userInfo.setSwitchAllowedTeamIds(Set.of((i + 1) % 2)); + userInfo.setSwitchTeams(true); + userInfo.setUsername(usernamePrefix + i); + userInfoList.add(userInfo); + } + + return userInfoList; + } + public List getUserInfoListModel(String username, String role) { List userInfoList = new ArrayList<>(); UserInfoModelResponse userInfo = new UserInfoModelResponse(); diff --git a/core/src/test/java/io/aiven/klaw/service/UsersTeamsControllerServiceTest.java b/core/src/test/java/io/aiven/klaw/service/UsersTeamsControllerServiceTest.java index 7e68c65e6d..e13015e2c6 100644 --- a/core/src/test/java/io/aiven/klaw/service/UsersTeamsControllerServiceTest.java +++ b/core/src/test/java/io/aiven/klaw/service/UsersTeamsControllerServiceTest.java @@ -41,11 +41,13 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.NoSuchElementException; import java.util.Objects; +import java.util.Set; import java.util.UUID; import java.util.stream.Stream; import org.junit.jupiter.api.Assertions; @@ -532,10 +534,178 @@ public void updateTeam() {} public void changePwd() {} @Test - public void showUsers() {} + public void showUsersWithNoFilterAndSwitchEnabled() { + List userInfosMock = showUsersSetupTest("testuser", 2, 0, "Octopus", 1, "Seahorses"); + + List userInfos = usersTeamsControllerService.showUsers(null, null, "1"); + + assertThat(userInfos.size()).isEqualTo(2); + showUsersValidateUserInfo(userInfos.get(0), userInfosMock.get(0).getTeamId(), "Seahorses"); + showUsersValidateUserInfo(userInfos.get(1), userInfosMock.get(1).getTeamId(), "Octopus"); + } + + @Test + public void showUsersWithNoFilterAndOneSwitchDisable() { + List userInfosMock = showUsersSetupTest("testuser", 2, 0, "Octopus", 1, "Seahorses"); + userInfosMock.get(1).setSwitchTeams(false); + + List userInfos = usersTeamsControllerService.showUsers(null, null, "1"); + + assertThat(userInfos.size()).isEqualTo(2); + showUsersValidateUserInfo(userInfos.get(0), userInfosMock.get(0).getTeamId(), "Seahorses"); + assertThat(userInfos.get(1).getTeamId()).isEqualTo(userInfosMock.get(1).getTeamId()); + assertThat(userInfos.get(1).getSwitchAllowedTeamNames()).isNull(); + } + + @Test + public void showUsersWithNoFilterAndSwitchTeamExistsButUserNotMember() { + List userInfosMock = + showUsersSetupTest( + "testuser", 1, 1, "Seahorses", Integer.MAX_VALUE, "Not Member of this Team"); + + List userInfos = usersTeamsControllerService.showUsers(null, null, "1"); + + assertThat(userInfos.size()).isEqualTo(1); + showUsersValidateUserInfo(userInfos.get(0), userInfosMock.get(0).getTeamId(), "Seahorses"); + } + + @Test + public void showUsersWithNoFilterAndSwitchToTeamThatDoesNotExist() { + String userName = "testuser"; + List userInfosMock = utilMethods.getUserInfoList(1, "testUser"); + + when(mailService.getUserName(any())).thenReturn(userName); + when(commonUtilsService.getTenantId(userName)).thenReturn(TEST_TENANT_ID); + when(handleDbRequests.getAllUsersInfo(TEST_TENANT_ID)).thenReturn(userInfosMock); + + List userInfos = usersTeamsControllerService.showUsers(null, null, "1"); + + assertThat(userInfos.size()).isEqualTo(1); + showUsersValidateUserInfo(userInfos.get(0), userInfosMock.get(0).getTeamId(), null); + } + + @Test + public void showUsersWithValidTeamIdFilter() { + int filterUserIndex = 1; + List userInfosMock = showUsersSetupTest("testuser", 2, 0, "Octopus", 1, "Seahorses"); + + List userInfos = + usersTeamsControllerService.showUsers(filterUserIndex, null, "1"); + + assertThat(userInfos.size()).isEqualTo(1); + showUsersValidateUserInfo( + userInfos.get(0), userInfosMock.get(filterUserIndex).getTeamId(), "Octopus"); + } @Test - public void getMyProfileInfo() {} + public void showUsersWithInValidTeamIdFilter() { + showUsersSetupTest("testuser", 2, 0, "Octopus", 1, "Seahorses"); + + List userInfos = + usersTeamsControllerService.showUsers(Integer.MAX_VALUE, null, "1"); + + assertThat(userInfos.size()).isEqualTo(0); + } + + @Test + public void showUsersWithValidUserSearchStrFilter() { + List userInfosMock = showUsersSetupTest("testuser", 2, 0, "Octopus", 1, "Seahorses"); + + List userInfos = usersTeamsControllerService.showUsers(null, "0", "1"); + + assertThat(userInfos.size()).isEqualTo(1); + showUsersValidateUserInfo(userInfos.get(0), userInfosMock.get(0).getTeamId(), "Seahorses"); + } + + @Test + public void showUsersWithInValidUserSearchStrFilter() { + showUsersSetupTest("testuser", 2, 0, "Octopus", 1, "Seahorses"); + + List userInfos = + usersTeamsControllerService.showUsers(null, "invalid", "1"); + + assertThat(userInfos.size()).isEqualTo(0); + } + + @Test + public void showUsersWithMultiplePages() { + String userName = "testuser"; + List userInfosMock = utilMethods.getUserInfoList(30, "testUser"); + + when(mailService.getUserName(any())).thenReturn(userName); + when(commonUtilsService.getTenantId(userName)).thenReturn(TEST_TENANT_ID); + when(handleDbRequests.getAllUsersInfo(TEST_TENANT_ID)).thenReturn(userInfosMock); + + List userInfos = usersTeamsControllerService.showUsers(null, null, "1"); + assertThat(userInfos.size()).isEqualTo(20); + + userInfos = usersTeamsControllerService.showUsers(null, null, "2"); + assertThat(userInfos.size()).isEqualTo(10); + + userInfos = usersTeamsControllerService.showUsers(null, null, "3"); + assertThat(userInfos.size()).isEqualTo(0); + } + + @Test + public void getMyProfileInfoWithValidSwitchTeam() { + String userTeamName = "Seahorse"; + String switchTeamName = "Octopus"; + getMyProfileInfoSetupTest(true, userTeamName, switchTeamName); + + UserInfoModelResponse userInfoModelResponse = usersTeamsControllerService.getMyProfileInfo(); + + getMyProfileInfoVerifyUserInfoModelResponse(userInfoModelResponse, userTeamName); + getMyProfileInfoVerifySwitchTeam(userInfoModelResponse, switchTeamName); + } + + @Test + public void getMyProfileInfoWithSwitchTeamDisabled() { + String userTeamName = "Seahorse"; + getMyProfileInfoSetupTest(false, userTeamName, null); + + UserInfoModelResponse userInfoModelResponse = usersTeamsControllerService.getMyProfileInfo(); + + getMyProfileInfoVerifyUserInfoModelResponse(userInfoModelResponse, userTeamName); + assertThat(userInfoModelResponse.isSwitchTeams()).isFalse(); + assertThat(userInfoModelResponse.getSwitchAllowedTeamNames()).isNull(); + + verify(manageDatabase, never()) + .getTeamNameFromTeamId(userInfo.getTenantId(), userInfo.getTeamId() - 1); + } + + @Test + public void getMyProfileInfoWithSwitchTeamExistsButUserNotMember() { + String userTeamName = "Seahorse"; + String switchTeamName = "Octopus"; + getMyProfileInfoSetupTest(true, userTeamName, switchTeamName); + + when(manageDatabase.getTeamNameFromTeamId(userInfo.getTenantId(), Integer.MAX_VALUE)) + .thenReturn("User not member of this team"); + + UserInfoModelResponse userInfoModelResponse = usersTeamsControllerService.getMyProfileInfo(); + + getMyProfileInfoVerifyUserInfoModelResponse(userInfoModelResponse, userTeamName); + getMyProfileInfoVerifySwitchTeam(userInfoModelResponse, switchTeamName); + } + + @Test + public void getMyProfileInfoWithSwitchToTeamThatDoesNotExist() { + String userTeamName = "Seahorse"; + String userName = "testUser"; + userInfo.setSwitchAllowedTeamIds(Set.of(userInfo.getTeamId() - 1)); + userInfo.setSwitchTeams(true); + + when(mailService.getUserName(any())).thenReturn(userName); + when(handleDbRequests.getUsersInfo(userName)).thenReturn(userInfo); + when(commonUtilsService.getTenantId(userName)).thenReturn(userInfo.getTenantId()); + when(manageDatabase.getTeamNameFromTeamId(userInfo.getTenantId(), userInfo.getTeamId())) + .thenReturn("Seahorse"); + + UserInfoModelResponse userInfoModelResponse = usersTeamsControllerService.getMyProfileInfo(); + + getMyProfileInfoVerifyUserInfoModelResponse(userInfoModelResponse, userTeamName); + getMyProfileInfoVerifySwitchTeam(userInfoModelResponse, null); + } @Test public void addTwoDefaultTeamsSuccess() throws KlawException { @@ -591,6 +761,122 @@ public void addTwoDefaultTeamsWithApiFailureForSecondTeam() throws KlawException "failure"); } + @Test + public void updateUserTeamFromSwitchTeamsSuccess() { + UserInfoModel userInfoModel = updateUserTeamFromSwitchTeamsSetupTest(); + + when(handleDbRequests.updateUserTeam(userInfoModel.getUsername(), userInfoModel.getTeamId())) + .thenReturn(ApiResultStatus.SUCCESS.value); + + ApiResponse apiResponse = + usersTeamsControllerService.updateUserTeamFromSwitchTeams(userInfoModel); + + assertThat(apiResponse.isSuccess()).isTrue(); + assertThat(apiResponse.getMessage()).isEqualTo(ApiResultStatus.SUCCESS.value); + verify(manageDatabase).loadUsersForAllTenants(); + } + + @Test + public void updateUserTeamFromSwitchTeamsWithDBApiFailure() { + UserInfoModel userInfoModel = updateUserTeamFromSwitchTeamsSetupTest(); + + when(handleDbRequests.updateUserTeam(userInfoModel.getUsername(), userInfoModel.getTeamId())) + .thenReturn(ApiResultStatus.FAILURE.value); + + ApiResponse apiResponse = + usersTeamsControllerService.updateUserTeamFromSwitchTeams(userInfoModel); + + assertThat(apiResponse.isSuccess()).isFalse(); + assertThat(apiResponse.getMessage()).isEqualTo(ApiResultStatus.FAILURE.value); + verify(manageDatabase, never()).loadUsersForAllTenants(); + } + + @Test + public void updateUserTeamFromSwitchTeamsWithDBExceptionFailure() { + UserInfoModel userInfoModel = updateUserTeamFromSwitchTeamsSetupTest(); + String errorMessage = "Failure Team doesn't exist"; + + when(handleDbRequests.updateUserTeam(userInfoModel.getUsername(), userInfoModel.getTeamId())) + .thenThrow(new RuntimeException(errorMessage)); + + assertThatExceptionOfType(RuntimeException.class) + .isThrownBy(() -> usersTeamsControllerService.updateUserTeamFromSwitchTeams(userInfoModel)) + .withMessage(errorMessage); + + verify(manageDatabase, never()).loadUsersForAllTenants(); + } + + @ParameterizedTest + @MethodSource + public void updateUserTeamFromSwitchTeamsWithUnAuthorizedUserFailure( + boolean setSwitchTeams, Set teamIds, Integer userProfileTeamId) { + UserInfoModel userInfoModel = utilMethods.getUserInfoMock(); + userInfoModel.setTeamId(userProfileTeamId); + + userInfo.setSwitchTeams(setSwitchTeams); + userInfo.setSwitchAllowedTeamIds(teamIds); + + when(handleDbRequests.getUsersInfo(userInfoModel.getUsername())).thenReturn(userInfo); + + ApiResponse apiResponse = + usersTeamsControllerService.updateUserTeamFromSwitchTeams(userInfoModel); + + assertThat(apiResponse.isSuccess()).isFalse(); + assertThat(apiResponse.getMessage()).isEqualTo(ApiResponse.NOT_AUTHORIZED.getMessage()); + verify(handleDbRequests, never()).updateUserTeam(anyString(), anyInt()); + } + + public static Stream updateUserTeamFromSwitchTeamsWithUnAuthorizedUserFailure() { + return Stream.of( + Arguments.of(false, Set.of(1, 2), 1), + Arguments.of(true, null, 1), + Arguments.of(true, Collections.emptySet(), 1), + Arguments.of(true, Set.of(1, 2), 3)); + } + + @ParameterizedTest + @MethodSource + public void getSwitchTeams(String userId, Set teamIds, boolean setSwitchTeams) { + String testUserName = "testUserName"; + + userInfo.setSwitchTeams(setSwitchTeams); + userInfo.setSwitchAllowedTeamIds(teamIds); + + when(handleDbRequests.getUsersInfo(userId)).thenReturn(userInfo); + when(mailService.getUserName(any())).thenReturn(testUserName); + when(commonUtilsService.getTenantId(testUserName)).thenReturn(TEST_TENANT_ID); + for (Integer teamId : teamIds) { + when(manageDatabase.getTeamNameFromTeamId(TEST_TENANT_ID, teamId)) + .thenReturn(String.format("team%d", teamId)); + } + + List responses = usersTeamsControllerService.getSwitchTeams(userId); + + if (userId.equals("testuser")) { + assertThat(responses).isEmpty(); + } else { + assertThat(responses).isNotEmpty(); + assertThat(responses.size()).isEqualTo(teamIds.size()); + + responses.sort(Comparator.comparing(TeamModelResponse::getTeamId)); + + assertThat(responses.get(0).getTeamId()).isEqualTo(1); + assertThat(responses.get(0).getTeamname()).isEqualTo("team1"); + + if (userId.equals("testuser2")) { + assertThat(responses.get(1).getTeamId()).isEqualTo(2); + assertThat(responses.get(1).getTeamname()).isEqualTo("team2"); + } + } + } + + public static Stream getSwitchTeams() { + return Stream.of( + Arguments.of("testuser", Collections.emptySet(), false), + Arguments.of("testuser1", Set.of(1), true), + Arguments.of("testuser2", Set.of(1, 2), true)); + } + @ParameterizedTest @MethodSource public void registerUserInternal( @@ -809,4 +1095,74 @@ private void addNewTeamVerifyCapturedTeam(TeamModel teamModel) { assertThat(capturedTeam.getTeamId()).isEqualTo(teamModel.getTeamId()); assertThat(capturedTeam.getTenantId()).isEqualTo(TEST_TENANT_ID); } + + private List showUsersSetupTest( + String userName, + int totalMocks, + int switchTeam1ID, + String switchTeam1Name, + int switchTeam2ID, + String switchTeam2Name) { + List userInfosMock = utilMethods.getUserInfoList(totalMocks, userName); + + when(mailService.getUserName(any())).thenReturn(userName); + when(commonUtilsService.getTenantId(userName)).thenReturn(TEST_TENANT_ID); + when(handleDbRequests.getAllUsersInfo(TEST_TENANT_ID)).thenReturn(userInfosMock); + when(manageDatabase.getTeamNameFromTeamId(TEST_TENANT_ID, switchTeam1ID)) + .thenReturn(switchTeam1Name); + when(manageDatabase.getTeamNameFromTeamId(TEST_TENANT_ID, switchTeam2ID)) + .thenReturn(switchTeam2Name); + + return userInfosMock; + } + + private void showUsersValidateUserInfo( + UserInfoModelResponse userInfoResponse, int teamId, String switchTeamName) { + assertThat(userInfoResponse.getTeamId()).isEqualTo(teamId); + assertThat(userInfoResponse.getSwitchAllowedTeamNames().size()).isEqualTo(1); + assertThat(userInfoResponse.getSwitchAllowedTeamNames().contains(switchTeamName)).isTrue(); + } + + private UserInfoModel updateUserTeamFromSwitchTeamsSetupTest() { + UserInfoModel userInfoModel = utilMethods.getUserInfoMock(); + + userInfo.setSwitchTeams(true); + userInfo.setSwitchAllowedTeamIds(Set.of(userInfoModel.getTeamId())); + + when(handleDbRequests.getUsersInfo(userInfoModel.getUsername())).thenReturn(userInfo); + + return userInfoModel; + } + + private void getMyProfileInfoSetupTest( + boolean switchTeamsEnabled, String userTeamName, String switchTeamName) { + userInfo.setSwitchAllowedTeamIds(Set.of(userInfo.getTeamId() - 1)); + userInfo.setSwitchTeams(switchTeamsEnabled); + + when(mailService.getUserName(any())).thenReturn("testuser"); + when(handleDbRequests.getUsersInfo("testuser")).thenReturn(userInfo); + when(commonUtilsService.getTenantId("testuser")).thenReturn(userInfo.getTenantId()); + when(manageDatabase.getTeamNameFromTeamId(userInfo.getTenantId(), userInfo.getTeamId())) + .thenReturn("Seahorse"); + if (switchTeamsEnabled) { + when(manageDatabase.getTeamNameFromTeamId(userInfo.getTenantId(), userInfo.getTeamId() - 1)) + .thenReturn("Octopus"); + } + } + + private void getMyProfileInfoVerifyUserInfoModelResponse( + UserInfoModelResponse userInfoModelResponse, String userTeamName) { + assertThat(userInfoModelResponse.getTeamId()).isEqualTo(userInfo.getTeamId()); + assertThat(userInfoModelResponse.getRole()).isEqualTo(userInfo.getRole()); + assertThat(userInfoModelResponse.getTenantId()).isEqualTo(userInfo.getTenantId()); + assertThat(userInfoModelResponse.getUsername()).isEqualTo(userInfo.getUsername()); + assertThat(userInfoModelResponse.getTeam()).isEqualTo(userTeamName); + } + + private void getMyProfileInfoVerifySwitchTeam( + UserInfoModelResponse userInfoModelResponse, String switchTeamName) { + assertThat(userInfoModelResponse.isSwitchTeams()).isTrue(); + assertThat(userInfoModelResponse.getSwitchAllowedTeamNames().size()).isEqualTo(1); + assertThat(userInfoModelResponse.getSwitchAllowedTeamNames().contains(switchTeamName)).isTrue(); + } }