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 );  }
@Test public void test_AssociateAppRole_succssfully() throws Exception { Response response = getMockResponse(HttpStatus.OK, true, ""); ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.OK).body("{\"messages\":[\"Approle associated to SDB\"]}"); 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.OK, 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); when(ControllerUtil.updateMetadata(Mockito.anyMap(),Mockito.anyString())).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.OK, responseEntityActual.getStatusCode()); assertEquals(responseEntityExpected, responseEntityActual); }
@Test public void test_AssociateAppRole_failure_approle_not_exists() throws Exception { Response response = getMockResponse(HttpStatus.NO_CONTENT, true, ""); ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body("{\"errors\":[\"Non existing role name. Please configure approle as first step\"]}"); 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.OK, 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); when(ControllerUtil.updateMetadata(Mockito.anyMap(),Mockito.anyString())).thenReturn(updateMetadataResponse); ResponseEntity<String> responseEntityActual = appRoleService.associateApprole(token, safeAppRoleAccess); assertEquals(HttpStatus.UNPROCESSABLE_ENTITY, responseEntityActual.getStatusCode()); assertEquals(responseEntityExpected, responseEntityActual); }
@Test public void test_AssociateAppRole_succssfully_new_meta() throws Exception { Response response = getMockResponse(HttpStatus.OK, true, ""); ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.OK).body("{\"messages\":[\"Approle associated to SDB\"]}"); 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.OK, 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))).thenAnswer(new Answer() { private int count = 0; public Object answer(InvocationOnMock invocation) { if (count++ == 1) return updateMetadataResponse; return 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")); 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.OK, responseEntityActual.getStatusCode()); assertEquals(responseEntityExpected, responseEntityActual); }
@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()); }
@Test public void test_AssociateAppRole_failed() throws Exception { Response response = getMockResponse(HttpStatus.OK, true, ""); ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"messages\":[\"Approle :approle1 failed to be associated with SDB\"]}"); 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.INTERNAL_SERVER_ERROR, true, "{\"errors\":[\"Internal server error\"]}"); 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); 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.BAD_REQUEST, responseEntityActual.getStatusCode()); }
@Test public void test_AssociateAppRole_failed_400() throws Exception { ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Invalid input values\"]}"); 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>>(){}); when(ControllerUtil.parseJson(Mockito.anyString())).thenReturn(requestMap); when(ControllerUtil.areSafeAppRoleInputsValid(Mockito.anyMap())).thenReturn(false); ResponseEntity<String> responseEntityActual = appRoleService.associateApprole(token, safeAppRoleAccess); assertEquals(HttpStatus.BAD_REQUEST, responseEntityActual.getStatusCode()); assertTrue(responseEntityActual.getBody().contains("Invalid input values")); }
@Test public void test_AssociateAppRole_failed_403() throws Exception { ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"messages\":[\"Approle : approle1 failed to be associated with SDB.. Invalid Path specified\"]}"); 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>>(){}); when(ControllerUtil.parseJson(Mockito.anyString())).thenReturn(requestMap); when(ControllerUtil.areSafeAppRoleInputsValid(Mockito.anyMap())).thenReturn(true); when(ControllerUtil.canAddPermission(Mockito.anyString(), Mockito.anyString())).thenReturn(false); ResponseEntity<String> responseEntityActual = appRoleService.associateApprole(token, safeAppRoleAccess); assertEquals(HttpStatus.BAD_REQUEST, responseEntityActual.getStatusCode()); assertTrue(responseEntityActual.getBody().contains("Approle :approle1 failed to be associated with SDB.. Invalid Path specified")); }