Search in sources :

Example 21 with FuzzingData

use of com.endava.cats.model.FuzzingData in project cats by Endava.

the class TestCaseListenerTest method shouldReportInfoWhenResponseCode200IsExpectedAndResponseBodyIsEmpty.

@ParameterizedTest
@CsvSource({ "{}", "[]", "''", "' '" })
void shouldReportInfoWhenResponseCode200IsExpectedAndResponseBodyIsEmpty(String body) {
    FuzzingData data = Mockito.mock(FuzzingData.class);
    CatsResponse response = Mockito.mock(CatsResponse.class);
    TestCaseListener spyListener = Mockito.spy(testCaseListener);
    Mockito.when(response.getBody()).thenReturn(body);
    Mockito.when(data.getResponseCodes()).thenReturn(Sets.newHashSet("200", "400"));
    Mockito.when(data.getResponses()).thenReturn(Collections.emptyMap());
    Mockito.when(response.responseCodeAsString()).thenReturn("400");
    spyListener.createAndExecuteTest(logger, fuzzer, () -> spyListener.reportResult(logger, data, response, ResponseCodeFamily.FOURXX));
    Mockito.verify(executionStatisticsListener, Mockito.times(1)).increaseSuccess();
    Mockito.verify(spyListener, Mockito.times(1)).reportInfo(logger, "Response matches expected result. Response code [{}] is documented and response body matches the corresponding schema.", response.responseCodeAsString());
}
Also used : CatsResponse(com.endava.cats.model.CatsResponse) FuzzingData(com.endava.cats.model.FuzzingData) CsvSource(org.junit.jupiter.params.provider.CsvSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 22 with FuzzingData

use of com.endava.cats.model.FuzzingData in project cats by Endava.

the class ExtraHeaderFuzzerTest method givenASetOfHeaders_whenCallingTheExtraHeadersFuzzer_thenTheResultsAreCorrectlyReported.

@Test
void givenASetOfHeaders_whenCallingTheExtraHeadersFuzzer_thenTheResultsAreCorrectlyReported() {
    Map<String, List<String>> responses = new HashMap<>();
    responses.put("200", Collections.singletonList("response"));
    FuzzingData data = FuzzingData.builder().headers(Collections.singleton(CatsHeader.builder().name("header").value("value").build())).responses(responses).reqSchema(new StringSchema()).build();
    CatsResponse catsResponse = CatsResponse.builder().body("{}").responseCode(200).build();
    Mockito.when(serviceCaller.call(Mockito.any())).thenReturn(catsResponse);
    Mockito.doNothing().when(testCaseListener).reportResult(Mockito.any(), Mockito.eq(data), Mockito.any(), Mockito.any());
    extraHeaderFuzzer.fuzz(data);
    Mockito.verify(testCaseListener, Mockito.times(1)).reportResult(Mockito.any(), Mockito.eq(data), Mockito.eq(catsResponse), Mockito.eq(ResponseCodeFamily.TWOXX));
}
Also used : CatsResponse(com.endava.cats.model.CatsResponse) HashMap(java.util.HashMap) FuzzingData(com.endava.cats.model.FuzzingData) List(java.util.List) StringSchema(io.swagger.v3.oas.models.media.StringSchema) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

Example 23 with FuzzingData

use of com.endava.cats.model.FuzzingData in project cats by Endava.

the class RemoveHeadersFuzzerTest method givenASetOfHeaders_whenAnErrorOccursCallingTheService_thenTheErrorIsProperlyReported.

@Test
void givenASetOfHeaders_whenAnErrorOccursCallingTheService_thenTheErrorIsProperlyReported() {
    FuzzingData data = FuzzingData.builder().headers(Collections.singleton(CatsHeader.builder().name("header").value("value").build())).build();
    Mockito.when(serviceCaller.call(Mockito.any())).thenThrow(new RuntimeException());
    Mockito.doNothing().when(testCaseListener).reportResult(Mockito.any(), Mockito.eq(data), Mockito.any(), Mockito.any());
    removeHeadersFuzzer.fuzz(data);
    Mockito.verify(testCaseListener, Mockito.times(2)).reportError(Mockito.any(), Mockito.anyString(), Mockito.any());
}
Also used : FuzzingData(com.endava.cats.model.FuzzingData) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

Example 24 with FuzzingData

use of com.endava.cats.model.FuzzingData in project cats by Endava.

the class FuzzingDataFactory method getFuzzDataForNonBodyMethods.

/**
 * A similar FuzzingData object will be created for GET or DELETE requests. The "payload" will be a JSON with all the query or path params.
 * In order to achieve this a synthetic object is created that will act as a root object holding all the query or path params as child schemas.
 * The method returns a list of FuzzingData as you might have oneOf operations which will create multiple payloads.
 *
 * @param path      the current path
 * @param item      the current path item
 * @param openAPI   the full OpenAPI object
 * @param operation the OpenApi operation
 * @return a list of FuzzingData objects
 */
private List<FuzzingData> getFuzzDataForNonBodyMethods(String path, PathItem item, Operation operation, OpenAPI openAPI, HttpMethod method) {
    ObjectSchema syntheticSchema = this.createSyntheticSchemaForGet(operation.getParameters());
    globalContext.getSchemaMap().put(SYNTH_SCHEMA_NAME + operation.getOperationId(), syntheticSchema);
    Set<String> queryParams = this.extractQueryParams(syntheticSchema);
    List<String> payloadSamples = this.getRequestPayloadsSamples(null, SYNTH_SCHEMA_NAME + operation.getOperationId());
    Map<String, List<String>> responses = this.getResponsePayloads(operation, operation.getResponses().keySet());
    Map<String, List<String>> responsesContentTypes = this.getResponseContentTypes(operation, operation.getResponses().keySet());
    List<String> requestContentTypes = this.getRequestContentTypes(operation, openAPI);
    return payloadSamples.stream().map(payload -> FuzzingData.builder().method(method).path(path).headers(this.extractHeaders(operation)).payload(payload).responseCodes(operation.getResponses().keySet()).reqSchema(syntheticSchema).pathItem(item).schemaMap(globalContext.getSchemaMap()).responses(responses).responseContentTypes(responsesContentTypes).requestPropertyTypes(globalContext.getRequestDataTypes()).requestContentTypes(requestContentTypes).queryParams(queryParams).openApi(openAPI).tags(operation.getTags()).reqSchemaName(SYNTH_SCHEMA_NAME).build()).collect(Collectors.toList());
}
Also used : CatsGlobalContext(com.endava.cats.model.CatsGlobalContext) OpenApiUtils(com.endava.cats.util.OpenApiUtils) Parameter(io.swagger.v3.oas.models.parameters.Parameter) HashMap(java.util.HashMap) Operation(io.swagger.v3.oas.models.Operation) JsonParser(com.google.gson.JsonParser) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) StringUtils(org.apache.commons.lang3.StringUtils) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) ArrayList(java.util.ArrayList) JsonElement(com.google.gson.JsonElement) Inject(javax.inject.Inject) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) FilesArguments(com.endava.cats.args.FilesArguments) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Map(java.util.Map) FuzzingData(com.endava.cats.model.FuzzingData) Schema(io.swagger.v3.oas.models.media.Schema) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) ProcessingArguments(com.endava.cats.args.ProcessingArguments) Content(io.swagger.v3.oas.models.media.Content) PathItem(io.swagger.v3.oas.models.PathItem) MediaType(io.swagger.v3.oas.models.media.MediaType) Set(java.util.Set) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) TreeMap(java.util.TreeMap) HttpMethod(com.endava.cats.http.HttpMethod) Optional(java.util.Optional) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) ApplicationScoped(javax.enterprise.context.ApplicationScoped) Collections(java.util.Collections) PayloadGenerator(com.endava.cats.model.generator.PayloadGenerator) CatsHeader(com.endava.cats.model.CatsHeader) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) ArrayList(java.util.ArrayList) List(java.util.List)

Example 25 with FuzzingData

use of com.endava.cats.model.FuzzingData in project cats by Endava.

the class FuzzingDataFactory method getFuzzDataForHttpMethod.

/**
 * The reason we get more than one {@code FuzzingData} objects is related to the usage of {@code anyOf, oneOf or allOf} elements inside the contract definition.
 * The method will compute all the possible combinations so that it covers all payload definitions.
 *
 * @param path      the current path
 * @param item      the current PathItem
 * @param operation the current OpenAPI Operation
 * @param method    the current HTTP method
 * @return a list  of FuzzingData used to Fuzz
 */
private List<FuzzingData> getFuzzDataForHttpMethod(String path, PathItem item, Operation operation, HttpMethod method, OpenAPI openAPI) {
    List<FuzzingData> fuzzingDataList = new ArrayList<>();
    MediaType mediaType = this.getMediaType(operation, openAPI);
    if (mediaType == null) {
        return Collections.emptyList();
    }
    List<String> reqSchemaNames = this.getCurrentRequestSchemaName(mediaType);
    Map<String, List<String>> responses = this.getResponsePayloads(operation, operation.getResponses().keySet());
    Map<String, List<String>> responsesContentTypes = this.getResponseContentTypes(operation, operation.getResponses().keySet());
    List<String> requestContentTypes = this.getRequestContentTypes(operation, openAPI);
    for (String reqSchemaName : reqSchemaNames) {
        List<String> payloadSamples = this.getRequestPayloadsSamples(mediaType, reqSchemaName);
        fuzzingDataList.addAll(payloadSamples.stream().map(payload -> FuzzingData.builder().method(method).path(path).headers(this.extractHeaders(operation)).payload(payload).responseCodes(operation.getResponses().keySet()).reqSchema(globalContext.getSchemaMap().get(reqSchemaName)).pathItem(item).responseContentTypes(responsesContentTypes).requestContentTypes(requestContentTypes).schemaMap(globalContext.getSchemaMap()).responses(responses).requestPropertyTypes(globalContext.getRequestDataTypes()).openApi(openAPI).tags(operation.getTags()).reqSchemaName(reqSchemaName).build()).collect(Collectors.toList()));
    }
    return fuzzingDataList;
}
Also used : FuzzingData(com.endava.cats.model.FuzzingData) ArrayList(java.util.ArrayList) MediaType(io.swagger.v3.oas.models.media.MediaType) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

FuzzingData (com.endava.cats.model.FuzzingData)167 QuarkusTest (io.quarkus.test.junit.QuarkusTest)139 Test (org.junit.jupiter.api.Test)139 StringSchema (io.swagger.v3.oas.models.media.StringSchema)73 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)47 CatsResponse (com.endava.cats.model.CatsResponse)43 HashMap (java.util.HashMap)43 Schema (io.swagger.v3.oas.models.media.Schema)36 FuzzingStrategy (com.endava.cats.model.FuzzingStrategy)35 CsvSource (org.junit.jupiter.params.provider.CsvSource)19 List (java.util.List)17 OpenAPI (io.swagger.v3.oas.models.OpenAPI)14 OpenAPIParser (io.swagger.parser.OpenAPIParser)12 PathItem (io.swagger.v3.oas.models.PathItem)12 HashSet (java.util.HashSet)8 File (java.io.File)7 CatsHeader (com.endava.cats.model.CatsHeader)6 Set (java.util.Set)6 CatsUtil (com.endava.cats.util.CatsUtil)5 NumberSchema (io.swagger.v3.oas.models.media.NumberSchema)5