Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented array store and added request and callback history endpoints #166

Merged
merged 4 commits into from
Jun 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
962 changes: 760 additions & 202 deletions audit-resolve.json

Large diffs are not rendered by default.

226 changes: 114 additions & 112 deletions package-lock.json

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions 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.16.0",
"version": "11.17.0",
"license": "Apache-2.0",
"author": "Vijaya Kumar Guthi, ModusBox Inc. ",
"contributors": [
Expand Down Expand Up @@ -84,7 +84,7 @@
"express": "^4.17.1",
"express-validator": "^6.4.0",
"fs": "0.0.1-security",
"handlebars": "4.7.6",
"handlebars": "^4.7.7",
"hapi-auth-bearer-token": "^6.1.4",
"hapi-openapi": "1.2.4",
"hapi-swagger": "^14.1.0",
Expand All @@ -106,8 +106,8 @@
"passport": "0.4.1",
"passport-jwt": "4.0.0",
"path": "^0.12.7",
"postman-collection": "^3.6.4",
"postman-sandbox": "^4.0.1",
"postman-collection": "^3.6.11",
"postman-sandbox": "^4.0.2",
"rc": "1.2.8",
"request": "^2.88.2",
"request-promise-native": "1.0.8",
Expand All @@ -118,15 +118,15 @@
"uuid": "8.1.0",
"uuid4": "1.1.4",
"vm": "0.1.0",
"ws": "7.4.0"
"ws": "^7.4.6"
},
"devDependencies": {
"@types/jest": "24.0.22",
"eslint": "6.6.0",
"get-port": "5.0.0",
"jest": "^25.1.0",
"jest-junit": "9.0.0",
"jsdoc": "3.6.3",
"jsdoc": "^3.6.7",
"nodemon": "^2.0.2",
"npm-audit-resolver": "^2.2.1",
"npm-check-updates": "11.3.0",
Expand Down
38 changes: 38 additions & 0 deletions src/lib/api-routes/history.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
const express = require('express')
const router = new express.Router()
const dbAdapter = require('../db/adapters/dbAdapter')
const arrayStore = require('../arrayStore')

router.get('/reports', async (req, res, next) => {
try {
Expand Down Expand Up @@ -53,4 +54,41 @@ router.get('/logs', async (req, res, next) => {
}
})

// Endpoints for getting the requests to TTK and callbacks from TTK
router.get('/requests', async (req, res, next) => {
try {
const requestsHistoryData = arrayStore.get('requestsHistory', undefined, req.user)
res.status(200).json((requestsHistoryData && requestsHistoryData.map(item => item.data)) || [])
} catch (err) {
res.status(500).json({ error: err && err.message })
}
})

router.delete('/requests', async (req, res, next) => {
try {
arrayStore.reset('requestsHistory', req.user)
res.status(200).send()
} catch (err) {
res.status(500).json({ error: err && err.message })
}
})

router.get('/callbacks', async (req, res, next) => {
try {
const callbacksHistoryData = arrayStore.get('callbacksHistory', undefined, req.user)
res.status(200).json((callbacksHistoryData && callbacksHistoryData.map(item => item.data)) || [])
} catch (err) {
res.status(500).json({ error: err && err.message })
}
})

router.delete('/callbacks', async (req, res, next) => {
try {
arrayStore.reset('callbacksHistory', req.user)
res.status(200).send()
} catch (err) {
res.status(500).json({ error: err && err.message })
}
})

module.exports = router
96 changes: 96 additions & 0 deletions src/lib/arrayStore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*****
License
--------------
Copyright © 2017 Bill & Melinda Gates Foundation
The Mojaloop files are made available by the Bill & Melinda Gates Foundation under the Apache License, Version 2.0 (the "License") and you may not use these files except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, the Mojaloop files are distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Contributors
--------------
This is the official list of the Mojaloop project contributors for this file.
Names of the original copyright holders (individuals or organizations)
should be listed with a '*' in the first column. People who have
contributed from an organization can be listed under the organization
that actually holds the copyright for their contributions (see the
Gates Foundation organization for an example). Those individuals should have
their names indented and be marked with a '-'. Email address can be added
optionally within square brackets <email>.
* Gates Foundation

* ModusBox
* Vijaya Kumar Guthi <vijaya.guthi@modusbox.com> (Original Author)
--------------
******/

var storedObject = {
data: {
requestsHistory: [],
callbacksHistory: []
}
}

const init = (key, user) => {
const context = user ? user.dfspId : 'data'
if (!storedObject[context]) {
storedObject[context] = {}
}
if (!storedObject[context][key]) {
storedObject[context][key] = []
}
return context
}

const reset = (key, user) => {
const context = init(key, user)
storedObject[context][key] = []
}

const get = (key, user) => {
const context = init(key, user)
return [...storedObject[context][key]]
}

const push = (key, value, user) => {
const context = init(key, user)
storedObject[context][key].push({
insertedDate: Date.now(),
data: JSON.parse(JSON.stringify(value))
})
}

const clear = (arrName, interval) => {
for (const context in storedObject) {
if (storedObject[context][arrName]) {
let delEndIndex = -1
for (let i = 0; i < storedObject[context][arrName].length; i++) {
const timeDiff = Date.now() - storedObject[context][arrName][i].insertedDate
if (timeDiff > interval) {
delEndIndex = i
} else {
break
}
}
if (delEndIndex >= 0) {
storedObject[context][arrName].splice(0, delEndIndex + 1)
}
}
}
}

const clearOldObjects = () => {
const interval = 10 * 60 * 1000
clear('requestsHistory', interval)
clear('callbacksHistory', interval)
}

const initArrayStore = () => {
setInterval(clearOldObjects, 1000)
}

module.exports = {
get,
reset,
initArrayStore,
push,
clear
}
3 changes: 2 additions & 1 deletion src/lib/callbackHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const Config = require('./config')
const axios = require('axios').default
const https = require('https')
const objectStore = require('./objectStore')
const arrayStore = require('./arrayStore')
const MyEventEmitter = require('./MyEventEmitter')
const JwsSigning = require('./jws/JwsSigning')
const ConnectionProvider = require('./configuration-providers/mb-connection-manager')
Expand Down Expand Up @@ -167,7 +168,7 @@ const handleCallback = async (callbackObject, context, req) => {
MyEventEmitter.getEmitter('assertionCallback', req.customInfo.user).emit(assertionPath, assertionData)

// Store all the callbacks in callbacksHistory
objectStore.push('callbacksHistory', callbackObject.method + ' ' + callbackObject.path, { headers: callbackObject.headers, body: callbackObject.body })
arrayStore.push('callbacksHistory', { timestamp: Date.now(), url: urlGenerated, method: callbackObject.method, path: callbackObject.path, headers: callbackObject.headers, body: callbackObject.body })

// Send callback
if (userConfig.SEND_CALLBACK_ENABLE) {
Expand Down
3 changes: 2 additions & 1 deletion src/lib/mocking/openApiMockHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const MyEventEmitter = require('../MyEventEmitter')
const utils = require('../utils')
const Config = require('../config')
const objectStore = require('../objectStore')
const arrayStore = require('../arrayStore')
const JwsSigning = require('../jws/JwsSigning')

var path = require('path')
Expand Down Expand Up @@ -203,7 +204,7 @@ const openApiBackendNotImplementedHandler = async (context, req, h, item) => {
MyEventEmitter.getEmitter('assertionRequest', req.customInfo.user).emit(assertionPath, assertionData)
}
// Store all the inbound requests
objectStore.push('requestsHistory', req.method + ' ' + req.path, { headers: req.headers, body: req.payload })
arrayStore.push('requestsHistory', { timestamp: Date.now(), method: req.method, path: req.path, headers: req.headers, body: req.payload })

req.customInfo.specFile = item.specFile
req.customInfo.jsfRefFile = item.jsfRefFile
Expand Down
8 changes: 4 additions & 4 deletions src/lib/test-outbound/outbound-initiator.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const openApiDefinitionsModel = require('../mocking/openApiDefinitionsModel')
const uuid = require('uuid')
const utilsInternal = require('../utilsInternal')
const dbAdapter = require('../db/adapters/dbAdapter')
const objectStore = require('../objectStore')
const arrayStore = require('../arrayStore')
const UniqueIdGenerator = require('../../lib/uniqueIdGenerator')
const httpAgentStore = require('../httpAgentStore')

Expand Down Expand Up @@ -296,9 +296,9 @@ const processTestCase = async (testCase, traceID, inputValues, variableData, dfs
}

const setResponse = async (convertedRequest, resp, variableData, request, status, tracing, testCase, scriptsExecution, contextObj, globalConfig) => {
// Get the requestsHistory and callbacksHistory from the objectStore
const requestsHistoryObj = objectStore.get('requestsHistory')
const callbacksHistoryObj = objectStore.get('callbacksHistory')
// Get the requestsHistory and callbacksHistory from the arrayStore
const requestsHistoryObj = arrayStore.get('requestsHistory')
const callbacksHistoryObj = arrayStore.get('callbacksHistory')
const backgroundData = {
requestsHistory: requestsHistoryObj,
callbacksHistory: callbacksHistoryObj
Expand Down
2 changes: 2 additions & 0 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const RequestLogger = require('./lib/requestLogger')
const OpenApiMockHandler = require('./lib/mocking/openApiMockHandler')
const UniqueIdGenerator = require('./lib/uniqueIdGenerator')
const objectStore = require('./lib/objectStore')
const arrayStore = require('./lib/arrayStore')
const httpAgentStore = require('./lib/httpAgentStore')
const ConnectionProvider = require('./lib/configuration-providers/mb-connection-manager')
const { TraceHeaderUtils } = require('ml-testing-toolkit-shared-lib')
Expand Down Expand Up @@ -163,6 +164,7 @@ const initialize = async () => {

if (serverInstance) {
objectStore.initObjectStore()
arrayStore.initArrayStore()
httpAgentStore.init()
RequestLogger.logMessage('info', `Toolkit Server running on ${serverInstance.info.uri}`, { notification: false })
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ const apiServer = require('../../../../src/lib/api-server')
const app = apiServer.getApp()
const dbAdapter = require('../../../../src/lib/db/adapters/dbAdapter')
const requestLogger = require('../../../../src/lib/requestLogger')
const arrayStore = require('../../../../src/lib/arrayStore')

const SpyArrayStoreGet = jest.spyOn(arrayStore, 'get')
const SpyArrayStoreReset = jest.spyOn(arrayStore, 'reset')
jest.mock('../../../../src/lib/requestLogger')
jest.mock('../../../../src/lib/db/adapters/dbAdapter')

Expand Down Expand Up @@ -84,3 +87,87 @@ describe('API route /api/hisotry', () => {
})
})
})

describe('API route /api/hisotry for requests and callbacks', () => {
beforeAll(() => {
jest.resetAllMocks()
requestLogger.logMessage.mockReturnValue()
})
afterEach(() => {
jest.resetAllMocks()
})
describe('GET /api/history/requests', () => {
it('Get requests history', async () => {
SpyArrayStoreGet.mockReturnValue([{ data: {sample: 'SampleText'}}])
const res = await request(app).get(`/api/history/requests`).send()
expect(res.statusCode).toEqual(200)
expect(Array.isArray(res.body)).toBe(true)
expect(res.body.length).toEqual(1)
expect(res.body[0]).toHaveProperty('sample')
expect(res.body[0].sample).toEqual('SampleText')
})
it('Get requests history empty array', async () => {
SpyArrayStoreGet.mockReturnValue([])
const res = await request(app).get(`/api/history/requests`).send()
expect(res.statusCode).toEqual(200)
expect(res.body).toEqual([])
})
it('Get requests history null value', async () => {
SpyArrayStoreGet.mockReturnValue(null)
const res = await request(app).get(`/api/history/requests`).send()
expect(res.statusCode).toEqual(200)
expect(res.body).toEqual([])
})
it('Get requests history exeception', async () => {
SpyArrayStoreGet.mockImplementation(() => {
throw new Error('Some error')
})
const res = await request(app).get(`/api/history/requests`).send()
expect(res.statusCode).toEqual(500)
})
})
describe('DELETE /api/history/requests', () => {
it('Delete requests history', async () => {
SpyArrayStoreReset.mockReturnValue()
const res = await request(app).delete(`/api/history/requests`).send()
expect(res.statusCode).toEqual(200)
})
})
describe('GET /api/history/callbacks', () => {
it('Get callbacks history', async () => {
SpyArrayStoreGet.mockReturnValue([{ data: {sample: 'SampleText'}}])
const res = await request(app).get(`/api/history/callbacks`).send()
expect(res.statusCode).toEqual(200)
expect(Array.isArray(res.body)).toBe(true)
expect(res.body.length).toEqual(1)
expect(res.body[0]).toHaveProperty('sample')
expect(res.body[0].sample).toEqual('SampleText')
})
it('Get callbacks history empty array', async () => {
SpyArrayStoreGet.mockReturnValue([])
const res = await request(app).get(`/api/history/callbacks`).send()
expect(res.statusCode).toEqual(200)
expect(res.body).toEqual([])
})
it('Get callbacks history null value', async () => {
SpyArrayStoreGet.mockReturnValue(null)
const res = await request(app).get(`/api/history/callbacks`).send()
expect(res.statusCode).toEqual(200)
expect(res.body).toEqual([])
})
it('Get callbacks history exeception', async () => {
SpyArrayStoreGet.mockImplementation(() => {
throw new Error('Some error')
})
const res = await request(app).get(`/api/history/callbacks`).send()
expect(res.statusCode).toEqual(500)
})
})
describe('DELETE /api/history/callbacks', () => {
it('Delete callbacks history', async () => {
SpyArrayStoreReset.mockReturnValue()
const res = await request(app).delete(`/api/history/callbacks`).send()
expect(res.statusCode).toEqual(200)
})
})
})
Loading