before.steps.js 3.47 KB
const assert = require('assert');
const { Before } = require('@cucumber/cucumber');

const axios = require("axios");
const MockAdapter = require("axios-mock-adapter");

// This sets the mock adapter on the default instance
const mockAxios = new MockAdapter(axios);

const { fixtures } = require('../../fixtures/server');

const mockValidConfig = fixtures.get('config-valid');
const mockInvalidConfig = fixtures.get('config-invalid');
const mockSchema = fixtures.get('schema-swagger');

const { GenericProtocol } = require('../../../dist/protocol');
const { Adapter } = require('../../../dist/adapter');

/**
 * Use assert.CallTracker to track internal method calls 
 * Instead of adding trackers to each method, create a 
 * single tracked stub function and then use the parameters
 * to record what is being tracked. 
 */
const tracker = new assert.CallTracker();
const trackedFunction = function(method, params) {
  // do nothing;
};
const recorder = tracker.calls(trackedFunction);

class TrackedAdapter extends Adapter {
  setupCallTracking(recorder, tracker) {
    this.recorder = recorder;
    this.tracker = tracker;
  }
  getAuthorizationHeader() {
    this.recorder('getAuthorizationHeader');
    return super.getAuthorizationHeader();
  }
  poll(is_retry) {
    this.recorder('poll', { is_retry });
    return super.poll(is_retry);
  }
  publish(topic, body, is_retry) {
    this.recorder('publish', { topic, body, is_retry });
    return super.publish(topic, body, is_retry);
  }
  broadcast(body, is_retry) {
    this.recorder('broadcast', { body, is_retry });
    return super.broadcast(body, is_retry);
  }
  getTrackedCalls(method) {
    let calls = this.tracker.getCalls(this.recorder);
    let methodCalls = calls.filter(call => call.arguments[0] === method);
    return methodCalls;
  }
  resetTracker() {
    this.tracker.reset();
  }
}

class TrackedGenericProtocol extends GenericProtocol {
  setupCallTracking(recorder, tracker) {
    this.recorder = recorder;
    this.tracker = tracker;
  }
  encode(type, message) {
    this.recorder('encode', {type, message});
    return super.encode(type, message)
  }
  decode(type, message) {
    this.recorder('decode', {type, message});
    return super.decode(type, message)
  }
  validate(message) {
    this.recorder('validate', {message});
    return super.validate(message);
  }
  getTrackedCalls(method) {
    let calls = this.tracker.getCalls(this.recorder);
    let methodCalls = calls.filter(call => call.arguments[0] === method);
    return methodCalls;
  }
  resetTracker() {
    this.tracker.reset();
  }
}

Before(function() {
  this.api = mockValidConfig.api;
  this.schema = mockSchema;
  this.tracker = tracker;
  this.recorder = recorder;
  let services = {
    recorder,
    tracker
  };
  this.classes = {
    TrackedAdapter,
    TrackedGenericProtocol,
  };
  this.callCounts = {};
  this.protocol = new this.classes.TrackedGenericProtocol(this.schema, services);
  this.protocol.setupCallTracking(this.recorder, this.tracker);
  this.protocol.resetTracker();
  this.mockAxios = mockAxios;
  this.mockAxios.reset();
  this.mockAxios.resetHistory();

  this.mockAxios.onGet(
    `${mockValidConfig.api}/token`, 
    { params: { client_id: mockValidConfig.client_id, secret: mockValidConfig.secret } }
  ).reply(200, fixtures.get('response-valid-token'));

  this.mockAxios.onGet(
    `${mockInvalidConfig.api}/token`, 
    { params: { client_id: mockInvalidConfig.client_id, secret: mockInvalidConfig.secret } }
  ).reply(403, fixtures.get('response-denied-token'));

});