@Test public void test_AssociateAppRole_failed_configuration() throws Exception { Response response = getMockResponse(HttpStatus.OK, true, ""); ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\"Role configuration failed.Contact Admin \"]}"); String token = "5PDrOhsy4ig8L3EpsJZSLAMg"; SafeAppRoleAccess safeAppRoleAccess = new SafeAppRoleAccess("approle1", "shared/mysafe01", "write"); String jsonStr = "{\"role_name\":\"approle1\",\"path\":\"shared/mysafe01\",\"access\":\"write\"}"; Map<String, Object> requestMap = new ObjectMapper().readValue(jsonStr, new TypeReference<Map<String, Object>>(){}); Response configureAppRoleResponse = getMockResponse(HttpStatus.NO_CONTENT, true, ""); Response updateMetadataResponse_404 = getMockResponse(HttpStatus.NOT_FOUND, true, ""); Response updateMetadataResponse = getMockResponse(HttpStatus.NO_CONTENT, true, ""); when(ControllerUtil.parseJson(Mockito.anyString())).thenReturn(requestMap); when(reqProcessor.process(any(String.class),any(String.class),any(String.class))).thenReturn(response); when(ControllerUtil.isValidSafePath(Mockito.anyString())).thenReturn(true); when(ControllerUtil.isValidSafe(Mockito.anyString(), Mockito.anyString())).thenReturn(true); when(reqProcessor.process(eq("/auth/approle/role/create"),Mockito.any(),eq(token))).thenReturn(configureAppRoleResponse); when(ControllerUtil.areSafeAppRoleInputsValid(Mockito.anyMap())).thenReturn(true); when(ControllerUtil.canAddPermission(Mockito.anyString(), Mockito.anyString())).thenReturn(true); Map<String,String> params = new HashMap<String,String>(); params.put("type", "app-roles"); params.put("name","approle1"); params.put("path","shared/mysafe01"); params.put("access","write"); when(ControllerUtil.updateMetadata(Mockito.anyMap(),eq(token))).thenReturn(updateMetadataResponse_404); when(ControllerUtil.getSafeType("shared/mysafe01")).thenReturn("shared"); when(ControllerUtil.getSafeName("shared/mysafe01")).thenReturn("mysafe01"); when(ControllerUtil.getAllExistingSafeNames("shared", token)).thenReturn(Arrays.asList("mysafe02")); params.put("path","shared/mysafe02"); when(ControllerUtil.updateMetadata(params,token)).thenReturn(updateMetadataResponse); Response appRoleResponse = getMockResponse(HttpStatus.OK, true, "{\"data\": {\"policies\":\"w_shared_mysafe01\"}}"); when(reqProcessor.process("/auth/approle/role/read","{\"role_name\":\"approle1\"}",token)).thenReturn(appRoleResponse); ResponseEntity<String> responseEntityActual = appRoleService.associateApprole(token, safeAppRoleAccess); assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntityActual.getStatusCode()); }
public ResponseEntity<String> associateApprole(String token, SafeAppRoleAccess safeAppRoleAccess){ if (TVaultConstants.SELF_SERVICE_APPROLE_NAME.equals(safeAppRoleAccess.getRole_name())) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Access denied: no permission to associate this AppRole to any safe\"]}"); } ResponseEntity<String> response = associateApproletoSafe(token,safeAppRoleAccess); if(response.getStatusCode().equals(HttpStatus.NO_CONTENT)) { return ResponseEntity.status(HttpStatus.OK).body("{\"messages\":[\"Approle associated to SDB\"]}"); } else if(response.getStatusCode().equals(HttpStatus.OK)) { return ResponseEntity.status(HttpStatus.OK).body("{\"messages\":[\"Approle associated to SDB\"]}"); } else { return ResponseEntity.status(response.getStatusCode()).body(response.getBody()); } }
AppRoleService { public ResponseEntity<String> associateApprole(String token, SafeAppRoleAccess safeAppRoleAccess){ if (TVaultConstants.SELF_SERVICE_APPROLE_NAME.equals(safeAppRoleAccess.getRole_name())) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Access denied: no permission to associate this AppRole to any safe\"]}"); } ResponseEntity<String> response = associateApproletoSafe(token,safeAppRoleAccess); if(response.getStatusCode().equals(HttpStatus.NO_CONTENT)) { return ResponseEntity.status(HttpStatus.OK).body("{\"messages\":[\"Approle associated to SDB\"]}"); } else if(response.getStatusCode().equals(HttpStatus.OK)) { return ResponseEntity.status(HttpStatus.OK).body("{\"messages\":[\"Approle associated to SDB\"]}"); } else { return ResponseEntity.status(response.getStatusCode()).body(response.getBody()); } } }
AppRoleService { public ResponseEntity<String> associateApprole(String token, SafeAppRoleAccess safeAppRoleAccess){ if (TVaultConstants.SELF_SERVICE_APPROLE_NAME.equals(safeAppRoleAccess.getRole_name())) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Access denied: no permission to associate this AppRole to any safe\"]}"); } ResponseEntity<String> response = associateApproletoSafe(token,safeAppRoleAccess); if(response.getStatusCode().equals(HttpStatus.NO_CONTENT)) { return ResponseEntity.status(HttpStatus.OK).body("{\"messages\":[\"Approle associated to SDB\"]}"); } else if(response.getStatusCode().equals(HttpStatus.OK)) { return ResponseEntity.status(HttpStatus.OK).body("{\"messages\":[\"Approle associated to SDB\"]}"); } else { return ResponseEntity.status(response.getStatusCode()).body(response.getBody()); } }  }
AppRoleService { public ResponseEntity<String> associateApprole(String token, SafeAppRoleAccess safeAppRoleAccess){ if (TVaultConstants.SELF_SERVICE_APPROLE_NAME.equals(safeAppRoleAccess.getRole_name())) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Access denied: no permission to associate this AppRole to any safe\"]}"); } ResponseEntity<String> response = associateApproletoSafe(token,safeAppRoleAccess); if(response.getStatusCode().equals(HttpStatus.NO_CONTENT)) { return ResponseEntity.status(HttpStatus.OK).body("{\"messages\":[\"Approle associated to SDB\"]}"); } else if(response.getStatusCode().equals(HttpStatus.OK)) { return ResponseEntity.status(HttpStatus.OK).body("{\"messages\":[\"Approle associated to SDB\"]}"); } else { return ResponseEntity.status(response.getStatusCode()).body(response.getBody()); } }  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 ); }
AppRoleService { public ResponseEntity<String> associateApprole(String token, SafeAppRoleAccess safeAppRoleAccess){ if (TVaultConstants.SELF_SERVICE_APPROLE_NAME.equals(safeAppRoleAccess.getRole_name())) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Access denied: no permission to associate this AppRole to any safe\"]}"); } ResponseEntity<String> response = associateApproletoSafe(token,safeAppRoleAccess); if(response.getStatusCode().equals(HttpStatus.NO_CONTENT)) { return ResponseEntity.status(HttpStatus.OK).body("{\"messages\":[\"Approle associated to SDB\"]}"); } else if(response.getStatusCode().equals(HttpStatus.OK)) { return ResponseEntity.status(HttpStatus.OK).body("{\"messages\":[\"Approle associated to SDB\"]}"); } else { return ResponseEntity.status(response.getStatusCode()).body(response.getBody()); } }  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 );  }