diff --git a/app/controllers/application_controller/buttons.rb b/app/controllers/application_controller/buttons.rb index 5c52d427473..1a5c7dbbcb2 100644 --- a/app/controllers/application_controller/buttons.rb +++ b/app/controllers/application_controller/buttons.rb @@ -852,12 +852,7 @@ def button_set_record_vars(button) end button.visibility ||= {} if @edit[:new][:visibility_typ] == "role" - roles = [] - @edit[:new][:roles].each do |r| - role = MiqUserRole.find_by(:id => r) - roles.push(role.name) if role - end - button.visibility[:roles] = roles + button.visibility[:roles] = @edit[:new][:roles] else button.visibility[:roles] = ["_ALL_"] end @@ -1026,13 +1021,9 @@ def button_set_form_vars if @custom_button.visibility[:roles][0] == "_ALL_" @edit[:new][:roles] = ["_ALL_"] else - @edit[:new][:roles] ||= [] - @custom_button.visibility[:roles].each do |r| - role = MiqUserRole.find_by(:name => r) - @edit[:new][:roles].push(role.id) if role - end + @edit[:new][:roles] = @custom_button.visibility[:roles] + @edit[:new][:roles].sort! if @edit[:new][:roles].present? end - @edit[:new][:roles].sort! if @edit[:new][:roles].present? end @edit[:sorted_user_roles] = [] @@ -1096,9 +1087,8 @@ def buttons_get_node_info(node) end @sb[:user_roles] = [] if @custom_button.visibility && @custom_button.visibility[:roles] && @custom_button.visibility[:roles][0] != "_ALL_" - MiqUserRole.all.sort_by(&:name).each do |r| - @sb[:user_roles].push(r.name) if @custom_button.visibility[:roles].include?(r.name) - end + role_ids = @custom_button.visibility[:roles] + @sb[:user_roles] = MiqUserRole.where(:id => role_ids).order(:name).pluck(:name) end @resolve[:new][:target_class] = "ServiceTemplate" dialog_id = @custom_button.resource_action.dialog_id diff --git a/app/controllers/miq_ae_customization_controller/custom_buttons.rb b/app/controllers/miq_ae_customization_controller/custom_buttons.rb index f6959c3b719..9ed2e6e60b2 100644 --- a/app/controllers/miq_ae_customization_controller/custom_buttons.rb +++ b/app/controllers/miq_ae_customization_controller/custom_buttons.rb @@ -76,9 +76,8 @@ def ab_get_node_info(node) end @sb[:user_roles] = [] if @custom_button.visibility && @custom_button.visibility[:roles] && @custom_button.visibility[:roles][0] != "_ALL_" - MiqUserRole.all.sort_by(&:name).each do |r| - @sb[:user_roles].push(r.name) if @custom_button.visibility[:roles].include?(r.name) - end + role_ids = @custom_button.visibility[:roles] + @sb[:user_roles] = MiqUserRole.where(:id => role_ids).order(:name).pluck(:name) end dialog_id = @custom_button.resource_action.dialog_id @sb[:dialog_label] = dialog_id ? Dialog.find(dialog_id).label : "" diff --git a/cypress/e2e/ui/Automation/Embedded-Automate/customization_buttons.cy.js b/cypress/e2e/ui/Automation/Embedded-Automate/customization_buttons.cy.js new file mode 100644 index 00000000000..89d3c960f3a --- /dev/null +++ b/cypress/e2e/ui/Automation/Embedded-Automate/customization_buttons.cy.js @@ -0,0 +1,87 @@ +/* eslint-disable no-undef */ +import { flashClassMap } from '../../../../support/assertions/assertion_constants'; + +describe('Automation > Embedded Automate > Customization > Buttons', () => { + beforeEach(() => { + cy.login(); + cy.menu('Automation', 'Embedded Automate', 'Customization'); + cy.get('#explorer_title_text'); + + // Navigate to Buttons accordion + cy.accordion('Buttons'); + }); + + afterEach(() => { + cy.appDbState('restore'); + }); + + describe('Button Creation', () => { + it('Creates a new button for Availability Zone', () => { + // Navigate through the accordion: Object Types > Availability Zone > [Unassigned Buttons] + cy.selectAccordionItem(['Object Types', 'Availability Zone', '[Unassigned Buttons]']); + + // Use toolbar command to add a new button + cy.toolbar('Configuration', 'Add a new Button'); + + // Verify we're on the add button page + cy.get('#explorer_title_text').contains('Adding a new Button'); + + // Fill in name and description with hardcoded values + cy.get('#name').type('Test Availability Zone Button'); + cy.get('#description').type('Test button for availability zone'); + + // Select an icon from the dropdown + cy.get('.icon-button').click(); + cy.get(':nth-child(1) > span > .ff').click(); + cy.get('.bx--modal-footer > .bx--btn--primary').click(); + + // Click the Advanced tab + cy.tabs({ tabLabel: 'Advanced' }); + + // Scroll to object details part of the form and fill in the request field + cy.get('#object_request').scrollIntoView(); + cy.get('#object_request').type('test_request'); + + // Scroll to role access section and select "" from dropdown + cy.get('#form_role_visibility button[data-id="visibility_typ"]').scrollIntoView(); + cy.get('#form_role_visibility button[data-id="visibility_typ"]').click(); + cy.get('#form_role_visibility .dropdown-menu [data-original-index="1"] > a').click(); + + // Scroll to User Roles section to make roles visible + cy.contains('User Roles').scrollIntoView(); + + // Check EvmRole-auditor and EvmRole-desktop + cy.interceptApi({ + alias: 'checkAuditorRole', + urlPattern: '/miq_ae_customization/automate_button_field_changed', + triggerFn: () => cy.get('#form_role_visibility').contains('EvmRole-auditor').closest('td').find('input[type="checkbox"]').check(), + waitOnlyIfRequestIntercepted: true, + }); + + cy.interceptApi({ + alias: 'checkDesktopRole', + urlPattern: '/miq_ae_customization/automate_button_field_changed', + triggerFn: () => cy.get('#form_role_visibility').contains('EvmRole-desktop').closest('td').find('input[type="checkbox"]').check(), + waitOnlyIfRequestIntercepted: true, + }); + + // Click Add button + // TODO: cy.getFormButtonByTypeWithText expects this in the main_content div, but it's in the form_buttons_div + // within the full_content div - enhance the cy.getFormButtonByTypeWithText to accept a div id or normalize the location of the button + cy.get('#buttons_on > .btn-primary').click(); + + // Verify flash message that button was added + cy.expect_flash(flashClassMap.success, 'was added'); + + // Verify the button was added successfully by clicking it in the accordion + cy.get('.clickable-row').contains('Test Availability Zone Button').click(); + + // Verify the name, description, request, and roles are displayed correctly in the main div + cy.get('#main_div').contains('Test Availability Zone Button'); + cy.get('#main_div').contains('Test button for availability zone'); + cy.get('#main_div').contains('test_request'); + cy.get('.visibility').contains('EvmRole-auditor'); + cy.get('.visibility').contains('EvmRole-desktop'); + }); + }); +}); diff --git a/spec/controllers/application_controller/buttons_spec.rb b/spec/controllers/application_controller/buttons_spec.rb index 2d64b350a3f..6b4c7b028d0 100644 --- a/spec/controllers/application_controller/buttons_spec.rb +++ b/spec/controllers/application_controller/buttons_spec.rb @@ -325,7 +325,7 @@ :object_request => 'request', :open_url => true, :visibility_typ => 'role', - :roles => [role.id.to_s], + :roles => [role.id], :display_for => :list} } } @@ -340,13 +340,13 @@ it "sets new role visibility for custom button" do controller.send(:button_set_record_vars, custom_button) - expect(custom_button.visibility[:roles]).to eq([role.name]) + expect(custom_button.visibility[:roles]).to eq([role.id]) end it "sets new role and preserves old role for custom button" do edit[:new][:roles] = [old_role.id, role.id.to_s] # old roles are represented by int and new ones by string controller.send(:button_set_record_vars, custom_button) - expect(custom_button.visibility[:roles]).to eq([old_role.name, role.name]) + expect(custom_button.visibility[:roles]).to eq([old_role.id, role.id.to_s]) end end