zendesk_import_test.rb 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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: 142,
  37. updated: 1,
  38. unchanged: 0,
  39. failed: 0,
  40. deactivated: 0,
  41. sum: 143,
  42. total: 143
  43. },
  44. Organizations: {
  45. skipped: 0,
  46. created: 1,
  47. updated: 0,
  48. unchanged: 1,
  49. failed: 0,
  50. deactivated: 0,
  51. sum: 2,
  52. total: 2
  53. },
  54. Tickets: {
  55. skipped: 1,
  56. created: 142,
  57. updated: 2,
  58. unchanged: 0,
  59. failed: 0,
  60. deactivated: 0,
  61. sum: 145,
  62. total: 145
  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(145, 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(143, Ticket.count, 'tickets')
  74. assert_equal(152, 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: 145,
  88. data: {
  89. firstname: 'Bob Smith',
  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: 142,
  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: 6,
  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: 143,
  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: 5,
  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. number: '2',
  303. title: 'test',
  304. note: nil,
  305. create_article_type_id: 1,
  306. create_article_sender_id: 2,
  307. article_count: 2,
  308. state_id: 3,
  309. group_id: 3,
  310. priority_id: 3,
  311. owner_id: User.find_by(login: 'bob.smith@znuny.com').id,
  312. customer_id: User.find_by(login: 'bernd.hofbecker@znuny.com').id,
  313. organization_id: 2,
  314. test_checkbox: true,
  315. custom_integer: 999,
  316. custom_dropdown: 'key2',
  317. custom_decimal: '1.6',
  318. not_existing: nil,
  319. },
  320. },
  321. {
  322. id: 3,
  323. data: {
  324. number: '3',
  325. title: 'Bob Smith, here is the test ticket you requested',
  326. note: nil,
  327. create_article_type_id: 10,
  328. create_article_sender_id: 2,
  329. article_count: 5,
  330. state_id: 3,
  331. group_id: 3,
  332. priority_id: 1,
  333. owner_id: User.find_by(login: 'bob.smith@znuny.com').id,
  334. customer_id: User.find_by(login: 'noreply@zendesk.com').id,
  335. organization_id: nil,
  336. test_checkbox: false,
  337. custom_integer: nil,
  338. custom_dropdown: '',
  339. custom_decimal: nil,
  340. not_existing: nil,
  341. },
  342. },
  343. {
  344. id: 5,
  345. data: {
  346. number: '5',
  347. title: 'Twitter',
  348. note: nil,
  349. create_article_type_id: 6,
  350. create_article_sender_id: 2,
  351. article_count: 1,
  352. state_id: 1,
  353. group_id: 3,
  354. priority_id: 2,
  355. owner_id: User.find_by(login: '-').id,
  356. customer_id: 69,
  357. organization_id: nil,
  358. },
  359. },
  360. {
  361. id: 143,
  362. data: {
  363. number: '143',
  364. title: 'Basti ist cool',
  365. note: nil,
  366. create_article_type_id: 8,
  367. create_article_sender_id: 2,
  368. article_count: 1,
  369. state_id: 1,
  370. group_id: 1,
  371. priority_id: 2,
  372. owner_id: User.find_by(login: '-').id,
  373. customer_id: 7,
  374. organization_id: nil,
  375. },
  376. },
  377. {
  378. id: 145,
  379. data: {
  380. number: '145',
  381. title: 'closed ticket - should be archived and imported',
  382. note: nil,
  383. create_article_type_id: 11,
  384. create_article_sender_id: 1,
  385. article_count: 1,
  386. state_id: Ticket::State.find_by(name: 'closed').id,
  387. group_id: Group.find_by(name: 'Additional Group').id,
  388. priority_id: Ticket::Priority.find_by(name: '2 normal').id,
  389. owner_id: User.find_by(login: 'hansimerkur@znuny.com').id,
  390. customer_id: User.find_by(login: 'bob.smith@znuny.com').id,
  391. organization_id: Organization.find_by(name: 'Znuny').id,
  392. },
  393. },
  394. # {
  395. # id: ,
  396. # data: {
  397. # title: ,
  398. # note: ,
  399. # create_article_type_id: ,
  400. # create_article_sender_id: ,
  401. # article_count: ,
  402. # state_id: ,
  403. # group_id: ,
  404. # priority_id: ,
  405. # owner_id: ,
  406. # customer_id: ,
  407. # organization_id: ,
  408. # },
  409. # },
  410. ]
  411. checks.each do |check|
  412. ticket = Ticket.find(check[:id])
  413. check[:data].each do |key, value|
  414. ticket_value = ticket[key]
  415. text = "ticket.#{key} for ticket_id #{check[:id]}"
  416. if value.nil?
  417. assert_nil(ticket_value, text)
  418. else
  419. assert_equal(value, ticket_value, text)
  420. end
  421. end
  422. end
  423. end
  424. test 'check article attachments' do
  425. checks = [
  426. {
  427. message_id: 39_984_258_725,
  428. data: {
  429. count: 1,
  430. 1 => {
  431. preferences: {
  432. 'Content-Type' => 'image/jpeg',
  433. 'resizable' => true,
  434. 'content_preview' => true
  435. },
  436. filename: '1a3496b9-53d9-494d-bbb0-e1d2e22074f8.jpeg',
  437. },
  438. },
  439. },
  440. {
  441. message_id: 32_817_827_921,
  442. data: {
  443. count: 1,
  444. 1 => {
  445. preferences: {
  446. 'Content-Type' => 'image/jpeg',
  447. 'resizable' => true,
  448. 'content_preview' => true
  449. },
  450. filename: 'paris.jpg',
  451. },
  452. },
  453. },
  454. {
  455. message_id: 538_901_840_720,
  456. data: {
  457. count: 1,
  458. 1 => {
  459. preferences: {
  460. 'Content-Type' => 'text/rtf'
  461. },
  462. filename: 'test.rtf',
  463. },
  464. },
  465. },
  466. ]
  467. checks.each do |check|
  468. article = Ticket::Article.find_by(message_id: check[:message_id])
  469. assert_equal(check[:data][:count], article.attachments.count, 'attachemnt count')
  470. (1..check[:data][:count]).each do |attachment_counter|
  471. attachment = article.attachments[ attachment_counter - 1 ]
  472. compare_attachment = check[:data][ attachment_counter ]
  473. assert_equal(compare_attachment[:filename], attachment.filename, 'attachment file name')
  474. assert_equal(compare_attachment[:preferences], attachment[:preferences], 'attachment preferences')
  475. end
  476. end
  477. end
  478. # check ticket fields
  479. test 'check ticket fields' do
  480. local_fields = Ticket.column_names
  481. copmare_fields = %w[
  482. id
  483. group_id
  484. priority_id
  485. state_id
  486. organization_id
  487. number
  488. title
  489. owner_id
  490. customer_id
  491. note
  492. first_response_at
  493. first_response_escalation_at
  494. first_response_in_min
  495. first_response_diff_in_min
  496. close_at
  497. close_escalation_at
  498. close_in_min
  499. close_diff_in_min
  500. update_escalation_at
  501. update_in_min
  502. update_diff_in_min
  503. last_contact_at
  504. last_contact_agent_at
  505. last_contact_customer_at
  506. last_owner_update_at
  507. create_article_type_id
  508. create_article_sender_id
  509. article_count
  510. escalation_at
  511. pending_time
  512. type
  513. time_unit
  514. preferences
  515. updated_by_id
  516. created_by_id
  517. created_at
  518. updated_at
  519. custom_decimal
  520. test_checkbox
  521. custom_date
  522. custom_integer
  523. custom_regex
  524. custom_dropdown
  525. ]
  526. assert_equal(copmare_fields, local_fields, 'ticket fields')
  527. end
  528. end