zendesk_import_test.rb 13 KB

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