ticket_csv_import_test.rb 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. require 'test_helper'
  3. class TicketCsvImportTest < ActiveSupport::TestCase
  4. test 'import example verify' do
  5. csv_string = Ticket.csv_example
  6. rows = CSV.parse(csv_string)
  7. header = rows.shift
  8. assert_equal('id', header[0])
  9. assert_equal('number', header[1])
  10. assert_equal('title', header[2])
  11. assert_equal('note', header[3])
  12. assert_equal('first_response_at', header[4])
  13. assert_equal('first_response_escalation_at', header[5])
  14. assert(header.include?('organization'))
  15. assert(header.include?('priority'))
  16. assert(header.include?('state'))
  17. assert(header.include?('owner'))
  18. assert(header.include?('customer'))
  19. end
  20. test 'empty payload' do
  21. csv_string = ''
  22. result = Ticket.csv_import(
  23. string: csv_string,
  24. parse_params: {
  25. col_sep: ';',
  26. },
  27. try: true,
  28. )
  29. assert_equal(true, result[:try])
  30. assert_nil(result[:records])
  31. assert_equal('failed', result[:result])
  32. assert_equal('Unable to parse empty file/string for Ticket.', result[:errors][0])
  33. csv_string = 'id;number;title;state;priority;'
  34. result = Ticket.csv_import(
  35. string: csv_string,
  36. parse_params: {
  37. col_sep: ';',
  38. },
  39. try: true,
  40. )
  41. assert_equal(true, result[:try])
  42. assert(result[:records].blank?)
  43. assert_equal('failed', result[:result])
  44. assert_equal('No records found in file/string for Ticket.', result[:errors][0])
  45. end
  46. test 'verify required lookup headers' do
  47. csv_string = "firstname;lastname;active;\nfirstname-simple-import1;lastname-simple-import1;;true\nfirstname-simple-import2;lastname-simple-import2;false\n"
  48. result = Ticket.csv_import(
  49. string: csv_string,
  50. parse_params: {
  51. col_sep: ';',
  52. },
  53. try: true,
  54. )
  55. assert_equal(true, result[:try])
  56. assert_equal('failed', result[:result])
  57. assert_equal('No lookup column like id,number for Ticket found.', result[:errors][0])
  58. end
  59. test 'simple import' do
  60. csv_string = "id;number;title;state;priority;owner;customer;group;note\n;123456;some title1;new;2 normal;-;nicole.braun@zammad.org;Users;some note1\n;123457;some title2;closed;1 low;admin@example.com;nicole.braun@zammad.org;Users;some note2\n"
  61. result = Ticket.csv_import(
  62. string: csv_string,
  63. parse_params: {
  64. col_sep: ';',
  65. },
  66. try: true,
  67. )
  68. assert_equal(true, result[:try])
  69. assert_equal(2, result[:records].count)
  70. assert_equal('success', result[:result])
  71. assert_nil(Ticket.find_by(number: '123456'))
  72. assert_nil(Ticket.find_by(number: '123457'))
  73. result = Ticket.csv_import(
  74. string: csv_string,
  75. parse_params: {
  76. col_sep: ';',
  77. },
  78. try: false,
  79. )
  80. assert_equal(false, result[:try])
  81. assert_equal(2, result[:records].count)
  82. assert_equal('success', result[:result])
  83. ticket1 = Ticket.find_by(number: '123456')
  84. assert(ticket1)
  85. assert_equal(ticket1.number, '123456')
  86. assert_equal(ticket1.title, 'some title1')
  87. assert_equal(ticket1.state.name, 'new')
  88. assert_equal(ticket1.priority.name, '2 normal')
  89. assert_equal(ticket1.owner.login, '-')
  90. assert_equal(ticket1.customer.login, 'nicole.braun@zammad.org')
  91. assert_equal(ticket1.note, 'some note1')
  92. ticket2 = Ticket.find_by(number: '123457')
  93. assert(ticket2)
  94. assert_equal(ticket2.number, '123457')
  95. assert_equal(ticket2.title, 'some title2')
  96. assert_equal(ticket2.state.name, 'closed')
  97. assert_equal(ticket2.priority.name, '1 low')
  98. assert_equal(ticket2.owner.login, 'admin@example.com')
  99. assert_equal(ticket2.customer.login, 'nicole.braun@zammad.org')
  100. assert_equal(ticket2.note, 'some note2')
  101. ticket1.destroy!
  102. ticket2.destroy!
  103. end
  104. test 'simple import with invalid id' do
  105. csv_string = "id;number;title;state;priority;owner;customer;group;note\n999999999;123456;some title1;new;2 normal;-;nicole.braun@zammad.org;Users;some note1\n;123457;some title2;closed;1 low;admin@example.com;nicole.braun@zammad.org;Users;some note2\n"
  106. result = Ticket.csv_import(
  107. string: csv_string,
  108. parse_params: {
  109. col_sep: ';',
  110. },
  111. try: true,
  112. )
  113. assert_equal(true, result[:try])
  114. assert_equal(1, result[:errors].count)
  115. assert_equal('failed', result[:result])
  116. assert_equal("Line 1: unknown Ticket with id '999999999'.", result[:errors][0])
  117. assert_nil(Ticket.find_by(number: '123456'))
  118. assert_nil(Ticket.find_by(number: '123457'))
  119. result = Ticket.csv_import(
  120. string: csv_string,
  121. parse_params: {
  122. col_sep: ';',
  123. },
  124. try: false,
  125. )
  126. assert_equal(false, result[:try])
  127. assert_equal(1, result[:records].count)
  128. assert_equal('failed', result[:result])
  129. assert_nil(Ticket.find_by(number: '123456'))
  130. # any single failure will cause the entire import to be aborted
  131. assert_nil(Ticket.find_by(number: '123457'))
  132. csv_string = "id;number;title;state;priority;owner;customer;group;note\n999999999;123456;some title1;new;2 normal;-;nicole.braun@zammad.org;Users;some note1\n;123457;some title22;closed;1 low;admin@example.com;nicole.braun@zammad.org;Users;some note22\n"
  133. result = Ticket.csv_import(
  134. string: csv_string,
  135. parse_params: {
  136. col_sep: ';',
  137. },
  138. try: false,
  139. )
  140. assert_equal(false, result[:try])
  141. assert_equal(1, result[:records].count)
  142. assert_equal('failed', result[:result])
  143. assert_nil(Ticket.find_by(number: '123456'))
  144. # any single failure will cause the entire import to be aborted
  145. assert_nil(Ticket.find_by(number: '123457'))
  146. end
  147. test 'invalid attributes' do
  148. csv_string = "id;number;not_existing;state;priority;owner;customer;group;note\n;123456;some title1;new;2 normal;-;nicole.braun@zammad.org;Users;some note1\n;123457;some title2;closed;1 low;admin@example.com;nicole.braun@zammad.org;Users;some note2\n"
  149. result = Ticket.csv_import(
  150. string: csv_string,
  151. parse_params: {
  152. col_sep: ';',
  153. },
  154. try: true,
  155. )
  156. assert_equal(true, result[:try])
  157. assert_equal(2, result[:errors].count)
  158. assert_equal('failed', result[:result])
  159. assert_equal("Line 1: Unable to create record - unknown attribute 'not_existing' for Ticket.", result[:errors][0])
  160. assert_equal("Line 2: Unable to create record - unknown attribute 'not_existing' for Ticket.", result[:errors][1])
  161. assert_nil(Ticket.find_by(number: '123456'))
  162. assert_nil(Ticket.find_by(number: '123457'))
  163. result = Ticket.csv_import(
  164. string: csv_string,
  165. parse_params: {
  166. col_sep: ';',
  167. },
  168. try: false,
  169. )
  170. assert_equal(false, result[:try])
  171. assert_equal(2, result[:errors].count)
  172. assert_equal('failed', result[:result])
  173. assert_equal("Line 1: Unable to create record - unknown attribute 'not_existing' for Ticket.", result[:errors][0])
  174. assert_equal("Line 2: Unable to create record - unknown attribute 'not_existing' for Ticket.", result[:errors][1])
  175. assert_nil(Ticket.find_by(number: '123456'))
  176. assert_nil(Ticket.find_by(number: '123457'))
  177. end
  178. end