organization_csv_import_test.rb 9.1 KB

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