Skip to content
Permalink
Browse files

fixes #2489 - fixes BMC password caused by rails bug accepts_nested_a…

…ttributes_for and serialized child field
  • Loading branch information...
isratrade authored and Dominic Cleal committed Jun 20, 2013
1 parent 3595a70 commit eaa6aec0023de21f0565b33e98f4c68990c0c2e2
Showing with 32 additions and 1 deletion.
  1. +1 −1 app/models/nic/bmc.rb
  2. +4 −0 app/views/hosts/_interfaces.html.erb
  3. +27 −0 test/functional/hosts_controller_test.rb
@@ -2,7 +2,7 @@ module Nic
class BMC < Managed

ATTRIBUTES = [:username, :password, :provider]
attr_accessible *ATTRIBUTES
attr_accessible :updated_at, *ATTRIBUTES

PROVIDERS = %w(IPMI)
validates_inclusion_of :provider, :in => PROVIDERS
@@ -22,6 +22,10 @@
<%= password_f f, :password, ifs_bmc_opts(f.object) %>
<%# TODO: current rails version does not allow to pass a selected value where there is no method (e.g. providers below) rescue here is a hack %>
<%= selectable_f f, :provider, Nic::BMC::PROVIDERS, {:selected => nil}, ifs_bmc_opts(f.object) rescue f.hidden_field :provider, :value => 'IPMI' %>
<%# Hack for Rails bug in 3.2.13 - serialized field attrs does not update through accepts_nested_attributes_for if it's
the only field that is dirty. If updated_at is explicited updated, then attrs will also be updated correctly %>
<%= f.hidden_field :updated_at, :value => Time.now %>

<% end %>

<% end %>
@@ -827,6 +827,33 @@ class Host::Valid < Host::Base ; end
assert_equal old_password, @host.interfaces.bmc.first.password
end

# To test that work-around works for Rails bug with accepts_nested_attributes_for and serialized child field
# Note that both :mac annd :updated_at are dirty in :interfaces_attributes
# For unknown reason, the test didn't pass if only :updated_at was dirty, but it works in the UI to only pass :updated_at
test "BMC password updates successful in attrs serialized field if updated_at is passed explictedly" do
bmc1 = @host.interfaces.build(:name => "bmc1", :mac => '52:54:00:b0:0c:fc', :type => 'Nic::BMC',
:ip => '10.0.1.101', :username => 'user1111', :password => 'abc123456', :provider => 'IPMI')
assert bmc1.save
new_password = "topsecret"
put :update, { :commit => "Update", :id => @host.name, :host => {:interfaces_attributes => {"0" => {:id => bmc1.id, :password => new_password, :mac => "32:54:00:b0:0c:fd", :updated_at => Time.now, :id => bmc1.id} } } }, set_session_user
@host = Host.find(@host.id)
assert_equal new_password, @host.interfaces.bmc.first.password
end

# To test Rails bug still exists with accepts_nested_attributes_for and serialized child field
test "BMC password does not update due to Rails bug" do
bmc1 = @host.interfaces.build(:name => "bmc1", :mac => '52:54:00:b0:0c:fc', :type => 'Nic::BMC',
:ip => '10.0.1.101', :username => 'user1111', :password => 'abc123456', :provider => 'IPMI')
assert bmc1.save
new_password = "topsecret"
put :update, { :commit => "Update", :id => @host.name, :host => {:interfaces_attributes => {"0" => {:id => bmc1.id, :password => new_password} } } }, set_session_user
@host = Host.find(@host.id)
# assert bug that password was NOT updated to "topsecret" because :password (in serialized attrs field) was the only parameter that changed
refute_equal 'topsecret', @host.interfaces.bmc.first.password
assert_equal 'abc123456', @host.interfaces.bmc.first.password
end



private
def initialize_host

0 comments on commit eaa6aec

Please sign in to comment.
You can’t perform that action at this time.