organization_csv_import_test.rb 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 'simple import' do
  17. 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"
  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_equal(2, result[:records].count)
  27. assert_equal('success', result[:result])
  28. assert_nil(Organization.find_by(name: 'org-simple-import1'))
  29. assert_nil(Organization.find_by(name: 'org-simple-import2'))
  30. result = Organization.csv_import(
  31. string: csv_string,
  32. parse_params: {
  33. col_sep: ';',
  34. },
  35. try: false,
  36. )
  37. assert_equal(false, result[:try])
  38. assert_equal(2, result[:records].count)
  39. assert_equal('success', result[:result])
  40. organization1 = Organization.find_by(name: 'org-simple-import1')
  41. assert(organization1)
  42. assert_equal(organization1.name, 'org-simple-import1')
  43. assert_equal(organization1.shared, true)
  44. assert_equal(organization1.domain, 'org-simple-import1.example.com')
  45. assert_equal(organization1.domain_assignment, false)
  46. assert_equal(organization1.note, 'some note1')
  47. assert_equal(organization1.active, true)
  48. organization2 = Organization.find_by(name: 'org-simple-import2')
  49. assert(organization2)
  50. assert_equal(organization2.name, 'org-simple-import2')
  51. assert_equal(organization2.shared, true)
  52. assert_equal(organization2.domain, 'org-simple-import2.example.com')
  53. assert_equal(organization2.domain_assignment, false)
  54. assert_equal(organization2.note, 'some note2')
  55. assert_equal(organization2.active, false)
  56. organization1.destroy!
  57. organization2.destroy!
  58. end
  59. test 'simple import with invalid id' do
  60. csv_string = "id;name;shared;domain;domain_assignment;active;note;\n999999999;organization-simple-invalid_id-import1;\n;organization-simple-invalid_id-import2;\n"
  61. result = Organization.csv_import(
  62. string: csv_string,
  63. parse_params: {
  64. col_sep: ';',
  65. },
  66. try: true,
  67. )
  68. assert_equal(true, result[:try])
  69. assert_equal(1, result[:errors].count)
  70. assert_equal('failed', result[:result])
  71. assert_equal("Line 1: unknown record with id '999999999' for Organization.", result[:errors][0])
  72. assert_nil(Organization.find_by(name: 'organization-simple-invalid_id-import1'))
  73. assert_nil(Organization.find_by(name: 'organization-simple-invalid_id-import2'))
  74. result = Organization.csv_import(
  75. string: csv_string,
  76. parse_params: {
  77. col_sep: ';',
  78. },
  79. try: false,
  80. )
  81. assert_equal(false, result[:try])
  82. assert_equal(1, result[:records].count)
  83. assert_equal('failed', result[:result])
  84. assert_nil(Organization.find_by(name: 'organization-simple-invalid_id-import1'))
  85. organization2 = Organization.find_by(name: 'organization-simple-invalid_id-import2')
  86. assert(organization2)
  87. assert_equal(organization2.name, 'organization-simple-invalid_id-import2')
  88. assert_equal(organization2.active, true)
  89. organization2.destroy!
  90. end
  91. test 'simple import with members' do
  92. UserInfo.current_user_id = 1
  93. name = rand(999_999_999)
  94. customer1 = User.create_or_update(
  95. login: "customer1-members#{name}@example.com",
  96. firstname: 'Member',
  97. lastname: "Customer#{name}",
  98. email: "customer1-members#{name}@example.com",
  99. password: 'customerpw',
  100. active: true,
  101. )
  102. customer2 = User.create_or_update(
  103. login: "customer2-members#{name}@example.com",
  104. firstname: 'Member',
  105. lastname: "Customer#{name}",
  106. email: "customer2-members#{name}@example.com",
  107. password: 'customerpw',
  108. active: true,
  109. )
  110. csv_string = "id;name;members;\n;organization-member-import1;\n;organization-member-import2;#{customer1.email}\n;;#{customer2.email}"
  111. result = Organization.csv_import(
  112. string: csv_string,
  113. parse_params: {
  114. col_sep: ';',
  115. },
  116. try: true,
  117. )
  118. assert_equal(true, result[:try])
  119. assert_equal(2, result[:records].count)
  120. assert_equal('success', result[:result])
  121. assert_nil(Organization.find_by(name: 'organization-member-import1'))
  122. assert_nil(Organization.find_by(name: 'organization-member-import2'))
  123. result = Organization.csv_import(
  124. string: csv_string,
  125. parse_params: {
  126. col_sep: ';',
  127. },
  128. try: false,
  129. )
  130. assert_equal(false, result[:try])
  131. assert_equal(2, result[:records].count)
  132. assert_equal('success', result[:result])
  133. organization1 = Organization.find_by(name: 'organization-member-import1')
  134. assert(organization1)
  135. assert_equal(organization1.name, 'organization-member-import1')
  136. assert_equal(organization1.members.count, 0)
  137. organization2 = Organization.find_by(name: 'organization-member-import2')
  138. assert(organization2)
  139. assert_equal(organization2.name, 'organization-member-import2')
  140. assert_equal(organization2.members.count, 2)
  141. customer1.destroy!
  142. customer2.destroy!
  143. organization1.destroy!
  144. organization2.destroy!
  145. end
  146. test 'invalid attributes' do
  147. csv_string = "name;note;not existing\norganization-invalid-import1;some note;abc\norganization-invalid-import2;some other note;123; with not exsiting header\n"
  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[:errors].count)
  157. assert_equal('failed', result[:result])
  158. assert_equal("Line 1: unknown attribute 'not existing' for Organization.", result[:errors][0])
  159. assert_equal("Line 2: unknown attribute 'not existing' for Organization.", result[:errors][1])
  160. assert_nil(Organization.find_by(name: 'organization-invalid-import1'))
  161. assert_nil(Organization.find_by(name: 'organization-invalid-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[:errors].count)
  171. assert_equal('failed', result[:result])
  172. assert_equal("Line 1: unknown attribute 'not existing' for Organization.", result[:errors][0])
  173. assert_equal("Line 2: unknown attribute 'not existing' for Organization.", result[:errors][1])
  174. assert_nil(Organization.find_by(name: 'organization-invalid-import1'))
  175. assert_nil(Organization.find_by(name: 'organization-invalid-import2'))
  176. end
  177. end