LDAPAuthService { public ResponseEntity<String> configureLdapUser(String token, LDAPUser ldapUser){ String jsonStr = JSONUtil.getJSON(ldapUser); ObjectMapper objMapper = new ObjectMapper(); String currentPolicies = ""; String latestPolicies = ""; String userName = "" ; try { JsonNode root = objMapper.readTree(jsonStr); userName = root.get("username").asText(); latestPolicies = root.get("policies").asText(); } catch (IOException e) { log.error(e); } log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Configure LDAP User"). put(LogMessage.MESSAGE, String.format ("Trying to configure LDAP user [%s]", jsonStr)). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); Response userResponse = reqProcessor.process("/auth/ldap/users","{\"username\":\""+userName+"\"}",token); String responseJson=""; if(HttpStatus.OK.equals(userResponse.getHttpstatus())){ responseJson = userResponse.getResponse(); try { currentPolicies = ControllerUtil.getPoliciesAsStringFromJson(objMapper, responseJson); } catch (IOException e) { log.error(e); } } Response response = reqProcessor.process("/auth/ldap/users/configure",jsonStr,token); if(response.getHttpstatus().equals(HttpStatus.NO_CONTENT)) { response = ControllerUtil.updateMetaDataOnConfigChanges(userName, "users", currentPolicies, latestPolicies, token); if(!HttpStatus.OK.equals(response.getHttpstatus())) { log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Configure LDAP User"). put(LogMessage.MESSAGE, "Configuring of LDAP user successful"). put(LogMessage.STATUS, response.getHttpstatus().toString()). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(response.getHttpstatus()).body("{\"messages\":[\"LDAP user configured\",\""+response.getResponse()+"\"]}"); } }else{ log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Configure LDAP User"). put(LogMessage.MESSAGE, "Configuring of LDAP user 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()); } log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Configure LDAP User"). put(LogMessage.MESSAGE, "Configuring of LDAP user completed"). put(LogMessage.STATUS, response.getHttpstatus().toString()). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(HttpStatus.OK).body("{\"messages\":[\"LDAP user configured\"]}"); }  ResponseEntity<String> authenticateLdap(UserLogin userLogin); ResponseEntity<String> configureLdapGroup(String token, LDAPGroup ldapGroup); ResponseEntity<String> listLdapGroups(String token); ResponseEntity<String> fetchLdapGroup(String token, String groupname); ResponseEntity<String> deleteLdapGroup(String token, String groupname); ResponseEntity<String> configureLdapUser(String token, LDAPUser ldapUser); ResponseEntity<String> listLdapUsers(String token); ResponseEntity<String> fetchLdapUser(String token, String username); ResponseEntity<String> deleteLdapUser(String token, String username);  }
@Test public void test_configureLdapUser_successfully() { String token = "4EpPYDSfgN2D4Gf7UmNO3nuL"; String jsonStr = "{ \"policies\": \"admin,default\",\"username\": \"safeadmin\"}"; Response responseNotFound = getMockResponse(HttpStatus.NOT_FOUND, false, ""); Response responseNoContent = getMockResponse(HttpStatus.NO_CONTENT, true, ""); Response responseOk = getMockResponse(HttpStatus.OK, true, ""); ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.OK).body("{\"messages\":[\"LDAP user configured\"]}"); LDAPUser ldapUser = new LDAPUser("safeadmin", "admin,default"); when(JSONUtil.getJSON(ldapUser)).thenReturn(jsonStr); when(ControllerUtil.updateMetaDataOnConfigChanges(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(responseOk); when(reqProcessor.process("/auth/ldap/users","{\"username\":\"safeadmin\"}",token)).thenReturn(responseNotFound); when(reqProcessor.process("/auth/ldap/users/configure",jsonStr,token)).thenReturn(responseNoContent); ResponseEntity<String> responseEntity = ldapAuthService.configureLdapUser(token, ldapUser); assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); assertEquals(responseEntityExpected, responseEntity); }
@Test public void test_configureLdapUser_existingUser_successfully() { String token = "4EpPYDSfgN2D4Gf7UmNO3nuL"; String jsonStr = "{ \"policies\": \"admin,default\",\"username\": \"safeadmin\"}"; Response responseUser = getMockResponse(HttpStatus.OK, true, "{ \"data\": {\"groups\": \"\",\"policies\": [ \"admin\", \"default\" ] }}"); Response responseNoContent = getMockResponse(HttpStatus.NO_CONTENT, true, ""); Response responseOk = getMockResponse(HttpStatus.OK, true, ""); ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.OK).body("{\"messages\":[\"LDAP user configured\"]}"); LDAPUser ldapUser = new LDAPUser("safeadmin", "admin,default"); when(JSONUtil.getJSON(ldapUser)).thenReturn(jsonStr); when(ControllerUtil.updateMetaDataOnConfigChanges(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(responseOk); when(reqProcessor.process("/auth/ldap/users","{\"username\":\"safeadmin\"}",token)).thenReturn(responseUser); when(reqProcessor.process("/auth/ldap/users/configure",jsonStr,token)).thenReturn(responseNoContent); ResponseEntity<String> responseEntity = ldapAuthService.configureLdapUser(token, ldapUser); assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); assertEquals(responseEntityExpected, responseEntity); }
@Test public void test_configureLdapUser_failure_500() { String token = "4EpPYDSfgN2D4Gf7UmNO3nuL"; String jsonStr = "{ \"policies\": \"admin,default\",\"username\": \"safeadmin\"}"; Response responseNotFound = getMockResponse(HttpStatus.NOT_FOUND, false, ""); Response responseFailure = getMockResponse(HttpStatus.INTERNAL_SERVER_ERROR, false, "{\"errors\":[\"Configuring of LDAP user failed\"]}"); Response responseOk = getMockResponse(HttpStatus.OK, true, ""); ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\"Configuring of LDAP user failed\"]}"); LDAPUser ldapUser = new LDAPUser("safeadmin", "admin,default"); when(JSONUtil.getJSON(ldapUser)).thenReturn(jsonStr); when(ControllerUtil.updateMetaDataOnConfigChanges(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(responseOk); when(reqProcessor.process("/auth/ldap/users","{\"username\":\"safeadmin\"}",token)).thenReturn(responseNotFound); when(reqProcessor.process("/auth/ldap/users/configure",jsonStr,token)).thenReturn(responseFailure); ResponseEntity<String> responseEntity = ldapAuthService.configureLdapUser(token, ldapUser); assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode()); assertEquals(responseEntityExpected, responseEntity); }
@Test public void test_configureLdapUser_successfully_metaUpdate_failure() { String token = "4EpPYDSfgN2D4Gf7UmNO3nuL"; String jsonStr = "{ \"policies\": \"admin,default\",\"username\": \"safeadmin\"}"; Response responseNotFound = getMockResponse(HttpStatus.NOT_FOUND, false, ""); Response responseNoContent = getMockResponse(HttpStatus.NO_CONTENT, true, ""); Response responseOk = getMockResponse(HttpStatus.MULTI_STATUS, false, "Meta data update failed"); ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.MULTI_STATUS).body("{\"messages\":[\"LDAP user configured\",\"Meta data update failed\"]}"); LDAPUser ldapUser = new LDAPUser("safeadmin", "admin,default"); when(JSONUtil.getJSON(ldapUser)).thenReturn(jsonStr); when(ControllerUtil.updateMetaDataOnConfigChanges(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(responseOk); when(reqProcessor.process("/auth/ldap/users","{\"username\":\"safeadmin\"}",token)).thenReturn(responseNotFound); when(reqProcessor.process("/auth/ldap/users/configure",jsonStr,token)).thenReturn(responseNoContent); ResponseEntity<String> responseEntity = ldapAuthService.configureLdapUser(token, ldapUser); assertEquals(HttpStatus.MULTI_STATUS, responseEntity.getStatusCode()); assertEquals(responseEntityExpected, responseEntity); }