AppRoleService { public ResponseEntity<String> updateAppRole(String token, AppRole appRole, UserDetails userDetails){ if (Objects.isNull(appRole) || StringUtils.isEmpty(appRole.getRole_name()) ) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "updateAppRole"). put(LogMessage.MESSAGE, "Not enough information to update AppRole"). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"AppRole can't be updated since insufficient information has been provided.\"]}"); } String rolename = appRole.getRole_name(); if (rolename.equals(TVaultConstants.SELF_SERVICE_APPROLE_NAME)) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Access denied: You don't have enough permission to modify the AppRole information\"]}"); } log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "updateAppRole"). put(LogMessage.MESSAGE, String.format("Trying to update AppRole [%s]", rolename)). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); if (!userDetails.isAdmin()) { token = userDetails.getSelfSupportToken(); } AppRole existingAppRole = readAppRoleBasicDetails(token, rolename); if (existingAppRole == null) { log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "updateAppRole"). put(LogMessage.MESSAGE, String.format("Unable to read AppRole information. AppRole [%s] doesn't exist", rolename)). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(HttpStatus.OK).body("{\"errors\":[\"AppRole doesn't exist\"]}"); } appRole.setPolicies(existingAppRole.getPolicies()); appRole.setBind_secret_id(existingAppRole.isBind_secret_id()); String jsonStr = JSONUtil.getJSON(appRole); Response response = reqProcessor.process("/auth/approle/role/create", jsonStr,token); if(response.getHttpstatus().equals(HttpStatus.NO_CONTENT) || response.getHttpstatus().equals(HttpStatus.OK)) { log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "updateAppRole"). put(LogMessage.MESSAGE, "AppRole updated successfully"). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(HttpStatus.OK).body("{\"messages\":[\"AppRole updated successfully.\"]}"); } log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "updateAppRole"). put(LogMessage.MESSAGE, "Update of AppRole failed"). put(LogMessage.RESPONSE, response.getResponse()). put(LogMessage.STATUS, response.getHttpstatus().toString()). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); 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_updateAppRole_successfully() throws Exception{ Response response =getMockResponse(HttpStatus.NO_CONTENT, true, ""); Response responseList = getMockResponse(HttpStatus.OK, true, "{\"keys\": [ \"role1\" ]}"); String token = "5PDrOhsy4ig8L3EpsJZSLAMg"; String role_name="approle1"; UserDetails userDetails = getMockUser("testuser1", false); ArrayList<String> policiesList = new ArrayList<String>(); policiesList.add("r_shared_safe01"); String[] policies = policiesList.toArray(new String[policiesList.size()]); AppRole appRole = new AppRole(role_name, policies, true, 0, 0, 0); String appRoleResponseJson = new ObjectMapper().writeValueAsString(appRole); Response appRoleResponse = getMockResponse(HttpStatus.OK, true, appRoleResponseJson); Map<String, Object> appRoleResponseMap = new HashMap<>(); Map<String, Object> dataMap = new HashMap<>(); appRoleResponseMap.put("data", dataMap); dataMap.put("policies",policiesList); dataMap.put("bind_secret_id",new Boolean(true)); dataMap.put("secret_id_num_uses", new Integer(0)); dataMap.put("secret_id_ttl", new Integer(0)); dataMap.put("token_num_uses", new Integer(0)); dataMap.put("token_ttl", new Integer(0)); dataMap.put("token_max_ttl", new Integer(0)); when(reqProcessor.process("/auth/approle/role/read", "{\"role_name\":\""+role_name+"\"}",userDetails.getSelfSupportToken())).thenReturn(appRoleResponse); when(ControllerUtil.parseJson(appRoleResponseJson)).thenReturn(appRoleResponseMap); String jsonStr = "{\"role_name\":\"approle1\",\"policies\":[\"default\"],\"bind_secret_id\":true,\"secret_id_num_uses\":\"1\",\"secret_id_ttl\":\"100m\",\"token_num_uses\":0,\"token_ttl\":null,\"token_max_ttl\":null}"; ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.OK).body("{\"messages\":[\"AppRole updated successfully.\"]}"); Map<String,Object> appRolesList = new HashMap<>(); ArrayList<String> arrayList = new ArrayList<>(); arrayList.add("role1"); appRolesList.put("keys", arrayList); when(ControllerUtil.parseJson("{\"keys\": [ \"role1\" ]}")).thenReturn(appRolesList); Response responseAfterHide = getMockResponse(HttpStatus.OK, true, "{\"keys\": [ \"role1\" ]}"); when(ControllerUtil.hideMasterAppRoleFromResponse(Mockito.any())).thenReturn(responseAfterHide); when(reqProcessor.process("/auth/approle/role/create", jsonStr,userDetails.getSelfSupportToken())).thenReturn(response); when(reqProcessor.process("/auth/approle/role/list","{}",token)).thenReturn(responseList); when(ControllerUtil.areAppRoleInputsValid(appRole)).thenReturn(true); when(JSONUtil.getJSON(appRole)).thenReturn(jsonStr); when(ControllerUtil.convertAppRoleInputsToLowerCase(Mockito.any())).thenReturn(jsonStr); when(reqProcessor.process(eq("/write"),Mockito.any(),eq(token))).thenReturn(response); when(ControllerUtil.createMetadata(Mockito.any(), eq(token))).thenReturn(true); ResponseEntity<String> responseEntityActual = appRoleService.updateAppRole(token, appRole, userDetails); assertEquals(HttpStatus.OK, responseEntityActual.getStatusCode()); assertEquals(responseEntityExpected, responseEntityActual); }
@Test public void test_updateAppRole_BAD_REQUEST() throws Exception{ Response response =getMockResponse(HttpStatus.BAD_REQUEST, true, ""); Response responseList = getMockResponse(HttpStatus.OK, true, "{\"keys\": [ \"role1\" ]}"); String token = "5PDrOhsy4ig8L3EpsJZSLAMg"; String role_name="approle1"; UserDetails userDetails = getMockUser("testuser1", false); ArrayList<String> policiesList = new ArrayList<String>(); policiesList.add("r_shared_safe01"); String[] policies = policiesList.toArray(new String[policiesList.size()]); AppRole appRole = new AppRole(role_name, policies, true, 0, 0, 0); String appRoleResponseJson = new ObjectMapper().writeValueAsString(appRole); Response appRoleResponse = getMockResponse(HttpStatus.OK, true, appRoleResponseJson); Map<String, Object> appRoleResponseMap = new HashMap<>(); Map<String, Object> dataMap = new HashMap<>(); appRoleResponseMap.put("data", dataMap); dataMap.put("policies",policiesList); dataMap.put("bind_secret_id",new Boolean(true)); dataMap.put("secret_id_num_uses", new Integer(0)); dataMap.put("secret_id_ttl", new Integer(0)); dataMap.put("token_num_uses", new Integer(0)); dataMap.put("token_ttl", new Integer(0)); dataMap.put("token_max_ttl", new Integer(0)); when(reqProcessor.process("/auth/approle/role/read", "{\"role_name\":\""+role_name+"\"}",userDetails.getSelfSupportToken())).thenReturn(appRoleResponse); when(ControllerUtil.parseJson(appRoleResponseJson)).thenReturn(appRoleResponseMap); String jsonStr = "{\"role_name\":\"approle1\",\"policies\":[\"default\"],\"bind_secret_id\":true,\"secret_id_num_uses\":\"1\",\"secret_id_ttl\":\"100m\",\"token_num_uses\":0,\"token_ttl\":null,\"token_max_ttl\":null}"; ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{}"); Map<String,Object> appRolesList = new HashMap<>(); ArrayList<String> arrayList = new ArrayList<>(); arrayList.add("role1"); appRolesList.put("keys", arrayList); when(ControllerUtil.parseJson("{\"keys\": [ \"role1\" ]}")).thenReturn(appRolesList); Response responseAfterHide = getMockResponse(HttpStatus.OK, true, "{\"keys\": [ \"role1\" ]}"); when(ControllerUtil.hideMasterAppRoleFromResponse(Mockito.any())).thenReturn(responseAfterHide); when(reqProcessor.process("/auth/approle/role/create", jsonStr,userDetails.getSelfSupportToken())).thenReturn(response); when(reqProcessor.process("/auth/approle/role/list","{}",token)).thenReturn(responseList); when(ControllerUtil.areAppRoleInputsValid(appRole)).thenReturn(true); when(JSONUtil.getJSON(appRole)).thenReturn(jsonStr); when(ControllerUtil.convertAppRoleInputsToLowerCase(Mockito.any())).thenReturn(jsonStr); when(reqProcessor.process(eq("/write"),Mockito.any(),eq(token))).thenReturn(response); when(ControllerUtil.createMetadata(Mockito.any(), eq(token))).thenReturn(true); ResponseEntity<String> responseEntityActual = appRoleService.updateAppRole(token, appRole, userDetails); assertEquals(HttpStatus.BAD_REQUEST, responseEntityActual.getStatusCode()); }