/
zendesk_import_test.rb
539 lines (498 loc) · 14.1 KB
/
zendesk_import_test.rb
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
require 'integration_test_helper'
class ZendeskImportTest < ActiveSupport::TestCase
self.test_order = :sorted
if !ENV['IMPORT_ZENDESK_ENDPOINT']
raise "ERROR: Need IMPORT_ZENDESK_ENDPOINT - hint IMPORT_ZENDESK_ENDPOINT='https://example.zendesk.com/api/v2'"
end
if !ENV['IMPORT_ZENDESK_ENDPOINT_KEY']
raise "ERROR: Need IMPORT_ZENDESK_ENDPOINT_KEY - hint IMPORT_ZENDESK_ENDPOINT_KEY='01234567899876543210'"
end
if !ENV['IMPORT_ZENDESK_ENDPOINT_USERNAME']
raise "ERROR: Need IMPORT_ZENDESK_ENDPOINT_USERNAME - hint IMPORT_ZENDESK_ENDPOINT_USERNAME='bob.ross@happylittletrees.com'"
end
Setting.set('import_zendesk_endpoint', ENV['IMPORT_ZENDESK_ENDPOINT'])
Setting.set('import_zendesk_endpoint_key', ENV['IMPORT_ZENDESK_ENDPOINT_KEY'])
Setting.set('import_zendesk_endpoint_username', ENV['IMPORT_ZENDESK_ENDPOINT_USERNAME'])
Setting.set('import_mode', true)
Setting.set('system_init_done', false)
job = ImportJob.create(name: 'Import::Zendesk')
job.start
# check statistic count
test 'check statistic' do
# retrive statistic
compare_statistic = {
Groups: {
skipped: 0,
created: 2,
updated: 0,
unchanged: 0,
failed: 0,
deactivated: 0,
sum: 2,
total: 2
},
Users: {
skipped: 0,
created: 141,
updated: 0,
unchanged: 0,
failed: 0,
deactivated: 0,
sum: 141,
total: 141
},
Organizations: {
skipped: 0,
created: 1,
updated: 0,
unchanged: 0,
failed: 0,
deactivated: 0,
sum: 1,
total: 1
},
Tickets: {
skipped: 0,
created: 142,
updated: 1,
unchanged: 0,
failed: 0,
deactivated: 0,
sum: 143,
total: 143
}
}
assert_equal(compare_statistic.with_indifferent_access, job.result, 'statistic')
end
# check count of imported items
test 'check counts' do
assert_equal(144, User.count, 'users')
assert_equal(3, Group.count, 'groups')
assert_equal(3, Role.count, 'roles')
assert_equal(2, Organization.count, 'organizations')
assert_equal(143, Ticket.count, 'tickets')
assert_equal(151, Ticket::Article.count, 'ticket articles')
assert_equal(2, Store.count, 'ticket article attachments')
# TODO: Macros, Views, Automations...
end
# check imported users and permission
test 'check users' do
role_admin = Role.find_by(name: 'Admin')
role_agent = Role.find_by(name: 'Agent')
role_customer = Role.find_by(name: 'Customer')
group_users = Group.find_by(name: 'Users')
group_support = Group.find_by(name: 'Support')
group_additional_group = Group.find_by(name: 'Additional Group')
checks = [
{
id: 5,
data: {
firstname: 'Bob',
lastname: 'Smith',
login: 'bob.smith@znuny.com',
email: 'bob.smith@znuny.com',
active: true,
phone: '00114124',
lieblingstier: 'Hundä',
},
roles: [role_admin, role_agent],
groups: [group_support],
},
{
id: 6,
data: {
firstname: 'Hansimerkur',
lastname: '',
login: 'hansimerkur@znuny.com',
email: 'hansimerkur@znuny.com',
active: true,
lieblingstier: nil,
},
roles: [role_admin, role_agent],
groups: [group_additional_group, group_support],
},
{
id: 7,
data: {
firstname: 'Bernd',
lastname: 'Hofbecker',
login: 'bernd.hofbecker@znuny.com',
email: 'bernd.hofbecker@znuny.com',
active: true,
},
roles: [role_customer],
groups: [],
},
{
id: 8,
data: {
firstname: 'Zendesk',
lastname: '',
login: 'noreply@zendesk.com',
email: 'noreply@zendesk.com',
active: true,
},
roles: [role_customer],
groups: [],
},
{
id: 90,
data: {
firstname: 'Hans',
lastname: 'Peter Wurst',
login: 'hansimerkur+zd-c1@znuny.com',
email: 'hansimerkur+zd-c1@znuny.com',
active: true,
},
roles: [role_customer],
groups: [],
},
]
checks.each do |check|
user = User.find(check[:id])
check[:data].each do |key, value|
user_value = user[key]
text = "user.#{key} for user_id #{check[:id]}"
if value.nil?
assert_nil(user_value, text)
else
assert_equal(value, user_value, text)
end
end
assert_equal(check[:roles], user.roles.sort.to_a, "#{user.login} roles")
assert_equal(check[:groups], user.groups_access('full').sort.to_a, "#{user.login} groups")
end
end
# check user fields
test 'check user fields' do
local_fields = User.column_names
copmare_fields = %w[
id
organization_id
login
firstname
lastname
email
image
image_source
web
password
phone
fax
mobile
department
street
zip
city
country
address
vip
verified
active
note
last_login
source
login_failed
out_of_office
out_of_office_start_at
out_of_office_end_at
out_of_office_replacement_id
preferences
updated_by_id
created_by_id
created_at
updated_at
lieblingstier
custom_dropdown
]
assert_equal(copmare_fields, local_fields, 'user fields')
end
# check groups/queues
test 'check groups' do
checks = [
{
id: 1,
data: {
name: 'Users',
active: true,
},
},
{
id: 2,
data: {
name: 'Additional Group',
active: true,
},
},
{
id: 3,
data: {
name: 'Support',
active: true,
},
},
]
checks.each do |check|
group = Group.find(check[:id])
check[:data].each do |key, value|
assert_equal(value, group[key], "group.#{key} for group_id #{check[:id]}")
end
end
end
# check imported organizations
test 'check organizations' do
checks = [
{
id: 1,
data: {
name: 'Zammad Foundation',
note: '',
api_key: nil,
custom_dropdown: nil,
},
},
{
id: 2,
data: {
name: 'Znuny',
note: nil,
api_key: 'my api öäüß',
custom_dropdown: 'b',
},
},
]
checks.each do |check|
organization = Organization.find(check[:id])
check[:data].each do |key, value|
organization_value = organization[key]
text = "organization.#{key} for organization_id #{check[:id]}"
if value.nil?
assert_nil(organization_value, text)
else
assert_equal(value, organization_value, text)
end
end
end
end
# check organization fields
test 'check organization fields' do
local_fields = Organization.column_names
copmare_fields = %w[
id
name
shared
domain
domain_assignment
active
note
updated_by_id
created_by_id
created_at
updated_at
api_key
custom_dropdown
]
assert_equal(copmare_fields, local_fields, 'organization fields')
end
# check imported tickets
test 'check tickets' do
checks = [
{
id: 2,
data: {
title: 'test',
#note: 'This is the first comment. Feel free to delete this sample ticket.',
note: 'test email',
create_article_type_id: 1,
create_article_sender_id: 2,
article_count: 2,
state_id: 3,
group_id: 3,
priority_id: 3,
owner_id: 1,
customer_id: 7,
organization_id: 2,
test_checkbox: true,
custom_integer: 999,
custom_drop_down: 'key2',
custom_decimal: '1.6',
not_existing: nil,
},
},
{
id: 3,
data: {
title: 'Bob Smith, here is the test ticket you requested',
note: 'Hello! This is a Zendesk ticket. We are going to go through the basic support ticket operation in Zendesk.
If you\'re reading this message in your email, click the ticket number link that immediately follows the line \'You have been assigned to this t',
create_article_type_id: 10,
create_article_sender_id: 2,
article_count: 4,
state_id: 3,
group_id: 3,
priority_id: 1,
owner_id: 1,
customer_id: 8,
organization_id: nil,
test_checkbox: false,
custom_integer: nil,
custom_drop_down: '',
custom_decimal: nil,
not_existing: nil,
},
},
{
id: 5,
data: {
title: 'Twitter',
note: "@gabyalanisr Brandon Arely Snuppy Jaz Jerry Liz Irvig & Wera\nY Losa Otrs Yop \npero si quieres Los Que Puedas",
create_article_type_id: 6,
create_article_sender_id: 2,
article_count: 1,
state_id: 1,
group_id: 3,
priority_id: 2,
owner_id: 1,
customer_id: 92,
organization_id: nil,
},
},
{
id: 143,
data: {
title: 'Basti ist cool',
note: 'Basti ist cool',
create_article_type_id: 8,
create_article_sender_id: 2,
article_count: 1,
state_id: 1,
group_id: 1,
priority_id: 2,
owner_id: 1,
customer_id: 144,
organization_id: nil,
},
},
# {
# id: ,
# data: {
# title: ,
# note: ,
# create_article_type_id: ,
# create_article_sender_id: ,
# article_count: ,
# state_id: ,
# group_id: ,
# priority_id: ,
# owner_id: ,
# customer_id: ,
# organization_id: ,
# },
# },
]
checks.each do |check|
ticket = Ticket.find(check[:id])
check[:data].each do |key, value|
ticket_value = ticket[key]
text = "ticket.#{key} for ticket_id #{check[:id]}"
if value.nil?
assert_nil(ticket_value, text)
else
assert_equal(value, ticket_value, text)
end
end
end
end
test 'check article attachments' do
checks = [
{
message_id: 39_984_258_725,
data: {
count: 1,
1 => {
preferences: {
'Content-Type' => 'image/jpeg',
'resizable' => true,
'content_preview' => true
},
filename: '1a3496b9-53d9-494d-bbb0-e1d2e22074f8.jpeg',
},
},
},
{
message_id: 32_817_827_921,
data: {
count: 1,
1 => {
preferences: {
'Content-Type' => 'image/jpeg',
'resizable' => true,
'content_preview' => true
},
filename: 'paris.jpg',
},
},
},
]
checks.each do |check|
article = Ticket::Article.find_by(message_id: check[:message_id])
assert_equal(check[:data][:count], article.attachments.count, 'attachemnt count')
(1..check[:data][:count]).each do |attachment_counter|
attachment = article.attachments[ attachment_counter - 1 ]
compare_attachment = check[:data][ attachment_counter ]
assert_equal(compare_attachment[:filename], attachment.filename, 'attachment file name')
assert_equal(compare_attachment[:preferences], attachment[:preferences], 'attachment preferences')
end
end
end
# check ticket fields
test 'check ticket fields' do
local_fields = Ticket.column_names
copmare_fields = %w[
id
group_id
priority_id
state_id
organization_id
number
title
owner_id
customer_id
note
first_response_at
first_response_escalation_at
first_response_in_min
first_response_diff_in_min
close_at
close_escalation_at
close_in_min
close_diff_in_min
update_escalation_at
update_in_min
update_diff_in_min
last_contact_at
last_contact_agent_at
last_contact_customer_at
last_owner_update_at
create_article_type_id
create_article_sender_id
article_count
escalation_at
pending_time
type
time_unit
preferences
updated_by_id
created_by_id
created_at
updated_at
custom_decimal
test_checkbox
custom_date
custom_integer
custom_regex
custom_drop_down
]
assert_equal(copmare_fields, local_fields, 'ticket fields')
end
end