Sha256: bf537ffbc167eb127333e257709f021615b338b16085040e2e40e90660fc6373

Contents?: true

Size: 1.6 KB

Versions: 1

Compression:

Stored size: 1.6 KB

Contents

require 'active_support/core_ext/hash/slice'

shared_examples_for 'a sanitizer' do
  subject{ described_class.new }

  let(:logger){ FakeLogger.new }
  let(:params){ {:one => 1, :two => 2} }

  it 'has its own logger' do
    logger.should_receive(:debug).once

    subject.logger = logger
    subject.logger.should == logger

    subject.logger.debug
  end

  it 'removes denied keys from a hash' do
    subject.stub(:deny?).with(:one).and_return(false)
    subject.stub(:deny?).with(:two).and_return(true)
    subject.sanitize(params).should == params.slice(:one)
  end

  it "doesn't remove keys if none are denied" do
    subject.stub(:deny? => false)
    subject.sanitize(params).should == params
  end

  it 'debugs removed keys' do
    subject.logger = logger
    subject.logger.should_receive(:debug).once

    subject.stub(:deny? => true)
    subject.sanitize(params)
  end

  it "doesn't debug if there's no logger" do
    subject.logger.should be_nil

    allow_message_expectations_on_nil
    subject.logger.should_receive(:debug).never

    subject.stub(:deny? => true)
    subject.sanitize(params)
  end

  it "doesn't debug if no keys are removed" do
    subject.logger = logger
    subject.logger.should_receive(:debug).never

    subject.stub(:deny? => false)
    subject.sanitize(params)
  end

  it 'only debugs the removed keys' do
    subject.logger = logger

    subject.stub(:deny?).with(:one).and_return(false)
    subject.stub(:deny?).with(:two).and_return(true)
    subject.sanitize(params)

    message = subject.logger.output.pop
    message.should_not match(/\bone\b/)
    message.should match(/\btwo\b/)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
periscope-0.1.0 spec/support/sanitizer_examples.rb