Skip to content

Commit

Permalink
extend overwrites to handle objects (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklloyd authored Jun 18, 2021
1 parent 5c6092a commit fd13ba2
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 17 deletions.
2 changes: 1 addition & 1 deletion portman-config.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"contentTests": [
{
"openApiOperationId": "leadsAll",
"responseBodyTest": [
"responseBodyTests": [
{
"key": "data[0].company_name",
"value": "Spacex"
Expand Down
18 changes: 9 additions & 9 deletions src/application/TestSuite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ export class TestSuite {
contractTests?: ContractTestConfig[]
): void => {
const tests = contractTests || this.contractTests

if (!tests) return

tests.map(contractTest => {
Expand All @@ -92,9 +91,9 @@ export class TestSuite {
}

public generateVariationTests = (): void => {
if (!this.variationTests) return

const variationTests = this.variationTests
if (!variationTests) return

const variationWriter = new VariationWriter({ testSuite: this })

variationTests.map(variationTest => {
Expand Down Expand Up @@ -248,10 +247,10 @@ export class TestSuite {
pmOperations?: PostmanMappedOperation[],
contentTests?: ContentTestConfig[]
): PostmanMappedOperation[] => {
if (!this.contentTests) return this.postmanParser.mappedOperations

const tests = contentTests || this.contentTests

if (!tests) return this.postmanParser.mappedOperations

tests.map(contentTest => {
//Get Postman operations to inject content test for
const operations = pmOperations || this.getOperationsFromSetting(contentTest)
Expand All @@ -271,8 +270,8 @@ export class TestSuite {
pmOperations?: PostmanMappedOperation[],
assignVariables?: AssignVariablesConfig[]
): PostmanMappedOperation[] => {
if (!this.config?.assignVariables) return this.postmanParser.mappedOperations
const settings = assignVariables || this.config.assignVariables
if (!settings) return this.postmanParser.mappedOperations

settings.map(assignVarSetting => {
if (!assignVarSetting?.collectionVariables) return
Expand All @@ -297,9 +296,10 @@ export class TestSuite {
pmOperations?: PostmanMappedOperation[],
extendedTestsSettings?: ExtendTestsConfig[]
): PostmanMappedOperation[] => {
if (!this.extendTests) return this.postmanParser.mappedOperations
const settings = extendedTestsSettings || this.extendTests

if (!settings) return this.postmanParser.mappedOperations

settings.map(extendedTestsSetting => {
//Get Postman operations to apply assign variables for
const operations = pmOperations || this.getOperationsFromSetting(extendedTestsSetting)
Expand All @@ -318,10 +318,10 @@ export class TestSuite {
pmOperations?: PostmanMappedOperation[],
overwriteSettings?: OverwriteRequestConfig[]
): PostmanMappedOperation[] => {
if (!this.config?.overwrites) return this.postmanParser.mappedOperations

const settings = overwriteSettings || this.config.overwrites

if (!settings) return this.postmanParser.mappedOperations

settings.map(overwriteSetting => {
//Get Postman operations to apply overwrites to
const operations = pmOperations || this.getOperationsFromSetting(overwriteSetting)
Expand Down
6 changes: 3 additions & 3 deletions src/application/VariationWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ export class VariationWriter {
oaOperation: OasMappedOperation | null,
variation: VariationConfig
): void {
const { overwrites: overwriteSettings, tests, assignVariables } = variation
const { overwrites, tests, assignVariables } = variation

if (overwriteSettings) {
this.testSuite.injectOverwrites([pmOperation], overwriteSettings)
if (overwrites) {
this.testSuite.injectOverwrites([pmOperation], overwrites)
}

if (oaOperation && tests?.contractTests) {
Expand Down
9 changes: 6 additions & 3 deletions src/application/overwrites/overwriteRequestBody.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PostmanMappedOperation } from '../../postman'
import { OverwriteRequestBodyConfig } from '../../types'
import { getByPath, omitByPath, setByPath } from '../../utils'
import { getByPath, isObject, omitByPath, setByPath } from '../../utils'

/**
* Overwrite Postman request body with values defined by the portman testsuite
Expand Down Expand Up @@ -30,9 +30,12 @@ export const overwriteRequestBody = (
let newValue = overwriteValue.value

if (overwriteValue.overwrite === false) {
newValue = originalValue + newValue
newValue = isObject(originalValue)
? { ...(originalValue as Record<string, unknown>), newValue }
: originalValue + newValue
}
bodyData = setByPath(bodyData, overwriteValue.key, newValue)

bodyData = setByPath(bodyData, overwriteValue.key, newValue, true)
}
if (overwriteValue.key && overwriteValue.remove === true) {
bodyData = omitByPath(bodyData, overwriteValue.key)
Expand Down
2 changes: 1 addition & 1 deletion src/utils/getByPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const getByPath = (
path: string,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
defaultValue: any | undefined = undefined
): string | undefined => {
): string | Record<string, unknown> | undefined => {
if (!isObject(obj)) return

const flatInput = dot.dot(obj)
Expand Down

0 comments on commit fd13ba2

Please sign in to comment.