Sha256: 3401f9ba0e7c8bb61cc0553559fb4fc24fb69174bf5dd75404b511fe57aa7f87

Contents?: true

Size: 1.88 KB

Versions: 1

Compression:

Stored size: 1.88 KB

Contents

require 'spec_helper'

begin
  require 'protected_attributes'
rescue LoadError
end

# Unfortunately, the only way to test both branches of this code is to
#   bundle --without protected_attributes
# and then run the spec again.
if defined?(ActiveModel::MassAssignmentSecurity)
  # Because mass-assignment security is based on ActiveModel we just have to
  # ensure that ActiveModel is called.
  describe DataMapper::MassAssignmentSecurity do
    before :all do
      class Fake
        super_module = Module.new do
          def _super_attributes=(*args)
          end

          def attributes=(*args)
            self.send(:_super_attributes=, *args)
          end
        end
        include super_module

        include ::DataMapper::MassAssignmentSecurity
      end
    end

    describe '#attributes=' do
      it 'calls super with sanitized attributes' do
        attributes = { :name => 'John', :is_admin => true }
        sanitized_attributes = { :name => 'John' }
        model = Fake.new
        model.should_receive(:sanitize_for_mass_assignment).with(attributes).and_return(sanitized_attributes)
        model.should_receive(:_super_attributes=).with(sanitized_attributes)

        model.attributes = attributes
      end

      it 'skips sanitation when called with true' do
        attributes = { :name => 'John', :is_admin => true }
        sanitized_attributes = { :name => 'John' }
        model = Fake.new
        model.should_receive(:_super_attributes=).with(attributes)

        model.send(:attributes=, attributes, true)
      end
    end
  end
else
  describe DataMapper::MassAssignmentSecurity do
    it "raises if the DataMapper::MassAssignmentSecurity is included" do
      expect {
        class Fake
          include ::DataMapper::MassAssignmentSecurity
        end
      }.to raise_error("Add 'protected_attributes' to your Gemfile to use DataMapper::MassAssignmentSecurity")
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ardm-rails-1.3.1 spec/unit/mass_assignment_security_spec.rb