Skip to content

Commit

Permalink
Fixes #34300 - correctly construct the fqdn for redirect
Browse files Browse the repository at this point in the history
In host form we've used a hack in 2876578 to redirect to the new Host details page.
It relied on the primary interface being the first interface in the table.

This fixes it to always incur the correct fqdn.
  • Loading branch information
ezr-ondrej committed Jan 24, 2022
1 parent 0d80e69 commit 5fa5bca
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 33 deletions.
2 changes: 1 addition & 1 deletion app/assets/javascripts/host_edit.js
Expand Up @@ -159,7 +159,7 @@ function update_capabilities(capabilities) {
var stop_pooling;

function submit_with_all_params() {
var fqdn = $('.fqdn')[0].textContent;
var fqdn = construct_fqdn();
$('form.hostresource-form input[type="submit"]').attr('disabled', true);
stop_pooling = false;
$('body').css('cursor', 'progress');
Expand Down
14 changes: 11 additions & 3 deletions app/assets/javascripts/host_edit_interfaces.js
Expand Up @@ -388,13 +388,21 @@ $(document).on('change', '.virtual', function() {
.toggle(!is_virtual);
});

function update_fqdn() {
var host_name = $('#host_name').val();
function construct_host_name() {
var $host_name = $('#host_name')
var host_name = $host_name.val();
if ($host_name.data('append_domain_name_for_hosts') === false) {
return host_name;
}
var domain_name = primary_nic_form()
.find('.interface_domain option:selected')
.text();
return fqdn(host_name, domain_name);
}

function update_fqdn() {
var name = construct_host_name();
var pathname = window.location.pathname;
var name = fqdn(host_name, domain_name);
if (name.length > 0 && pathname === tfm.tools.foremanUrl('/hosts/new')) {
name = __('Create Host') + ' | ' + name;
tfm.store.dispatch('updateBreadcrumbTitle', name);
Expand Down
3 changes: 2 additions & 1 deletion app/views/hosts/_form.html.erb
Expand Up @@ -31,7 +31,8 @@
<div class="tab-pane active" id="primary">
<%= text_f f, :name, :size => "col-md-4", :value => name_field(@host),
:input_group_btn => randomize_mac_link,
:help_inline => _("This value is used also as the host's primary interface name.") %>
:help_inline => _("This value is used also as the host's primary interface name."),
:data => { 'append_domain_name_for_hosts' => Setting[:append_domain_name_for_hosts] } %>
<% if show_organization_tab? %>
<%= host_taxonomy_select(f, Organization) %>
Expand Down
100 changes: 72 additions & 28 deletions test/integration/host_js_test.rb
Expand Up @@ -123,35 +123,79 @@ class HostJSTest < IntegrationTestWithJavascript
find('h1', :text => /Manage Host's Statuses/)
end

test "create new host and redirect to the new host details page" do
compute_resource = FactoryBot.create(:compute_resource, :libvirt)
os = FactoryBot.create(:ubuntu14_10, :with_associations)
Nic::Managed.any_instance.stubs(:dns_conflict_detected?).returns(true)
visit new_host_path

fill_in 'host_name', :with => 'newhost1'
select2 'Organization 1', :from => 'host_organization_id'
wait_for_ajax
select2 'Location 1', :from => 'host_location_id'
wait_for_ajax
select2 compute_resource.name, :from => 'host_compute_resource_id'

click_link 'Operating System'
wait_for_ajax
select2 os.architectures.first.name, :from => 'host_architecture_id'
select2 os.title, :from => 'host_operatingsystem_id'
uncheck('host_build')
select2 os.media.first.name, :from => 'host_medium_id'
select2 os.ptables.first.name, :from => 'host_ptable_id'
fill_in 'host_root_pass', :with => '12345678'
describe 'create and redirect' do
test 'redirects correctly with second nic being primary' do
compute_resource = FactoryBot.create(:compute_resource, :libvirt)
os = FactoryBot.create(:ubuntu14_10, :with_associations)
Nic::Managed.any_instance.stubs(:dns_conflict_detected?).returns(true)
visit new_host_path

fill_in 'host_name', :with => 'newhost1'
select2 'Organization 1', :from => 'host_organization_id'
wait_for_ajax
select2 'Location 1', :from => 'host_location_id'
wait_for_ajax
select2 compute_resource.name, :from => 'host_compute_resource_id'

click_link 'Operating System'
wait_for_ajax
select2 os.architectures.first.name, :from => 'host_architecture_id'
select2 os.title, :from => 'host_operatingsystem_id'
uncheck('host_build')
select2 os.media.first.name, :from => 'host_medium_id'
select2 os.ptables.first.name, :from => 'host_ptable_id'
fill_in 'host_root_pass', :with => '12345678'

switch_form_tab_to_interfaces
page.find(:button, 'Edit').click
select2 domains(:mydomain).name, :from => "host_interfaces_attributes_0_domain_id"
fill_in "host_interfaces_attributes_0_ip", :with => '1.1.1.1'
close_interfaces_modal

page.find(:button, '+ Add Interface').click
interface_id = page.evaluate_script("$('#interfaceModal').data('current-id');")
fill_in "host_interfaces_attributes_#{interface_id}_name", :with => 'newhost2'
select2 domains(:mydomain).name, :from => "host_interfaces_attributes_#{interface_id}_domain_id"
fill_in "host_interfaces_attributes_#{interface_id}_ip", :with => '1.1.1.2'
accept_confirm do
find("#host_interfaces_attributes_#{interface_id}_primary").check
end
close_interfaces_modal
click_button('Submit')
find('h5', :text => /newhost2.*/) # wait for the new host details page
end

switch_form_tab_to_interfaces
click_button 'Edit'
select2 domains(:mydomain).name, :from => 'host_interfaces_attributes_0_domain_id'
fill_in 'host_interfaces_attributes_0_ip', :with => '1.1.1.1'
close_interfaces_modal
click_button('Submit')
find('h5', :text => /newhost1.*/) # wait for the new host details page
test "redirects correctly with append_domain_name_for_hosts turned off" do
Setting['append_domain_name_for_hosts'] = false
compute_resource = FactoryBot.create(:compute_resource, :libvirt)
os = FactoryBot.create(:ubuntu14_10, :with_associations)
Nic::Managed.any_instance.stubs(:dns_conflict_detected?).returns(true)
visit new_host_path

fill_in 'host_name', :with => 'newhost1'
select2 'Organization 1', :from => 'host_organization_id'
wait_for_ajax
select2 'Location 1', :from => 'host_location_id'
wait_for_ajax
select2 compute_resource.name, :from => 'host_compute_resource_id'

click_link 'Operating System'
wait_for_ajax
select2 os.architectures.first.name, :from => 'host_architecture_id'
select2 os.title, :from => 'host_operatingsystem_id'
uncheck('host_build')
select2 os.media.first.name, :from => 'host_medium_id'
select2 os.ptables.first.name, :from => 'host_ptable_id'
fill_in 'host_root_pass', :with => '12345678'

switch_form_tab_to_interfaces
page.find(:button, 'Edit').click
select2 domains(:mydomain).name, :from => "host_interfaces_attributes_0_domain_id"
fill_in "host_interfaces_attributes_0_ip", :with => '1.1.1.1'
close_interfaces_modal
click_button('Submit')
find('h5', :text => /newhost1/) # wait for the new host details page
end
end
end

Expand Down

0 comments on commit 5fa5bca

Please sign in to comment.