zendesk_import_test.rb 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. require 'integration_test_helper'
  2. class ZendeskImportTest < ActiveSupport::TestCase
  3. self.test_order = :sorted
  4. if !ENV['IMPORT_ZENDESK_ENDPOINT']
  5. raise "ERROR: Need IMPORT_ZENDESK_ENDPOINT - hint IMPORT_ZENDESK_ENDPOINT='https://example.zendesk.com/api/v2'"
  6. end
  7. if !ENV['IMPORT_ZENDESK_ENDPOINT_KEY']
  8. raise "ERROR: Need IMPORT_ZENDESK_ENDPOINT_KEY - hint IMPORT_ZENDESK_ENDPOINT_KEY='01234567899876543210'"
  9. end
  10. if !ENV['IMPORT_ZENDESK_ENDPOINT_USERNAME']
  11. raise "ERROR: Need IMPORT_ZENDESK_ENDPOINT_USERNAME - hint IMPORT_ZENDESK_ENDPOINT_USERNAME='bob.ross@happylittletrees.com'"
  12. end
  13. Setting.set('import_zendesk_endpoint', ENV['IMPORT_ZENDESK_ENDPOINT'])
  14. Setting.set('import_zendesk_endpoint_key', ENV['IMPORT_ZENDESK_ENDPOINT_KEY'])
  15. Setting.set('import_zendesk_endpoint_username', ENV['IMPORT_ZENDESK_ENDPOINT_USERNAME'])
  16. Setting.set('import_mode', true)
  17. Setting.set('system_init_done', false)
  18. job = ImportJob.create(name: 'Import::Zendesk')
  19. job.start
  20. # check statistic count
  21. test 'check statistic' do
  22. # retrive statistic
  23. compare_statistic = {
  24. Groups: {
  25. skipped: 0,
  26. created: 2,
  27. updated: 0,
  28. unchanged: 0,
  29. failed: 0,
  30. deactivated: 0,
  31. sum: 2,
  32. total: 2
  33. },
  34. Users: {
  35. skipped: 0,
  36. created: 141,
  37. updated: 0,
  38. unchanged: 0,
  39. failed: 0,
  40. deactivated: 0,
  41. sum: 141,
  42. total: 141
  43. },
  44. Organizations: {
  45. skipped: 0,
  46. created: 1,
  47. updated: 0,
  48. unchanged: 0,
  49. failed: 0,
  50. deactivated: 0,
  51. sum: 1,
  52. total: 1
  53. },
  54. Tickets: {
  55. skipped: 0,
  56. created: 141,
  57. updated: 1,
  58. unchanged: 0,
  59. failed: 0,
  60. deactivated: 0,
  61. sum: 142,
  62. total: 142
  63. }
  64. }
  65. assert_equal(compare_statistic.with_indifferent_access, job.result, 'statistic')
  66. end
  67. # check count of imported items
  68. test 'check counts' do
  69. assert_equal(144, User.count, 'users')
  70. assert_equal(3, Group.count, 'groups')
  71. assert_equal(3, Role.count, 'roles')
  72. assert_equal(2, Organization.count, 'organizations')
  73. assert_equal(142, Ticket.count, 'tickets')
  74. assert_equal(151, Ticket::Article.count, 'ticket articles')
  75. assert_equal(3, Store.count, 'ticket article attachments')
  76. # TODO: Macros, Views, Automations...
  77. end
  78. # check imported users and permission
  79. test 'check users' do
  80. role_admin = Role.find_by(name: 'Admin')
  81. role_agent = Role.find_by(name: 'Agent')
  82. role_customer = Role.find_by(name: 'Customer')
  83. group_support = Group.find_by(name: 'Support')
  84. group_additional_group = Group.find_by(name: 'Additional Group')
  85. checks = [
  86. {
  87. id: 5,
  88. data: {
  89. firstname: 'Bob',
  90. lastname: 'Smith',
  91. login: 'bob.smith@znuny.com',
  92. email: 'bob.smith@znuny.com',
  93. active: true,
  94. phone: '00114124',
  95. lieblingstier: 'Hundä',
  96. },
  97. roles: [role_admin, role_agent],
  98. groups: [group_support],
  99. },
  100. {
  101. id: 6,
  102. data: {
  103. firstname: 'Hansimerkur',
  104. lastname: '',
  105. login: 'hansimerkur@znuny.com',
  106. email: 'hansimerkur@znuny.com',
  107. active: true,
  108. lieblingstier: nil,
  109. },
  110. roles: [role_admin, role_agent],
  111. groups: [group_additional_group, group_support],
  112. },
  113. {
  114. id: 7,
  115. data: {
  116. firstname: 'Bernd',
  117. lastname: 'Hofbecker',
  118. login: 'bernd.hofbecker@znuny.com',
  119. email: 'bernd.hofbecker@znuny.com',
  120. active: true,
  121. },
  122. roles: [role_customer],
  123. groups: [],
  124. },
  125. {
  126. id: 8,
  127. data: {
  128. firstname: 'Zendesk',
  129. lastname: '',
  130. login: 'noreply@zendesk.com',
  131. email: 'noreply@zendesk.com',
  132. active: true,
  133. },
  134. roles: [role_customer],
  135. groups: [],
  136. },
  137. {
  138. id: 90,
  139. data: {
  140. firstname: 'Hans',
  141. lastname: 'Peter Wurst',
  142. login: 'hansimerkur+zd-c1@znuny.com',
  143. email: 'hansimerkur+zd-c1@znuny.com',
  144. active: true,
  145. },
  146. roles: [role_customer],
  147. groups: [],
  148. },
  149. ]
  150. checks.each do |check|
  151. user = User.find(check[:id])
  152. check[:data].each do |key, value|
  153. user_value = user[key]
  154. text = "user.#{key} for user_id #{check[:id]}"
  155. if value.nil?
  156. assert_nil(user_value, text)
  157. else
  158. assert_equal(value, user_value, text)
  159. end
  160. end
  161. assert_equal(check[:roles], user.roles.sort.to_a, "#{user.login} roles")
  162. assert_equal(check[:groups], user.groups_access('full').sort.to_a, "#{user.login} groups")
  163. end
  164. end
  165. # check user fields
  166. test 'check user fields' do
  167. local_fields = User.column_names
  168. copmare_fields = %w[
  169. id
  170. organization_id
  171. login
  172. firstname
  173. lastname
  174. email
  175. image
  176. image_source
  177. web
  178. password
  179. phone
  180. fax
  181. mobile
  182. department
  183. street
  184. zip
  185. city
  186. country
  187. address
  188. vip
  189. verified
  190. active
  191. note
  192. last_login
  193. source
  194. login_failed
  195. out_of_office
  196. out_of_office_start_at
  197. out_of_office_end_at
  198. out_of_office_replacement_id
  199. preferences
  200. updated_by_id
  201. created_by_id
  202. created_at
  203. updated_at
  204. lieblingstier
  205. custom_dropdown
  206. ]
  207. assert_equal(copmare_fields, local_fields, 'user fields')
  208. end
  209. # check groups/queues
  210. test 'check groups' do
  211. checks = [
  212. {
  213. id: 1,
  214. data: {
  215. name: 'Users',
  216. active: true,
  217. },
  218. },
  219. {
  220. id: 2,
  221. data: {
  222. name: 'Additional Group',
  223. active: true,
  224. },
  225. },
  226. {
  227. id: 3,
  228. data: {
  229. name: 'Support',
  230. active: true,
  231. },
  232. },
  233. ]
  234. checks.each do |check|
  235. group = Group.find(check[:id])
  236. check[:data].each do |key, value|
  237. assert_equal(value, group[key], "group.#{key} for group_id #{check[:id]}")
  238. end
  239. end
  240. end
  241. # check imported organizations
  242. test 'check organizations' do
  243. checks = [
  244. {
  245. id: 1,
  246. data: {
  247. name: 'Zammad Foundation',
  248. note: '',
  249. api_key: nil,
  250. custom_dropdown: nil,
  251. },
  252. },
  253. {
  254. id: 2,
  255. data: {
  256. name: 'Znuny',
  257. note: nil,
  258. api_key: 'my api öäüß',
  259. custom_dropdown: 'b',
  260. },
  261. },
  262. ]
  263. checks.each do |check|
  264. organization = Organization.find(check[:id])
  265. check[:data].each do |key, value|
  266. organization_value = organization[key]
  267. text = "organization.#{key} for organization_id #{check[:id]}"
  268. if value.nil?
  269. assert_nil(organization_value, text)
  270. else
  271. assert_equal(value, organization_value, text)
  272. end
  273. end
  274. end
  275. end
  276. # check organization fields
  277. test 'check organization fields' do
  278. local_fields = Organization.column_names
  279. copmare_fields = %w[
  280. id
  281. name
  282. shared
  283. domain
  284. domain_assignment
  285. active
  286. note
  287. updated_by_id
  288. created_by_id
  289. created_at
  290. updated_at
  291. api_key
  292. custom_dropdown
  293. ]
  294. assert_equal(copmare_fields, local_fields, 'organization fields')
  295. end
  296. # check imported tickets
  297. test 'check tickets' do
  298. checks = [
  299. {
  300. id: 2,
  301. data: {
  302. title: 'test',
  303. note: nil,
  304. create_article_type_id: 1,
  305. create_article_sender_id: 2,
  306. article_count: 2,
  307. state_id: 3,
  308. group_id: 3,
  309. priority_id: 3,
  310. owner_id: User.find_by(login: 'bob.smith@znuny.com').id,
  311. customer_id: 7,
  312. organization_id: 2,
  313. test_checkbox: true,
  314. custom_integer: 999,
  315. custom_dropdown: 'key2',
  316. custom_decimal: '1.6',
  317. not_existing: nil,
  318. },
  319. },
  320. {
  321. id: 3,
  322. data: {
  323. title: 'Bob Smith, here is the test ticket you requested',
  324. note: nil,
  325. create_article_type_id: 10,
  326. create_article_sender_id: 2,
  327. article_count: 5,
  328. state_id: 3,
  329. group_id: 3,
  330. priority_id: 1,
  331. owner_id: User.find_by(login: 'bob.smith@znuny.com').id,
  332. customer_id: 8,
  333. organization_id: nil,
  334. test_checkbox: false,
  335. custom_integer: nil,
  336. custom_dropdown: '',
  337. custom_decimal: nil,
  338. not_existing: nil,
  339. },
  340. },
  341. {
  342. id: 5,
  343. data: {
  344. title: 'Twitter',
  345. note: nil,
  346. create_article_type_id: 6,
  347. create_article_sender_id: 2,
  348. article_count: 1,
  349. state_id: 1,
  350. group_id: 3,
  351. priority_id: 2,
  352. owner_id: 1,
  353. customer_id: 92,
  354. organization_id: nil,
  355. },
  356. },
  357. {
  358. id: 143,
  359. data: {
  360. title: 'Basti ist cool',
  361. note: nil,
  362. create_article_type_id: 8,
  363. create_article_sender_id: 2,
  364. article_count: 1,
  365. state_id: 1,
  366. group_id: 1,
  367. priority_id: 2,
  368. owner_id: 1,
  369. customer_id: 144,
  370. organization_id: nil,
  371. },
  372. },
  373. # {
  374. # id: ,
  375. # data: {
  376. # title: ,
  377. # note: ,
  378. # create_article_type_id: ,
  379. # create_article_sender_id: ,
  380. # article_count: ,
  381. # state_id: ,
  382. # group_id: ,
  383. # priority_id: ,
  384. # owner_id: ,
  385. # customer_id: ,
  386. # organization_id: ,
  387. # },
  388. # },
  389. ]
  390. checks.each do |check|
  391. ticket = Ticket.find(check[:id])
  392. check[:data].each do |key, value|
  393. ticket_value = ticket[key]
  394. text = "ticket.#{key} for ticket_id #{check[:id]}"
  395. if value.nil?
  396. assert_nil(ticket_value, text)
  397. else
  398. assert_equal(value, ticket_value, text)
  399. end
  400. end
  401. end
  402. end
  403. test 'check article attachments' do
  404. checks = [
  405. {
  406. message_id: 39_984_258_725,
  407. data: {
  408. count: 1,
  409. 1 => {
  410. preferences: {
  411. 'Content-Type' => 'image/jpeg',
  412. 'resizable' => true,
  413. 'content_preview' => true
  414. },
  415. filename: '1a3496b9-53d9-494d-bbb0-e1d2e22074f8.jpeg',
  416. },
  417. },
  418. },
  419. {
  420. message_id: 32_817_827_921,
  421. data: {
  422. count: 1,
  423. 1 => {
  424. preferences: {
  425. 'Content-Type' => 'image/jpeg',
  426. 'resizable' => true,
  427. 'content_preview' => true
  428. },
  429. filename: 'paris.jpg',
  430. },
  431. },
  432. },
  433. {
  434. message_id: 538_901_840_720,
  435. data: {
  436. count: 1,
  437. 1 => {
  438. preferences: {
  439. 'Content-Type' => 'text/rtf'
  440. },
  441. filename: 'test.rtf',
  442. },
  443. },
  444. },
  445. ]
  446. checks.each do |check|
  447. article = Ticket::Article.find_by(message_id: check[:message_id])
  448. assert_equal(check[:data][:count], article.attachments.count, 'attachemnt count')
  449. (1..check[:data][:count]).each do |attachment_counter|
  450. attachment = article.attachments[ attachment_counter - 1 ]
  451. compare_attachment = check[:data][ attachment_counter ]
  452. assert_equal(compare_attachment[:filename], attachment.filename, 'attachment file name')
  453. assert_equal(compare_attachment[:preferences], attachment[:preferences], 'attachment preferences')
  454. end
  455. end
  456. end
  457. # check ticket fields
  458. test 'check ticket fields' do
  459. local_fields = Ticket.column_names
  460. copmare_fields = %w[
  461. id
  462. group_id
  463. priority_id
  464. state_id
  465. organization_id
  466. number
  467. title
  468. owner_id
  469. customer_id
  470. note
  471. first_response_at
  472. first_response_escalation_at
  473. first_response_in_min
  474. first_response_diff_in_min
  475. close_at
  476. close_escalation_at
  477. close_in_min
  478. close_diff_in_min
  479. update_escalation_at
  480. update_in_min
  481. update_diff_in_min
  482. last_contact_at
  483. last_contact_agent_at
  484. last_contact_customer_at
  485. last_owner_update_at
  486. create_article_type_id
  487. create_article_sender_id
  488. article_count
  489. escalation_at
  490. pending_time
  491. type
  492. time_unit
  493. preferences
  494. updated_by_id
  495. created_by_id
  496. created_at
  497. updated_at
  498. custom_decimal
  499. test_checkbox
  500. custom_date
  501. custom_integer
  502. custom_regex
  503. custom_dropdown
  504. ]
  505. assert_equal(copmare_fields, local_fields, 'ticket fields')
  506. end
  507. end