ticket_csv_import_test.rb 7.1 KB

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