zendesk_import_test.rb 13 KB

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