2013-02-07 15:27:29 -05:00
|
|
|
require 'test_helper'
|
|
|
|
|
|
|
|
class ProtectedAttrsTest < ActiveSupport::TestCase
|
|
|
|
subject { ProtectedWidget.new }
|
|
|
|
|
2016-02-15 18:13:45 -05:00
|
|
|
# These ActiveModel matchers (provided by shoulda-matchers) only work for
|
|
|
|
# Rails 3.
|
|
|
|
if ActiveRecord::VERSION::MAJOR < 4
|
2013-07-31 20:37:04 -04:00
|
|
|
accessible_attrs = ProtectedWidget.accessible_attributes.to_a
|
|
|
|
accessible_attrs.each do |attr_name|
|
|
|
|
should allow_mass_assignment_of(attr_name.to_sym)
|
|
|
|
end
|
2016-02-15 18:13:45 -05:00
|
|
|
inaccessible = ProtectedWidget.
|
|
|
|
column_names.
|
|
|
|
reject { |column_name| accessible_attrs.include?(column_name) }
|
|
|
|
inaccessible.each do |attr_name|
|
2013-07-31 20:37:04 -04:00
|
|
|
should_not allow_mass_assignment_of(attr_name.to_sym)
|
|
|
|
end
|
2013-02-07 15:27:29 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'A model with `attr_accessible` created' do
|
|
|
|
setup do
|
|
|
|
@widget = ProtectedWidget.create! :name => 'Henry'
|
|
|
|
@initial_attributes = @widget.attributes
|
|
|
|
end
|
|
|
|
|
|
|
|
should 'be `nil` in its previous version' do
|
|
|
|
assert_nil @widget.previous_version
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'which is then updated' do
|
|
|
|
setup do
|
|
|
|
@widget.assign_attributes(:name => 'Jeff', :a_text => 'Short statement')
|
|
|
|
@widget.an_integer = 42
|
|
|
|
@widget.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
should 'not be `nil` in its previous version' do
|
|
|
|
assert_not_nil @widget.previous_version
|
|
|
|
end
|
|
|
|
|
|
|
|
should 'the previous version should contain right attributes' do
|
2015-12-13 01:14:59 -05:00
|
|
|
# For some reason this test seems to be broken in JRuby 1.9 mode in the
|
|
|
|
# test env even though it works in the console. WTF?
|
|
|
|
unless ActiveRecord::VERSION::MAJOR >= 4 && defined?(JRUBY_VERSION)
|
2015-12-21 08:00:57 -05:00
|
|
|
assert_attributes_equal @widget.previous_version.attributes, @initial_attributes
|
2013-08-01 14:59:58 -04:00
|
|
|
end
|
2013-02-07 15:27:29 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|