Spy on the randomBytes function

This commit is contained in:
Shane Kilkelly 2016-07-22 11:53:55 +01:00
parent 9fba98cd45
commit f866bd03bc

View file

@ -6,6 +6,7 @@ modulePath = "../../../../app/js/Features/Collaborators/CollaboratorsInviteHandl
SandboxedModule = require('sandboxed-module') SandboxedModule = require('sandboxed-module')
events = require "events" events = require "events"
ObjectId = require("mongojs").ObjectId ObjectId = require("mongojs").ObjectId
Crypto = require('crypto')
describe "CollaboratorsInviteHandler", -> describe "CollaboratorsInviteHandler", ->
beforeEach -> beforeEach ->
@ -18,6 +19,7 @@ describe "CollaboratorsInviteHandler", ->
save: sinon.stub() save: sinon.stub()
findOne: sinon.stub() findOne: sinon.stub()
@Project = {} @Project = {}
@Crypto = Crypto
@CollaboratorsInviteHandler = SandboxedModule.require modulePath, requires: @CollaboratorsInviteHandler = SandboxedModule.require modulePath, requires:
'settings-sharelatex': @settings = {} 'settings-sharelatex': @settings = {}
'logger-sharelatex': @logger = {err: sinon.stub(), error: sinon.stub(), log: sinon.stub()} 'logger-sharelatex': @logger = {err: sinon.stub(), error: sinon.stub(), log: sinon.stub()}
@ -25,6 +27,7 @@ describe "CollaboratorsInviteHandler", ->
'../Contacts/ContactManager': @ContactManager = {} '../Contacts/ContactManager': @ContactManager = {}
'../../models/Project': {Project: @Project} '../../models/Project': {Project: @Project}
'../../models/ProjectInvite': {ProjectInvite: @ProjectInvite} '../../models/ProjectInvite': {ProjectInvite: @ProjectInvite}
'crypto': @Crypto
@projectId = ObjectId() @projectId = ObjectId()
@sendingUserId = ObjectId() @sendingUserId = ObjectId()
@ -36,10 +39,14 @@ describe "CollaboratorsInviteHandler", ->
beforeEach -> beforeEach ->
@ProjectInvite::save = sinon.spy (cb) -> cb(null, this) @ProjectInvite::save = sinon.spy (cb) -> cb(null, this)
@randomBytesSpy = sinon.spy(@Crypto, 'randomBytes')
@CollaboratorsEmailHandler.notifyUserOfProjectInvite = sinon.stub() @CollaboratorsEmailHandler.notifyUserOfProjectInvite = sinon.stub()
@call = (callback) => @call = (callback) =>
@CollaboratorsInviteHandler.inviteToProject @projectId, @sendingUserId, @email, @privileges, callback @CollaboratorsInviteHandler.inviteToProject @projectId, @sendingUserId, @email, @privileges, callback
afterEach ->
@randomBytesSpy.restore()
describe 'when all goes well', -> describe 'when all goes well', ->
beforeEach -> beforeEach ->
@ -57,6 +64,11 @@ describe "CollaboratorsInviteHandler", ->
expect(invite).to.have.all.keys ['_id', 'email', 'token', 'sendingUserId', 'projectId', 'privileges'] expect(invite).to.have.all.keys ['_id', 'email', 'token', 'sendingUserId', 'projectId', 'privileges']
done() done()
it 'should have generated a random token', (done) ->
@call (err, invite) =>
@randomBytesSpy.callCount.should.equal 1
done()
it 'should have called ProjectInvite.save', (done) -> it 'should have called ProjectInvite.save', (done) ->
@call (err, invite) => @call (err, invite) =>
@ProjectInvite::save.callCount.should.equal 1 @ProjectInvite::save.callCount.should.equal 1