TSK-531 Access id validation failing refinement

This commit is contained in:
Martin Rojas Miguel Angel 2018-06-21 16:31:07 +02:00 committed by Holger Hagen
parent b8138db327
commit f763089e38
3 changed files with 39 additions and 10 deletions

View File

@ -1,5 +1,6 @@
package pro.taskana.rest; package pro.taskana.rest;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
@ -19,21 +20,25 @@ import org.springframework.hateoas.hal.Jackson2HalModule;
import org.springframework.http.HttpEntity; import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import pro.taskana.exceptions.InvalidArgumentException;
import pro.taskana.ldap.LdapCacheTestImpl; import pro.taskana.ldap.LdapCacheTestImpl;
import pro.taskana.rest.resource.AccessIdResource; import pro.taskana.rest.resource.AccessIdResource;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest(classes = RestConfiguration.class, webEnvironment = WebEnvironment.RANDOM_PORT, properties = {"devMode=true"}) @SpringBootTest(classes = RestConfiguration.class, webEnvironment = WebEnvironment.RANDOM_PORT, properties = {
"devMode=true"})
public class AccessIdValidationControllerTest { public class AccessIdValidationControllerTest {
@LocalServerPort @LocalServerPort
@ -49,6 +54,7 @@ public class AccessIdValidationControllerTest {
ResponseEntity<List<AccessIdResource>> response = template.exchange( ResponseEntity<List<AccessIdResource>> response = template.exchange(
"http://127.0.0.1:" + port + "/v1/access-ids?searchFor=ali", HttpMethod.GET, request, "http://127.0.0.1:" + port + "/v1/access-ids?searchFor=ali", HttpMethod.GET, request,
new ParameterizedTypeReference<List<AccessIdResource>>() { new ParameterizedTypeReference<List<AccessIdResource>>() {
}); });
List<AccessIdResource> body = response.getBody(); List<AccessIdResource> body = response.getBody();
assertNotNull(body); assertNotNull(body);
@ -59,6 +65,26 @@ public class AccessIdValidationControllerTest {
} }
} }
@Test
public void testBadRequestWhenSearchForIsTooShort() {
AccessIdController.setLdapCache(new LdapCacheTestImpl());
RestTemplate template = getRestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "Basic dGVhbWxlYWRfMTp0ZWFtbGVhZF8x");
HttpEntity<String> request = new HttpEntity<String>(headers);
try {
template.exchange(
"http://127.0.0.1:" + port + "/v1/access-ids?searchFor=al", HttpMethod.GET, request,
new ParameterizedTypeReference<List<AccessIdResource>>() {
});
} catch (HttpClientErrorException e) {
assertEquals(HttpStatus.BAD_REQUEST, e.getStatusCode());
assertTrue(e.getResponseBodyAsString().contains("Minimum searchFor length ="));
}
}
/** /**
* Return a REST template which is capable of dealing with responses in HAL format * Return a REST template which is capable of dealing with responses in HAL format
* *

View File

@ -89,28 +89,31 @@ public class GenenalExceptionHandlingTest {
try { try {
AccessIdController.setLdapCache(new LdapCacheTestImpl()); AccessIdController.setLdapCache(new LdapCacheTestImpl());
ResponseEntity<List<AccessIdResource>> response = template.exchange( template.exchange(
server + port + "/v1/access-ids?searchFor=al", HttpMethod.GET, request, server + port + "/v1/access-ids?searchFor=al", HttpMethod.GET, request,
new ParameterizedTypeReference<List<AccessIdResource>>() { new ParameterizedTypeReference<List<AccessIdResource>>() {
}); });
} catch (Exception ex) { } catch (Exception ex) {
verify(mockAppender).doAppend(captorLoggingEvent.capture()); verify(mockAppender).doAppend(captorLoggingEvent.capture());
assertTrue(captorLoggingEvent.getValue().getMessage().contains("is too short. Minimum Length =")); assertTrue(
captorLoggingEvent.getValue().getMessage().contains("is too short. Minimum searchFor length = "));
} }
} }
@Test @Test
public void testDeleteNonExisitingClassificationExceptionIsLogged() { public void testDeleteNonExisitingClassificationExceptionIsLogged() {
try { try {
ResponseEntity<PagedResources<ClassificationSummaryResource>> response = template.exchange( template.exchange(
server + port + "/v1/classifications/non-existing-id", HttpMethod.DELETE, request, server + port + "/v1/classifications/non-existing-id", HttpMethod.DELETE, request,
new ParameterizedTypeReference<PagedResources<ClassificationSummaryResource>>() { new ParameterizedTypeReference<PagedResources<ClassificationSummaryResource>>() {
}); });
} catch (Exception ex) { } catch (Exception ex) {
verify(mockAppender).doAppend(captorLoggingEvent.capture()); verify(mockAppender).doAppend(captorLoggingEvent.capture());
assertTrue(captorLoggingEvent.getValue().getMessage().contains("The classification \"non-existing-id\" wasn't found")); assertTrue(captorLoggingEvent.getValue()
.getMessage()
.contains("The classification \"non-existing-id\" wasn't found"));
} }
} }

View File

@ -35,9 +35,9 @@ public class AccessIdController {
@GetMapping @GetMapping
public ResponseEntity<List<AccessIdResource>> validateAccessIds( public ResponseEntity<List<AccessIdResource>> validateAccessIds(
@RequestParam(required = false) String searchFor) throws InvalidArgumentException { @RequestParam String searchFor) throws InvalidArgumentException {
if (searchFor == null || searchFor.length() < ldapClient.getMinSearchForLength()) { if (searchFor.length() < ldapClient.getMinSearchForLength()) {
throw new InvalidArgumentException("searchFor string " + searchFor + " is too short. Minimum Length = " throw new InvalidArgumentException("searchFor string '" + searchFor + "' is too short. Minimum searchFor length = "
+ ldapClient.getMinSearchForLength()); + ldapClient.getMinSearchForLength());
} }
if (ldapClient.useLdap()) { if (ldapClient.useLdap()) {