/
miq_request_workflow_spec.rb
180 lines (143 loc) · 6.58 KB
/
miq_request_workflow_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
require "spec_helper"
describe MiqRequestWorkflow do
let(:workflow) { FactoryGirl.build(:miq_provision_workflow) }
context "#validate" do
let(:dialog) { workflow.instance_variable_get(:@dialogs) }
context "validation_method" do
it "skips validation if no validation_method is defined" do
expect(workflow.get_all_dialogs[:customize][:fields][:root_password][:validation_method]).to eq(nil)
expect(workflow.validate({})).to be_true
end
it "calls the validation_method if defined" do
dialog.store_path(:dialogs, :customize, :fields, :root_password, :validation_method, :some_validation_method)
expect(workflow).to receive(:respond_to?).with(:some_validation_method).and_return(true)
expect(workflow).to receive(:some_validation_method).once
expect(workflow.validate({})).to be_true
end
it "returns false when validation fails" do
dialog.store_path(:dialogs, :customize, :fields, :root_password, :validation_method, :some_validation_method)
expect(workflow).to receive(:respond_to?).with(:some_validation_method).and_return(true)
expect(workflow).to receive(:some_validation_method).and_return("Some Error")
expect(workflow.validate({})).to be_false
end
end
context 'required_method is only run on visible fields' do
it "field hidden" do
dialog.store_path(:dialogs, :customize, :fields, :root_password, :required_method, :some_required_method)
dialog.store_path(:dialogs, :customize, :fields, :root_password, :required, true)
dialog.store_path(:dialogs, :customize, :fields, :root_password, :display, :hide)
expect(workflow).to_not receive(:some_required_method)
expect(workflow.validate({})).to be_true
end
it "field visible" do
dialog.store_path(:dialogs, :customize, :fields, :root_password, :required_method, :some_required_method)
dialog.store_path(:dialogs, :customize, :fields, :root_password, :required, true)
expect(workflow).to receive(:some_required_method).and_return("Some Error")
expect(workflow.validate({})).to be_false
end
end
end
describe "#init_from_dialog" do
let(:dialogs) { workflow.instance_variable_get(:@dialogs) }
let(:init_values) { {} }
context "when the initial values already have a value for the field name" do
let(:init_values) { {:root_password => "root"} }
it "does not modify the initial values" do
workflow.init_from_dialog(init_values, 123)
expect(init_values).to eq(:root_password => "root")
end
end
context "when the dialog fields ignore display" do
before do
dialogs[:dialogs].keys.each do |dialog_name|
workflow.get_all_fields(dialog_name).each_pair do |_, field_values|
field_values[:display] = :ignore
end
end
end
it "does not modify the initial values" do
workflow.init_from_dialog(init_values, 123)
expect(init_values).to eq({})
end
end
context "when the dialog field values default is not nil" do
before do
dialogs[:dialogs].keys.each do |dialog_name|
workflow.get_all_fields(dialog_name).each_pair do |_, field_values|
field_values[:default] = "not nil"
end
end
end
it "modifies the initial values with the default value" do
workflow.init_from_dialog(init_values, 123)
expect(init_values).to eq(:root_password => "not nil")
end
end
context "when the dialog field values default is nil" do
before do
dialogs[:dialogs].keys.each do |dialog_name|
workflow.get_all_fields(dialog_name).each_pair do |_, field_values|
field_values[:default] = nil
end
end
end
context "when the field values are a hash" do
before do
dialogs[:dialogs].keys.each do |dialog_name|
workflow.get_all_fields(dialog_name).each_pair do |_, field_values|
field_values[:values] = {:something => "test"}
end
end
end
it "uses the first field value" do
workflow.init_from_dialog(init_values, 123)
expect(init_values).to eq(:root_password => [:something, "test"])
end
end
context "when the field values are not a hash" do
before do
dialogs[:dialogs].keys.each do |dialog_name|
workflow.get_all_fields(dialog_name).each_pair do |_, field_values|
field_values[:values] = [%w(test 100), %w(test2 0)]
end
end
end
it "uses values as [value, description] for timezones aray" do
workflow.init_from_dialog(init_values, 123)
expect(init_values).to eq(:root_password => [nil, "test2"])
end
end
end
end
describe "#provisioning_tab_list" do
let(:dialogs) { workflow.instance_variable_get(:@dialogs) }
before do
dialogs[:dialog_order] = [:test, :test2, :test3]
dialogs[:dialogs][:test] = {:description => "test description", :display => :hide}
dialogs[:dialogs][:test2] = {:description => "test description 2", :display => :ignore}
dialogs[:dialogs][:test3] = {:description => "test description 3", :display => :edit}
end
it "returns a list of tabs without the hidden or ignored ones" do
expect(workflow.provisioning_tab_list).to eq([{:name => "test3", :description => "test description 3"}])
end
end
context "'allowed_*' methods" do
let(:cluster) { FactoryGirl.create(:ems_cluster, :ems_id => ems.id) }
let(:ems) { FactoryGirl.create(:ext_management_system) }
let(:resource_pool) { FactoryGirl.create(:resource_pool, :ems_id => ems.id) }
it "#allowed_clusters calls allowed_ci with the correct set of cluster ids" do
FactoryGirl.create(:ems_cluster)
allow_any_instance_of(User).to receive(:get_timezone).and_return("UTC")
allow(workflow).to receive(:get_source_and_targets).and_return(:ems => ems)
expect(workflow).to receive(:allowed_ci).with(:cluster, [:respool, :host, :folder], [cluster.id])
workflow.allowed_clusters
end
it "#allowed_resource_pools calls allowed_ci with the correct set of resource_pool ids" do
FactoryGirl.create(:resource_pool)
allow_any_instance_of(User).to receive(:get_timezone).and_return("UTC")
allow(workflow).to receive(:get_source_and_targets).and_return(:ems => ems)
expect(workflow).to receive(:allowed_ci).with(:respool, [:cluster, :host, :folder], [resource_pool.id])
workflow.allowed_resource_pools
end
end
end