Sha256: 7a3584406ea0c35b721bddb7616dcc5a3fc30f83dcc821d7e87a7ad23f06311f

Contents?: true

Size: 1.32 KB

Versions: 3

Compression:

Stored size: 1.32 KB

Contents

require 'spec_helper'

describe ActiveFedora::Attributes, ".new" do
  before(:all) do
    class ProtectedParams < ActiveSupport::HashWithIndifferentAccess
      attr_accessor :permitted
      alias_method :permitted?, :permitted

      def initialize(attributes)
        super(attributes)
        @permitted = false
      end

      def permit!
        @permitted = true
        self
      end

      def dup
        super.tap do |duplicate|
          duplicate.instance_variable_set :@permitted, @permitted
        end
      end
    end

    class Person < ActiveFedora::Base
      property :first_name, predicate: ::RDF::FOAF.firstName, multiple: false
      property :gender, predicate: ::RDF::FOAF.gender, multiple: false
    end
  end

  after(:all) do
    Object.send(:remove_const, :ProtectedParams)
    Object.send(:remove_const, :Person)
  end

  context "forbidden attributes" do
    let(:params) { ProtectedParams.new(first_name: 'Guille', gender: 'm') }
    it "cannot be used for mass assignment" do
      expect { Person.new(params) }.to raise_error ActiveModel::ForbiddenAttributesError
    end
  end

  context "permitted attributes" do
    let(:params) { ProtectedParams.new(first_name: 'Guille', gender: 'm').permit! }
    it "can be used for mass assignment" do
      expect { Person.new(params) }.not_to raise_error
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
active-fedora-9.7.0 spec/unit/forbidden_attributes_protection_spec.rb
active-fedora-9.6.2 spec/unit/forbidden_attributes_protection_spec.rb
active-fedora-9.6.1 spec/unit/forbidden_attributes_protection_spec.rb