All files / tests/helpers index.js

100% Statements 20/20
100% Branches 6/6
100% Functions 3/3
100% Lines 20/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 591x 1x   1x   1x 1x   1x   1x   1x         1x             1x         1x   26x 26x     44x         44x   44x   44x   176x       44x         44x      
const url = require(`url`),
  qs = require(`querystring`);
 
const nock = require(`nock`);
 
const Coinpayments = require(`../../lib`),
  CoinpaymentsConfig = require(`../../lib/config`);
 
const { credentials } = require(`../config`);
 
const { API_VERSION, API_PROTOCOL, API_HOST, API_PATH } = CoinpaymentsConfig;
 
const mockUrl = url.format({
  protocol: API_PROTOCOL,
  hostname: API_HOST
});
 
const mock = nock(mockUrl, {
  reqheaders: {
    'Content-Type': `application/x-www-form-urlencoded`,
    HMAC: /^.{128}$/
  }
});
 
const responseGood = {
  code: 200,
  payload: { error: `ok`, result: true }
};
 
module.exports = {
  getClient: function(credentialsArg = {}) {
    credentialsArg = Object.assign({}, credentials, credentialsArg);
    return new Coinpayments(credentialsArg);
  },
  prepareMock: function(mockPayload, repeat = 1, forceResponse = false) {
    const fullPayload = Object.assign({}, mockPayload, {
      version: `${API_VERSION}`,
      key: `${credentials.key}`
    });
 
    const response = forceResponse ? forceResponse : responseGood;
 
    mock
      .post(API_PATH, function(body) {
        for (const key in fullPayload) {
          /* istanbul ignore if */
          if (fullPayload[key] !== body[key]) {
            return false;
          }
        }
        return true;
      })
      .times(repeat)
      .reply(response.code, JSON.stringify(response.payload));
 
    return mock;
  }
};