zendesk_import_test.rb 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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: '1150734731',
  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: '1202726471',
  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: '1202726611',
  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: '1202737821',
  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: '1205512622',
  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. active
  239. note
  240. updated_by_id
  241. created_by_id
  242. created_at
  243. updated_at
  244. api_key
  245. custom_dropdown
  246. )
  247. assert_equal(copmare_fields, local_fields, 'organization fields')
  248. end
  249. # check imported tickets
  250. test 'check tickets' do
  251. checks = [
  252. {
  253. id: 2,
  254. data: {
  255. title: 'test',
  256. #note: 'This is the first comment. Feel free to delete this sample ticket.',
  257. note: 'test email',
  258. create_article_type_id: 1,
  259. create_article_sender_id: 2,
  260. article_count: 2,
  261. state_id: 3,
  262. group_id: 3,
  263. priority_id: 3,
  264. owner_id: 1,
  265. customer_id: 6,
  266. organization_id: 2,
  267. test_checkbox: true,
  268. custom_integer: 999,
  269. custom_dropdown: 'key2',
  270. custom_decimal: '1.6',
  271. not_existing: nil,
  272. },
  273. },
  274. {
  275. id: 3,
  276. data: {
  277. title: 'Bob Smith, here is the test ticket you requested',
  278. note: 'Hello! This is a Zendesk ticket. We are going to go through the basic support ticket operation in Zendesk.
  279. 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',
  280. create_article_type_id: 10,
  281. create_article_sender_id: 2,
  282. article_count: 4,
  283. state_id: 3,
  284. group_id: 3,
  285. priority_id: 1,
  286. owner_id: 1,
  287. customer_id: 7,
  288. organization_id: nil,
  289. test_checkbox: false,
  290. custom_integer: nil,
  291. custom_dropdown: '',
  292. custom_decimal: nil,
  293. not_existing: nil,
  294. },
  295. },
  296. {
  297. id: 5,
  298. data: {
  299. title: 'Twitter',
  300. note: "@gabyalanisr Brandon Arely Snuppy Jaz Jerry Liz Irvig &amp; Wera\nY Losa Otrs Yop \npero si quieres Los Que Puedas",
  301. create_article_type_id: 6,
  302. create_article_sender_id: 2,
  303. article_count: 1,
  304. state_id: 1,
  305. group_id: 3,
  306. priority_id: 2,
  307. owner_id: 1,
  308. customer_id: 91,
  309. organization_id: nil,
  310. },
  311. },
  312. {
  313. id: 143,
  314. data: {
  315. title: 'Basti ist cool',
  316. note: 'Basti ist cool',
  317. create_article_type_id: 8,
  318. create_article_sender_id: 2,
  319. article_count: 1,
  320. state_id: 1,
  321. group_id: 1,
  322. priority_id: 2,
  323. owner_id: 1,
  324. customer_id: 143,
  325. organization_id: nil,
  326. },
  327. },
  328. # {
  329. # id: ,
  330. # data: {
  331. # title: ,
  332. # note: ,
  333. # create_article_type_id: ,
  334. # create_article_sender_id: ,
  335. # article_count: ,
  336. # state_id: ,
  337. # group_id: ,
  338. # priority_id: ,
  339. # owner_id: ,
  340. # customer_id: ,
  341. # organization_id: ,
  342. # },
  343. # },
  344. ]
  345. checks.each { |check|
  346. ticket = Ticket.find(check[:id])
  347. check[:data].each { |key, value|
  348. assert_equal(value, ticket[key], "ticket.#{key} for ticket_id #{check[:id]}")
  349. }
  350. }
  351. end
  352. test 'check article attachments' do
  353. checks = [
  354. {
  355. id: 5,
  356. data: {
  357. count: 1,
  358. 1 => {
  359. preferences: {
  360. 'Content-Type' => 'image/jpeg'
  361. },
  362. filename: '1a3496b9-53d9-494d-bbb0-e1d2e22074f8.jpeg',
  363. },
  364. },
  365. },
  366. {
  367. id: 7,
  368. data: {
  369. count: 1,
  370. 1 => {
  371. preferences: {
  372. 'Content-Type' => 'image/jpeg'
  373. },
  374. filename: 'paris.jpg',
  375. },
  376. },
  377. },
  378. ]
  379. checks.each { |check|
  380. article = Ticket::Article.find(check[:id])
  381. assert_equal(check[:data][:count], article.attachments.count, 'attachemnt count')
  382. (1..check[:data][:count]).each { |attachment_counter|
  383. attachment = article.attachments[ attachment_counter - 1 ]
  384. compare_attachment = check[:data][ attachment_counter ]
  385. assert_equal(compare_attachment[:filename], attachment.filename, 'attachment file name')
  386. assert_equal(compare_attachment[:preferences], attachment[:preferences], 'attachment preferences')
  387. }
  388. }
  389. end
  390. # check ticket fields
  391. test 'check ticket fields' do
  392. local_fields = Ticket.column_names
  393. copmare_fields = %w(
  394. id
  395. group_id
  396. priority_id
  397. state_id
  398. organization_id
  399. number
  400. title
  401. owner_id
  402. customer_id
  403. note
  404. first_response_at
  405. first_response_escalation_at
  406. first_response_in_min
  407. first_response_diff_in_min
  408. close_at
  409. close_escalation_at
  410. close_in_min
  411. close_diff_in_min
  412. update_escalation_at
  413. update_in_min
  414. update_diff_in_min
  415. last_contact_at
  416. last_contact_agent_at
  417. last_contact_customer_at
  418. create_article_type_id
  419. create_article_sender_id
  420. article_count
  421. escalation_at
  422. pending_time
  423. type
  424. preferences
  425. updated_by_id
  426. created_by_id
  427. created_at
  428. updated_at
  429. custom_decimal
  430. test_checkbox
  431. custom_date
  432. custom_integer
  433. custom_regex
  434. custom_dropdown
  435. )
  436. assert_equal(copmare_fields, local_fields, 'ticket fields')
  437. end
  438. end