Unit test of authenticateUser - aws-cognito-identity-js - sinon/proxyquire

3.7k Views Asked by At

I am new to node js and testing in general. I manage to use sinon to stub my functions etc but now I have to test a function that sends callback depending on event (onSuccess, onFailure).

Here is the code I need to test.

var AWSCognito = require('amazon-cognito-identity-js');

exports.getToken = function (options, callback) {
  var poolData = {
    UserPoolId : options.UserPoolId,
    ClientId : options.ClientId,
  };
  var authenticationData = {
    Username : options.username,
    Password : options.password,
  };
  var userPool = new AWSCognito.CognitoUserPool(poolData);
  var authenticationDetails = new AWSCognito.AuthenticationDetails(authenticationData);
  var userData = {
    Username : options.username,
    Pool : userPool
  };

  var cognitoUser = new AWSCognito.CognitoUser(userData);

  cognitoUser.authenticateUser(authenticationDetails, {
    onSuccess: function (result) {
      callback(null, {idToken: result.getIdToken().getJwtToken()});
    },
    onFailure: function (err) {
      callback(err);
    },
  });
}

this is what I did so far.

var proxyquire = require('proxyquire'); var should = require('should'); var sinon = require('sinon'); var AWSCognito = require('amazon-cognito-identity-js');

describe('authentication tests', function () {   var expectedResult;

  it('should invoke a lambda function correctly', function (done) {
    var options = {
      username: 'username1',
      password: 'pwd',
      UserPoolId : 'user_Pool',
      ClientId : 'clientId'
    };
    var expectedResult = {
      idToken: '123u'
    };

    var authenticateUserStub = sinon.stub().yieldsTo('onSuccess');

    var testedModule = proxyquire('../../../api/helpers/authentication.js', {
      'amazon-cognito-identity-js': {
        'CognitoUser': function () {
          return {
            authenticateUser: authenticateUserStub
          }
        }
      }
    });

    testedModule.getToken(options, function (err, data) {
      // should.not.exist(err);
      // data.should.eql(expectedResult);
      done();
    });   }); });

This is what I get as error

TypeError: Cannot read property 'getIdToken' of undefined
    at onSuccess (api/helpers/authentication.js:25:38)
    at callCallback (node_modules/sinon/lib/sinon/behavior.js:95:18)
    at Object.invoke (node_modules/sinon/lib/sinon/behavior.js:128:9)
    at Object.functionStub (node_modules/sinon/lib/sinon/stub.js:98:47)
    at Function.invoke (node_modules/sinon/lib/sinon/spy.js:193:47)
    at Object.proxy [as authenticateUser] (node_modules/sinon/lib/sinon/spy.js:89:22)
    at Object.exports.getToken (api/helpers/authentication.js:23:15)
    at Context.<anonymous> (test/api/helpers/authenticationTests.js:37:18)

It looks like it is going into the onSuccess function and then it does not recognize getIdToken. But it is going too far into the test, no? I only want to stub/mock authenticateUser and return a dummy response.

How can I just tell sinon to give me back a callback on 'onSuccess' without going into the details of the callback?

Thanks for your help

1

There are 1 best solutions below

1
tswaters On

You need to pass additional parameters to the callback via yieldsTo. e.g.,

const getJwtTokenStub = sinon.stub()
const authenticateUserStub = sinon.stub().yieldsTo('onSuccess', {
  getIdToken: sinon.stub().returns({getJwtToken: getJwtTokenStub})
});

// then later, 
assert.equal(getJwtTokenStub.callCount, 1)

That said, it may not be valuable to have this stuff unit tested. You're basically stubbing out a slew of third party functionality and to do what - verify a function gets called?

Having test coverage for this might be better as an integration test - where you take out the stubs and actually hit aws with credentials specifically used for testing to make sure your app is calling everything correctly.