zendesk_import_test.rb 14 KB

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