organization_csv_import_test.rb 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 'simple import' do
  43. 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"
  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(2, result[:records].count)
  53. assert_equal('success', result[:result])
  54. assert_nil(Organization.find_by(name: 'org-simple-import1'))
  55. assert_nil(Organization.find_by(name: 'org-simple-import2'))
  56. result = Organization.csv_import(
  57. string: csv_string,
  58. parse_params: {
  59. col_sep: ';',
  60. },
  61. try: false,
  62. )
  63. assert_equal(false, result[:try])
  64. assert_equal(2, result[:records].count)
  65. assert_equal('success', result[:result])
  66. organization1 = Organization.find_by(name: 'org-simple-import1')
  67. assert(organization1)
  68. assert_equal(organization1.name, 'org-simple-import1')
  69. assert_equal(organization1.shared, true)
  70. assert_equal(organization1.domain, 'org-simple-import1.example.com')
  71. assert_equal(organization1.domain_assignment, false)
  72. assert_equal(organization1.note, 'some note1')
  73. assert_equal(organization1.active, true)
  74. organization2 = Organization.find_by(name: 'org-simple-import2')
  75. assert(organization2)
  76. assert_equal(organization2.name, 'org-simple-import2')
  77. assert_equal(organization2.shared, true)
  78. assert_equal(organization2.domain, 'org-simple-import2.example.com')
  79. assert_equal(organization2.domain_assignment, false)
  80. assert_equal(organization2.note, 'some note2')
  81. assert_equal(organization2.active, false)
  82. organization1.destroy!
  83. organization2.destroy!
  84. end
  85. test 'simple import with invalid id' do
  86. csv_string = "id;name;shared;domain;domain_assignment;active;note;\n999999999;organization-simple-invalid_id-import1;\n;organization-simple-invalid_id-import2;\n"
  87. result = Organization.csv_import(
  88. string: csv_string,
  89. parse_params: {
  90. col_sep: ';',
  91. },
  92. try: true,
  93. )
  94. assert_equal(true, result[:try])
  95. assert_equal(1, result[:errors].count)
  96. assert_equal('failed', result[:result])
  97. assert_equal("Line 1: unknown record with id '999999999' for Organization.", result[:errors][0])
  98. assert_nil(Organization.find_by(name: 'organization-simple-invalid_id-import1'))
  99. assert_nil(Organization.find_by(name: 'organization-simple-invalid_id-import2'))
  100. result = Organization.csv_import(
  101. string: csv_string,
  102. parse_params: {
  103. col_sep: ';',
  104. },
  105. try: false,
  106. )
  107. assert_equal(false, result[:try])
  108. assert_equal(1, result[:records].count)
  109. assert_equal('failed', result[:result])
  110. assert_nil(Organization.find_by(name: 'organization-simple-invalid_id-import1'))
  111. organization2 = Organization.find_by(name: 'organization-simple-invalid_id-import2')
  112. assert(organization2)
  113. assert_equal(organization2.name, 'organization-simple-invalid_id-import2')
  114. assert_equal(organization2.active, true)
  115. organization2.destroy!
  116. end
  117. test 'simple import with members' do
  118. UserInfo.current_user_id = 1
  119. name = rand(999_999_999)
  120. customer1 = User.create_or_update(
  121. login: "customer1-members#{name}@example.com",
  122. firstname: 'Member',
  123. lastname: "Customer#{name}",
  124. email: "customer1-members#{name}@example.com",
  125. password: 'customerpw',
  126. active: true,
  127. )
  128. customer2 = User.create_or_update(
  129. login: "customer2-members#{name}@example.com",
  130. firstname: 'Member',
  131. lastname: "Customer#{name}",
  132. email: "customer2-members#{name}@example.com",
  133. password: 'customerpw',
  134. active: true,
  135. )
  136. csv_string = "id;name;members;\n;organization-member-import1;\n;organization-member-import2;#{customer1.email}\n;;#{customer2.email}"
  137. result = Organization.csv_import(
  138. string: csv_string,
  139. parse_params: {
  140. col_sep: ';',
  141. },
  142. try: true,
  143. )
  144. assert_equal(true, result[:try])
  145. assert_equal(2, result[:records].count)
  146. assert_equal('success', result[:result])
  147. assert_nil(Organization.find_by(name: 'organization-member-import1'))
  148. assert_nil(Organization.find_by(name: 'organization-member-import2'))
  149. result = Organization.csv_import(
  150. string: csv_string,
  151. parse_params: {
  152. col_sep: ';',
  153. },
  154. try: false,
  155. )
  156. assert_equal(false, result[:try])
  157. assert_equal(2, result[:records].count)
  158. assert_equal('success', result[:result])
  159. organization1 = Organization.find_by(name: 'organization-member-import1')
  160. assert(organization1)
  161. assert_equal(organization1.name, 'organization-member-import1')
  162. assert_equal(organization1.members.count, 0)
  163. organization2 = Organization.find_by(name: 'organization-member-import2')
  164. assert(organization2)
  165. assert_equal(organization2.name, 'organization-member-import2')
  166. assert_equal(organization2.members.count, 2)
  167. customer1.destroy!
  168. customer2.destroy!
  169. organization1.destroy!
  170. organization2.destroy!
  171. end
  172. test 'invalid attributes' do
  173. csv_string = "name;note;not existing\norganization-invalid-import1;some note;abc\norganization-invalid-import2;some other note;123; with not exsiting header\n"
  174. result = Organization.csv_import(
  175. string: csv_string,
  176. parse_params: {
  177. col_sep: ';',
  178. },
  179. try: true,
  180. )
  181. assert_equal(true, result[:try])
  182. assert_equal(2, result[:errors].count)
  183. assert_equal('failed', result[:result])
  184. assert_equal("Line 1: unknown attribute 'not existing' for Organization.", result[:errors][0])
  185. assert_equal("Line 2: unknown attribute 'not existing' for Organization.", result[:errors][1])
  186. assert_nil(Organization.find_by(name: 'organization-invalid-import1'))
  187. assert_nil(Organization.find_by(name: 'organization-invalid-import2'))
  188. result = Organization.csv_import(
  189. string: csv_string,
  190. parse_params: {
  191. col_sep: ';',
  192. },
  193. try: false,
  194. )
  195. assert_equal(false, result[:try])
  196. assert_equal(2, result[:errors].count)
  197. assert_equal('failed', result[:result])
  198. assert_equal("Line 1: unknown attribute 'not existing' for Organization.", result[:errors][0])
  199. assert_equal("Line 2: unknown attribute 'not existing' for Organization.", result[:errors][1])
  200. assert_nil(Organization.find_by(name: 'organization-invalid-import1'))
  201. assert_nil(Organization.find_by(name: 'organization-invalid-import2'))
  202. end
  203. test 'simple import with delete' do
  204. 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"
  205. result = Organization.csv_import(
  206. string: csv_string,
  207. parse_params: {
  208. col_sep: ';',
  209. },
  210. try: true,
  211. delete: true,
  212. )
  213. assert_equal(true, result[:try])
  214. assert_equal('failed', result[:result])
  215. assert_equal('Delete is not possible for Organization.', result[:errors][0])
  216. end
  217. end