SelfSupportService { public ResponseEntity<String> getSafe(UserDetails userDetails, String userToken, String path) { String token = userDetails.getClientToken(); if (userDetails.isAdmin()) { return safesService.getSafe(token, path); } else { String safeType = ControllerUtil.getSafeType(path); String safeName = ControllerUtil.getSafeName(path); if (StringUtils.isEmpty(safeType) || StringUtils.isEmpty(safeName)) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Invalid path specified\"]}"); } String powerToken = userDetails.getSelfSupportToken(); String username = userDetails.getUsername(); Safe safeMetaData = safeUtils.getSafeMetaData(powerToken, safeType, safeName); String[] latestPolicies = policyUtils.getCurrentPolicies(powerToken, username, userDetails); ArrayList<String> policiesTobeChecked = policyUtils.getPoliciesTobeCheked(safeType, safeName); boolean isAuthorized = authorizationUtils.isAuthorized(userDetails, safeMetaData, latestPolicies, policiesTobeChecked, false); if (isAuthorized) { token = userDetails.getSelfSupportToken(); return safesService.getSafe(token, path); } else { return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("{\"errors\":[\"Access denied: no permission to read this safe information\"]}"); } } }  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_getSafe_successfully_admin() { String path = "shared/mysafe01"; String token = "5PDrOhsy4ig8L3EpsJZSLAMg"; UserDetails userDetails = getMockUser(true); String responseJson = "{ \"keys\": [ \"mysafe01\" ]}"; ResponseEntity<String> response = ResponseEntity.status(HttpStatus.OK).body(responseJson); ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.OK).body(responseJson); when(safesService.getSafe(token, path)).thenReturn(response); ResponseEntity<String> responseEntity = selfSupportService.getSafe(userDetails, token, path); assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); assertEquals(responseEntityExpected, responseEntity); }
@Test public void test_getSafe_failure_400() { String path = "shared/mysafe01"; String token = "5PDrOhsy4ig8L3EpsJZSLAMg"; UserDetails userDetails = getMockUser(false); String responseJson = "{\"errors\":[\"Invalid path specified\"]}"; ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.BAD_REQUEST).body(responseJson); when(ControllerUtil.getSafeType(path)).thenReturn(""); when(ControllerUtil.getSafeName(path)).thenReturn("mysafe01"); ResponseEntity<String> responseEntity = selfSupportService.getSafe(userDetails, token, path); assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode()); assertEquals(responseEntityExpected, responseEntity); }