AppRoleService { public ResponseEntity<String> listAppRoles(String token, UserDetails userDetails) { log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "listAppRoles"). put(LogMessage.MESSAGE, String.format("Trying to get list of AppRole")). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); String _path = TVaultConstants.APPROLE_USERS_METADATA_MOUNT_PATH + "/" + userDetails.getUsername(); Response response = null; if (userDetails.isAdmin()) { response = reqProcessor.process("/auth/approle/role/list","{\"path\":\""+_path+"\"}",token); } else { response = reqProcessor.process("/auth/approles/rolesbyuser/list","{\"path\":\""+_path+"\"}",userDetails.getSelfSupportToken()); } log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "listAppRoles"). put(LogMessage.MESSAGE, "Reading List of AppRoles completed"). put(LogMessage.STATUS, response.getHttpstatus().toString()). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); if (HttpStatus.OK.equals(response.getHttpstatus())) { if (TVaultConstants.HIDEMASTERAPPROLE) { response = ControllerUtil.hideMasterAppRoleFromResponse(response); } return ResponseEntity.status(response.getHttpstatus()).body(response.getResponse()); } else if (HttpStatus.NOT_FOUND.equals(response.getHttpstatus())) { return ResponseEntity.status(HttpStatus.OK).body("{\"keys\":[]}"); } return ResponseEntity.status(response.getHttpstatus()).body(response.getResponse()); }  ResponseEntity<String> createAppRole(String token, AppRole appRole, UserDetails userDetails); ResponseEntity<String> readAppRole(String token, String rolename); ResponseEntity<String> readAppRoles(String token); ResponseEntity<String> listAppRoles(String token,  UserDetails userDetails); ResponseEntity<String> readAppRoleRoleId(String token, String rolename); String readRoleId(String token, String rolename); List<String> readAccessorIds(String token, String rolename); AppRoleMetadata readAppRoleMetadata(String token, String rolename); AppRole readAppRoleBasicDetails(String token, String rolename); ResponseEntity<String> readAppRoleRoleId(String token, String rolename, UserDetails userDetails); ResponseEntity<String> readAppRoleSecretId(String token, String rolename, UserDetails userDetails); ResponseEntity<String> readSecretIdAccessors(String token, String rolename, UserDetails userDetails); ResponseEntity<String> readAppRoleDetails(String token, String rolename, UserDetails userDetails); ResponseEntity<String> createsecretId(String token, AppRoleSecretData appRoleSecretData); ResponseEntity<String> readAppRoleSecretId(String token, String rolename); ResponseEntity<String> deleteSecretId(String token, AppRoleNameSecretId appRoleNameSecretId); ResponseEntity<String> deleteAppRole(String token, AppRole appRole, UserDetails userDetails); ResponseEntity<String> deleteSecretIds(String token, AppRoleAccessorIds appRoleAccessorIds, UserDetails userDetails); ResponseEntity<String> login(AppRoleIdSecretId appRoleIdSecretId); ResponseEntity<String> associateApprole(String token, SafeAppRoleAccess safeAppRoleAccess); ResponseEntity<String> updateAppRole(String token, AppRole appRole, UserDetails userDetails); Response configureApprole(String rolename,String policies,String token );  }
@Test public void test_listAppRoles_successfully() { String token = "5PDrOhsy4ig8L3EpsJZSLAMg"; String responseJson = "{\r\n" + " \"keys\": [\r\n" + " \"testapprole01\"\r\n" + " ]\r\n" + "}"; ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.OK).body(responseJson); UserDetails userDetails = getMockUser("testuser1", false); Response response =getMockResponse(HttpStatus.OK, true, responseJson); Response responseAfterHide = response; String _path = "metadata/approle_users/" + userDetails.getUsername(); String jsonStr = "{\"path\":\""+_path+"\"}"; when(reqProcessor.process("/auth/approles/rolesbyuser/list", jsonStr,userDetails.getSelfSupportToken())).thenReturn(response); when(ControllerUtil.hideMasterAppRoleFromResponse(Mockito.any())).thenReturn(responseAfterHide); ResponseEntity<String> responseEntityActual = appRoleService.listAppRoles(token, userDetails); assertEquals(HttpStatus.OK, responseEntityActual.getStatusCode()); assertEquals(responseEntityExpected, responseEntityActual); }
@Test public void test_listAppRoles_failure() { String token = "5PDrOhsy4ig8L3EpsJZSLAMg"; String responseJson = "{\r\n" + " \"error\": \r\n" + " \"\"\r\n" + " \r\n" + "}"; ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.BAD_REQUEST).body(responseJson); UserDetails userDetails = getMockUser("testuser1", false); Response response =getMockResponse(HttpStatus.BAD_REQUEST, true, responseJson); String _path = "metadata/approle_users/" + userDetails.getUsername(); String jsonStr = "{\"path\":\""+_path+"\"}"; when(reqProcessor.process("/auth/approles/rolesbyuser/list", jsonStr,userDetails.getSelfSupportToken())).thenReturn(response); when(ControllerUtil.hideMasterAppRoleFromResponse(Mockito.any())).thenReturn(response); ResponseEntity<String> responseEntityActual = appRoleService.listAppRoles(token, userDetails); assertEquals(HttpStatus.BAD_REQUEST, responseEntityActual.getStatusCode()); assertEquals(responseEntityExpected, responseEntityActual); }