Skip to content

Commit

Permalink
[API-30823] fix: Adding e2e test for request body validator (#178)
Browse files Browse the repository at this point in the history
fix: adding e2e test for request body validator

Signed-off-by: jhwuz <[email protected]>
  • Loading branch information
jeremyhwu authored Dec 7, 2023
1 parent 042ff67 commit 4962792
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,7 @@ Keep in mind:

### Removing a Spectral driven suite

Delete the associated yaml under `\src\suites\rulesets` and LOAST will automatically stop offering the suite
Delete the associated yaml under `\src\suites\rulesets` and LOAST will automatically stop offering the suite.

### Custom Rulesets (suite: oas-ruleset)

Expand Down
100 changes: 100 additions & 0 deletions test/fixtures/oas/e2e/request_body_validator.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
{
"openapi": "3.0.1",
"info": {
"title": "Request Body Validator E2E OAS",
"description": "E2E test OAS that contains errors that should be caught by the RequestBodyValidator",
"contact": {
"name": "developer.va.gov"
},
"version": "0.0.1"
},
"servers": [
{
"url": "https://example.com/services/request_body_validator/{version}",
"description": "Sandbox",
"variables": {
"version": {
"default": "v0"
}
}
}
],
"tags": [
{
"name": "request-body-validator",
"description": "Request Body Validator E2E test"
}
],
"paths": {
"/type_schema_mismatch": {
"get": {
"tags": ["request-body-validator"],
"summary": "Summary",
"description": "Description",
"operationId": "type_schema_mismatch",
"parameters": [],
"requestBody": {
"description": "description",
"required": true,
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"name": {
"type": "string",
"example": 123
},
"age": {
"type": "integer",
"example": "string"
},
"enum": {
"type": "string",
"enum": ["a", "b", "c", "c"],
"example": "d"
}
}
},
"examples": {
"exampleObject": {
"value": {
"name": 123,
"age": "string",
"enum": "d"
}
}
}
}
}
},
"responses": {
"200": {
"description": "Success",
"content": {
"application/json": {}
}
}
},
"security": [
{
"OAuth": []
}
]
}
}
},
"components": {
"securitySchemes": {
"OAuth": {
"type": "oauth2",
"flows": {
"clientCredentials": {
"tokenUrl": "https://example.com/oauth2/request_body_validator/system/v1/token",
"scopes": {}
}
}
}
}
}
}
45 changes: 45 additions & 0 deletions test/suites/positive/positive-suite.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,48 @@ describe('ResponseValidator', () => {
);
});
});

describe('RequestBodyValidator', () => {
it('contains expected failures', async () => {
mockExecuteResponse = {
ok: true,
status: 200,
headers: {
'content-type': 'application/json',
},
};

const conduct = jest.spyOn(PositiveSuite.prototype, 'conduct');
await Suites.run([
'test/fixtures/oas/e2e/request_body_validator.json',
'-s',
'https://example.com/services/request_body_validator/{version}',
'-b',
'test_token',
'-i',
'positive',
]);

expect(conduct).toHaveBeenCalledTimes(1);

const results = await conduct.mock.results[0].value;
const failedOperations = results.filter(
(result) => result.failures.size > 0,
);
const failures = failedOperations[0].failures;

expect(failures.size).toEqual(4);
expect(failures).toContainValidationFailure(
'Actual type did not match schema. Schema type: integer. Actual type: string. Path: requestBody -> example -> age',
);
expect(failures).toContainValidationFailure(
'Actual type did not match schema. Schema type: string. Actual type: number. Path: requestBody -> example -> name',
);
expect(failures).toContainValidationFailure(
'Schema enum contains duplicate values. Enum values: ["a","b","c","c"]. Path: requestBody -> example -> enum',
);
expect(failures).toContainValidationFailure(
'Actual value does not match schema enum. Enum values: ["a","b","c","c"]. Actual value: "d". Path: requestBody -> example -> enum',
);
});
});

0 comments on commit 4962792

Please sign in to comment.