2021-06-01 12:20:20 +00:00
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
2018-02-20 04:29:30 +00:00
require 'test_helper'
class OrganizationCsvImportTest < ActiveSupport :: TestCase
test 'import example verify' do
csv_string = Organization . csv_example
rows = CSV . parse ( csv_string )
header = rows . shift
assert_equal ( 'id' , header [ 0 ] )
assert_equal ( 'name' , header [ 1 ] )
assert_equal ( 'shared' , header [ 2 ] )
assert_equal ( 'domain' , header [ 3 ] )
assert_equal ( 'domain_assignment' , header [ 4 ] )
assert_equal ( 'active' , header [ 5 ] )
assert_equal ( 'note' , header [ 6 ] )
assert ( header . include? ( 'members' ) )
end
2018-06-06 01:30:17 +00:00
test 'empty payload' do
csv_string = ''
result = Organization . csv_import (
2018-12-19 17:31:51 +00:00
string : csv_string ,
2018-06-06 01:30:17 +00:00
parse_params : {
col_sep : ';' ,
} ,
2018-12-19 17:31:51 +00:00
try : true ,
2018-06-06 01:30:17 +00:00
)
assert_equal ( true , result [ :try ] )
assert_nil ( result [ :records ] )
assert_equal ( 'failed' , result [ :result ] )
assert_equal ( 'Unable to parse empty file/string for Organization.' , result [ :errors ] [ 0 ] )
csv_string = 'id;name;shared;domain;domain_assignment;active;'
result = Organization . csv_import (
2018-12-19 17:31:51 +00:00
string : csv_string ,
2018-06-06 01:30:17 +00:00
parse_params : {
col_sep : ';' ,
} ,
2018-12-19 17:31:51 +00:00
try : true ,
2018-06-06 01:30:17 +00:00
)
assert_equal ( true , result [ :try ] )
assert ( result [ :records ] . blank? )
assert_equal ( 'failed' , result [ :result ] )
assert_equal ( 'No records found in file/string for Organization.' , result [ :errors ] [ 0 ] )
end
2018-11-06 05:42:52 +00:00
test 'verify required lookup headers' do
csv_string = " firstname;lastname;active; \n firstname-simple-import1;lastname-simple-import1;;true \n firstname-simple-import2;lastname-simple-import2;false \n "
result = Organization . csv_import (
2018-12-19 17:31:51 +00:00
string : csv_string ,
2018-11-06 05:42:52 +00:00
parse_params : {
col_sep : ';' ,
} ,
2018-12-19 17:31:51 +00:00
try : true ,
2018-11-06 05:42:52 +00:00
)
assert_equal ( true , result [ :try ] )
assert_equal ( 'failed' , result [ :result ] )
assert_equal ( 'No lookup column like id,name for Organization found.' , result [ :errors ] [ 0 ] )
end
2018-02-20 04:29:30 +00:00
test 'simple import' do
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 "
result = Organization . csv_import (
2018-12-19 17:31:51 +00:00
string : csv_string ,
2018-02-20 04:29:30 +00:00
parse_params : {
col_sep : ';' ,
} ,
2018-12-19 17:31:51 +00:00
try : true ,
2018-02-20 04:29:30 +00:00
)
assert_equal ( true , result [ :try ] )
assert_equal ( 2 , result [ :records ] . count )
assert_equal ( 'success' , result [ :result ] )
assert_nil ( Organization . find_by ( name : 'org-simple-import1' ) )
assert_nil ( Organization . find_by ( name : 'org-simple-import2' ) )
result = Organization . csv_import (
2018-12-19 17:31:51 +00:00
string : csv_string ,
2018-02-20 04:29:30 +00:00
parse_params : {
col_sep : ';' ,
} ,
2018-12-19 17:31:51 +00:00
try : false ,
2018-02-20 04:29:30 +00:00
)
assert_equal ( false , result [ :try ] )
assert_equal ( 2 , result [ :records ] . count )
assert_equal ( 'success' , result [ :result ] )
organization1 = Organization . find_by ( name : 'org-simple-import1' )
assert ( organization1 )
assert_equal ( organization1 . name , 'org-simple-import1' )
assert_equal ( organization1 . shared , true )
assert_equal ( organization1 . domain , 'org-simple-import1.example.com' )
assert_equal ( organization1 . domain_assignment , false )
assert_equal ( organization1 . note , 'some note1' )
assert_equal ( organization1 . active , true )
organization2 = Organization . find_by ( name : 'org-simple-import2' )
assert ( organization2 )
assert_equal ( organization2 . name , 'org-simple-import2' )
assert_equal ( organization2 . shared , true )
assert_equal ( organization2 . domain , 'org-simple-import2.example.com' )
assert_equal ( organization2 . domain_assignment , false )
assert_equal ( organization2 . note , 'some note2' )
assert_equal ( organization2 . active , false )
organization1 . destroy!
organization2 . destroy!
end
test 'simple import with invalid id' do
csv_string = " id;name;shared;domain;domain_assignment;active;note; \n 999999999;organization-simple-invalid_id-import1; \n ;organization-simple-invalid_id-import2; \n "
result = Organization . csv_import (
2018-12-19 17:31:51 +00:00
string : csv_string ,
2018-02-20 04:29:30 +00:00
parse_params : {
col_sep : ';' ,
} ,
2018-12-19 17:31:51 +00:00
try : true ,
2018-02-20 04:29:30 +00:00
)
assert_equal ( true , result [ :try ] )
assert_equal ( 1 , result [ :errors ] . count )
assert_equal ( 'failed' , result [ :result ] )
2019-10-25 06:42:20 +00:00
assert_equal ( " Line 1: unknown Organization with id '999999999'. " , result [ :errors ] [ 0 ] )
2018-02-20 04:29:30 +00:00
assert_nil ( Organization . find_by ( name : 'organization-simple-invalid_id-import1' ) )
assert_nil ( Organization . find_by ( name : 'organization-simple-invalid_id-import2' ) )
result = Organization . csv_import (
2018-12-19 17:31:51 +00:00
string : csv_string ,
2018-02-20 04:29:30 +00:00
parse_params : {
col_sep : ';' ,
} ,
2018-12-19 17:31:51 +00:00
try : false ,
2018-02-20 04:29:30 +00:00
)
assert_equal ( false , result [ :try ] )
assert_equal ( 1 , result [ :records ] . count )
assert_equal ( 'failed' , result [ :result ] )
assert_nil ( Organization . find_by ( name : 'organization-simple-invalid_id-import1' ) )
2019-10-21 10:44:52 +00:00
# any single failure will cause the entire import to be aborted
assert_nil ( Organization . find_by ( name : 'organization-simple-invalid_id-import2' ) )
2018-02-20 04:29:30 +00:00
end
test 'simple import with members' do
UserInfo . current_user_id = 1
name = rand ( 999_999_999 )
customer1 = User . create_or_update (
2018-12-19 17:31:51 +00:00
login : " customer1-members #{ name } @example.com " ,
2018-02-20 04:29:30 +00:00
firstname : 'Member' ,
2018-12-19 17:31:51 +00:00
lastname : " Customer #{ name } " ,
email : " customer1-members #{ name } @example.com " ,
password : 'customerpw' ,
active : true ,
2018-02-20 04:29:30 +00:00
)
customer2 = User . create_or_update (
2018-12-19 17:31:51 +00:00
login : " customer2-members #{ name } @example.com " ,
2018-02-20 04:29:30 +00:00
firstname : 'Member' ,
2018-12-19 17:31:51 +00:00
lastname : " Customer #{ name } " ,
email : " customer2-members #{ name } @example.com " ,
password : 'customerpw' ,
active : true ,
2018-02-20 04:29:30 +00:00
)
csv_string = " id;name;members; \n ;organization-member-import1; \n ;organization-member-import2; #{ customer1 . email } \n ;; #{ customer2 . email } "
result = Organization . csv_import (
2018-12-19 17:31:51 +00:00
string : csv_string ,
2018-02-20 04:29:30 +00:00
parse_params : {
col_sep : ';' ,
} ,
2018-12-19 17:31:51 +00:00
try : true ,
2018-02-20 04:29:30 +00:00
)
assert_equal ( true , result [ :try ] )
assert_equal ( 2 , result [ :records ] . count )
assert_equal ( 'success' , result [ :result ] )
assert_nil ( Organization . find_by ( name : 'organization-member-import1' ) )
assert_nil ( Organization . find_by ( name : 'organization-member-import2' ) )
result = Organization . csv_import (
2018-12-19 17:31:51 +00:00
string : csv_string ,
2018-02-20 04:29:30 +00:00
parse_params : {
col_sep : ';' ,
} ,
2018-12-19 17:31:51 +00:00
try : false ,
2018-02-20 04:29:30 +00:00
)
assert_equal ( false , result [ :try ] )
assert_equal ( 2 , result [ :records ] . count )
assert_equal ( 'success' , result [ :result ] )
organization1 = Organization . find_by ( name : 'organization-member-import1' )
assert ( organization1 )
assert_equal ( organization1 . name , 'organization-member-import1' )
assert_equal ( organization1 . members . count , 0 )
organization2 = Organization . find_by ( name : 'organization-member-import2' )
assert ( organization2 )
assert_equal ( organization2 . name , 'organization-member-import2' )
assert_equal ( organization2 . members . count , 2 )
customer1 . destroy!
customer2 . destroy!
organization1 . destroy!
organization2 . destroy!
end
test 'invalid attributes' do
csv_string = " name;note;not existing \n organization-invalid-import1;some note;abc \n organization-invalid-import2;some other note;123; with not exsiting header \n "
result = Organization . csv_import (
2018-12-19 17:31:51 +00:00
string : csv_string ,
2018-02-20 04:29:30 +00:00
parse_params : {
col_sep : ';' ,
} ,
2018-12-19 17:31:51 +00:00
try : true ,
2018-02-20 04:29:30 +00:00
)
assert_equal ( true , result [ :try ] )
assert_equal ( 2 , result [ :errors ] . count )
assert_equal ( 'failed' , result [ :result ] )
2018-11-06 05:42:52 +00:00
assert_equal ( " Line 1: Unable to create record - unknown attribute 'not existing' for Organization. " , result [ :errors ] [ 0 ] )
assert_equal ( " Line 2: Unable to create record - unknown attribute 'not existing' for Organization. " , result [ :errors ] [ 1 ] )
2018-02-20 04:29:30 +00:00
assert_nil ( Organization . find_by ( name : 'organization-invalid-import1' ) )
assert_nil ( Organization . find_by ( name : 'organization-invalid-import2' ) )
result = Organization . csv_import (
2018-12-19 17:31:51 +00:00
string : csv_string ,
2018-02-20 04:29:30 +00:00
parse_params : {
col_sep : ';' ,
} ,
2018-12-19 17:31:51 +00:00
try : false ,
2018-02-20 04:29:30 +00:00
)
assert_equal ( false , result [ :try ] )
assert_equal ( 2 , result [ :errors ] . count )
assert_equal ( 'failed' , result [ :result ] )
2018-11-06 05:42:52 +00:00
assert_equal ( " Line 1: Unable to create record - unknown attribute 'not existing' for Organization. " , result [ :errors ] [ 0 ] )
assert_equal ( " Line 2: Unable to create record - unknown attribute 'not existing' for Organization. " , result [ :errors ] [ 1 ] )
2018-02-20 04:29:30 +00:00
assert_nil ( Organization . find_by ( name : 'organization-invalid-import1' ) )
assert_nil ( Organization . find_by ( name : 'organization-invalid-import2' ) )
end
2018-06-12 20:58:59 +00:00
test 'simple import with delete' do
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 "
result = Organization . csv_import (
2018-12-19 17:31:51 +00:00
string : csv_string ,
2018-06-12 20:58:59 +00:00
parse_params : {
col_sep : ';' ,
} ,
2018-12-19 17:31:51 +00:00
try : true ,
delete : true ,
2018-06-12 20:58:59 +00:00
)
assert_equal ( true , result [ :try ] )
assert_equal ( 'failed' , result [ :result ] )
assert_equal ( 'Delete is not possible for Organization.' , result [ :errors ] [ 0 ] )
end
2018-02-20 04:29:30 +00:00
end