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 (
string : csv_string ,
parse_params : {
col_sep : ';' ,
} ,
try : true ,
)
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 (
string : csv_string ,
parse_params : {
col_sep : ';' ,
} ,
try : true ,
)
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-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 (
string : csv_string ,
parse_params : {
col_sep : ';' ,
} ,
try : true ,
)
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 (
string : csv_string ,
parse_params : {
col_sep : ';' ,
} ,
try : false ,
)
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 (
string : csv_string ,
parse_params : {
col_sep : ';' ,
} ,
try : true ,
)
assert_equal ( true , result [ :try ] )
assert_equal ( 1 , result [ :errors ] . count )
assert_equal ( 'failed' , result [ :result ] )
assert_equal ( " Line 1: unknown record with id '999999999' for Organization. " , result [ :errors ] [ 0 ] )
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 (
string : csv_string ,
parse_params : {
col_sep : ';' ,
} ,
try : false ,
)
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' ) )
organization2 = Organization . find_by ( name : 'organization-simple-invalid_id-import2' )
assert ( organization2 )
assert_equal ( organization2 . name , 'organization-simple-invalid_id-import2' )
assert_equal ( organization2 . active , true )
organization2 . destroy!
end
test 'simple import with members' do
UserInfo . current_user_id = 1
name = rand ( 999_999_999 )
customer1 = User . create_or_update (
login : " customer1-members #{ name } @example.com " ,
firstname : 'Member' ,
lastname : " Customer #{ name } " ,
email : " customer1-members #{ name } @example.com " ,
password : 'customerpw' ,
active : true ,
)
customer2 = User . create_or_update (
login : " customer2-members #{ name } @example.com " ,
firstname : 'Member' ,
lastname : " Customer #{ name } " ,
email : " customer2-members #{ name } @example.com " ,
password : 'customerpw' ,
active : true ,
)
csv_string = " id;name;members; \n ;organization-member-import1; \n ;organization-member-import2; #{ customer1 . email } \n ;; #{ customer2 . email } "
result = Organization . csv_import (
string : csv_string ,
parse_params : {
col_sep : ';' ,
} ,
try : true ,
)
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 (
string : csv_string ,
parse_params : {
col_sep : ';' ,
} ,
try : false ,
)
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 (
string : csv_string ,
parse_params : {
col_sep : ';' ,
} ,
try : true ,
)
assert_equal ( true , result [ :try ] )
assert_equal ( 2 , result [ :errors ] . count )
assert_equal ( 'failed' , result [ :result ] )
assert_equal ( " Line 1: unknown attribute 'not existing' for Organization. " , result [ :errors ] [ 0 ] )
assert_equal ( " Line 2: unknown attribute 'not existing' for Organization. " , result [ :errors ] [ 1 ] )
assert_nil ( Organization . find_by ( name : 'organization-invalid-import1' ) )
assert_nil ( Organization . find_by ( name : 'organization-invalid-import2' ) )
result = Organization . csv_import (
string : csv_string ,
parse_params : {
col_sep : ';' ,
} ,
try : false ,
)
assert_equal ( false , result [ :try ] )
assert_equal ( 2 , result [ :errors ] . count )
assert_equal ( 'failed' , result [ :result ] )
assert_equal ( " Line 1: unknown attribute 'not existing' for Organization. " , result [ :errors ] [ 0 ] )
assert_equal ( " Line 2: unknown attribute 'not existing' for Organization. " , result [ :errors ] [ 1 ] )
assert_nil ( Organization . find_by ( name : 'organization-invalid-import1' ) )
assert_nil ( Organization . find_by ( name : 'organization-invalid-import2' ) )
end
end