forked from ManageIQ/manageiq
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmiq_product_feature_spec.rb
424 lines (361 loc) · 16.7 KB
/
miq_product_feature_spec.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
describe MiqProductFeature do
let(:miq_product_feature_class) do
Class.new(described_class) do
def self.with_parent_tenant_nodes
includes(:parent).where(:parents_miq_product_features => {:identifier => self::TENANT_FEATURE_ROOT_IDENTIFIERS})
end
def self.tenant_features_in_hash
with_parent_tenant_nodes.map { |x| x.slice(:name, :description, :identifier, :tenant_id) }
end
end
end
# - container_dashboard
# - miq_report_widget_editor
# - miq_report_widget_admin
# - widget_edit
# - widget_copy
# - widget_refresh (H)
let(:hierarchical_features) do
EvmSpecHelper.seed_specific_product_features(
%w(miq_report_widget_editor miq_report_widget_admin widget_refresh widget_edit widget_copy container_dashboard)
)
end
def assert_product_feature_attributes(pf)
expect(pf).to include(*described_class::REQUIRED_ATTRIBUTES)
expect(pf.keys - described_class::ALLOWED_ATTRIBUTES).to be_empty
expect(pf[:children]).not_to be_empty if pf.key?(:children)
end
def traverse_product_feature_children(pfs, &block)
pfs.each do |pf|
yield pf
traverse_product_feature_children(pf[:children], &block) if pf.key?(:children)
end
end
it "is properly configured" do
root_file, other_files = described_class.seed_files
hash = YAML.load_file(root_file)
assert_product_feature_attributes(hash)
traverse_product_feature_children(hash[:children]) do |pf|
assert_product_feature_attributes(pf)
end
other_files.each do |f|
hash = YAML.load_file(f)
traverse_product_feature_children(Array.wrap(hash)) do |pf|
assert_product_feature_attributes(pf)
end
end
end
context ".seed" do
it "creates feature identifiers once on first seed, changes nothing on second seed" do
expect { MiqProductFeature.seed }.to change(MiqProductFeature, :count)
orig_ids = MiqProductFeature.pluck(:id)
expect { MiqProductFeature.seed }.not_to change(MiqProductFeature, :count)
expect(MiqProductFeature.pluck(:id)).to match_array orig_ids
end
end
context ".seed_features" do
let(:tempdir) { Dir.mktmpdir }
let(:feature_path) { File.join(tempdir, "miq_product_features") }
let(:root_file) { File.join(tempdir, "miq_product_features.yml") }
let(:base) do
{
:feature_type => "node",
:identifier => "everything",
:children => [
{
:feature_type => "node",
:identifier => "dialog_new_editor",
:name => "One",
:children => []
}
]
}
end
before do
File.write(root_file, base.to_yaml)
expect(described_class).to receive(:seed_files).at_least(:once) do
[root_file, Dir.glob(File.join(feature_path, "*.yml"))]
end
end
after do
FileUtils.rm_rf(tempdir)
end
it "creates/updates/deletes records" do
deleted = FactoryBot.create(:miq_product_feature, :identifier => "xxx")
changed = FactoryBot.create(:miq_product_feature, :identifier => "dialog_new_editor", :name => "XXX")
unchanged = FactoryBot.create(:miq_product_feature_everything)
unchanged_orig_updated_at = unchanged.updated_at
MiqProductFeature.seed_features
expect { deleted.reload }.to raise_error(ActiveRecord::RecordNotFound)
expect(changed.reload.name).to eq("One")
expect(unchanged.reload.updated_at).to be_within(0.1).of unchanged_orig_updated_at
end
context "additional yaml feature" do
before do
additional_hash = {
:feature_type => "node",
:identifier => "dialog_edit_editor",
:children => []
}
additional_array = [
{
:feature_type => "node",
:identifier => "policy_edit_editor",
:children => []
}
]
FileUtils.mkdir_p(feature_path)
File.write(File.join(feature_path, "additional_hash.yml"), additional_hash.to_yaml)
File.write(File.join(feature_path, "additional_array.yml"), additional_array.to_yaml)
end
it "creates/updates/deletes records" do
MiqProductFeature.seed_features
expect(MiqProductFeature.pluck(:identifier)).to match_array %w(everything dialog_new_editor dialog_edit_editor policy_edit_editor)
end
end
context 'dynamic product features' do
context 'add new' do
let(:base) do
{
:feature_type => "node",
:identifier => "everything",
:children => [
{
:feature_type => "node",
:identifier => "one",
:name => "One",
:children => [
{
:feature_type => "admin",
:identifier => "dialog_copy_editor",
:name => "Edit",
:description => "XXX"
}
]
}
]
}
end
let(:root_tenant) do
Tenant.seed
Tenant.default_tenant
end
let!(:tenant) { FactoryBot.create(:tenant, :parent => root_tenant) }
before do
MiqProductFeature.seed_features
end
it "creates tenant features" do
features = miq_product_feature_class.tenant_features_in_hash
expect(features).to match_array([{ "name" => "Edit (#{root_tenant.name})", "description" => "XXX for tenant #{root_tenant.name}",
"identifier" => "dialog_copy_editor_tenant_#{root_tenant.id}", "tenant_id" => root_tenant.id},
{"name" => "Edit (#{tenant.name})", "description" => "XXX for tenant #{tenant.name}",
"identifier" => "dialog_copy_editor_tenant_#{tenant.id}", "tenant_id" => tenant.id}])
expect(MiqProductFeature.where(:identifier => "dialog_copy_editor", :name => "Edit").count).to eq(1)
end
context "with tenants from remote region" do
before do
MiqRegion.seed
end
def id_for_model_in_region(model, region)
model.id_in_region(model.count + 1_000_000, region.region)
end
let!(:other_miq_region) { FactoryBot.create(:miq_region) }
let!(:tenant_product_feature_other_region) do
Tenant.skip_callback(:create, :after, :create_miq_product_features_for_tenant_nodes)
tenant = FactoryGirl.create(:tenant, :id => id_for_model_in_region(Tenant, other_miq_region))
Tenant.set_callback(:create, :after, :create_miq_product_features_for_tenant_nodes)
tenant
end
it "doesn't seed tenant features for tenants from remote regions" do
MiqProductFeature.seed_tenant_miq_product_features
expect(tenant_product_feature_other_region.miq_product_features.to_a).to be_empty
expect(tenant.miq_product_features.map(&:identifier)).to match_array(["dialog_copy_editor_tenant_#{tenant.id}"])
end
end
context "add tenant node product features" do
let(:base) do
{
:feature_type => "node",
:identifier => "everything",
:children => [
{
:feature_type => "node",
:identifier => "one",
:name => "One",
:children => [
{
:feature_type => "admin",
:identifier => "dialog_copy_editor",
:name => "Edit",
:description => "XXX"
}
]
},
{
:feature_type => "admin",
:identifier => "dialog_delete",
:name => "Add",
:description => "YYY"
}
]
}
end
it "add new tenant feature" do
features = miq_product_feature_class.tenant_features_in_hash
expect(features).to match_array([{ "name" => "Edit (#{root_tenant.name})", "description" => "XXX for tenant #{root_tenant.name}",
"identifier" => "dialog_copy_editor_tenant_#{root_tenant.id}", "tenant_id" => root_tenant.id},
{"name" => "Edit (#{tenant.name})", "description" => "XXX for tenant #{tenant.name}",
"identifier" => "dialog_copy_editor_tenant_#{tenant.id}", "tenant_id" => tenant.id},
{"name" => "Add (#{root_tenant.name})", "description" => "YYY for tenant #{root_tenant.name}",
"identifier" => "dialog_delete_tenant_#{root_tenant.id}", "tenant_id" => root_tenant.id},
{"name" => "Add (#{tenant.name})", "description" => "YYY for tenant #{tenant.name}",
"identifier" => "dialog_delete_tenant_#{tenant.id}", "tenant_id" => tenant.id}])
expect(MiqProductFeature.where(:identifier => "dialog_delete", :name => "Add").count).to eq(1)
end
context "remove added tenant feaure" do
let(:base) do
{
:feature_type => "node",
:identifier => "everything",
:children => [
{
:feature_type => "node",
:identifier => "one",
:name => "One",
:children => [
{
:feature_type => "admin",
:identifier => "dialog_copy_editor",
:name => "Edit",
:description => "XXX"
}
]
}
]
}
end
it "removes tenant features" do
features = miq_product_feature_class.tenant_features_in_hash
expect(features).to match_array([{ "name" => "Edit (#{root_tenant.name})", "description" => "XXX for tenant #{root_tenant.name}",
"identifier" => "dialog_copy_editor_tenant_#{root_tenant.id}", "tenant_id" => root_tenant.id},
{"name" => "Edit (#{tenant.name})", "description" => "XXX for tenant #{tenant.name}",
"identifier" => "dialog_copy_editor_tenant_#{tenant.id}", "tenant_id" => tenant.id}])
expect(MiqProductFeature.where(:identifier => "dialog_copy_editor", :name => "Edit").count).to eq(1)
end
end
end
end
end
end
describe '#feature_children' do
it "returns only visible features" do
hierarchical_features
expect(MiqProductFeature).not_to receive(:sort_children)
expect(MiqProductFeature.feature_children("miq_report_widget_admin", false)).to match_array(%w(widget_copy widget_edit))
end
it "returns direct children only" do
hierarchical_features
expect(MiqProductFeature.feature_children("miq_report_widget_editor")).to match_array(%w(miq_report_widget_admin))
end
it "sorts features" do
hierarchical_features
expect(MiqProductFeature).to receive(:sort_children).and_call_original
expect(MiqProductFeature.feature_children("miq_report_widget_admin")).to match_array(%w(widget_copy widget_edit))
end
end
describe '#feature_all_children' do
it "returns all visible children" do
hierarchical_features
expect(MiqProductFeature).not_to receive(:sort_children)
expect(MiqProductFeature.feature_all_children("miq_report_widget_editor", false)).to match_array(
%w(widget_copy widget_edit miq_report_widget_admin))
end
it "returns all visible children sorted" do
hierarchical_features
expect(MiqProductFeature).to receive(:sort_children).and_call_original
expect(MiqProductFeature.feature_all_children("miq_report_widget_editor")).to eq(
%w(widget_copy widget_edit miq_report_widget_admin))
end
end
describe "#feature_details" do
it "returns data for visible features" do
EvmSpecHelper.seed_specific_product_features("container_dashboard")
expect(MiqProductFeature.feature_details("container_dashboard")).to be_truthy
end
it "eats hidden features" do
EvmSpecHelper.seed_specific_product_features("widget_refresh")
expect(MiqProductFeature.feature_details("widget_refresh")).not_to be
end
end
describe "#feature_hidden" do
it "detects visible features" do
EvmSpecHelper.seed_specific_product_features("container_dashboard")
expect(MiqProductFeature.feature_hidden("container_dashboard")).not_to be
end
it "detects hidden features" do
EvmSpecHelper.seed_specific_product_features("widget_refresh")
expect(MiqProductFeature.feature_hidden("widget_refresh")).to be_truthy
end
end
describe ".features" do
before { MiqProductFeature.instance_variable_set(:@feature_cache, nil) }
after { MiqProductFeature.instance_variable_set(:@feature_cache, nil) }
# 1
# 2 3
# 4 5
it "populates parent and children" do
f1 = FactoryBot.create(:miq_product_feature, :identifier => "f1", :name => "F1n")
FactoryBot.create(:miq_product_feature, :identifier => "f2", :name => "F2n", :parent_id => f1.id)
f3 = FactoryBot.create(:miq_product_feature, :identifier => "f3", :name => "F3n", :parent_id => f1.id)
FactoryBot.create(:miq_product_feature, :identifier => "f4", :name => "F4n", :parent_id => f3.id)
FactoryBot.create(:miq_product_feature, :identifier => "f5", :name => "F5n", :parent_id => f3.id)
expect { MiqProductFeature.features }.to match_query_limit_of(1)
expect { MiqProductFeature.features }.to match_query_limit_of(0)
expect(MiqProductFeature.feature_root).to eq("f1")
expect(MiqProductFeature.feature_children("f1")).to match_array(%w(f2 f3))
expect(MiqProductFeature.feature_children("f2")).to match_array([])
expect(MiqProductFeature.feature_children("f3")).to match_array(%w(f4 f5))
expect(MiqProductFeature.feature_parent("f1")).to be_nil
expect(MiqProductFeature.feature_parent("f2")).to eq("f1")
expect(MiqProductFeature.feature_parent("f4")).to eq("f3")
end
end
describe "feature object cache" do
let!(:f0) { FactoryBot.create(:miq_product_feature, :identifier => "everything", :name => "F0n") }
let!(:f1) { FactoryBot.create(:miq_product_feature, :identifier => "f1", :name => "F1n", :parent_id => f0.id) }
let!(:f2) { FactoryBot.create(:miq_product_feature, :identifier => "f2", :name => "F2n", :parent_id => f1.id) }
let!(:f3) { FactoryBot.create(:miq_product_feature, :identifier => "f3", :name => "F3n", :parent_id => f1.id) }
let!(:f4) { FactoryBot.create(:miq_product_feature, :identifier => "f4", :name => "F4n", :parent_id => f3.id) }
let!(:f5) { FactoryBot.create(:miq_product_feature, :identifier => "f5", :name => "F5n", :parent_id => f3.id) }
it "memoizes hash to prevent extra db queries" do
expect { MiqProductFeature.obj_features }.to match_query_limit_of(1)
expect { MiqProductFeature.obj_features }.to match_query_limit_of(0)
end
it "builds a hash with feature objects keyed by identifier" do
expect(MiqProductFeature.obj_features["f1"][:feature]).to eq(f1)
end
it "builds a hash of features objects with child objects" do
expect(MiqProductFeature.feature_root).to eq("everything")
expect(MiqProductFeature.obj_feature_children("f1")).to match_array([f2, f3])
expect(MiqProductFeature.obj_feature_children("f2")).to match_array([])
expect(MiqProductFeature.obj_feature_children("f3")).to match_array([f4, f5])
end
it "references the parent by identifier" do
expect(MiqProductFeature.obj_feature_parent("everything")).to be_nil
expect(MiqProductFeature.obj_feature_parent("f2")).to eq("f1")
expect(MiqProductFeature.obj_feature_parent("f4")).to eq("f3")
end
it "finds the ancestors of a feature" do
ancestors = MiqProductFeature.obj_feature_ancestors("f4")
expect(ancestors.count).to eq(3)
expect(ancestors).to include(f0)
expect(ancestors).to include(f3)
expect(ancestors).to include(f1)
end
it "handles bad feature names" do
expect(MiqProductFeature.obj_feature_ancestors("foo")).to eq([])
expect(MiqProductFeature.obj_feature_children("foo")).to be_nil
expect(MiqProductFeature.obj_feature_all_children("foo")).to be_nil
end
end
end