Skip to content

Commit

Permalink
Bugfix/testcase execution (#120)
Browse files Browse the repository at this point in the history
* Made response and callback accessible from post request script

* Added unit tests

* Bumped up the version and postponed audits
  • Loading branch information
vijayg10 authored Dec 16, 2020
1 parent 189347b commit 05b9878
Show file tree
Hide file tree
Showing 6 changed files with 16,118 additions and 16,022 deletions.
32,070 changes: 16,053 additions & 16,017 deletions audit-resolve.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ml-testing-toolkit",
"description": "Testing Toolkit for Mojaloop implementations",
"version": "11.7.2",
"version": "11.7.3",
"license": "Apache-2.0",
"author": "Vijaya Kumar Guthi, ModusBox Inc.",
"contributors": [
Expand Down
12 changes: 10 additions & 2 deletions src/lib/scripting-engines/vm-javascript-sandbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,16 @@ const executeAsync = async (script, data, contextObj) => {
const fullScript = preScript + script.join('\n') + postScript
let consoleLog = []

if (data.response) {
contextObj.response = data.response
if (data.context.response) {
contextObj.response = data.context.response
}

if (data.context.callback) {
contextObj.callback = data.context.callback
}

if (data.context.collectionVariables) {
contextObj.collectionVariables = data.context.collectionVariables.reduce((rObj, item) => { rObj[item.key] = item.value; return rObj }, {})
}

try {
Expand Down
7 changes: 6 additions & 1 deletion src/lib/test-outbound/outbound-initiator.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,11 @@ const executePostRequestScript = async (convertedRequest, resp, scriptsExecution
response = { code: resp.syncResponse.status, status: resp.syncResponse.statusText, body: resp.syncResponse.body || resp.syncResponse.data }
}

let callback
if (resp.callback) {
callback = resp.callback
}

// Pass the requestsHistory and callbacksHistory to postman sandbox
const collectionVariables = []
collectionVariables.push(
Expand All @@ -370,7 +375,7 @@ const executePostRequestScript = async (convertedRequest, resp, scriptsExecution
if (convertedRequest.scriptingEngine && convertedRequest.scriptingEngine === 'javascript') {
context = javascriptContext
}
scriptsExecution.postRequest = await context.executeAsync(convertedRequest.scripts.postRequest.exec, { context: { response, collectionVariables }, id: uuid.v4() }, contextObj)
scriptsExecution.postRequest = await context.executeAsync(convertedRequest.scripts.postRequest.exec, { context: { response, callback, collectionVariables }, id: uuid.v4() }, contextObj)
variableData.environment = scriptsExecution.postRequest.environment
}
}
Expand Down
47 changes: 47 additions & 0 deletions test/unit/lib/scripting-engines/vm-javascript-sandbox.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,53 @@ describe('Test Outbound Context', () => {

})

it('executeAsync should return consoleLog with response, callback and collectionVariables', async () => {

const amountBefore = 100
const expectedAmount = 200
const contextObj = await Context.generateContextObj({amountBefore})

const collectionVariables = []
collectionVariables.push(
{
type: 'any',
key: 'data',
value: 'sampleCollectionVariableData'
}
)

const args = {
script: [
"console.log('response', response.data)",
"console.log('callback', callback.data)",
"console.log('collectionVariables', collectionVariables.data)",
],
data: { context: {...contextObj, collectionVariables, response: { data: 'sampleResponseData' }, callback: { data: 'sampleCallbackData' }}, id: uuid.v4()},
contextObj: contextObj
}

let scriptResult
try {
scriptResult = await Context.executeAsync(args.script, args.data, args.contextObj)
} finally {
contextObj.ctx.dispose()
contextObj.ctx = null
}
expect(scriptResult.consoleLog[0][1]).toEqual('log')
expect(scriptResult.consoleLog[0][2]).toEqual('response')
expect(scriptResult.consoleLog[0][3]).toEqual('sampleResponseData')

expect(scriptResult.consoleLog[1][1]).toEqual('log')
expect(scriptResult.consoleLog[1][2]).toEqual('callback')
expect(scriptResult.consoleLog[1][3]).toEqual('sampleCallbackData')

expect(scriptResult.consoleLog[2][1]).toEqual('log')
expect(scriptResult.consoleLog[2][2]).toEqual('collectionVariables')
expect(scriptResult.consoleLog[2][3]).toEqual('sampleCollectionVariableData')


})

it('executeAsync should run axios commands', async () => {

axios.mockImplementation(() => Promise.resolve(true))
Expand Down

0 comments on commit 05b9878

Please sign in to comment.