can_csv_import_user_examples.rb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'csv'
  3. RSpec.shared_examples 'CanCsvImport - User specific tests', :aggregate_failures do
  4. describe '.csv_example' do
  5. context 'when no data avaiable' do
  6. let(:headers) do
  7. CSV.parse(User.csv_example).shift
  8. end
  9. it 'returns expected headers' do
  10. expect(headers).to start_with('id', 'login', 'firstname', 'lastname', 'email')
  11. expect(headers).to include('organization')
  12. end
  13. end
  14. end
  15. describe '.csv_import' do
  16. let(:try) { true }
  17. let(:delete) { false }
  18. let(:params) { { string: csv_string, parse_params: { col_sep: ';' }, try: try, delete: delete } }
  19. let(:result) { User.csv_import(**params) }
  20. shared_examples 'fails with error' do |errors|
  21. shared_examples 'checks error handling' do
  22. it 'returns error(s)' do
  23. expect(result).to include({ try: try, result: 'failed', errors: errors })
  24. end
  25. it 'does not import users' do
  26. # Any single failure will cause the entire import to be aborted.
  27. expect { result }.not_to change(User, :count)
  28. end
  29. end
  30. context 'with :try' do
  31. include_examples 'checks error handling'
  32. end
  33. context 'without :try' do
  34. let(:try) { false }
  35. include_examples 'checks error handling'
  36. end
  37. end
  38. context 'with empty string' do
  39. let(:csv_string) { '' }
  40. include_examples 'fails with error', ['Unable to parse empty file/string for User.']
  41. end
  42. context 'with just CSV header line' do
  43. let(:csv_string) { "login;firstname;lastname;email;active;\n" }
  44. include_examples 'fails with error', ['No records found in file/string for User.']
  45. end
  46. context 'without required lookup header' do
  47. let(:csv_string) { "firstname;lastname;active;\nfirstname-simple-import1;lastname-simple-import1;;true\nfirstname-simple-import2;lastname-simple-import2;false\n" }
  48. include_examples 'fails with error', ['No lookup column like id,login,email for User found.']
  49. end
  50. context 'with invalid id' do
  51. let(:csv_string) { "id;login;firstname;lastname;email;active;\n999999999;user-simple-invalid_id-import1;firstname-simple-import1;lastname-simple-import1;user-simple-invalid_id-import1@example.com;true\n;user-simple-invalid_id-import2;firstname-simple-import2;lastname-simple-import2;user-simple-invalid_id-import2@example.com;false\n" }
  52. include_examples 'fails with error', ["Line 1: unknown User with id '999999999'."]
  53. end
  54. context 'with readonly id' do
  55. let(:csv_string) { "id;login;firstname;lastname;email;active;\n1;user-simple-readonly_id-import1;firstname-simple-import1;lastname-simple-import1;user-simple-readonly_id-import1@example.com;true\n;user-simple-readonly_id-import2;firstname-simple-import2;lastname-simple-import2;user-simple-readonly_id-import2@example.com;false\n" }
  56. include_examples 'fails with error', ["Line 1: unable to update User with id '1'."]
  57. end
  58. context 'with invalid attributes' do
  59. let(:csv_string) { "login;firstname2;lastname;email\nuser-invalid-import1;firstname-invalid-import1;firstname-invalid-import1;user-invalid-import1@example.com\nuser-invalid-import2;firstname-invalid-import2;firstname-invalid-import2;user-invalid-import2@example.com\n" }
  60. include_examples 'fails with error', [
  61. "Line 1: Unable to create record - unknown attribute 'firstname2' for User.",
  62. "Line 2: Unable to create record - unknown attribute 'firstname2' for User.",
  63. ]
  64. end
  65. context 'with delete' do
  66. let(:csv_string) { "login;firstname;lastname;email\nuser-simple-import-fixed1;firstname-simple-import-fixed1;lastname-simple-import-fixed1;user-simple-import-fixed1@example.com\nuser-simple-import-fixed2;firstname-simple-import-fixed2;lastname-simple-import-fixed2;user-simple-import-fixed2@example.com\n" }
  67. let(:delete) { true }
  68. include_examples 'fails with error', ['Delete is not possible for User.']
  69. end
  70. context 'with duplicates' do
  71. let(:csv_string) { "login;firstname;lastname;email\nuser-duplicate-import1;firstname-duplicate-import1;firstname-duplicate-import1;user-duplicate-import1@example.com\nuser-duplicate-import2;firstname-duplicate-import2;firstname-duplicate-import2;user-duplicate-import2@example.com\nuser-duplicate-import2;firstname-duplicate-import3;firstname-duplicate-import3;user-duplicate-import3@example.com" }
  72. include_examples 'fails with error', ['Line 3: duplicate record found.']
  73. end
  74. context 'with references to nonexisting organizations' do
  75. let(:csv_string) { "login;firstname;lastname;email;organization\nuser-reference-import1;firstname-reference-import1;firstname-reference-import1;user-reference-import1@example.com;organization-reference-import1\nuser-reference-import2;firstname-reference-import2;firstname-reference-import2;user-reference-import2@example.com;organization-reference-import2\nuser-reference-import3;firstname-reference-import3;firstname-reference-import3;user-reference-import3@example.com;Zammad Foundation\n" }
  76. include_examples 'fails with error', [
  77. "Line 1: No lookup value found for 'organization': \"organization-reference-import1\"",
  78. "Line 2: No lookup value found for 'organization': \"organization-reference-import2\"",
  79. ]
  80. context 'when organizations are available' do
  81. before do
  82. create(:organization, name: 'organization-reference-import1')
  83. create(:organization, name: 'organization-reference-import2')
  84. end
  85. let(:try) { false }
  86. let(:first_user) { User.last(3).first }
  87. let(:second_user) { User.last(3).second }
  88. let(:third_user) { User.last }
  89. it 'returns success' do
  90. expect(result).to include({ try: try, result: 'success' })
  91. expect(result[:records].count).to be(3)
  92. end
  93. it 'does import users' do
  94. expect { result }.to change(User, :count).by(3)
  95. expect(first_user.organization.name).to eq('organization-reference-import1')
  96. expect(second_user.organization.name).to eq('organization-reference-import2')
  97. expect(third_user.organization.name).to eq('Zammad Foundation')
  98. end
  99. end
  100. end
  101. context 'with valid import data' do
  102. let(:csv_string) { "login;firstname;lastname;email;active;\nuser-simple-IMPORT1;firstname-simple-import1;lastname-simple-import1;user-simple-IMPORT1@example.com ;true\nuser-simple-import2;firstname-simple-import2;lastname-simple-import2;user-simple-import2@example.com;false\n" }
  103. let(:csv_string_without_email) { "login;firstname;lastname;email;active;\nuser-simple-IMPORT1;firstname-simple-import1;lastname-simple-import1;user-simple-IMPORT1@example.com ;true\nuser-simple-import2;firstname-simple-import2;lastname-simple-import2;;false\n" }
  104. let(:second_result) { User.csv_import(**params) }
  105. let(:second_params) { { string: second_csv_string, parse_params: { col_sep: ';' }, try: try, delete: delete } }
  106. context 'with :try' do
  107. it 'returns success' do
  108. expect(result).to include({ try: try, result: 'success' })
  109. expect(result[:records].count).to be(2)
  110. end
  111. it 'does not import users' do
  112. expect { result }.not_to change(User, :count)
  113. end
  114. end
  115. context 'without :try' do
  116. let(:try) { false }
  117. let(:first_user) { User.last(2).first }
  118. let(:second_user) { User.last }
  119. it 'returns success' do
  120. expect(result).to include({ try: try, result: 'success', records: have_attributes(count: 2), stats: { created: 2, updated: 0 } })
  121. expect(second_result).to include({ try: try, result: 'success', records: have_attributes(count: 2), stats: { created: 0, updated: 2 } })
  122. end
  123. it 'does import users' do
  124. expect { result }.to change(User, :count).by(2)
  125. expect(first_user).to have_attributes(
  126. login: 'user-simple-import1',
  127. firstname: 'firstname-simple-import1',
  128. lastname: 'lastname-simple-import1',
  129. email: 'user-simple-import1@example.com',
  130. active: true,
  131. )
  132. expect(second_user).to have_attributes(
  133. login: 'user-simple-import2',
  134. firstname: 'firstname-simple-import2',
  135. lastname: 'lastname-simple-import2',
  136. email: 'user-simple-import2@example.com',
  137. active: false,
  138. )
  139. expect { second_result }.not_to change(User, :count)
  140. expect(first_user.reload).to have_attributes(
  141. login: 'user-simple-import1',
  142. firstname: 'firstname-simple-import1',
  143. lastname: 'lastname-simple-import1',
  144. email: 'user-simple-import1@example.com',
  145. active: true,
  146. )
  147. # Email is still present, though missing in CSV.
  148. expect(second_user.reload).to have_attributes(
  149. login: 'user-simple-import2',
  150. firstname: 'firstname-simple-import2',
  151. lastname: 'lastname-simple-import2',
  152. email: 'user-simple-import2@example.com',
  153. active: false,
  154. )
  155. end
  156. end
  157. end
  158. context 'with roles and fixed params' do
  159. let(:result) { User.csv_import(**params, fixed_params: { note: 'some note' }) }
  160. let(:csv_string) { "login;firstname;lastname;email;roles;\nuser-role-import1;firstname-role-import1;lastname-role-import1;user-role-import1@example.com;Customer;\nuser-role-import2;firstname-role-import2;lastname-role-import2;user-role-import2@example.com;Agent\n;;;;Admin" }
  161. context 'with :try' do
  162. it 'returns success' do
  163. expect(result).to include({ try: try, result: 'success' })
  164. expect(result[:records].count).to be(2)
  165. end
  166. it 'does not import users' do
  167. expect { result }.not_to change(User, :count)
  168. end
  169. end
  170. context 'without :try' do
  171. let(:try) { false }
  172. let(:first_user) { User.last(2).first }
  173. let(:second_user) { User.last }
  174. it 'returns success' do
  175. expect(result).to include({ try: try, result: 'success', records: have_attributes(count: 2), stats: { created: 2, updated: 0 } })
  176. end
  177. it 'does import users with roles' do
  178. expect { result }.to change(User, :count).by(2)
  179. expect(first_user.roles.count).to be(1)
  180. expect(first_user.note).to eq('some note')
  181. expect(second_user.roles.count).to be(2)
  182. expect(second_user.note).to eq('some note')
  183. end
  184. end
  185. end
  186. end
  187. end