can_csv_import_organization_examples.rb 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'csv'
  3. RSpec.shared_examples 'CanCsvImport - Organization specific tests', :aggregate_failures do
  4. describe '.csv_example' do
  5. before do
  6. Organization.destroy_all
  7. end
  8. context 'when no data avaiable' do
  9. let(:headers) do
  10. CSV.parse(Organization.csv_example).shift
  11. end
  12. it 'returns expected headers' do
  13. expect(headers).to start_with('id', 'name', 'shared', 'domain', 'domain_assignment', 'active', 'vip', 'note')
  14. expect(headers).to include('members')
  15. end
  16. end
  17. end
  18. describe '.csv_import' do
  19. let(:try) { true }
  20. let(:delete) { false }
  21. let(:params) { { string: csv_string, parse_params: { col_sep: ';' }, try: try, delete: delete } }
  22. let(:result) { Organization.csv_import(**params) }
  23. shared_examples 'fails with error' do |errors|
  24. shared_examples 'checks error handling' do
  25. it 'returns error(s)' do
  26. expect(result).to include({ try: try, result: 'failed', errors: errors })
  27. end
  28. it 'does not import organizations' do
  29. # Any single failure will cause the entire import to be aborted.
  30. expect { result }.not_to change(Organization, :count)
  31. end
  32. end
  33. context 'with :try' do
  34. include_examples 'checks error handling'
  35. end
  36. context 'without :try' do
  37. let(:try) { false }
  38. include_examples 'checks error handling'
  39. end
  40. end
  41. context 'with empty string' do
  42. let(:csv_string) { '' }
  43. include_examples 'fails with error', ['Unable to parse empty file/string for Organization.']
  44. end
  45. context 'with just CSV header line' do
  46. let(:csv_string) { 'id;name;shared;domain;domain_assignment;active;' }
  47. include_examples 'fails with error', ['No records found in file/string for Organization.']
  48. end
  49. context 'without required lookup header' do
  50. let(:csv_string) { "firstname;lastname;active;\nfirstname-simple-import1;lastname-simple-import1;;true\nfirstname-simple-import2;lastname-simple-import2;false\n" }
  51. include_examples 'fails with error', ['No lookup column like id,name for Organization found.']
  52. end
  53. context 'with invalid id' do
  54. let(:csv_string) { "id;name;shared;domain;domain_assignment;active;note;\n999999999;organization-simple-invalid_id-import1;\n;organization-simple-invalid_id-import2;\n" }
  55. include_examples 'fails with error', ["Line 1: unknown Organization with id '999999999'."]
  56. end
  57. context 'with invalid attributes' do
  58. let(:csv_string) { "name;note;not existing\norganization-invalid-import1;some note;abc\norganization-invalid-import2;some other note;123; with not exsiting header\n" }
  59. include_examples 'fails with error', [
  60. "Line 1: Unable to create record - unknown attribute 'not existing' for Organization.",
  61. "Line 2: Unable to create record - unknown attribute 'not existing' for Organization.",
  62. ]
  63. end
  64. context 'with delete' do
  65. let(:csv_string) { "id;name;shared;domain;domain_assignment;active;note\n;org-simple-import1;true;org-simple-import1.example.com;false;true;some note1\n;org-simple-import2;true;org-simple-import2.example.com;false;false;some note2\n" }
  66. let(:delete) { true }
  67. include_examples 'fails with error', ['Delete is not possible for Organization.']
  68. end
  69. context 'with valid import data' do
  70. let(:csv_string) { "id;name;shared;domain;domain_assignment;active;note\n;org-simple-import1;true;org-simple-import1.example.com;false;true;some note1\n;org-simple-import2;true;org-simple-import2.example.com;false;false;some note2\n" }
  71. context 'with :try' do
  72. it 'returns success' do
  73. expect(result).to include({ try: try, result: 'success' })
  74. expect(result[:records].count).to be(2)
  75. end
  76. it 'does not import organizations' do
  77. expect { result }.not_to change(Organization, :count)
  78. end
  79. end
  80. context 'without :try' do
  81. let(:try) { false }
  82. let(:first_org) { Organization.last(2).first }
  83. let(:second_org) { Organization.last }
  84. it 'returns success' do
  85. expect(result).to include({ try: try, result: 'success' })
  86. expect(result[:records].count).to be(2)
  87. end
  88. it 'does import organizations' do
  89. expect { result }.to change(Organization, :count).by(2)
  90. expect(first_org).to have_attributes(
  91. name: 'org-simple-import1',
  92. shared: true,
  93. domain: 'org-simple-import1.example.com',
  94. domain_assignment: false,
  95. note: 'some note1',
  96. active: true,
  97. )
  98. expect(second_org).to have_attributes(
  99. name: 'org-simple-import2',
  100. shared: true,
  101. domain: 'org-simple-import2.example.com',
  102. domain_assignment: false,
  103. note: 'some note2',
  104. active: false,
  105. )
  106. end
  107. end
  108. end
  109. context 'with valid import data including members' do
  110. let(:customer1) { create(:customer) }
  111. let(:customer2) { create(:customer) }
  112. let(:csv_string) { "id;name;members;\n;organization-member-import1;\n;organization-member-import2;#{customer1.email}\n;;#{customer2.email}" }
  113. context 'with :try' do
  114. it 'returns success' do
  115. expect(result).to include({ try: try, result: 'success' })
  116. expect(result[:records].count).to be(2)
  117. end
  118. it 'does not import organizations' do
  119. expect { result }.not_to change(Organization, :count)
  120. end
  121. end
  122. context 'without :try' do
  123. let(:try) { false }
  124. let(:first_org) { Organization.last(2).first }
  125. let(:second_org) { Organization.last }
  126. it 'returns success' do
  127. expect(result).to include({ try: try, result: 'success' })
  128. expect(result[:records].count).to be(2)
  129. end
  130. it 'does import organizations' do
  131. expect { result }.to change(Organization, :count).by(2)
  132. expect(first_org).to have_attributes(
  133. name: 'organization-member-import1',
  134. members: have_attributes(count: 0)
  135. )
  136. expect(second_org).to have_attributes(
  137. name: 'organization-member-import2',
  138. members: have_attributes(count: 2)
  139. )
  140. end
  141. end
  142. end
  143. end
  144. end