zendesk_import_test.rb 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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' => 1,
  26. 'OrganizationFields' => 1,
  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( 5, 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. },
  67. roles: [role_agent, role_admin],
  68. groups: [group_support],
  69. },
  70. {
  71. id: 5,
  72. data: {
  73. firstname: 'Hansimerkur',
  74. lastname: '',
  75. login: '1202726471',
  76. email: 'hansimerkur@znuny.com',
  77. active: true,
  78. },
  79. roles: [role_agent, role_admin],
  80. groups: [group_additional_group, group_support],
  81. },
  82. {
  83. id: 6,
  84. data: {
  85. firstname: 'Bernd',
  86. lastname: 'Hofbecker',
  87. login: '1202726611',
  88. email: 'bernd.hofbecker@znuny.com',
  89. active: true,
  90. },
  91. roles: [role_customer],
  92. groups: [],
  93. },
  94. {
  95. id: 7,
  96. data: {
  97. firstname: 'Zendesk',
  98. lastname: '',
  99. login: '1202737821',
  100. email: 'noreply@zendesk.com',
  101. active: true,
  102. },
  103. roles: [role_customer],
  104. groups: [],
  105. },
  106. {
  107. id: 89,
  108. data: {
  109. firstname: 'Hans',
  110. lastname: 'Peter Wurst',
  111. login: '1205512622',
  112. email: 'hansimerkur+zd-c1@znuny.com',
  113. active: true,
  114. },
  115. roles: [role_customer],
  116. groups: [],
  117. },
  118. ]
  119. checks.each { |check|
  120. user = User.find( check[:id] )
  121. assert_equal( check[:data][:firstname], user.firstname, 'firstname' )
  122. assert_equal( check[:data][:lastname], user.lastname, 'lastname' )
  123. assert_equal( check[:data][:login], user.login, 'login' )
  124. assert_equal( check[:data][:email], user.email, 'email' )
  125. assert_equal( check[:data][:phone], user.phone, 'phone' )
  126. assert_equal( check[:data][:active], user.active, 'active' )
  127. assert_equal( check[:roles], user.roles.to_a, "#{user.login} roles" )
  128. assert_equal( check[:groups], user.groups.to_a, "#{user.login} groups" )
  129. }
  130. end
  131. # check user fields
  132. test 'check user fields' do
  133. local_fields = User.column_names
  134. # TODO
  135. copmare_fields = %w(
  136. id
  137. organization_id
  138. login
  139. firstname
  140. lastname
  141. email
  142. image
  143. image_source
  144. web
  145. password
  146. phone
  147. fax
  148. mobile
  149. department
  150. street
  151. zip
  152. city
  153. country
  154. address
  155. vip
  156. verified
  157. active
  158. note
  159. last_login
  160. source
  161. login_failed
  162. preferences
  163. updated_by_id
  164. created_by_id
  165. created_at
  166. updated_at)
  167. assert_equal( copmare_fields, local_fields, 'user fields' )
  168. end
  169. # check groups/queues
  170. test 'check groups' do
  171. checks = [
  172. {
  173. id: 1,
  174. data: {
  175. name: 'Users',
  176. active: true,
  177. },
  178. },
  179. {
  180. id: 2,
  181. data: {
  182. name: 'Additional Group',
  183. active: true,
  184. },
  185. },
  186. {
  187. id: 3,
  188. data: {
  189. name: 'Support',
  190. active: true,
  191. },
  192. },
  193. ]
  194. checks.each { |check|
  195. group = Group.find( check[:id] )
  196. assert_equal( check[:data][:name], group.name, 'name' )
  197. assert_equal( check[:data][:active], group.active, 'active' )
  198. }
  199. end
  200. # check imported organizations
  201. test 'check organizations' do
  202. checks = [
  203. {
  204. id: 1,
  205. data: {
  206. name: 'Zammad Foundation',
  207. note: '',
  208. },
  209. },
  210. {
  211. id: 2,
  212. data: {
  213. name: 'Znuny',
  214. note: nil,
  215. },
  216. },
  217. ]
  218. checks.each { |check|
  219. organization = Organization.find( check[:id] )
  220. assert_equal( check[:data][:name], organization.name, 'name' )
  221. assert_equal( check[:data][:note], organization.note, 'note' )
  222. }
  223. end
  224. # check organization fields
  225. test 'check organization fields' do
  226. local_fields = Organization.column_names
  227. # TODO
  228. copmare_fields = %w(
  229. id
  230. name
  231. shared
  232. active
  233. note
  234. updated_by_id
  235. created_by_id
  236. created_at
  237. updated_at)
  238. assert_equal( copmare_fields, local_fields, 'organization fields' )
  239. end
  240. # check imported tickets
  241. test 'check tickets' do
  242. checks = [
  243. {
  244. id: 2,
  245. data: {
  246. title: 'test',
  247. note: 'This is the first comment. Feel free to delete this sample ticket.',
  248. create_article_type_id: 1,
  249. create_article_sender_id: 2,
  250. article_count: 2,
  251. state_id: 3,
  252. group_id: 3,
  253. priority_id: 3,
  254. owner_id: 1,
  255. customer_id: 6,
  256. organization_id: 2,
  257. },
  258. },
  259. {
  260. id: 3,
  261. data: {
  262. title: 'Bob Smith, here is the test ticket you requested',
  263. note: 'test email',
  264. create_article_type_id: 10,
  265. create_article_sender_id: 2,
  266. article_count: 4,
  267. state_id: 3,
  268. group_id: 3,
  269. priority_id: 1,
  270. owner_id: 1,
  271. customer_id: 7,
  272. organization_id: nil,
  273. },
  274. },
  275. {
  276. id: 5,
  277. data: {
  278. title: 'Twitter',
  279. note: '@DesafioCaracol sh q acaso sto se vale ver el jueg...',
  280. create_article_type_id: 6,
  281. create_article_sender_id: 2,
  282. article_count: 1,
  283. state_id: 1,
  284. group_id: 3,
  285. priority_id: 2,
  286. owner_id: 1,
  287. customer_id: 91,
  288. organization_id: nil,
  289. },
  290. },
  291. {
  292. id: 143,
  293. data: {
  294. title: 'Basti ist cool',
  295. note: 'Basti ist cool',
  296. create_article_type_id: 8,
  297. create_article_sender_id: 2,
  298. article_count: 1,
  299. state_id: 1,
  300. group_id: 1,
  301. priority_id: 2,
  302. owner_id: 1,
  303. customer_id: 143,
  304. organization_id: nil,
  305. },
  306. },
  307. # {
  308. # id: ,
  309. # data: {
  310. # title: ,
  311. # note: ,
  312. # create_article_type_id: ,
  313. # create_article_sender_id: ,
  314. # article_count: ,
  315. # state_id: ,
  316. # group_id: ,
  317. # priority_id: ,
  318. # owner_id: ,
  319. # customer_id: ,
  320. # organization_id: ,
  321. # },
  322. # },
  323. ]
  324. checks.each { |check|
  325. ticket = Ticket.find( check[:id] )
  326. assert_equal( check[:data][:title], ticket.title, "title of Ticket.find(#{check[:id]})" )
  327. assert_equal( check[:data][:create_article_type_id], ticket.create_article_type_id, "created_article_type_id of Ticket.find(#{check[:id]})" )
  328. assert_equal( check[:data][:create_article_sender_id], ticket.create_article_sender_id, "created_article_sender_id of Ticket.find(#{check[:id]})" )
  329. assert_equal( check[:data][:article_count], ticket.article_count, "article_count of Ticket.find(#{check[:id]})" )
  330. assert_equal( check[:data][:state_id], ticket.state.id, "state_id of Ticket.find(#{check[:id]})" )
  331. assert_equal( check[:data][:group_id], ticket.group.id, "group_id of Ticket.find(#{check[:id]})" )
  332. assert_equal( check[:data][:priority_id], ticket.priority.id, "priority_id of Ticket.find(#{check[:id]})" )
  333. assert_equal( check[:data][:owner_id], ticket.owner.id, "owner_id of Ticket.find(#{check[:id]})" )
  334. assert_equal( check[:data][:customer_id], ticket.customer.id, "customer_id of Ticket.find(#{check[:id]})" )
  335. assert_equal( check[:data][:organization_id], ticket.organization.try(:id), "organization_id of Ticket.find(#{check[:id]})" )
  336. }
  337. end
  338. test 'check article attachments' do
  339. checks = [
  340. {
  341. id: 5,
  342. data: {
  343. count: 1,
  344. 1 => {
  345. preferences: {
  346. 'Content-Type' => 'image/jpeg'
  347. },
  348. filename: '1a3496b9-53d9-494d-bbb0-e1d2e22074f8.jpeg',
  349. },
  350. },
  351. },
  352. {
  353. id: 7,
  354. data: {
  355. count: 1,
  356. 1 => {
  357. preferences: {
  358. 'Content-Type' => 'image/jpeg'
  359. },
  360. filename: 'paris.jpg',
  361. },
  362. },
  363. },
  364. ]
  365. checks.each { |check|
  366. article = Ticket::Article.find(check[:id])
  367. assert_equal( check[:data][:count], article.attachments.count, 'attachemnt count' )
  368. (1..check[:data][:count] ).each { |attachment_counter|
  369. attachment = article.attachments[ attachment_counter - 1 ]
  370. compare_attachment = check[:data][ attachment_counter ]
  371. assert_equal( compare_attachment[:filename], attachment.filename, 'attachment file name' )
  372. assert_equal( compare_attachment[:preferences], attachment[:preferences], 'attachment preferences')
  373. }
  374. }
  375. end
  376. # check ticket fields
  377. test 'check ticket fields' do
  378. local_fields = Ticket.column_names
  379. # TODO
  380. copmare_fields = %w(
  381. id
  382. group_id
  383. priority_id
  384. state_id
  385. organization_id
  386. number
  387. title
  388. owner_id
  389. customer_id
  390. note
  391. first_response
  392. first_response_escal_date
  393. first_response_sla_time
  394. first_response_in_min
  395. first_response_diff_in_min
  396. close_time
  397. close_time_escal_date
  398. close_time_sla_time
  399. close_time_in_min
  400. close_time_diff_in_min
  401. update_time_escal_date
  402. update_time_sla_time
  403. update_time_in_min
  404. update_time_diff_in_min
  405. last_contact
  406. last_contact_agent
  407. last_contact_customer
  408. create_article_type_id
  409. create_article_sender_id
  410. article_count
  411. escalation_time
  412. pending_time
  413. type
  414. preferences
  415. updated_by_id
  416. created_by_id
  417. created_at
  418. updated_at)
  419. assert_equal( copmare_fields, local_fields, 'ticket fields' )
  420. end
  421. end