1 | _ = require('underscore')
|
2 |
|
3 | CrmModule = require('../../../lib/products/crm/crm-module')
|
4 | BaseModule = require('../../../lib/base-module')
|
5 |
|
6 | Request = require('../../../lib/request')
|
7 | Response = require('../../../lib/response')
|
8 |
|
9 | describe 'crm module', ->
|
10 |
|
11 | crmModule = null
|
12 | product = null
|
13 | record = null
|
14 |
|
15 | beforeEach ->
|
16 | product = {}
|
17 | record = {
|
18 | "Lead Source": "Unit Test",
|
19 | "First Name": "Test",
|
20 | "Last Name": "Testerson",
|
21 | "Email": "test@testerson.com"
|
22 | }
|
23 | crmModule = new CrmModule(product)
|
24 |
|
25 | describe 'build', ->
|
26 | beforeEach ->
|
27 | spyOn(crmModule,'buildRecords').andReturn([{$:{no:1},FL:[{_:'TEST'},{_:'TEST2'}]}])
|
28 |
|
29 | it 'accepts array', ->
|
30 |
|
31 | it 'returns string', ->
|
32 |
|
33 | it 'calls buildRecords', ->
|
34 | crmModule.build([record])
|
35 | expect(crmModule.buildRecords).toHaveBeenCalledWith([record])
|
36 |
|
37 | it 'returns expected string', ->
|
38 | result = crmModule.build([record])
|
39 | expect(result).toMatch(/\?xml/)
|
40 | expect(result).toMatch('<row no="1"')
|
41 | expect(result).toMatch('<FL>TEST</FL>')
|
42 |
|
43 | describe 'buildRecord', ->
|
44 |
|
45 | it "accepts Object", ->
|
46 | expect(() -> crmModule.buildRecord("asdf")).toThrow('Object required')
|
47 |
|
48 | it "returns Array", ->
|
49 | result = crmModule.buildRecord({})
|
50 | expect(_.isArray(result)).toBeTruthy()
|
51 |
|
52 | it "key in to $.val", ->
|
53 | result = crmModule.buildRecord({test:'value'})
|
54 | expect(result.length).toEqual(1)
|
55 | expect(result[0].$.val).toEqual('test')
|
56 |
|
57 | it "value to be in _", ->
|
58 | result = crmModule.buildRecord({test:'value'})
|
59 | expect(result.length).toEqual(1)
|
60 | expect(result[0]._).toEqual('value')
|
61 |
|
62 | it 'supports nested records', ->
|
63 | result = crmModule.build [
|
64 | "Subject": "Test",
|
65 | "Account Name": "phone account11 9pm",
|
66 | "Product Details": [
|
67 | "Product Id": "269840000000136287",
|
68 | "Product Name": "prd1"
|
69 | ]
|
70 | ]
|
71 | expect(result).toMatch(/\?xml/)
|
72 | expect(result).toContain('<row no="1"')
|
73 | expect(result).toContain('<FL val="Subject">Test</FL><FL val="Account Name">phone account11 9pm</FL>')
|
74 | expect(result).toContain('<FL val="Product Details"><product no="1">')
|
75 | expect(result).toContain('<FL val="Product Id">269840000000136287</FL><FL val="Product Name">prd1</FL>')
|
76 |
|
77 |
|
78 | describe 'buildRecords', ->
|
79 | record_set = undefined
|
80 |
|
81 | beforeEach ->
|
82 | record_set = [ record ]
|
83 |
|
84 | it "requires array", ->
|
85 | expect( () -> crmModule.buildRecords({})).toThrow('Requires array of records')
|
86 |
|
87 | it "returns array", ->
|
88 |
|
89 | result = crmModule.buildRecords(record_set)
|
90 | expect(_.isArray(result)).toBeTruthy()
|
91 |
|
92 | it "calls buildRecord", ->
|
93 | spyOn(crmModule,'buildRecord').andReturn([])
|
94 | result = crmModule.buildRecords(record_set)
|
95 | expect(crmModule.buildRecord).toHaveBeenCalledWith(record)
|
96 |
|
97 | it "sets row no", ->
|
98 | result = crmModule.buildRecords(record_set)
|
99 | expect(result[0].$.no).toBe(1)
|
100 |
|
101 | it "sets record on FL", ->
|
102 | _record = [ 'a','record' ]
|
103 | spyOn(crmModule,'buildRecord').andReturn(_record)
|
104 | result = crmModule.buildRecords(record_set)
|
105 | expect(result[0].FL).toEqual(_record)
|
106 |
|
107 | describe 'processRecord', ->
|
108 |
|
109 | describe "single FL response", ->
|
110 | compiled_record = undefined
|
111 |
|
112 | beforeEach ->
|
113 | compiled_record = crmModule.buildRecord(record)
|
114 |
|
115 | it 'handles FL array', ->
|
116 | compiled_record = { FL: [ {$:{val:"test"},_:"value"}]}
|
117 | processed = crmModule.processRecord(compiled_record)
|
118 | expect(processed).toEqual({test:"value"})
|
119 |
|
120 | it 'handles complicated response', ->
|
121 | compiled_record = [{"FL":[{"_":"953419000000673233","$":{"val":"Id"}},{"_":"2014-01-23 14:55:23","$":{"val":"Created Time"}},{"_":"2014-01-23 14:55:23","$":{"val":"Modified Time"}},{"_":"Parmar","$":{"val":"Created By"}},{"_":"Parmar","$":{"val":"Modified By"}}]}]
|
122 | processed = crmModule.processRecord(compiled_record)
|
123 | expect(processed).toEqual({ Id : '953419000000673233', "Created Time" : '2014-01-23 14:55:23', "Modified Time" : '2014-01-23 14:55:23', "Created By" : 'Parmar', "Modified By" : 'Parmar' })
|
124 |
|
125 |
|
126 | describe "param labeled row", ->
|
127 |
|
128 | it "returns single object with key and value", ->
|
129 | expect(crmModule.processRecord({_:"value",$:{param:"key"}})).toEqual({key:"value"})
|
130 |
|
131 | describe "val labeled row", ->
|
132 |
|
133 | it "returns single object with key and value", ->
|
134 | expect(crmModule.processRecord({_:"value",$:{val:"key"}})).toEqual({key:"value"})
|
135 |
|
136 | describe "simple success", ->
|
137 |
|
138 | single_response = {"success":{"Contact":[{"_":"1071983000000075005","$":{"param":"id"}}]}}
|
139 | multiple_response = {"success":{"Contact":[{"_":"1071983000000075009","$":{"param":"id"}}],
|
140 | "Potential":[{"_":"1071983000000075011","$":{"param":"id"}}]}}
|
141 |
|
142 | it "single response", ->
|
143 | processed = crmModule.processRecord(single_response)
|
144 | expect(processed).toEqual({Contact: {id:'1071983000000075005'}})
|
145 |
|
146 | it "multiple response", ->
|
147 | processed = crmModule.processRecord(multiple_response)
|
148 | expect(processed).toEqual({Contact: {id:'1071983000000075009'},Potential: {id:'1071983000000075011'}})
|
149 |
|
150 | describe 'insertRecords', ->
|
151 | response = next = undefined
|
152 |
|
153 | beforeEach ->
|
154 | spyOn(crmModule,'build').andReturn('<?xml version="1.0" encoding="UTF-8"?><crmModule/>')
|
155 | response = new Response({})
|
156 | next = false
|
157 | spyOn(Request.prototype,'request').andCallFake( (cb) ->
|
158 | setImmediate(cb,null,response)
|
159 | )
|
160 | spyOn(crmModule,'buildUrl').andReturn({})
|
161 | spyOn(crmModule,'processRecord').andReturn({})
|
162 |
|
163 | it 'requires array', ->
|
164 | expect( () -> crmModule.insertRecords(record,() -> ) ).toThrow('Requires array of records')
|
165 |
|
166 | it 'requires at least one record', ->
|
167 | expect( () -> crmModule.insertRecords([],() -> ) ).toThrow('Requires as least one record')
|
168 |
|
169 | it 'calls buildRecords', ->
|
170 | crmModule.insertRecords([record],undefined)
|
171 | expect(crmModule.build).toHaveBeenCalledWith([record])
|
172 |
|
173 | it 'builds Url', ->
|
174 | crmModule.insertRecords([record],undefined)
|
175 | expect(crmModule.buildUrl).toHaveBeenCalledWith(
|
176 | {newFormat:1,xmlData:'<?xml version="1.0" encoding="UTF-8"?><crmModule/>'},
|
177 | ['insertRecords'],
|
178 | {method:'POST'}
|
179 | )
|
180 |
|
181 | it 'calls callback with response', ->
|
182 | r = undefined
|
183 | runs ->
|
184 | crmModule.insertRecords([record], (err,_r) ->
|
185 | r = _r
|
186 | next = true
|
187 | )
|
188 | waitsFor ->
|
189 | return next
|
190 | runs ->
|
191 | expect(r).toEqual(response)
|
192 |
|
193 | it 'calls callback with options with response', ->
|
194 | r = undefined
|
195 | runs ->
|
196 | crmModule.insertRecords([record], {}, (err,_r) ->
|
197 | r = _r
|
198 | next = true
|
199 | )
|
200 | waitsFor ->
|
201 | return next
|
202 | runs ->
|
203 | expect(r).toEqual(response)
|
204 |
|
205 | describe 'getRecordById', ->
|
206 | next = response = undefined
|
207 |
|
208 | beforeEach ->
|
209 | next = false
|
210 | response = new Response({})
|
211 | spyOn(crmModule,'buildUrl').andReturn({})
|
212 | spyOn(Request.prototype,'request').andCallFake( (cb) ->
|
213 | setImmediate(cb,null,response)
|
214 | )
|
215 |
|
216 | it 'requires record id param', ->
|
217 | expect( () -> crmModule.getRecordById() ).toThrow('Requires an Id to fetch')
|
218 |
|
219 | it 'build Url', ->
|
220 | crmModule.getRecordById('1234567890123456',undefined)
|
221 | expect(crmModule.buildUrl).toHaveBeenCalledWith(
|
222 | {id:'1234567890123456', newFormat:1},
|
223 | ['getRecordById'],
|
224 | {method:'GET'}
|
225 | )
|
226 |
|
227 | it 'calls callback with response', ->
|
228 | r = undefined
|
229 | runs ->
|
230 | crmModule.getRecordById('1234567890123456', (err,_r) ->
|
231 | r = _r
|
232 | next = true
|
233 | )
|
234 | waitsFor ->
|
235 | return next
|
236 | runs ->
|
237 | expect(r).toEqual(response)
|
238 |
|
239 | describe 'getSearchRecords', ->
|
240 | next = response = undefined
|
241 |
|
242 | beforeEach ->
|
243 | next = false
|
244 | response = new Response({})
|
245 | spyOn(crmModule,'buildUrl').andReturn({})
|
246 | spyOn(Request.prototype,'request').andCallFake( (cb) ->
|
247 | setImmediate(cb,null,response)
|
248 | )
|
249 |
|
250 | it 'requires query object', ->
|
251 | expect( () -> crmModule.getSearchRecords() ).toThrow('Requires a query object')
|
252 |
|
253 | it 'requires searchCondition object', ->
|
254 | expect( () -> crmModule.getSearchRecords({}) ).toThrow('Requires a searchCondition to fetch')
|
255 |
|
256 | it 'build Url', ->
|
257 | crmModule.getSearchRecords({searchCondition: '(Start DateTime|starts with|2014-09-22)', selectColumns : 'All'},undefined)
|
258 | expect(crmModule.buildUrl).toHaveBeenCalledWith(
|
259 | {searchCondition: '(Start DateTime|starts with|2014-09-22)', selectColumns : 'All', newFormat:1},
|
260 | ['getSearchRecords'],
|
261 | {method:'GET'}
|
262 | )
|
263 |
|
264 | it 'calls callback with response', ->
|
265 | r = undefined
|
266 | runs ->
|
267 | crmModule.getSearchRecords({searchCondition: '(Start DateTime|starts with|2014-09-22)'}, (err,_r) ->
|
268 | r = _r
|
269 | next = true
|
270 | )
|
271 | waitsFor ->
|
272 | return next
|
273 | runs ->
|
274 | expect(r).toEqual(response)
|
275 |
|
276 | describe 'searchRecords', ->
|
277 | next = response = undefined
|
278 |
|
279 | beforeEach ->
|
280 | next = false
|
281 | response = new Response({})
|
282 | spyOn(crmModule,'buildUrl').andReturn({})
|
283 | spyOn(Request.prototype,'request').andCallFake( (cb) ->
|
284 | setImmediate(cb,null,response)
|
285 | )
|
286 |
|
287 | it 'requires query object', ->
|
288 | expect( () -> crmModule.searchRecords() ).toThrow('Requires a query object')
|
289 |
|
290 | it 'requires criteria object', ->
|
291 | expect( () -> crmModule.searchRecords({}) ).toThrow('Requires a criteria to fetch')
|
292 |
|
293 | it 'build Url', ->
|
294 | crmModule.searchRecords({criteria: '(Start DateTime:2014-09-22)', selectColumns : 'All'},undefined)
|
295 | expect(crmModule.buildUrl).toHaveBeenCalledWith(
|
296 | {criteria: '(Start DateTime:2014-09-22)', selectColumns : 'All', newFormat:1},
|
297 | ['searchRecords'],
|
298 | {method:'GET'}
|
299 | )
|
300 |
|
301 | it 'calls callback with response', ->
|
302 | r = undefined
|
303 | runs ->
|
304 | crmModule.searchRecords({criteria: '(Start DateTime|starts with|2014-09-22)'}, (err,_r) ->
|
305 | r = _r
|
306 | next = true
|
307 | )
|
308 | waitsFor ->
|
309 | return next
|
310 | runs ->
|
311 | expect(r).toEqual(response)
|
312 |
|
313 | describe 'uploadFile', ->
|
314 | fakeForm = fakeFile = fakeDescriptor = response = undefined
|
315 |
|
316 | beforeEach ->
|
317 | fakeForm = {append: ->}
|
318 | fakeFile = {}
|
319 | fakeDescriptor = {}
|
320 | response = {a: 1}
|
321 |
|
322 | spyOn(crmModule,'buildUrl').andReturn({})
|
323 | spyOn(Request.prototype, 'request').andCallFake( (cb) ->
|
324 | setImmediate(cb, null, response)
|
325 |
|
326 | {form: -> fakeForm}
|
327 | )
|
328 |
|
329 | it 'builds correct url', ->
|
330 | crmModule.uploadFile '1234567890123456', fakeFile, fakeDescriptor
|
331 |
|
332 | expect(crmModule.buildUrl).toHaveBeenCalledWith {}, ['uploadFile'], {method: 'POST'}
|
333 |
|
334 | it 'appends data into multipart request', ->
|
335 | spy = spyOn fakeForm, 'append'
|
336 |
|
337 | crmModule.uploadFile '1234567890123456', fakeFile, fakeDescriptor
|
338 |
|
339 | expect(spy).toHaveBeenCalledWith 'id', '1234567890123456'
|
340 | expect(spy).toHaveBeenCalledWith 'content', fakeFile, fakeDescriptor
|
341 |
|
342 | it 'appends file url to request', ->
|
343 | fakeFile = 'http://fake.string/url.ext'
|
344 | spy = spyOn fakeForm, 'append'
|
345 |
|
346 | crmModule.uploadFile '1234567890123456', fakeFile
|
347 |
|
348 | expect(spy).toHaveBeenCalledWith 'id', '1234567890123456'
|
349 | expect(spy).toHaveBeenCalledWith 'attachmentUrl', fakeFile
|
350 |
|
351 | it 'calls callback with response', ->
|
352 | spy = jasmine.createSpy('callback')
|
353 |
|
354 | crmModule.uploadFile '1234567890123456', fakeFile, fakeDescriptor, spy
|
355 |
|
356 | waitsFor ->
|
357 | if (spy.callCount == 1)
|
358 | expect(spy).toHaveBeenCalledWith(null, {a: 1, data: {}})
|
359 |
|
360 | true
|
361 |
|
362 | describe 'deleteFile', ->
|
363 | fakeForm = response = undefined
|
364 |
|
365 | beforeEach ->
|
366 | fakeForm = {append: ->}
|
367 | response = {a: 1}
|
368 |
|
369 | spyOn(crmModule,'buildUrl').andReturn({})
|
370 | spyOn(Request.prototype, 'request').andCallFake( (cb) ->
|
371 | setImmediate(cb, null, response)
|
372 |
|
373 | {form: -> fakeForm}
|
374 | )
|
375 |
|
376 | it 'builds correct url', ->
|
377 | crmModule.deleteFile '1234567890123456'
|
378 |
|
379 | expect(crmModule.buildUrl).toHaveBeenCalledWith {}, ['deleteFile'], {method: 'POST'}
|
380 |
|
381 | it 'appends id into multipart request', ->
|
382 | spy = spyOn fakeForm, 'append'
|
383 |
|
384 | crmModule.deleteFile '1234567890123456'
|
385 |
|
386 | expect(spy).toHaveBeenCalledWith 'id', '1234567890123456'
|
387 |
|
388 | it 'calls callback with response', ->
|
389 | spy = jasmine.createSpy('callback')
|
390 |
|
391 | crmModule.deleteFile '1234567890123456', spy
|
392 |
|
393 | waitsFor ->
|
394 | if (spy.callCount == 1)
|
395 | expect(spy).toHaveBeenCalledWith(null, {a: 1, data: {}})
|
396 |
|
397 | true
|