organization_csv_import_test.rb 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. require 'test_helper'
  3. class OrganizationCsvImportTest < ActiveSupport::TestCase
  4. test 'import example verify' do
  5. csv_string = Organization.csv_example
  6. rows = CSV.parse(csv_string)
  7. header = rows.shift
  8. assert_equal('id', header[0])
  9. assert_equal('name', header[1])
  10. assert_equal('shared', header[2])
  11. assert_equal('domain', header[3])
  12. assert_equal('domain_assignment', header[4])
  13. assert_equal('active', header[5])
  14. assert_equal('note', header[6])
  15. assert(header.include?('members'))
  16. end
  17. test 'empty payload' do
  18. csv_string = ''
  19. result = Organization.csv_import(
  20. string: csv_string,
  21. parse_params: {
  22. col_sep: ';',
  23. },
  24. try: true,
  25. )
  26. assert_equal(true, result[:try])
  27. assert_nil(result[:records])
  28. assert_equal('failed', result[:result])
  29. assert_equal('Unable to parse empty file/string for Organization.', result[:errors][0])
  30. csv_string = 'id;name;shared;domain;domain_assignment;active;'
  31. result = Organization.csv_import(
  32. string: csv_string,
  33. parse_params: {
  34. col_sep: ';',
  35. },
  36. try: true,
  37. )
  38. assert_equal(true, result[:try])
  39. assert(result[:records].blank?)
  40. assert_equal('failed', result[:result])
  41. assert_equal('No records found in file/string for Organization.', result[:errors][0])
  42. end
  43. test 'verify required lookup headers' do
  44. csv_string = "firstname;lastname;active;\nfirstname-simple-import1;lastname-simple-import1;;true\nfirstname-simple-import2;lastname-simple-import2;false\n"
  45. result = Organization.csv_import(
  46. string: csv_string,
  47. parse_params: {
  48. col_sep: ';',
  49. },
  50. try: true,
  51. )
  52. assert_equal(true, result[:try])
  53. assert_equal('failed', result[:result])
  54. assert_equal('No lookup column like id,name for Organization found.', result[:errors][0])
  55. end
  56. test 'simple import' do
  57. 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"
  58. result = Organization.csv_import(
  59. string: csv_string,
  60. parse_params: {
  61. col_sep: ';',
  62. },
  63. try: true,
  64. )
  65. assert_equal(true, result[:try])
  66. assert_equal(2, result[:records].count)
  67. assert_equal('success', result[:result])
  68. assert_nil(Organization.find_by(name: 'org-simple-import1'))
  69. assert_nil(Organization.find_by(name: 'org-simple-import2'))
  70. result = Organization.csv_import(
  71. string: csv_string,
  72. parse_params: {
  73. col_sep: ';',
  74. },
  75. try: false,
  76. )
  77. assert_equal(false, result[:try])
  78. assert_equal(2, result[:records].count)
  79. assert_equal('success', result[:result])
  80. organization1 = Organization.find_by(name: 'org-simple-import1')
  81. assert(organization1)
  82. assert_equal(organization1.name, 'org-simple-import1')
  83. assert_equal(organization1.shared, true)
  84. assert_equal(organization1.domain, 'org-simple-import1.example.com')
  85. assert_equal(organization1.domain_assignment, false)
  86. assert_equal(organization1.note, 'some note1')
  87. assert_equal(organization1.active, true)
  88. organization2 = Organization.find_by(name: 'org-simple-import2')
  89. assert(organization2)
  90. assert_equal(organization2.name, 'org-simple-import2')
  91. assert_equal(organization2.shared, true)
  92. assert_equal(organization2.domain, 'org-simple-import2.example.com')
  93. assert_equal(organization2.domain_assignment, false)
  94. assert_equal(organization2.note, 'some note2')
  95. assert_equal(organization2.active, false)
  96. organization1.destroy!
  97. organization2.destroy!
  98. end
  99. test 'simple import with invalid id' do
  100. csv_string = "id;name;shared;domain;domain_assignment;active;note;\n999999999;organization-simple-invalid_id-import1;\n;organization-simple-invalid_id-import2;\n"
  101. result = Organization.csv_import(
  102. string: csv_string,
  103. parse_params: {
  104. col_sep: ';',
  105. },
  106. try: true,
  107. )
  108. assert_equal(true, result[:try])
  109. assert_equal(1, result[:errors].count)
  110. assert_equal('failed', result[:result])
  111. assert_equal("Line 1: unknown Organization with id '999999999'.", result[:errors][0])
  112. assert_nil(Organization.find_by(name: 'organization-simple-invalid_id-import1'))
  113. assert_nil(Organization.find_by(name: 'organization-simple-invalid_id-import2'))
  114. result = Organization.csv_import(
  115. string: csv_string,
  116. parse_params: {
  117. col_sep: ';',
  118. },
  119. try: false,
  120. )
  121. assert_equal(false, result[:try])
  122. assert_equal(1, result[:records].count)
  123. assert_equal('failed', result[:result])
  124. assert_nil(Organization.find_by(name: 'organization-simple-invalid_id-import1'))
  125. # any single failure will cause the entire import to be aborted
  126. assert_nil(Organization.find_by(name: 'organization-simple-invalid_id-import2'))
  127. end
  128. test 'simple import with members' do
  129. UserInfo.current_user_id = 1
  130. name = SecureRandom.uuid
  131. customer1 = User.create_or_update(
  132. login: "customer1-members#{name}@example.com",
  133. firstname: 'Member',
  134. lastname: "Customer#{name}",
  135. email: "customer1-members#{name}@example.com",
  136. password: 'customerpw',
  137. active: true,
  138. )
  139. customer2 = User.create_or_update(
  140. login: "customer2-members#{name}@example.com",
  141. firstname: 'Member',
  142. lastname: "Customer#{name}",
  143. email: "customer2-members#{name}@example.com",
  144. password: 'customerpw',
  145. active: true,
  146. )
  147. csv_string = "id;name;members;\n;organization-member-import1;\n;organization-member-import2;#{customer1.email}\n;;#{customer2.email}"
  148. result = Organization.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[:records].count)
  157. assert_equal('success', result[:result])
  158. assert_nil(Organization.find_by(name: 'organization-member-import1'))
  159. assert_nil(Organization.find_by(name: 'organization-member-import2'))
  160. result = Organization.csv_import(
  161. string: csv_string,
  162. parse_params: {
  163. col_sep: ';',
  164. },
  165. try: false,
  166. )
  167. assert_equal(false, result[:try])
  168. assert_equal(2, result[:records].count)
  169. assert_equal('success', result[:result])
  170. organization1 = Organization.find_by(name: 'organization-member-import1')
  171. assert(organization1)
  172. assert_equal(organization1.name, 'organization-member-import1')
  173. assert_equal(organization1.members.count, 0)
  174. organization2 = Organization.find_by(name: 'organization-member-import2')
  175. assert(organization2)
  176. assert_equal(organization2.name, 'organization-member-import2')
  177. assert_equal(organization2.members.count, 2)
  178. customer1.destroy!
  179. customer2.destroy!
  180. organization1.destroy!
  181. organization2.destroy!
  182. end
  183. test 'invalid attributes' do
  184. csv_string = "name;note;not existing\norganization-invalid-import1;some note;abc\norganization-invalid-import2;some other note;123; with not exsiting header\n"
  185. result = Organization.csv_import(
  186. string: csv_string,
  187. parse_params: {
  188. col_sep: ';',
  189. },
  190. try: true,
  191. )
  192. assert_equal(true, result[:try])
  193. assert_equal(2, result[:errors].count)
  194. assert_equal('failed', result[:result])
  195. assert_equal("Line 1: Unable to create record - unknown attribute 'not existing' for Organization.", result[:errors][0])
  196. assert_equal("Line 2: Unable to create record - unknown attribute 'not existing' for Organization.", result[:errors][1])
  197. assert_nil(Organization.find_by(name: 'organization-invalid-import1'))
  198. assert_nil(Organization.find_by(name: 'organization-invalid-import2'))
  199. result = Organization.csv_import(
  200. string: csv_string,
  201. parse_params: {
  202. col_sep: ';',
  203. },
  204. try: false,
  205. )
  206. assert_equal(false, result[:try])
  207. assert_equal(2, result[:errors].count)
  208. assert_equal('failed', result[:result])
  209. assert_equal("Line 1: Unable to create record - unknown attribute 'not existing' for Organization.", result[:errors][0])
  210. assert_equal("Line 2: Unable to create record - unknown attribute 'not existing' for Organization.", result[:errors][1])
  211. assert_nil(Organization.find_by(name: 'organization-invalid-import1'))
  212. assert_nil(Organization.find_by(name: 'organization-invalid-import2'))
  213. end
  214. test 'simple import with delete' do
  215. 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"
  216. result = Organization.csv_import(
  217. string: csv_string,
  218. parse_params: {
  219. col_sep: ';',
  220. },
  221. try: true,
  222. delete: true,
  223. )
  224. assert_equal(true, result[:try])
  225. assert_equal('failed', result[:result])
  226. assert_equal('Delete is not possible for Organization.', result[:errors][0])
  227. end
  228. end