Skip to content

Commit

Permalink
fix: contract tests enabled flag
Browse files Browse the repository at this point in the history
  • Loading branch information
anceschia committed Jul 26, 2021
1 parent 2d1fb88 commit 9148bf1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
48 changes: 30 additions & 18 deletions src/application/TestSuite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,6 @@ export class TestSuite {
return pmOperations
}

public getTestTypeFromContractTests = (
contractTest: ContractTestConfig,
type: string
): ContractTestConfig | undefined => {
return contractTest[type]
}

public injectContractTests = (
pmOperation: PostmanMappedOperation,
oaOperation: OasMappedOperation,
Expand Down Expand Up @@ -198,21 +191,29 @@ export class TestSuite {
const responseObject = response[0][1] as OpenAPIV3.ResponseObject

// List excludeForOperations
const optStatusSuccess = this.getTestTypeFromContractTests(contractTest, 'statusSuccess')
const optStatusCode = this.getTestTypeFromContractTests(contractTest, 'statusCode')
const optResponseTime = this.getTestTypeFromContractTests(contractTest, 'responseTime')
const optContentType = this.getTestTypeFromContractTests(contractTest, 'contentType')
const optJsonBody = this.getTestTypeFromContractTests(contractTest, 'jsonBody')
const optSchemaValidation = this.getTestTypeFromContractTests(contractTest, 'schemaValidation')
const optHeadersPresent = this.getTestTypeFromContractTests(contractTest, 'headersPresent')
const optStatusSuccess = contractTest['statusSuccess']
const optStatusCode = contractTest['statusCode']
const optResponseTime = contractTest['responseTime']
const optContentType = contractTest['contentType']
const optJsonBody = contractTest['jsonBody']
const optSchemaValidation = contractTest['schemaValidation']
const optHeadersPresent = contractTest['headersPresent']

// Add status success check
if (optStatusSuccess && !inOperations(pmOperation, optStatusSuccess?.excludeForOperations)) {
if (
optStatusSuccess &&
optStatusSuccess.enabled &&
!inOperations(pmOperation, optStatusSuccess?.excludeForOperations)
) {
pmOperation = testResponseStatusSuccess(pmOperation)
}

// Add status code check
if (optStatusCode && !inOperations(pmOperation, optStatusCode?.excludeForOperations)) {
if (
optStatusCode &&
optStatusCode.enabled &&
!inOperations(pmOperation, optStatusCode?.excludeForOperations)
) {
const statusCodeSetting = optStatusCode as StatusCode
if (!statusCodeSetting.code && responseCode) {
statusCodeSetting.code = responseCode
Expand All @@ -221,7 +222,11 @@ export class TestSuite {
}

// Add responseTime check
if (optResponseTime && !inOperations(pmOperation, optResponseTime?.excludeForOperations)) {
if (
optResponseTime &&
optResponseTime.enabled &&
!inOperations(pmOperation, optResponseTime?.excludeForOperations)
) {
pmOperation = testResponseTime(optResponseTime as ResponseTime, pmOperation)
}

Expand All @@ -239,13 +244,18 @@ export class TestSuite {
if (contentTypesCounter > 0) continue

// Add contentType check
if (optContentType && !inOperations(pmOperation, optContentType?.excludeForOperations)) {
if (
optContentType &&
optContentType.enabled &&
!inOperations(pmOperation, optContentType?.excludeForOperations)
) {
pmOperation = testResponseContentType(contentType, pmOperation, oaOperation)
}

// Add json body check
if (
optJsonBody &&
optJsonBody.enabled &&
contentType === 'application/json' &&
!inOperations(pmOperation, optJsonBody?.excludeForOperations)
) {
Expand All @@ -255,6 +265,7 @@ export class TestSuite {
// Add json schema check
if (
optSchemaValidation &&
optSchemaValidation.enabled &&
content?.schema &&
!inOperations(pmOperation, optSchemaValidation?.excludeForOperations)
) {
Expand All @@ -273,6 +284,7 @@ export class TestSuite {
// Add response header checks headersPresent
if (
optHeadersPresent &&
optHeadersPresent.enabled &&
!inOperations(pmOperation, optHeadersPresent?.excludeForOperations)
) {
pmOperation = testResponseHeader(headerName, pmOperation, oaOperation)
Expand Down
1 change: 1 addition & 0 deletions src/types/PortmanConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { PostmanParser } from '../postman'

type ContractTest = {
enabled: boolean
excludeForOperations?: string[]
}

type StatusSuccess = ContractTest
Expand Down

0 comments on commit 9148bf1

Please sign in to comment.