Socket
Socket
Sign inDemoInstall

api-mock-objects

Package Overview
Dependencies
50
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    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


Version published
Maintainers
1
Created

Readme

Source

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.

Testing Strategy

  • Swagger
  • Test Generator
  • API Request
  • Mocha Tests

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@eaze.com",
    "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)} })()

FAQs

Last updated on 27 Apr 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc