Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 5 additions & 15 deletions app/controllers/application_controller/buttons.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: keep the lookup but drop the loop...

button.visibility[:roles] = MiqUserRole.where(:id => @edit[:new][:roles]).pluck(:id)

else
button.visibility[:roles] = ["_ALL_"]
end
Expand Down Expand Up @@ -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] = []
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 : ""
Expand Down
Original file line number Diff line number Diff line change
@@ -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 "<By Role>" 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');
});
});
});
6 changes: 3 additions & 3 deletions spec/controllers/application_controller/buttons_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@
:object_request => 'request',
:open_url => true,
:visibility_typ => 'role',
:roles => [role.id.to_s],
:roles => [role.id],
:display_for => :list}
}
}
Expand All @@ -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])
Copy link
Member Author

@jrafanie jrafanie Nov 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: that comment on line 347 is nuts. I need to figure out if roles as strings and ints is an actual thing.

end
end

Expand Down