can_csv_import_ticket_examples.rb 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'csv'
  3. RSpec.shared_examples 'CanCsvImport - Ticket specific tests', :aggregate_failures do
  4. describe '.csv_example' do
  5. before do
  6. Ticket.destroy_all
  7. end
  8. context 'when no data avaiable' do
  9. let(:headers) do
  10. CSV.parse(Ticket.csv_example).shift
  11. end
  12. it 'returns expected headers' do
  13. expect(headers).to start_with('id', 'number', 'title', 'note', 'first_response_at', 'first_response_escalation_at')
  14. expect(headers).to include('organization', 'priority', 'state', 'owner', 'customer')
  15. end
  16. end
  17. end
  18. describe '.csv_import' do
  19. let(:try) { true }
  20. let(:params) { { string: csv_string, parse_params: { col_sep: ';' }, try: try } }
  21. let(:result) { Ticket.csv_import(**params) }
  22. shared_examples 'fails with error' do |errors|
  23. shared_examples 'checks error handling' do
  24. it 'returns error(s)' do
  25. expect(result).to include({ try: try, result: 'failed', errors: errors })
  26. end
  27. it 'does not import tickets' do
  28. # Any single failure will cause the entire import to be aborted.
  29. expect { result }.not_to change(Ticket, :count)
  30. end
  31. end
  32. context 'with :try' do
  33. include_examples 'checks error handling'
  34. end
  35. context 'without :try' do
  36. let(:try) { false }
  37. include_examples 'checks error handling'
  38. end
  39. end
  40. context 'with empty string' do
  41. let(:csv_string) { '' }
  42. include_examples 'fails with error', ['Unable to parse empty file/string for Ticket.']
  43. end
  44. context 'with just CSV header line' do
  45. let(:csv_string) { 'id;number;title;state;priority;' }
  46. include_examples 'fails with error', ['No records found in file/string for Ticket.']
  47. end
  48. context 'without required lookup header' do
  49. let(:csv_string) { "firstname;lastname;active;\nfirstname-simple-import1;lastname-simple-import1;;true\nfirstname-simple-import2;lastname-simple-import2;false\n" }
  50. include_examples 'fails with error', ['No lookup column like id,number for Ticket found.']
  51. end
  52. context 'with invalid id' do
  53. let(: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;-;nicole.braun@zammad.org;Users;some note2\n" }
  54. include_examples 'fails with error', ["Line 1: unknown Ticket with id '999999999'."]
  55. end
  56. context 'with invalid attributes' do
  57. let(: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;-;nicole.braun@zammad.org;Users;some note2\n" }
  58. include_examples 'fails with error', [
  59. "Line 1: Unable to create record - unknown attribute 'not_existing' for Ticket.",
  60. "Line 2: Unable to create record - unknown attribute 'not_existing' for Ticket.",
  61. ]
  62. end
  63. context 'with valid import data' do
  64. let(: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;-;nicole.braun@zammad.org;Users;some note2\n" }
  65. context 'with :try' do
  66. it 'returns success' do
  67. expect(result).to include({ try: try, result: 'success' })
  68. expect(result[:records].count).to be(2)
  69. end
  70. it 'does not import tickets' do
  71. expect { result }.not_to change(Ticket, :count)
  72. end
  73. end
  74. context 'without :try' do
  75. let(:try) { false }
  76. let(:first_ticket) { Ticket.last(2).first }
  77. let(:second_ticket) { Ticket.last }
  78. it 'returns success' do
  79. expect(result).to include({ try: try, result: 'success' })
  80. expect(result[:records].count).to be(2)
  81. end
  82. it 'does import tickets' do
  83. expect { result }.to change(Ticket, :count).by(2)
  84. expect(first_ticket).to have_attributes(
  85. number: '123456',
  86. title: 'some title1',
  87. note: 'some note1',
  88. state: have_attributes(name: 'new'),
  89. priority: have_attributes(name: '2 normal'),
  90. owner: have_attributes(login: '-'),
  91. customer: have_attributes(login: 'nicole.braun@zammad.org'),
  92. )
  93. expect(second_ticket).to have_attributes(
  94. number: '123457',
  95. title: 'some title2',
  96. note: 'some note2',
  97. state: have_attributes(name: 'closed'),
  98. priority: have_attributes(name: '1 low'),
  99. owner: have_attributes(login: '-'),
  100. customer: have_attributes(login: 'nicole.braun@zammad.org'),
  101. )
  102. end
  103. end
  104. end
  105. end
  106. end