organization_csv_import_test.rb 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 record with id '999999999' for Organization.", 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. organization2 = Organization.find_by(name: 'organization-simple-invalid_id-import2')
  125. assert(organization2)
  126. assert_equal(organization2.name, 'organization-simple-invalid_id-import2')
  127. assert_equal(organization2.active, true)
  128. organization2.destroy!
  129. end
  130. test 'simple import with members' do
  131. UserInfo.current_user_id = 1
  132. name = rand(999_999_999)
  133. customer1 = User.create_or_update(
  134. login: "customer1-members#{name}@example.com",
  135. firstname: 'Member',
  136. lastname: "Customer#{name}",
  137. email: "customer1-members#{name}@example.com",
  138. password: 'customerpw',
  139. active: true,
  140. )
  141. customer2 = User.create_or_update(
  142. login: "customer2-members#{name}@example.com",
  143. firstname: 'Member',
  144. lastname: "Customer#{name}",
  145. email: "customer2-members#{name}@example.com",
  146. password: 'customerpw',
  147. active: true,
  148. )
  149. csv_string = "id;name;members;\n;organization-member-import1;\n;organization-member-import2;#{customer1.email}\n;;#{customer2.email}"
  150. result = Organization.csv_import(
  151. string: csv_string,
  152. parse_params: {
  153. col_sep: ';',
  154. },
  155. try: true,
  156. )
  157. assert_equal(true, result[:try])
  158. assert_equal(2, result[:records].count)
  159. assert_equal('success', result[:result])
  160. assert_nil(Organization.find_by(name: 'organization-member-import1'))
  161. assert_nil(Organization.find_by(name: 'organization-member-import2'))
  162. result = Organization.csv_import(
  163. string: csv_string,
  164. parse_params: {
  165. col_sep: ';',
  166. },
  167. try: false,
  168. )
  169. assert_equal(false, result[:try])
  170. assert_equal(2, result[:records].count)
  171. assert_equal('success', result[:result])
  172. organization1 = Organization.find_by(name: 'organization-member-import1')
  173. assert(organization1)
  174. assert_equal(organization1.name, 'organization-member-import1')
  175. assert_equal(organization1.members.count, 0)
  176. organization2 = Organization.find_by(name: 'organization-member-import2')
  177. assert(organization2)
  178. assert_equal(organization2.name, 'organization-member-import2')
  179. assert_equal(organization2.members.count, 2)
  180. customer1.destroy!
  181. customer2.destroy!
  182. organization1.destroy!
  183. organization2.destroy!
  184. end
  185. test 'invalid attributes' do
  186. csv_string = "name;note;not existing\norganization-invalid-import1;some note;abc\norganization-invalid-import2;some other note;123; with not exsiting header\n"
  187. result = Organization.csv_import(
  188. string: csv_string,
  189. parse_params: {
  190. col_sep: ';',
  191. },
  192. try: true,
  193. )
  194. assert_equal(true, result[:try])
  195. assert_equal(2, result[:errors].count)
  196. assert_equal('failed', result[:result])
  197. assert_equal("Line 1: Unable to create record - unknown attribute 'not existing' for Organization.", result[:errors][0])
  198. assert_equal("Line 2: Unable to create record - unknown attribute 'not existing' for Organization.", result[:errors][1])
  199. assert_nil(Organization.find_by(name: 'organization-invalid-import1'))
  200. assert_nil(Organization.find_by(name: 'organization-invalid-import2'))
  201. result = Organization.csv_import(
  202. string: csv_string,
  203. parse_params: {
  204. col_sep: ';',
  205. },
  206. try: false,
  207. )
  208. assert_equal(false, result[:try])
  209. assert_equal(2, result[:errors].count)
  210. assert_equal('failed', result[:result])
  211. assert_equal("Line 1: Unable to create record - unknown attribute 'not existing' for Organization.", result[:errors][0])
  212. assert_equal("Line 2: Unable to create record - unknown attribute 'not existing' for Organization.", result[:errors][1])
  213. assert_nil(Organization.find_by(name: 'organization-invalid-import1'))
  214. assert_nil(Organization.find_by(name: 'organization-invalid-import2'))
  215. end
  216. test 'simple import with delete' do
  217. 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"
  218. result = Organization.csv_import(
  219. string: csv_string,
  220. parse_params: {
  221. col_sep: ';',
  222. },
  223. try: true,
  224. delete: true,
  225. )
  226. assert_equal(true, result[:try])
  227. assert_equal('failed', result[:result])
  228. assert_equal('Delete is not possible for Organization.', result[:errors][0])
  229. end
  230. end