Sha256: 5fc7be2f4277e2074235012f24e77427127065be9989a5a00db7608618016630

Contents?: true

Size: 1.32 KB

Versions: 5

Compression:

Stored size: 1.32 KB

Contents

require 'spec_helper'

describe InputSanitizer::RestrictedHash do
  let(:hash) { InputSanitizer::RestrictedHash.new([:a]) }

  it 'does not allow bad keys' do
    lambda{ hash[:b] }.should raise_error(InputSanitizer::KeyNotAllowedError)
  end

  it 'does allow correct keys' do
    hash[:a].should be_nil
  end

  it 'returns value for correct key' do
    hash[:a] = 'stuff'
    hash[:a].should == 'stuff'
  end

  it 'allows to set value for a new key' do
    hash[:b] = 'other stuff'
    hash[:b].should == 'other stuff'
    hash.key_allowed?(:b).should be_truthy
  end

  it 'adds new allowed keys after `transform_keys` is done' do
    hash[:a] = 'stuff'
    hash.transform_keys! { |key| key.to_s }
    hash['a'].should == 'stuff'
    hash.key_allowed?('a').should be_truthy
  end

  it 'removes previous allowed keys after `transform_keys` is done' do
    hash[:a] = 'stuff'
    hash.transform_keys! { |key| key.to_s }
    lambda{ hash[:a] }.should raise_error(InputSanitizer::KeyNotAllowedError)
  end

  it 'updates allowed keys on merge!' do
    merge_hash = { merged: '1' }
    hash.merge!(merge_hash)
    hash.key_allowed?(:merged).should be_truthy
  end

  it 'updates allowed keys on merge' do
    merge_hash = { merged: '1' }
    new_hash = hash.merge(some_key: merge_hash)
    new_hash.key_allowed?(:some_key).should be_truthy
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
input_sanitizer-0.6.0 spec/restricted_hash_spec.rb
input_sanitizer-0.5.0 spec/restricted_hash_spec.rb
input_sanitizer-0.4.1 spec/restricted_hash_spec.rb
input_sanitizer-0.4.0 spec/restricted_hash_spec.rb
input_sanitizer-0.3.33 spec/restricted_hash_spec.rb