@incdevco/framework
Version:
node.js lambda framework
181 lines (137 loc) • 4.31 kB
JavaScript
var expect = require('chai').expect;
var sinon = require('sinon');
var Utilities = require('../utilities');
var User = require('./index');
describe('user', function () {
var sandbox;
beforeEach(function() {
sandbox = sinon.sandbox.create();
});
afterEach(function () {
sandbox.verifyAndRestore();
});
describe('createAuthenticatedUserCpii', function() {
it('should concat both identityPoolId and identityId', function () {
var event = {
cognito: {
identityPoolId: 'identityPoolId',
identityId: 'identityId'
}
};
var result = User.createAuthenticatedUserCpii(event);
expect(result).to.equal('identityId:identityPoolId', 'result');
});
});
describe('updateAuthenticatedUser', function() {
var currentEpoch, config, ddb, event, expected, request;
beforeEach(function () {
currentEpoch = 'current_epoch';
ddb = {
update: function () {
}
};
config = {
ddb: ddb,
tableName: 'tableName'
};
event = {
cognito: {
identityId: 'identityId',
identityPoolId: 'identityPoolId'
}
};
expected = {
last_updated: {
epoch: currentEpoch,
user: null
}
};
request = {
body: {
last_updated: {
epoch: 'epoch',
user: null
}
},
cognito: {
identityId: 'identityId',
identityPoolId: 'identityPoolId'
}
};
sandbox.stub(Utilities, 'getCurrentEpoch')
.returns(currentEpoch);
});
it('should have correct ConditionExpression', function (done) {
sandbox.mock(ddb)
.expects('update')
.withArgs({
ConditionExpression: '#cpii = :cpii'
+ ' AND #last_updated.epoch = :submitted_epoch',
ExpressionAttributeNames: {
'#address': 'address',
'#cpii': 'cpii',
'#email_address': 'email_address',
'#first_name': 'first_name',
'#full_name': 'full_name',
'#last_name': 'last_name',
'#last_updated': 'last_updated',
'#name': 'name',
'#name_prefix': 'name_prefix',
'#name_pronunciation': 'name_pronunciation',
'#name_suffix': 'name_suffix',
'#phone_number': 'phone_number',
'#photo_url': 'photo_url',
'#settings': 'settings'
},
ExpressionAttributeValues: {
':address': null,
':cpii': 'identityId:identityPoolId',
':email_address': null,
':first_name': null,
':full_name': null,
':last_name': null,
':last_updated': {
epoch: 'epoch',
user: null
},
':name': null,
':name_prefix': null,
':name_pronunciation': null,
':name_suffix': null,
':phone_number': null,
':photo_url': null,
':settings': null,
':submitted_epoch': 'epoch'
},
Key: {
id: undefined
},
TableName: 'tableName',
UpdateExpression: 'SET #last_updated = :last_updated,'
+ ' #address = :address,'
+ ' #email_address = :email_address,'
+ ' #first_name = :first_name,'
+ ' #full_name = :full_name,'
+ ' #last_name = :last_name,'
+ ' #name = :name,'
+ ' #name_prefix = :name_prefix,'
+ ' #name_pronunciation = :name_pronunciation,'
+ ' #name_suffix = :name_suffix,'
+ ' #phone_number = :phone_number,'
+ ' #photo_url = :photo_url,'
+ ' #settings = :settings'
})
.returns({
promise: function () {
return Promise.resolve(expected);
}
});
User.updateAuthenticatedUser(request, config)
.then(function (result) {
expect(result).to.deep.equal(expected, 'result');
return done();
})
.catch(done);
});
});
});