api-mock-objects
v1.0.5
Published
This repository will hold the happy-path input objects for eaze api endpoints. Each endpoint will have its own json file of working input params that can be pulled in by the index.js service or cli. Pass it the path and request-type of an endpoint and it
Downloads
1
Readme
API Mock Objects
This repository will hold the happy-path input objects for eaze api endpoints. Each endpoint will have its own json file of working input params that can be pulled in by the index.js service or cli. Pass it the path and request-type of an endpoint and it will return an object of path, query and body/post parameters (by command line passing "RUN_ENDPOINT" will also return the response). For endpoints that require data from other endpoints, a "parent" can be assigned, to chain requests together. Wrap any variable-name/key in brackets and the path/request-type of the parent endpoint, and the script will pull values from that endpoint's parent endpoint. For instance, to place an item in a cart, the parent endpoint might be a create-cart endpoint, which also has a parent of a logged-in user; so it would execute that request before generating the mock data necessary to add an item into a cart.
Dependencies / Related Repos
Set up
npm install --save-dev https://github.com/eaze/api-mock-objects.git
Usage (script)
const mock = require('api-mock-objects')
mock('auth/signin', 'post').then(function (mockData) {
console.log(mockData)
})
Usage (cli)
# Return a sign-in happy-path object, that can be used directly in api-request functions
GETMOCK_ENDPOINT="auth/signin" GETMOCK_TYPE="post" DEUBG_LEVEL=INFO node index.js
# Return a sign-in happy-path object, and return the response from api-request
RUN_ENDPOINT=TRUE GETMOCK_ENDPOINT="auth/signin" GETMOCK_TYPE="post" DEUBG_LEVEL=INFO node index.js
Sample Mock Objects
# Get Location (requires zipCode and xAuthToken from get-user endpoint response)
{
"headers": {
"X-Auth-Token": "{xAuthToken}"
},
"parent": {
"path": "users/{user}/basic",
"requestType": "get"
},
"path": {},
"query": {
"query": "{zipCode}"
},
"body": {}
}
# Get User (requires userId and xAuthToken from sign-in endpoint response)
{
"headers": {
"X-Auth-Token": "{xAuthToken}"
},
"parent": {
"path": "auth/signin",
"requestType": "post"
},
"path": {
"user": "{userId}"
},
"query": {},
"body": {}
}
# Sign In
{
"headers": {},
"parent": {},
"path": {},
"query": {},
"body": {
"email": "[email protected]",
"password": "password"
}
}
More Ideas:
Generate a hierarchical graph of the parent/child relationship of each endpoint. https://bl.ocks.org/mbostock/4063550
There could be a "cleanup" parameter that could indicate an endpoint necessary to remove anything created in the process of creating the mock data. Those endpoints could be held back until all tests are run.
The generated api-request test should be run hierarchically so that no endpoint is tested before it's parent is tested.
The mock-data test could iterate through each mock object using JOI to verify the successful responses and handle error/edge cases.
It would be nice to add the concept of randomizing a parameter to handle those endpoints (with no parents) that cannot be run multiple times with the same data, like /auth/signup.
const num = (() => { let num = Math.round(Math.random() * 10000000) return
555${Date.now().toString().substr(-7)}
})()