user_csv_import_test.rb 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. require 'test_helper'
  2. class UserCsvImportTest < ActiveSupport::TestCase
  3. test 'import example verify' do
  4. csv_string = User.csv_example
  5. rows = CSV.parse(csv_string)
  6. header = rows.shift
  7. assert_equal('id', header[0])
  8. assert_equal('login', header[1])
  9. assert_equal('firstname', header[2])
  10. assert_equal('lastname', header[3])
  11. assert_equal('email', header[4])
  12. assert(header.include?('organization'))
  13. end
  14. test 'simple import' do
  15. csv_string = "login;firstname;lastname;email;active;\nuser-simple-import1;firstname-simple-import1;lastname-simple-import1;user-simple-import1@example.com;true\nuser-simple-import2;firstname-simple-import2;lastname-simple-import2;user-simple-import2@example.com;false\n"
  16. result = User.csv_import(
  17. string: csv_string,
  18. parse_params: {
  19. col_sep: ';',
  20. },
  21. try: true,
  22. )
  23. assert_equal(true, result[:try])
  24. assert_equal(2, result[:records].count)
  25. assert_equal('success', result[:result])
  26. assert_nil(User.find_by(login: 'user-simple-import1'))
  27. assert_nil(User.find_by(login: 'user-simple-import2'))
  28. result = User.csv_import(
  29. string: csv_string,
  30. parse_params: {
  31. col_sep: ';',
  32. },
  33. try: false,
  34. )
  35. assert_equal(false, result[:try])
  36. assert_equal(2, result[:records].count)
  37. assert_equal('success', result[:result])
  38. user1 = User.find_by(login: 'user-simple-import1')
  39. assert(user1)
  40. assert_equal(user1.login, 'user-simple-import1')
  41. assert_equal(user1.firstname, 'firstname-simple-import1')
  42. assert_equal(user1.lastname, 'lastname-simple-import1')
  43. assert_equal(user1.email, 'user-simple-import1@example.com')
  44. assert_equal(user1.active, true)
  45. user2 = User.find_by(login: 'user-simple-import2')
  46. assert(user2)
  47. assert_equal(user2.login, 'user-simple-import2')
  48. assert_equal(user2.firstname, 'firstname-simple-import2')
  49. assert_equal(user2.lastname, 'lastname-simple-import2')
  50. assert_equal(user2.email, 'user-simple-import2@example.com')
  51. assert_equal(user2.active, false)
  52. user1.destroy!
  53. user2.destroy!
  54. end
  55. test 'simple import with invalid id' do
  56. csv_string = "id;login;firstname;lastname;email;active;\n999999999;user-simple-invalid_id-import1;firstname-simple-import1;lastname-simple-import1;user-simple-invalid_id-import1@example.com;true\n;user-simple-invalid_id-import2;firstname-simple-import2;lastname-simple-import2;user-simple-invalid_id-import2@example.com;false\n"
  57. result = User.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(1, result[:errors].count)
  66. assert_equal('failed', result[:result])
  67. assert_equal("Line 1: unknown record with id '999999999' for User.", result[:errors][0])
  68. assert_nil(User.find_by(login: 'user-simple-invalid_id-import1'))
  69. assert_nil(User.find_by(login: 'user-simple-invalid_id-import2'))
  70. result = User.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(1, result[:records].count)
  79. assert_equal('failed', result[:result])
  80. assert_nil(User.find_by(login: 'user-simple-invalid_id-import1'))
  81. user2 = User.find_by(login: 'user-simple-invalid_id-import2')
  82. assert(user2)
  83. assert_equal(user2.login, 'user-simple-invalid_id-import2')
  84. assert_equal(user2.firstname, 'firstname-simple-import2')
  85. assert_equal(user2.lastname, 'lastname-simple-import2')
  86. assert_equal(user2.email, 'user-simple-invalid_id-import2@example.com')
  87. assert_equal(user2.active, false)
  88. user2.destroy!
  89. end
  90. test 'simple import with read only id' do
  91. csv_string = "id;login;firstname;lastname;email;active;\n1;user-simple-readonly_id-import1;firstname-simple-import1;lastname-simple-import1;user-simple-readonly_id-import1@example.com;true\n;user-simple-readonly_id-import2;firstname-simple-import2;lastname-simple-import2;user-simple-readonly_id-import2@example.com;false\n"
  92. result = User.csv_import(
  93. string: csv_string,
  94. parse_params: {
  95. col_sep: ';',
  96. },
  97. try: true,
  98. )
  99. assert_equal(true, result[:try])
  100. assert_equal(1, result[:errors].count)
  101. assert_equal('failed', result[:result])
  102. assert_equal("Line 1: unable to update record with id '1' for User.", result[:errors][0])
  103. assert_nil(User.find_by(login: 'user-simple-readonly_id-import1'))
  104. assert_nil(User.find_by(login: 'user-simple-readonly_id-import2'))
  105. result = User.csv_import(
  106. string: csv_string,
  107. parse_params: {
  108. col_sep: ';',
  109. },
  110. try: false,
  111. )
  112. assert_equal(false, result[:try])
  113. assert_equal(1, result[:records].count)
  114. assert_equal('failed', result[:result])
  115. assert_nil(User.find_by(login: 'user-simple-readonly_id-import1'))
  116. user2 = User.find_by(login: 'user-simple-readonly_id-import2')
  117. assert(user2)
  118. assert_equal(user2.login, 'user-simple-readonly_id-import2')
  119. assert_equal(user2.firstname, 'firstname-simple-import2')
  120. assert_equal(user2.lastname, 'lastname-simple-import2')
  121. assert_equal(user2.email, 'user-simple-readonly_id-import2@example.com')
  122. assert_equal(user2.active, false)
  123. user2.destroy!
  124. end
  125. test 'simple import with roles' do
  126. UserInfo.current_user_id = 1
  127. admin = User.create_or_update(
  128. login: 'admin1@example.com',
  129. firstname: 'Admin',
  130. lastname: '1',
  131. email: 'admin1@example.com',
  132. password: 'agentpw',
  133. active: true,
  134. roles: Role.where(name: 'Admin'),
  135. )
  136. csv_string = "login;firstname;lastname;email;roles;\nuser-role-import1;firstname-role-import1;lastname-role-import1;user-role-import1@example.com;Customer;\nuser-role-import2;firstname-role-import2;lastname-role-import2;user-role-import2@example.com;Agent\n;;;;Admin"
  137. result = User.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(User.find_by(login: 'user-role-import1'))
  148. assert_nil(User.find_by(login: 'user-role-import2'))
  149. result = User.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. user1 = User.find_by(login: 'user-role-import1')
  160. assert(user1)
  161. assert_equal(user1.login, 'user-role-import1')
  162. assert_equal(user1.firstname, 'firstname-role-import1')
  163. assert_equal(user1.lastname, 'lastname-role-import1')
  164. assert_equal(user1.email, 'user-role-import1@example.com')
  165. assert_equal(user1.roles.count, 1)
  166. user2 = User.find_by(login: 'user-role-import2')
  167. assert(user2)
  168. assert_equal(user2.login, 'user-role-import2')
  169. assert_equal(user2.firstname, 'firstname-role-import2')
  170. assert_equal(user2.lastname, 'lastname-role-import2')
  171. assert_equal(user2.email, 'user-role-import2@example.com')
  172. assert_equal(user2.roles.count, 2)
  173. user1.destroy!
  174. user2.destroy!
  175. admin.destroy!
  176. end
  177. test 'simple import + fixed params' do
  178. csv_string = "login;firstname;lastname;email\nuser-simple-import-fixed1;firstname-simple-import-fixed1;lastname-simple-import-fixed1;user-simple-import-fixed1@example.com\nuser-simple-import-fixed2;firstname-simple-import-fixed2;lastname-simple-import-fixed2;user-simple-import-fixed2@example.com\n"
  179. result = User.csv_import(
  180. string: csv_string,
  181. parse_params: {
  182. col_sep: ';',
  183. },
  184. fixed_params: {
  185. note: 'some note',
  186. },
  187. try: true,
  188. )
  189. assert_equal(true, result[:try])
  190. assert_equal(2, result[:records].count)
  191. assert_equal('success', result[:result])
  192. assert_nil(User.find_by(login: 'user-simple-import-fixed1'))
  193. assert_nil(User.find_by(login: 'user-simple-import-fixed2'))
  194. result = User.csv_import(
  195. string: csv_string,
  196. parse_params: {
  197. col_sep: ';',
  198. },
  199. fixed_params: {
  200. note: 'some note',
  201. },
  202. try: false,
  203. )
  204. assert_equal(false, result[:try])
  205. assert_equal(2, result[:records].count)
  206. assert_equal('success', result[:result])
  207. user1 = User.find_by(login: 'user-simple-import-fixed1')
  208. user2 = User.find_by(login: 'user-simple-import-fixed2')
  209. assert(user1)
  210. assert_equal('some note', user1.note)
  211. assert_equal('user-simple-import-fixed1', user1.login)
  212. assert_equal('firstname-simple-import-fixed1', user1.firstname)
  213. assert_equal('lastname-simple-import-fixed1', user1.lastname)
  214. assert_equal('user-simple-import-fixed1@example.com', user1.email)
  215. assert(user2)
  216. assert_equal('some note', user2.note)
  217. assert_equal('user-simple-import-fixed2', user2.login)
  218. assert_equal('firstname-simple-import-fixed2', user2.firstname)
  219. assert_equal('lastname-simple-import-fixed2', user2.lastname)
  220. assert_equal('user-simple-import-fixed2@example.com', user2.email)
  221. user1.destroy!
  222. user2.destroy!
  223. end
  224. test 'duplicate import' do
  225. csv_string = "login;firstname;lastname;email\nuser-duplicate-import1;firstname-duplicate-import1;firstname-duplicate-import1;user-duplicate-import1@example.com\nuser-duplicate-import2;firstname-duplicate-import2;firstname-duplicate-import2;user-duplicate-import2@example.com\nuser-duplicate-import2;firstname-duplicate-import3;firstname-duplicate-import3;user-duplicate-import3@example.com"
  226. result = User.csv_import(
  227. string: csv_string,
  228. parse_params: {
  229. col_sep: ';',
  230. },
  231. try: true,
  232. )
  233. assert_equal(true, result[:try])
  234. assert_equal(3, result[:records].count)
  235. assert_equal('success', result[:result])
  236. assert_nil(User.find_by(login: 'user-duplicate-import1'))
  237. assert_nil(User.find_by(login: 'user-duplicate-import2'))
  238. assert_nil(User.find_by(login: 'user-duplicate-import3'))
  239. result = User.csv_import(
  240. string: csv_string,
  241. parse_params: {
  242. col_sep: ';',
  243. },
  244. try: false,
  245. )
  246. assert_equal(false, result[:try])
  247. assert_equal(3, result[:records].count)
  248. assert_equal('success', result[:result])
  249. assert(User.find_by(login: 'user-duplicate-import1'))
  250. assert(User.find_by(login: 'user-duplicate-import2'))
  251. assert_nil(User.find_by(login: 'user-duplicate-import3'))
  252. User.find_by(login: 'user-duplicate-import1').destroy!
  253. User.find_by(login: 'user-duplicate-import2').destroy!
  254. end
  255. test 'invalid attributes' do
  256. csv_string = "login;firstname2;lastname;email\nuser-invalid-import1;firstname-invalid-import1;firstname-invalid-import1;user-invalid-import1@example.com\nuser-invalid-import2;firstname-invalid-import2;firstname-invalid-import2;user-invalid-import2@example.com\n"
  257. result = User.csv_import(
  258. string: csv_string,
  259. parse_params: {
  260. col_sep: ';',
  261. },
  262. try: true,
  263. )
  264. assert_equal(true, result[:try])
  265. assert_equal(2, result[:errors].count)
  266. assert_equal('failed', result[:result])
  267. assert_equal("Line 1: unknown attribute 'firstname2' for User.", result[:errors][0])
  268. assert_equal("Line 2: unknown attribute 'firstname2' for User.", result[:errors][1])
  269. assert_nil(User.find_by(login: 'user-invalid-import1'))
  270. assert_nil(User.find_by(login: 'user-invalid-import2'))
  271. result = User.csv_import(
  272. string: csv_string,
  273. parse_params: {
  274. col_sep: ';',
  275. },
  276. try: false,
  277. )
  278. assert_equal(false, result[:try])
  279. assert_equal(2, result[:errors].count)
  280. assert_equal('failed', result[:result])
  281. assert_equal("Line 1: unknown attribute 'firstname2' for User.", result[:errors][0])
  282. assert_equal("Line 2: unknown attribute 'firstname2' for User.", result[:errors][1])
  283. assert_nil(User.find_by(login: 'user-invalid-import1'))
  284. assert_nil(User.find_by(login: 'user-invalid-import2'))
  285. end
  286. test 'reference import' do
  287. csv_string = "login;firstname;lastname;email;organization\nuser-reference-import1;firstname-reference-import1;firstname-reference-import1;user-reference-import1@example.com;organization-reference-import1\nuser-reference-import2;firstname-reference-import2;firstname-reference-import2;user-reference-import2@example.com;organization-reference-import2\nuser-reference-import3;firstname-reference-import3;firstname-reference-import3;user-reference-import3@example.com;Zammad Foundation\n"
  288. result = User.csv_import(
  289. string: csv_string,
  290. parse_params: {
  291. col_sep: ';',
  292. },
  293. try: true,
  294. )
  295. assert_equal(true, result[:try])
  296. assert_equal(2, result[:errors].count)
  297. assert_equal('failed', result[:result])
  298. assert_nil(User.find_by(login: 'user-reference-import1'))
  299. assert_nil(User.find_by(login: 'user-reference-import2'))
  300. assert_nil(User.find_by(login: 'user-reference-import3'))
  301. assert_equal("Line 1: No lookup value found for 'organization': \"organization-reference-import1\"", result[:errors][0])
  302. assert_equal("Line 2: No lookup value found for 'organization': \"organization-reference-import2\"", result[:errors][1])
  303. result = User.csv_import(
  304. string: csv_string,
  305. parse_params: {
  306. col_sep: ';',
  307. },
  308. try: false,
  309. )
  310. assert_equal(false, result[:try])
  311. assert_equal(2, result[:errors].count)
  312. assert_equal('failed', result[:result])
  313. assert_nil(User.find_by(login: 'user-reference-import1'))
  314. assert_nil(User.find_by(login: 'user-reference-import2'))
  315. assert(User.find_by(login: 'user-reference-import3'))
  316. assert_equal("Line 1: No lookup value found for 'organization': \"organization-reference-import1\"", result[:errors][0])
  317. assert_equal("Line 2: No lookup value found for 'organization': \"organization-reference-import2\"", result[:errors][1])
  318. UserInfo.current_user_id = 1
  319. orgaization1 = Organization.create_if_not_exists(name: 'organization-reference-import1')
  320. orgaization2 = Organization.create_if_not_exists(name: 'organization-reference-import2')
  321. result = User.csv_import(
  322. string: csv_string,
  323. parse_params: {
  324. col_sep: ';',
  325. },
  326. try: true,
  327. )
  328. assert_equal(true, result[:try])
  329. assert_equal(0, result[:errors].count)
  330. assert_equal('success', result[:result])
  331. assert_nil(User.find_by(login: 'user-reference-import1'))
  332. assert_nil(User.find_by(login: 'user-reference-import2'))
  333. assert(User.find_by(login: 'user-reference-import3'))
  334. result = User.csv_import(
  335. string: csv_string,
  336. parse_params: {
  337. col_sep: ';',
  338. },
  339. try: false,
  340. )
  341. assert_equal(false, result[:try])
  342. assert_equal(0, result[:errors].count)
  343. assert_equal('success', result[:result])
  344. assert(User.find_by(login: 'user-reference-import1'))
  345. assert(User.find_by(login: 'user-reference-import2'))
  346. assert(User.find_by(login: 'user-reference-import3'))
  347. User.find_by(login: 'user-reference-import1').destroy!
  348. User.find_by(login: 'user-reference-import2').destroy!
  349. User.find_by(login: 'user-reference-import3').destroy!
  350. orgaization1.destroy!
  351. orgaization2.destroy!
  352. end
  353. end