-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyncforce-server-tests.js
453 lines (416 loc) · 15 KB
/
syncforce-server-tests.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
import {Meteor} from 'meteor/meteor'
import {Mongo} from 'meteor/mongo';
import {sinon} from 'meteor/practicalmeteor:sinon';
import {expect, should} from 'meteor/practicalmeteor:chai'
// Import and rename a variable exported by syncforce.js.
//import {SyncForce} from 'meteor/nicocrm:syncforce'
//import {SyncForce} from './syncforce-server'
import {SyncForce} from 'meteor/nicocrm:syncforce'
import Hooks from './lib/Hooks'
import CollectionSync from './CollectionSync'
import EventEmitter from 'events'
should()
require('./lib/Lookup.tests')
require('./lib/LookupCollection.tests')
describe('SyncForce', () => {
const fakeConnection = {}
beforeEach(() => {
SyncForce.getConnection = () => fakeConnection
})
it('should be defined', () => {
expect(SyncForce).to.be.ok
})
describe('CollectionSync', () => {
let collection, findEmitter, sobjectMock, connectionMock, sfMock
beforeEach(() => {
// setup default versions of the mocks...
collection = new Mongo.Collection(null)
findEmitter = new EventEmitter()
findEmitter.sort = sinon.stub().returns(findEmitter)
findEmitter.run = sinon.stub()
sobjectMock = {
find: sinon.stub().returns(findEmitter),
deleted: sinon.stub().yields(null, {deletedRecords: []})
}
connectionMock = {
sobject: () => sobjectMock
}
sfMock = {
getConnection: () => connectionMock,
_notifyReceived: sinon.stub()
}
})
it('should sync new records from SF', sinon.test(function() {
const collectionSync = new CollectionSync(sfMock, collection, 'Account', '', {})
const callback = sinon.stub()
// send records when query runs
findEmitter.run = () => {
findEmitter.emit('record', {
Name: 'new account'
})
findEmitter.emit('end')
}
// run it
collectionSync.run(callback)
// check results
expect(callback).to.have.been.called
expect(collection.findOne({Name: 'new account'})).to.be.ok
expect(collectionSync.running).to.equal(false)
}))
it('should report error to callback', () => {
const collectionSync = new CollectionSync(sfMock, collection, 'Account', '', {})
const callback = sinon.stub()
// send records when query runs
findEmitter.run = () => {
findEmitter.emit('error', 'oh noes')
}
// run it
collectionSync.run(callback)
// check results
expect(callback).to.have.been.calledWith('oh noes')
})
// note this one can fail when the test does a large number of outputs (eg logs) for each insert call
it('should sync large number of new records', sinon.test(function() {
this.timeout(10000); // set timeout to 20 sec.
const collectionSync = new CollectionSync(sfMock, collection, 'Account', '', {})
const callback = sinon.stub()
// send records when query runs
findEmitter.run = () => {
for(let i=0; i < 10000; i++) {
findEmitter.emit('record', {
Name: 'new account ' + i
})
}
findEmitter.emit('end')
}
// run it
collectionSync.run(callback)
// check results
expect(callback).to.have.been.called
expect(collection.findOne({Name: 'new account 9999'})).to.be.ok
expect(collectionSync.running).to.equal(false)
}))
it('should run transform on incoming records', () => {
const collectionSync = new CollectionSync(sfMock, collection, 'Account', '', {
transform: rec => {
rec.Testing = 'foo'
}
})
// send records when query runs
findEmitter.run = () => {
findEmitter.emit('record', {
Name: 'new account'
})
findEmitter.emit('end')
}
// run it
collectionSync.run()
// check results
expect(collection.findOne({Name: 'new account'})).to.be.ok
.and.to.have.property('Testing')
.that.equals('foo')
})
it('should run onRemoved method when a record is removed from SF', () => {
const onRemoved = sinon.stub()
const collectionSync = new CollectionSync(sfMock, collection, 'Account', '', {
onRemoved
})
collection.insert({
Testing: '123456', Id: 'SFID'
})
findEmitter.run = () => {
findEmitter.emit('end')
}
sobjectMock.deleted = sinon.stub().yields(null, {deletedRecords: [{Id: 'SFID'}]})
// run it
collectionSync.run()
// check that the method was called
expect(onRemoved).to.have.been.called
})
it('should send sync event when a record is removed from SF', () => {
const collectionSync = new CollectionSync(sfMock, collection, 'Account', '', {
})
collection.insert({
Testing: '123456', Id: 'SFID'
})
findEmitter.run = () => {
findEmitter.emit('end')
}
sobjectMock.deleted = sinon.stub().yields(null, {deletedRecords: [{Id: 'SFID'}]})
// run it
collectionSync.run()
// check that the method was called
expect(sfMock._notifyReceived).to.have.been.called
})
it('should not delete if onRemoved return false', () => {
// get onRemoved to return false for the "SFID" record
const onRemoved = id => id !== 'SFID'
const collectionSync = new CollectionSync(sfMock, collection, 'Account', '', {
onRemoved
})
collection.insert({
Testing: '123456', Id: 'SFID'
})
collection.insert({
Testing: '123456', Id: 'OTHERID'
})
findEmitter.run = () => {
findEmitter.emit('end')
}
sobjectMock.deleted = sinon.stub().yields(null, {deletedRecords: [{Id: 'SFID'}, {Id: 'OTHERID'}]})
// run it
collectionSync.run()
// check that we did not delete SFID, and we did delete OTHERID
expect(collection.findOne({Id: 'SFID'}), 'Should not delete SFID').to.be.ok
expect(collection.findOne({Id: 'OTHERID'}), 'Should delete other one').to.not.be.ok
})
it('should update records when push topic received', () => {
// TODO
})
it('should use last modified date to run query', () => {
const collectionSync = new CollectionSync(sfMock, collection, 'Account', '', {
timeStampField: 'ModifyDate'
})
collection.insert({Name: 'something something', ModifyDate: '2016-03-03T12:00:00Z' })
collectionSync.run()
expect(sobjectMock.find)
// we take out a few minutes, so can't do an exact match
.to.have.been.calledWithMatch(/ModifyDate >= 2016-03-03T.*/, '*')
})
it('should update parent lookup field when the record is synced from SF', () => {
const parentCollection = new Mongo.Collection(null)
parentCollection.insert({Id: 'parentid', Name: 'Foo'})
const collectionSync = new CollectionSync(sfMock, collection, 'Account', '', {
lookupDefinitions: [{
lookupField: 'ParentId', parentCollection, parentFieldName: 'Parent'
}]
})
// send records when query runs
findEmitter.run = () => {
findEmitter.emit('record', {
Name: 'new account', ParentId: 'parentid'
})
findEmitter.emit('end')
}
// run it
collectionSync.run()
// check results
console.log(collection.findOne({Name: 'new account'}))
expect(collection.findOne({Name: 'new account'})).to.be.ok
.and.to.have.property('Parent')
.that.eql({Id: 'parentid', Name: 'Foo'})
})
})
describe('syncMetadata', () => {
it('should sync picklists from SF', () => {
fakeConnection.metadata = {
read: sinon.stub().yields(null, [
{ fullName: 'Account', fields: [1,2] }
])
}
const Metadata = new Mongo.Collection(null)
SyncForce.syncMetadata(Metadata, 'CustomObject', ['Account'])
const accountMeta = Metadata.findOne({fullName: 'Account'})
expect(accountMeta).to.be.ok
expect(accountMeta.fields).to.have.length(2)
})
})
describe('CollectionHooks', () => {
it('should run hook on insert, and populate id', () => {
const collection = new Mongo.Collection(null)
const syncforce = {
create: sinon.stub().returns({ id: '123' })
}
const hook = new Hooks(syncforce, collection, 'ResourceName')
hook.init()
collection.insert({
Testing: true
})
expect(syncforce.create).to.have.been.calledWith('ResourceName', {Testing: true})
const rec = collection.findOne({})
expect(rec.Id).to.equal('123')
})
it('should insert object with complex property, but not send them to Salesforce', () => {
const collection = new Mongo.Collection(null)
const syncforce = {
create: sinon.stub().returns({ id: '123' })
}
const hook = new Hooks(syncforce, collection, 'ResourceName')
hook.init()
collection.insert({
Testing: true,
OtherProperty: { somethingelse: true }
})
expect(syncforce.create).to.have.been.calledWith('ResourceName', {Testing: true})
const rec = collection.findOne({})
expect(rec.Id).to.equal('123')
expect(rec.OtherProperty).to.eql({somethingelse: true})
})
it('should not send update to SF when there is no SF data', () => {
const collection = new Mongo.Collection(null)
const syncforce = {
create: sinon.stub().returns({ id: '123' }),
update: sinon.stub()
}
const hook = new Hooks(syncforce, collection, 'ResourceName')
hook.init()
collection.insert({
Testing: true
})
collection.update({Id: '123'}, {$set: { OtherProperty: {somethingelse: true }}})
expect(syncforce.create).to.have.been.calledWith('ResourceName', {Testing: true})
const rec = collection.findOne({Id: '123'})
expect(syncforce.update).to.not.have.been.called
expect(rec.OtherProperty, 'it should still update in mongo').to.eql({somethingelse: true})
})
it('should run outbound hooks on insert, skip insert if return false', () => {
const collection = new Mongo.Collection(null)
const syncforce = {
create: sinon.stub().returns({ id: '123' })
}
const outboundHooks = {
insert: sinon.stub().returns(false)
}
const hook = new Hooks(syncforce, collection, 'ResourceName', {
outboundHooks: outboundHooks
})
hook.init()
collection.insert({
Testing: true
})
expect(outboundHooks.insert).to.have.been.called
expect(syncforce.create).to.not.have.been.called
const rec = collection.findOne({})
expect(rec, 'we should not have created record').to.not.be.ok
})
it('should run outbound hooks on create, use modified record, but do not carry those modifications to the DB', () => {
const collection = new Mongo.Collection(null)
const syncforce = {
create: sinon.stub().returns({ id: '123' })
}
const outboundHooks = {
insert: {
before: rec => ({...rec, Name: 'foo'})
}
}
const hook = new Hooks(syncforce, collection, 'ResourceName', {
outboundHooks: outboundHooks
})
hook.init()
collection.insert({
Testing: true
})
expect(syncforce.create).to.have.been.calledWith('ResourceName', {
Testing: true, Name: 'foo'
})
const rec = collection.findOne({})
expect(rec, 'we should not carry over mod to the local DB').to.be.ok
.and.to.not.have.property('Name')
})
it('should run outbound hooks on update', () => {
const collection = new Mongo.Collection(null)
collection.insert({
Testing: '123456', Id: 'SFID'
})
const syncforce = {
update: sinon.stub().returns({ id: '123' })
}
const outboundHooks = {
update: {
before: rec => ({...rec, Name: 'foo'})
}
}
const hook = new Hooks(syncforce, collection, 'ResourceName', {
outboundHooks: outboundHooks
})
hook.init()
collection.update({ Testing: '123456' }, {$set: { Stuff: 'whatever' }})
expect(syncforce.update).to.have.been.called
const rec = collection.findOne({Testing: '123456'})
expect(rec).to.be.ok.and.to.not.have.property('Name')
})
it('should run outbound save hooks on update', () => {
const collection = new Mongo.Collection(null)
collection.insert({
Testing: '123456', Id: 'SFID'
})
const syncforce = {
update: sinon.stub().returns({ id: '123' })
}
const outboundHooks = {
save: {
before: rec => ({...rec, Name: 'foo'})
}
}
const hook = new Hooks(syncforce, collection, 'ResourceName', {
outboundHooks: outboundHooks
})
hook.init()
collection.update({ Testing: '123456' }, {$set: { Stuff: 'whatever' }})
expect(syncforce.update).to.have.been.called
const rec = collection.findOne({Testing: '123456'})
expect(rec).to.be.ok.and.to.not.have.property('Name')
})
it('should run outbound hooks on update, cancel update if false', () => {
const collection = new Mongo.Collection(null)
collection.insert({
Testing: '123456', Id: 'SFID'
})
const syncforce = {
update: sinon.stub().returns({ id: '123' })
}
const outboundHooks = {
update: rec => false
}
const hook = new Hooks(syncforce, collection, 'ResourceName', {
outboundHooks: outboundHooks
})
hook.init()
collection.update({ Testing: '123456' }, {$set: { Stuff: 'whatever' }})
expect(syncforce.update).to.not.have.been.called
const rec = collection.findOne({Testing: '123456'})
expect(rec).to.be.ok.and.to.not.have.property('Stuff')
})
it('should run outbound hooks on delete', () => {
const collection = new Mongo.Collection(null)
collection.insert({
Testing: '123456', Id: 'SFID'
})
const syncforce = {
destroy: sinon.stub().returns({ id: '123' })
}
const outboundHooks = {
remove: sinon.stub()
}
const hook = new Hooks(syncforce, collection, 'ResourceName', {
outboundHooks: outboundHooks
})
hook.init()
collection.remove({ Testing: '123456' })
expect(syncforce.destroy).to.have.been.called
expect(outboundHooks.remove).to.have.been.called
const rec = collection.findOne({Testing: '123456'})
expect(rec).to.not.be.ok
})
it('should run outbound hooks on delete, cancel delete if false', () => {
const collection = new Mongo.Collection(null)
collection.insert({
Testing: '123456', Id: 'SFID'
})
const syncforce = {
destroy: sinon.stub().returns({ id: '123' })
}
const outboundHooks = {
remove: sinon.stub().returns(false)
}
const hook = new Hooks(syncforce, collection, 'ResourceName', {
outboundHooks: outboundHooks
})
hook.init()
collection.remove({ Testing: '123456' })
expect(syncforce.destroy).to.not.have.been.called
const rec = collection.findOne({Testing: '123456'})
expect(rec).to.be.ok
})
})
})