Skip to content

Commit 9bd51cc

Browse files
authored
Merge pull request #9501 from GilbertCherrie/fix_button_form_change_field_validation
Fix button form save button validation
2 parents ab6ea41 + d046c07 commit 9bd51cc

File tree

3 files changed

+161
-4
lines changed

3 files changed

+161
-4
lines changed

app/controllers/application_controller/buttons.rb

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,38 @@ def group_update
100100
# we need a "Provider" so adding an "ExtManagementSystem" to the list.
101101
MODEL_WITH_OPEN_URL = %w[ExtManagementSystem Service User MiqGroup Tenant CloudTenant GenericObject Vm].freeze
102102

103+
# Types of visibility for the custom buttons
104+
VISIBILITY_TYPES = {'role' => 'role', 'all' => 'all'}.freeze
105+
ALL_ROLES = ["_ALL_", "all"].freeze
106+
FIELDS_EMPTY = [[""], ["", ""]].freeze
107+
108+
def button_visibility_box_edit
109+
typ_changed = params[:visibility_typ].present?
110+
@edit[:new][:visibility_typ] = VISIBILITY_TYPES[params[:visibility_typ]] if typ_changed
111+
visibility_typ = @edit[:new][:visibility_typ]
112+
113+
if visibility_typ.to_s == "role"
114+
plural = visibility_typ.pluralize
115+
key = plural.to_sym
116+
prefix = "#{plural}_"
117+
118+
@edit[:new][key] = [] if typ_changed
119+
params.each do |var, value|
120+
next unless var.starts_with?(prefix)
121+
122+
name = var.split(prefix).last.to_i
123+
if value == "1"
124+
@edit[:new][key] |= [name] # union
125+
elsif value.downcase == "null"
126+
@edit[:new][key].delete(name)
127+
end
128+
end
129+
else
130+
@edit[:new][:roles] ||= []
131+
@edit[:new][:roles] |= ["_ALL_"]
132+
end
133+
end
134+
103135
def automate_button_field_changed
104136
assert_privileges(feature_by_action)
105137

@@ -125,7 +157,7 @@ def automate_button_field_changed
125157
@edit[:new][:dialog_id] = nil if params[:display_for].present? && params[:display_for] != 'single'
126158

127159
@edit[:new][:dialog_id] = params[:dialog_id] == "" ? nil : params[:dialog_id] if params.key?("dialog_id")
128-
visibility_box_edit
160+
button_visibility_box_edit
129161

130162
if params[:button_type] == 'default'
131163
clear_playbook_variables
@@ -147,7 +179,55 @@ def automate_button_field_changed
147179
page.replace("form_role_visibility", :partial => "layouts/role_visibility", :locals => {:rec_id => (@custom_button.id || "new").to_s, :action => "automate_button_field_changed"})
148180
end
149181
unless params[:target_class]
150-
@changed = session[:changed] = (@edit[:new] != @edit[:current])
182+
@changed = session[:changed] = false
183+
# This block checks if any of the fields have changed
184+
# Broken into different branches for specific fields
185+
# If no fields are changed it will not set @changed or session[:changed] to true
186+
@edit[:new].each_key do |key|
187+
if @edit[:new][key] != @edit[:current][key]
188+
if @edit[:new][key].blank? && @edit[:current][key].blank? # check empty string / nil case
189+
next
190+
elsif @edit[:new][key] == @edit[:current][key].to_s # check string / integer case
191+
next
192+
elsif key == :roles # check role values
193+
if (ALL_ROLES.include?(@edit[:new][key]) || @edit[:new][key].empty?) && (ALL_ROLES.include?(@edit[:current][key]) || @edit[:current][key].nil?) # if visibility is set to "To All"
194+
next
195+
elsif @edit[:new][key].collect(&:to_i).sort == @edit[:current][key].collect(&:to_i).sort # check if new roles array and current roles array are equal after sorting the ids
196+
next
197+
else # if new roles array and current roles array are not equal
198+
@changed = session[:changed] = true
199+
break
200+
201+
end
202+
elsif key == :attrs # check attribute values
203+
@edit[:new][key].each_with_index do |_item, index|
204+
if FIELDS_EMPTY.include?(@edit[:new][:attrs][index]) && @edit[:current][:attrs][index].empty? # check if attribute and value field is empty
205+
next
206+
elsif @edit[:new][:attrs][index].empty? && @edit[:current][:attrs][index].empty? # check if attribute or value field is empty
207+
next
208+
else # if new attribute array and current attribute array are not equal
209+
@changed = session[:changed] = true
210+
break
211+
212+
end
213+
end
214+
215+
elsif key == :visibility_typ # check visibility type
216+
if @edit[:new][key] == "all" && @edit[:current][key].nil? # if visibility is set to "To All"
217+
next
218+
else # if new visibility type value and current visibility type value are not equal
219+
@changed = session[:changed] = true
220+
break
221+
222+
end
223+
else # if new value and current value are not equal
224+
@changed = session[:changed] = true
225+
break
226+
227+
end
228+
end
229+
end
230+
151231
page << javascript_for_miq_button_visibility(@changed)
152232
end
153233
page << "miqSparkle(false);"

cypress.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module.exports = defineConfig({
77
baseUrl: 'http://localhost:3000',
88
viewportHeight: 800,
99
viewportWidth: 1800,
10-
numTestsKeptInMemory: 0,
10+
numTestsKeptInMemory: 5,
1111
videoCompression: false,
1212

1313
// See: https://docs.cypress.io/app/references/experiments#Experimental-Flake-Detection-Features

cypress/e2e/ui/Automation/Embedded-Automate/customization.cy.js

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
describe('Automation > Embedded Automate > Customization', () => {
44
beforeEach(() => {
55
cy.login();
6-
cy.intercept('POST', '/ops/accordion_select?id=rbac_accord').as('accordion');
76
cy.menu('Automation', 'Embedded Automate', 'Customization');
87
cy.get('#explorer_title_text');
98
});
@@ -162,4 +161,82 @@ describe('Automation > Embedded Automate > Customization', () => {
162161
cy.get('[class="list-group"]').should('not.contain', 'Test Description');
163162
});
164163
});
164+
165+
describe('Button Form', () => {
166+
beforeEach(() => {
167+
cy.intercept('POST', '/miq_ae_customization/accordion_select?id=ab_accord').as('accordion');
168+
cy.get('#control_ab_accord > .panel-title > .collapsed').click();
169+
cy.wait('@accordion');
170+
cy.wait(5000);
171+
});
172+
173+
it('Validates the save button correctly', () => {
174+
cy.get('.clickable-row').contains('Availability Zone').click({force: true});
175+
cy.get('.clickable-row').contains('Unassigned Buttons').click({force: true});
176+
cy.get('[title="Configuration"]').click({force: true});
177+
cy.get('[title="Add a new Button"]').click({force: true});
178+
cy.get('#explorer_title_text').contains('Adding a new Button');
179+
180+
cy.get('#name').type('Test Button');
181+
cy.get('#description').type('Test Description');
182+
cy.get('.icon-button').click();
183+
cy.get(':nth-child(1) > span > .ff').click();
184+
cy.get('.bx--modal-footer > .bx--btn--primary').click();
185+
186+
cy.get('#ab_advanced_tab_tab > a').click();
187+
cy.get('#object_request').type('Test Request');
188+
cy.get('#attribute_1').type('1-attribute');
189+
cy.get('#value_1').type('1-value');
190+
cy.get('#attribute_2').type('2-attribute');
191+
cy.get('#value_2').type('2-value');
192+
cy.get('.col-md-10 > .btn-group > .btn').click();
193+
cy.get('.col-md-10 > .btn-group > .open > .dropdown-menu > [data-original-index="1"] > a').click();
194+
195+
cy.get('#roles_1').click();
196+
cy.get('#roles_2').click();
197+
cy.get('#roles_3').click();
198+
cy.get('#roles_4').click();
199+
200+
cy.get('#buttons_on > .btn-primary').click();
201+
cy.get('.clickable-row').contains('Test Button').click();
202+
203+
cy.get('.attribute_value_pair').contains('1-attribute, 1-value');
204+
cy.get('.attribute_value_pair').contains('2-attribute, 2-value');
205+
cy.get('.visibility').contains('EvmRole-administrator, EvmRole-approver, EvmRole-physical_storages_administrator, EvmRole-super_administrator');
206+
207+
cy.get('[title="Configuration"]').click();
208+
cy.get('[title="Edit this Button"]').click();
209+
210+
cy.get('#buttons_off > .btn-primary');
211+
cy.get('#ab_advanced_tab_tab > a').click();
212+
cy.get('#attribute_2').clear();
213+
cy.get('#value_2').clear();
214+
215+
cy.get('#buttons_on > .btn-primary').click();
216+
217+
cy.get('.attribute_value_pair').contains('1-attribute, 1-value');
218+
cy.get('.attribute_value_pair').should('not.contain', '2-attribute, 2-value');
219+
220+
cy.get('[title="Configuration"]').click();
221+
cy.get('[title="Edit this Button"]').click();
222+
223+
cy.get('#buttons_off > .btn-primary');
224+
cy.get('#ab_advanced_tab_tab > a').click();
225+
226+
cy.get('#roles_2').click();
227+
cy.get('#roles_3').click();
228+
229+
cy.get('#buttons_on > .btn-primary').click();
230+
231+
cy.get('.visibility').contains('EvmRole-approver, EvmRole-super_administrator');
232+
cy.get('.visibility').should('not.contain', 'EvmRole-administrator, EvmRole-physical_storages_administrator');
233+
234+
cy.get('[title="Configuration"]').click();
235+
cy.get('[title="Remove this Button"]').click();
236+
cy.get('h4 > strong').contains('Test Button');
237+
cy.get('.Delete').click({ force: true });
238+
239+
cy.get('.alert').contains('The item "Test Button" has been successfully deleted');
240+
});
241+
});
165242
});

0 commit comments

Comments
 (0)