-
Notifications
You must be signed in to change notification settings - Fork 11
/
gas-freshdesk-tests.js
430 lines (331 loc) · 13.4 KB
/
gas-freshdesk-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
function freshdeskTestRunner() {
'use strict'
/**
*
* GasFreshdesk - Freshdesk API Class for Google Apps Script
*
* GasFreshdesk is a easy to use Freshdesk API Class for GAS(Google Apps Script)
* It provides a OO(Object-Oriented) way to use Freshdesk Ticket / Contacts, etc.
*
* Github - https://github.com/zixia/gas-freshdesk
*
* Example:
```javascript
var MyFreshdesk = new Freshdesk('https://mikebo.freshdesk.com', 'Jrg0FQNzX3tzuHbiFjYQ')
var ticket = new MyFreshdesk.Ticket({
helpdesk_ticket: {
description:'A description'
, subject: 'A subject'
, email: 'you@example.com'
}
})
ticket.assign(9000658396)
ticket.note({
body: 'Hi tom, Still Angry'
, private: true
})
ticket.setPriority(2)
ticket.setStatus(2)
ticket.del()
ticket.restore()
```
*/
if ((typeof GasLog)==='undefined') { // GasL Initialization. (only if not initialized yet.)
eval(UrlFetchApp.fetch('https://raw.githubusercontent.com/zixia/gasl/master/src/gas-log-lib.js').getContentText())
} // Class GasLog is ready for use now!
var log = new GasLog()
if ((typeof GasTap)==='undefined') { // GasT Initialization. (only if not initialized yet.)
eval(UrlFetchApp.fetch('https://raw.githubusercontent.com/zixia/gast/master/src/gas-tap-lib.js').getContentText())
} // Class GasTap is ready for use now!
var test = new GasTap()
// This is my test account, don't worry, thanks. ;]
var FRESHDESK_URL = 'https://mikebo.freshdesk.com'
// key for agent 'zixia@zixia.net' at 'https://mikebo.freshdesk.com'
var FRESHDESK_KEY = 'Jrg0FQNzX3tzuHbiFjYQ'
/******************************************************************
*/
// return development()
/*
*******************************************************************/
/******************************************************************
*
* Test cases
*
*/
testHttpBackend()
testUtils()
testValidators()
testFreshdeskAuth()
testFreshdeskTicket()
testFreshdeskContact()
testFreshdeskAgent()
testSearch()
test.finish()
////////////////////////////////////////////////////////////////////////
function development() {
var MyFreshdesk = new Freshdesk(FRESHDESK_URL, FRESHDESK_KEY)
var t = new MyFreshdesk.Ticket(105)
Logger.log(JSON.stringify(t.getRawObj()))
}
function testSearch() {
var MyFreshdesk = new Freshdesk(FRESHDESK_URL, FRESHDESK_KEY)
test('listTickets', function (t) {
var EMAIL = 'you@example.com'
tickets = MyFreshdesk.listTickets({ email: EMAIL })
t.ok(tickets.length, 'get listTickets result')
t.ok(tickets[0].getId(), 'ticket id valid')
var contactId = tickets[0].getRequesterId()
var contact = new MyFreshdesk.Contact(contactId)
t.equal(contact.getEmail(), EMAIL, 'email match')
})
}
function testFreshdeskAgent() {
test ('Agent', function (t) {
var EMAIL = 'zixia@zixia.net'
var MyFreshdesk = new Freshdesk(FRESHDESK_URL, FRESHDESK_KEY)
var agent = MyFreshdesk.listAgents({ email: EMAIL })
t.ok(agent.getName(), 'agent has name')
t.ok(agent.getId(), 'agent has id')
})
}
function testFreshdeskContact() {
test ('Contact', function (t) {
var EMAIL = 'you@example.com'
var EXPECTED_NAME = 'expected name'
var MyFreshdesk = new Freshdesk(FRESHDESK_URL, FRESHDESK_KEY)
var contact = MyFreshdesk.listContacts({ email: EMAIL })
t.ok(contact.getName(), 'contact has name')
contact.setName(EXPECTED_NAME)
t.equal(contact.getName(), EXPECTED_NAME, 'contact name as expected')
contact.setName('You You')
t.ok(contact.getId(), 'contact has id')
})
}
function testValidators() {
test ('Validate Ticket Object', function (t) {
var OK_TICKET_OBJ = {
helpdesk_ticket: {
email: 'email@email.com'
}
}
var NOT_OK_TICKET_OBJ = {
test: {
email: 'email@email.com'
}
}
var OK_TICKET_WITH_ATT_OBJ = {
helpdesk_ticket: {
email: 'email@email.com'
, attachments: [
{ resource: 'blob' }
]
}
}
var NOT_OK_TICKET_WITH_ATT_OBJ1 = {
helpdesk_ticket: {
email: 'email@email.com'
, attachments: [
{ errkey: 'blob' }
]
}
}
var NOT_OK_TICKET_WITH_ATT_OBJ2 = {
helpdesk_ticket: {
email: 'email@email.com'
, attachments:
{ resource: 'blob, not array' }
}
}
var OK_TICKET_EMAIL_OBJ = {
helpdesk_ticket: {
email: 'test_.-email@email_em-ail.co.jp'
}
}
var NOT_OK_TICKET_EMAIL_OBJ = {
helpdesk_ticket: {
email: 'n@t a valid address'
}
}
t.notThrow(function () { Freshdesk.validateHelpdeskObject(OK_TICKET_OBJ) }, 'ticket obj with right key')
t.throws(function () { Freshdesk.validateHelpdeskObject(NOT_OK_TICKET_OBJ) }, 'ticket obj with wrong key')
t.notThrow(function () { Freshdesk.validateHelpdeskObject(OK_TICKET_WITH_ATT_OBJ) }, 'ticket obj with right attachment')
t.throws(function () { Freshdesk.validateHelpdeskObject(NOT_OK_TICKET_WITH_ATT_OBJ1) }, 'ticket obj with wrong key of attachment')
t.throws(function () { Freshdesk.validateHelpdeskObject(NOT_OK_TICKET_WITH_ATT_OBJ2) }, 'ticket obj with wrong array of attachment')
t.notThrow(function () { Freshdesk.validateHelpdeskObject(OK_TICKET_EMAIL_OBJ) }, 'ticket obj with valid email address')
t.throws(function () { Freshdesk.validateHelpdeskObject(NOT_OK_TICKET_EMAIL_OBJ) }, 'ticket obj with invalid email address')
})
}
function testUtils() {
test ('Attachment Helper', function (t) {
var HAS_ATTACHMENT = {
a: {
b: {
attachments: [1,2]
}
}
}
var NO_ATTACHMENT = {
a: {
b: {
haha: [1,2]
}
}
}
var http = Freshdesk.Http('a','b')
var hasAtt = http.hasAttachment(HAS_ATTACHMENT)
t.ok(hasAtt, 'HAS_ATTACHMENT has attachment')
var noAtt = http.hasAttachment(NO_ATTACHMENT)
t.ok(!noAtt, 'NO_ATTACHMENT has NO attachment')
})
}
function testFreshdeskTicket() {
test('Ticket', function (t) {
var TICKET_ID = 1
var EXPECTED_ID = 9000658396 // Agent ID of Mike@zixia.net
var MyFreshdesk = new Freshdesk(FRESHDESK_URL, FRESHDESK_KEY)
var oldTicket = new MyFreshdesk.Ticket(TICKET_ID)
t.ok(oldTicket, 'loaded')
t.equal(oldTicket.getRawObj().helpdesk_ticket.id, EXPECTED_ID, '9000658396 match id')
var EXAMPLE_TICKET = {
'helpdesk_ticket': {
'description':'A totally rad description of a what the problem is'
, 'subject':'Something like "Cannot log in"'
, 'email': 'you@example.com'
}
}
var newTicket = new MyFreshdesk.Ticket(EXAMPLE_TICKET)
t.ok(newTicket, 'newTicket created')
t.ok(newTicket.getId(), 'newTicket id exist')
var ZIXIA_RESPONDER_ID = 9005923152 // zixia@zixia.net
var MIKE_RESPONDER_ID = 9005923143 // mike@zixia.net
t.equal(newTicket.getResponderId(), null, 'new ticket default no responder')
newTicket.assign(MIKE_RESPONDER_ID)
t.equal(newTicket.getResponderId(), MIKE_RESPONDER_ID, 'assigned to mike')
var numNotes = newTicket.getRawObj().helpdesk_ticket.notes.length
newTicket.note({
helpdesk_note: {
body: 'Hi tom, Still Angry'
, private: true
, attachments: [
{resource: Utilities.newBlob('TEST DATA').setName('test-data.dat')}
, {resource: Utilities.newBlob('TEST DATA2').setName('test-data2.dat')}
]
}
})
newTicket.note({
helpdesk_note: {
body: 'Hi tom, Still Angry'
, private: true
}
})
t.equal(newTicket.getRawObj().helpdesk_ticket.notes.length, numNotes+2, 'new note created')
var priority = newTicket.getPriority()
newTicket.setPriority(priority+1)
t.equal(newTicket.getPriority(), priority+1, 'inc priority by 1')
var status = newTicket.getStatus()
newTicket.setStatus(status+1)
t.equal(newTicket.getStatus(), status+1, 'inc status by 1')
t.ok(newTicket.del(), 'delete newTicket')
t.ok(newTicket.restore(), 'restore newTicket')
t.ok(newTicket.del(), 'delete newTicket again')
var EXAMPLE_TICKET_WITH_ATTACHMENTS = {
'helpdesk_ticket': {
'description':'A totally rad description of a what the problem is'
, 'subject':'Something like "Cannot log in"'
, 'email': 'you@example.com'
, attachments: [ {resource: Utilities.newBlob('TEST DATA').setName('test-data.dat')}
, {resource: Utilities.newBlob('TEST DATA2').setName('test-data2.dat')}
]
}
}
var newTicketWithAttachment = new MyFreshdesk.Ticket(EXAMPLE_TICKET_WITH_ATTACHMENTS)
t.ok(newTicketWithAttachment, 'newTicketWithAttachment created')
t.ok(newTicketWithAttachment.getId(), 'newTicketWithAttachment id exist')
t.ok(newTicketWithAttachment.del(), 'delete newTicketWithAttachment')
})
}
function testFreshdeskAuth() {
test('Auth Fail', function (t) {
var ERR_URL = 'http://zixia.freshdesk.com'
t.throws(function () {
new Freshdesk(ERR_URL, FRESHDESK_KEY)
}, 'Auth should throw with ERR_URL')
t.throws(function () {
new Freshdesk(FRESHDESK_URL, 'error_key')
}, 'Auth should throw with error_key')
t.throws(function () {
new Freshdesk('not_exist_url', FRESHDESK_KEY)
}, 'Auth should throw with not_exist_url')
t.notThrow(function () {
new Freshdesk(FRESHDESK_URL, FRESHDESK_KEY)
}, 'Auth should not throw with right setting')
})
}
function testHttpBackend() {
test('Multipart body process', function (t) {
var http = new Freshdesk.Http(FRESHDESK_URL, FRESHDESK_KEY)
var BLOB1 = Utilities.newBlob('XXX').setName('xxx')
var BLOB2 = Utilities.newBlob('TODO').setName('todo')
var OBJ = {
helpdesk_ticket: {
attachments: [
{ resource: BLOB1 }
, { resource: BLOB2 }
]
, email: 'example@example.com'
, subject: 'Ticket Title'
, description: 'this is a sample ticket'
}
}
var EXPECTED_MULTIPART_ARRAY = [
['helpdesk_ticket[attachments][][resource]', BLOB1]
, ['helpdesk_ticket[attachments][][resource]', BLOB2]
, ['helpdesk_ticket[email]', 'example@example.com']
, ['helpdesk_ticket[subject]', 'Ticket Title']
, ['helpdesk_ticket[description]', 'this is a sample ticket']
]
/**
* makeMultipartArray
*/
var multipartArray = http.makeMultipartArray(OBJ)
t.deepEqual(multipartArray, EXPECTED_MULTIPART_ARRAY, 'makeMultipartArray')
////////////////////////////////////////////////////////////////////////////////////////////////////
var EXPECTED_MULTIPART_BODY =
'----boundary-seprator\r\n'
+ 'Content-Disposition: form-data; name="helpdesk_ticket[attachments][][resource]"; filename="xxx"\r\n'
+ 'Content-Type: text/plain\r\n\r\nXXX\r\n'
+ '----boundary-seprator\r\n'
+ 'Content-Disposition: form-data; name="helpdesk_ticket[attachments][][resource]"; filename="todo"\r\n'
+ 'Content-Type: text/plain\r\n\r\nTODO\r\n'
+ '----boundary-seprator\r\nContent-Disposition: form-data; name="helpdesk_ticket[email]"\r\n\r\n'
+ 'example@example.com\r\n----boundary-seprator\r\n'
+ 'Content-Disposition: form-data; name="helpdesk_ticket[subject]"\r\n\r\n'
+ 'Ticket Title\r\n----boundary-seprator\r\n'
+ 'Content-Disposition: form-data; name="helpdesk_ticket[description]"\r\n\r\n'
+ 'this is a sample ticket\r\n----boundary-seprator--\r\n'
EXPECTED_MULTIPART_BODY = Utilities.newBlob(EXPECTED_MULTIPART_BODY).getBytes()
/**
* makeMultipartBody
*/
var multipartBody = http.makeMultipartBody(multipartArray, '--boundary-seprator')
t.deepEqual(multipartBody, EXPECTED_MULTIPART_BODY, 'makeMultipartBody')
})
test('Http Methods', function (t) {
var http = new Freshdesk.Http(FRESHDESK_URL, FRESHDESK_KEY)
var data = http.get('http://httpbin.org/get?test=ok')
t.equal(typeof data, 'object', 'json')
if (data && data.args) var tmp = data.args.test || ''
t.equal(tmp, 'ok', 'http.get')
data = http.put('http://httpbin.org/put', 'test=ok')
t.equal(typeof data, 'object', 'x-www-form-urlencoded')
if (data && data.form) var tmp = data.form.test || ''
t.equal(tmp, 'ok', 'http.put')
data = http.del('http://httpbin.org/delete')
t.equal(data.url, 'http://httpbin.org/delete', 'http.del')
data = http.post('http://httpbin.org/post', 'test=ok')
t.equal(typeof data, 'object', 'x-www-form-urlencoded')
if (data && data.form) var tmp = data.form.test || ''
t.equal(tmp, 'ok', 'http.post')
})
}
}