SelfSupportService { public ResponseEntity<String> getSafes(UserDetails userDetails, String userToken) { oidcUtil.renewUserToken(userDetails.getClientToken()); String token = userDetails.getClientToken(); if (!userDetails.isAdmin()) { token = userDetails.getSelfSupportToken(); } String[] policies = policyUtils.getCurrentPolicies(token, userDetails.getUsername(), userDetails); policies = filterPoliciesBasedOnPrecedence(Arrays.asList(policies)); List<Map<String, String>> safeListUsers = new ArrayList<>(); List<Map<String, String>> safeListShared = new ArrayList<>(); List<Map<String, String>> safeListApps = new ArrayList<>(); Map<String, List<Map<String, String>>> safeList = new HashMap<>(); if (policies != null) { for (String policy: policies) { Map<String, String> safePolicy = new HashMap<>(); String[] _policies = policy.split("_", -1); if (_policies.length >= 3) { String[] policyName = Arrays.copyOfRange(_policies, 2, _policies.length); String safeName = String.join("_", policyName); String safeType = _policies[1]; if (policy.startsWith("r_")) { safePolicy.put(safeName, "read"); } else if (policy.startsWith("w_")) { safePolicy.put(safeName, "write"); } else if (policy.startsWith("d_")) { safePolicy.put(safeName, "deny"); } if (!safePolicy.isEmpty()) { if (safeType.equals(TVaultConstants.USERS)) { safeListUsers.add(safePolicy); } else if (safeType.equals(TVaultConstants.SHARED)) { safeListShared.add(safePolicy); } else if (safeType.equals(TVaultConstants.APPS)) { safeListApps.add(safePolicy); } } } } safeList.put(TVaultConstants.USERS, safeListUsers); safeList.put(TVaultConstants.SHARED, safeListShared); safeList.put(TVaultConstants.APPS, safeListApps); } return ResponseEntity.status(HttpStatus.OK).body(JSONUtil.getJSON(safeList)); }  ResponseEntity<String> createSafe(UserDetails userDetails, String userToken, Safe safe); ResponseEntity<String> addUserToSafe(UserDetails userDetails, String userToken, SafeUser safeUser); ResponseEntity<String> getInfo(UserDetails userDetails, String userToken, String path); ResponseEntity<String> getSafe(UserDetails userDetails, String userToken, String path); ResponseEntity<String> removeUserFromSafe(UserDetails userDetails, String userToken, SafeUser safeUser); ResponseEntity<String> getFoldersRecursively(UserDetails userDetails, String userToken, String path); ResponseEntity<String> isAuthorized(UserDetails userDetails, String path); ResponseEntity<String> updateSafe(UserDetails userDetails, String userToken, Safe safe); ResponseEntity<String> deletefolder(UserDetails userDetails, String userToken, String path); ResponseEntity<String> addGroupToSafe(UserDetails userDetails, String userToken, SafeGroup safeGroup); ResponseEntity<String> removeGroupFromSafe(UserDetails userDetails, String userToken, SafeGroup safeGroup); ResponseEntity<String> addAwsRoleToSafe(UserDetails userDetails, String userToken, AWSRole awsRole); ResponseEntity<String> removeAWSRoleFromSafe(UserDetails userDetails, String userToken, AWSRole awsRole, boolean detachOnly); ResponseEntity<String> associateApproletoSDB(UserDetails userDetails, String userToken, SafeAppRoleAccess safeAppRoleAccess); ResponseEntity<String> deleteApproleFromSDB(UserDetails userDetails, String userToken, SafeAppRoleAccess safeAppRoleAccess); ResponseEntity<String> createRole(UserDetails userDetails, String userToken, AWSLoginRole awsLoginRole, String path); ResponseEntity<String> updateRole(UserDetails userDetails, String userToken, AWSLoginRole awsLoginRole, String path); ResponseEntity<String> createIAMRole(UserDetails userDetails, String userToken, AWSIAMRole awsiamRole, String path); ResponseEntity<String> updateIAMRole(UserDetails userDetails, String userToken, AWSIAMRole awsiamRole, String path); ResponseEntity<String> getAllSafeNames(UserDetails userDetails); ResponseEntity<String> createAppRole(String userToken, AppRole appRole, UserDetails userDetails); ResponseEntity<String> deleteAppRole(String userToken, AppRole appRole, UserDetails userDetails); ResponseEntity<String> getSafes(UserDetails userDetails, String userToken); ResponseEntity<String> readAppRoles(String userToken, UserDetails userDetails); ResponseEntity<String> listAppRoles(String userToken, UserDetails userDetails); ResponseEntity<String> readAppRole(String userToken, String rolename, UserDetails userDetails); ResponseEntity<String> readAppRoleRoleId(String userToken, String rolename, UserDetails userDetails); ResponseEntity<String> readAppRoleSecretId(String userToken, String rolename, UserDetails userDetails); ResponseEntity<String> readAppRoleDetails(String userToken, String rolename, UserDetails userDetails); ResponseEntity<String> readSecretIdAccessors(String userToken, String rolename, UserDetails userDetails); ResponseEntity<String> deleteSecretIds(String userToken, AppRoleAccessorIds appRoleAccessorIds, UserDetails userDetails); ResponseEntity<String> updateAppRole(String userToken, AppRole appRole, UserDetails userDetails);  }
@Test public void test_getSafes_successfully() { String token = "5PDrOhsy4ig8L3EpsJZSLAMg"; UserDetails userDetails = getMockUser(false); String [] policies = {"r_users_s1", "w_users_s2", "r_shared_s3", "w_shared_s4", "r_apps_s5", "w_apps_s6", "d_apps_s7"}; ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.OK).body("{\"shared\":[{\"s3\":\"read\"},{\"s4\":\"write\"}],\"users\":[{\"s1\":\"read\"},{\"s2\":\"write\"}],\"apps\":[{\"s5\":\"read\"},{\"s6\":\"write\"},{\"s7\":\"deny\"}]}"); when(policyUtils.getCurrentPolicies(token, userDetails.getUsername(), userDetails)).thenReturn(policies); when(JSONUtil.getJSON(Mockito.any())).thenReturn("{\"shared\":[{\"s3\":\"read\"},{\"s4\":\"write\"}],\"users\":[{\"s1\":\"read\"},{\"s2\":\"write\"}],\"apps\":[{\"s5\":\"read\"},{\"s6\":\"write\"},{\"s7\":\"deny\"}]}"); ResponseEntity<String> responseEntity = selfSupportService.getSafes(userDetails, token); assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); assertEquals(responseEntityExpected, responseEntity); }