zendesk_import_test.rb 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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: 142,
  57. updated: 1,
  58. unchanged: 0,
  59. failed: 0,
  60. deactivated: 0,
  61. sum: 143,
  62. total: 143
  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(143, Ticket.count, 'tickets')
  74. assert_equal(151, Ticket::Article.count, 'ticket articles')
  75. assert_equal(2, 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: 'This is the first comment. Feel free to delete this sample ticket.',
  304. note: 'test email',
  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: 1,
  312. customer_id: 7,
  313. organization_id: 2,
  314. test_checkbox: true,
  315. custom_integer: 999,
  316. custom_drop_down: 'key2',
  317. custom_decimal: '1.6',
  318. not_existing: nil,
  319. },
  320. },
  321. {
  322. id: 3,
  323. data: {
  324. title: 'Bob Smith, here is the test ticket you requested',
  325. note: 'Hello! This is a Zendesk ticket. We are going to go through the basic support ticket operation in Zendesk.
  326. 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',
  327. create_article_type_id: 10,
  328. create_article_sender_id: 2,
  329. article_count: 4,
  330. state_id: 3,
  331. group_id: 3,
  332. priority_id: 1,
  333. owner_id: 1,
  334. customer_id: 8,
  335. organization_id: nil,
  336. test_checkbox: false,
  337. custom_integer: nil,
  338. custom_drop_down: '',
  339. custom_decimal: nil,
  340. not_existing: nil,
  341. },
  342. },
  343. {
  344. id: 5,
  345. data: {
  346. title: 'Twitter',
  347. note: "@gabyalanisr Brandon Arely Snuppy Jaz Jerry Liz Irvig &amp; Wera\nY Losa Otrs Yop \npero si quieres Los Que Puedas",
  348. create_article_type_id: 6,
  349. create_article_sender_id: 2,
  350. article_count: 1,
  351. state_id: 1,
  352. group_id: 3,
  353. priority_id: 2,
  354. owner_id: 1,
  355. customer_id: 92,
  356. organization_id: nil,
  357. },
  358. },
  359. {
  360. id: 143,
  361. data: {
  362. title: 'Basti ist cool',
  363. note: 'Basti ist cool',
  364. create_article_type_id: 8,
  365. create_article_sender_id: 2,
  366. article_count: 1,
  367. state_id: 1,
  368. group_id: 1,
  369. priority_id: 2,
  370. owner_id: 1,
  371. customer_id: 144,
  372. organization_id: nil,
  373. },
  374. },
  375. # {
  376. # id: ,
  377. # data: {
  378. # title: ,
  379. # note: ,
  380. # create_article_type_id: ,
  381. # create_article_sender_id: ,
  382. # article_count: ,
  383. # state_id: ,
  384. # group_id: ,
  385. # priority_id: ,
  386. # owner_id: ,
  387. # customer_id: ,
  388. # organization_id: ,
  389. # },
  390. # },
  391. ]
  392. checks.each do |check|
  393. ticket = Ticket.find(check[:id])
  394. check[:data].each do |key, value|
  395. ticket_value = ticket[key]
  396. text = "ticket.#{key} for ticket_id #{check[:id]}"
  397. if value.nil?
  398. assert_nil(ticket_value, text)
  399. else
  400. assert_equal(value, ticket_value, text)
  401. end
  402. end
  403. end
  404. end
  405. test 'check article attachments' do
  406. checks = [
  407. {
  408. message_id: 39_984_258_725,
  409. data: {
  410. count: 1,
  411. 1 => {
  412. preferences: {
  413. 'Content-Type' => 'image/jpeg',
  414. 'resizable' => true,
  415. 'content_preview' => true
  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. 'resizable' => true,
  429. 'content_preview' => true
  430. },
  431. filename: 'paris.jpg',
  432. },
  433. },
  434. },
  435. ]
  436. checks.each do |check|
  437. article = Ticket::Article.find_by(message_id: check[:message_id])
  438. assert_equal(check[:data][:count], article.attachments.count, 'attachemnt count')
  439. (1..check[:data][:count]).each do |attachment_counter|
  440. attachment = article.attachments[ attachment_counter - 1 ]
  441. compare_attachment = check[:data][ attachment_counter ]
  442. assert_equal(compare_attachment[:filename], attachment.filename, 'attachment file name')
  443. assert_equal(compare_attachment[:preferences], attachment[:preferences], 'attachment preferences')
  444. end
  445. end
  446. end
  447. # check ticket fields
  448. test 'check ticket fields' do
  449. local_fields = Ticket.column_names
  450. copmare_fields = %w[
  451. id
  452. group_id
  453. priority_id
  454. state_id
  455. organization_id
  456. number
  457. title
  458. owner_id
  459. customer_id
  460. note
  461. first_response_at
  462. first_response_escalation_at
  463. first_response_in_min
  464. first_response_diff_in_min
  465. close_at
  466. close_escalation_at
  467. close_in_min
  468. close_diff_in_min
  469. update_escalation_at
  470. update_in_min
  471. update_diff_in_min
  472. last_contact_at
  473. last_contact_agent_at
  474. last_contact_customer_at
  475. last_owner_update_at
  476. create_article_type_id
  477. create_article_sender_id
  478. article_count
  479. escalation_at
  480. pending_time
  481. type
  482. time_unit
  483. preferences
  484. updated_by_id
  485. created_by_id
  486. created_at
  487. updated_at
  488. custom_decimal
  489. test_checkbox
  490. custom_date
  491. custom_integer
  492. custom_regex
  493. custom_drop_down
  494. ]
  495. assert_equal(copmare_fields, local_fields, 'ticket fields')
  496. end
  497. end