/
can_csv_import_organization_examples.rb
177 lines (144 loc) · 6.43 KB
/
can_csv_import_organization_examples.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
require 'csv'
RSpec.shared_examples 'CanCsvImportOrganization', :aggregate_failures do
describe '.csv_example' do
before do
described_class.destroy_all
end
context 'when no data avaiable' do
let(:headers) do
CSV.parse(described_class.csv_example).shift
end
it 'returns expected headers' do
expect(headers).to start_with('id', 'name', 'shared', 'domain', 'domain_assignment', 'active', 'note')
expect(headers).to include('members')
end
end
end
describe '.csv_import' do
let(:try) { true }
let(:delete) { false }
let(:params) { { string: csv_string, parse_params: { col_sep: ';' }, try: try, delete: delete } }
let(:result) { described_class.csv_import(**params) }
shared_examples 'fails with error' do |errors|
shared_examples 'checks error handling' do
it 'returns error(s)' do
expect(result).to include({ try: try, result: 'failed', errors: errors })
end
it 'does not import organizations' do
# Any single failure will cause the entire import to be aborted.
expect { result }.not_to change(Organization, :count)
end
end
context 'with :try' do
include_examples 'checks error handling'
end
context 'without :try' do
let(:try) { false }
include_examples 'checks error handling'
end
end
context 'with empty string' do
let(:csv_string) { '' }
include_examples 'fails with error', ['Unable to parse empty file/string for Organization.']
end
context 'with just CSV header line' do
let(:csv_string) { 'id;name;shared;domain;domain_assignment;active;' }
include_examples 'fails with error', ['No records found in file/string for Organization.']
end
context 'without required lookup header' do
let(:csv_string) { "firstname;lastname;active;\nfirstname-simple-import1;lastname-simple-import1;;true\nfirstname-simple-import2;lastname-simple-import2;false\n" }
include_examples 'fails with error', ['No lookup column like id,name for Organization found.']
end
context 'with invalid id' do
let(:csv_string) { "id;name;shared;domain;domain_assignment;active;note;\n999999999;organization-simple-invalid_id-import1;\n;organization-simple-invalid_id-import2;\n" }
include_examples 'fails with error', ["Line 1: unknown Organization with id '999999999'."]
end
context 'with invalid attributes' do
let(:csv_string) { "name;note;not existing\norganization-invalid-import1;some note;abc\norganization-invalid-import2;some other note;123; with not exsiting header\n" }
include_examples 'fails with error', [
"Line 1: Unable to create record - unknown attribute 'not existing' for Organization.",
"Line 2: Unable to create record - unknown attribute 'not existing' for Organization.",
]
end
context 'with delete' do
let(: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" }
let(:delete) { true }
include_examples 'fails with error', ['Delete is not possible for Organization.']
end
context 'with valid import data' do
let(: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" }
context 'with :try' do
it 'returns success' do
expect(result).to include({ try: try, result: 'success' })
expect(result[:records].count).to be(2)
end
it 'does not import organizations' do
expect { result }.not_to change(Organization, :count)
end
end
context 'without :try' do
let(:try) { false }
let(:first_org) { Organization.last(2).first }
let(:second_org) { Organization.last }
it 'returns success' do
expect(result).to include({ try: try, result: 'success' })
expect(result[:records].count).to be(2)
end
it 'does import organizations' do
expect { result }.to change(Organization, :count).by(2)
expect(first_org).to have_attributes(
name: 'org-simple-import1',
shared: true,
domain: 'org-simple-import1.example.com',
domain_assignment: false,
note: 'some note1',
active: true,
)
expect(second_org).to have_attributes(
name: 'org-simple-import2',
shared: true,
domain: 'org-simple-import2.example.com',
domain_assignment: false,
note: 'some note2',
active: false,
)
end
end
end
context 'with valid import data including members' do
let(:customer1) { create(:customer) }
let(:customer2) { create(:customer) }
let(:csv_string) { "id;name;members;\n;organization-member-import1;\n;organization-member-import2;#{customer1.email}\n;;#{customer2.email}" }
context 'with :try' do
it 'returns success' do
expect(result).to include({ try: try, result: 'success' })
expect(result[:records].count).to be(2)
end
it 'does not import organizations' do
expect { result }.not_to change(Organization, :count)
end
end
context 'without :try' do
let(:try) { false }
let(:first_org) { Organization.last(2).first }
let(:second_org) { Organization.last }
it 'returns success' do
expect(result).to include({ try: try, result: 'success' })
expect(result[:records].count).to be(2)
end
it 'does import organizations' do
expect { result }.to change(Organization, :count).by(2)
expect(first_org).to have_attributes(
name: 'organization-member-import1',
members: have_attributes(count: 0)
)
expect(second_org).to have_attributes(
name: 'organization-member-import2',
members: have_attributes(count: 2)
)
end
end
end
end
end