123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- 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
- 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
- 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;\n999999999;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\norganization-invalid-import1;some note;abc\norganization-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
- 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(
- string: csv_string,
- parse_params: {
- col_sep: ';',
- },
- try: true,
- delete: true,
- )
- assert_equal(true, result[:try])
- assert_equal('failed', result[:result])
- assert_equal('Delete is not possible for Organization.', result[:errors][0])
- end
- end
|