zendesk_import_test.rb 14 KB

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