Thursday 18 May 2017

Writing mocha tests for API POST request and AWS signature

I am trying to write some mocha tests for the following code:

const sign = require("ee-aws-v4-signature");
const request = require("request");


function signRequest(key, secret, url) {
  return sign({
    url: url,
    key: key,
    secret: secret,
    region: 'eu-west-1',
    method: 'post',
    service: 'iam',
    version: '20120812',
    headers: {
      "Content-type":   "application/x-www-form-urlencoded; charset=utf-8",
      date: new Date()
    }
  });
}

function trackDataEvent(signature, event, url) {
  request.post({
    url: url,
    method: 'POST',
    headers: {
      'authorization': signature
    },
    form: {
      'transactionId': event.conversationId,
      'customerId': event.customerId,
      'occurredAt': new Date().toISOString(),
      'type': event.description,
      'properties': event.properties
    }
  });
}

export default function(key, secret, url, whitelist) {
  return event => {
    if (whitelist.indexOf(event.properties) != -1) {
      trackDataEvent(signRequest(key, secret, url), event, url);
    }
  }
}

I want to ideally spy on the post request and the signRequest function, and maybe check the return values if that is possible? But any additional tests would be good. As I understand and, I have tried to do, it would be difficult to check the return value of signRequest because it generates a unique AWS signature which is part of the authorization header in the POST request.



via jord

No comments:

Post a Comment