Skip to content

Instantly share code, notes, and snippets.

@coreyhaines
Forked from steveklabnik/privacy_filter.rb
Created December 12, 2011 04:44
Show Gist options
  • Save coreyhaines/1464967 to your computer and use it in GitHub Desktop.
Save coreyhaines/1464967 to your computer and use it in GitHub Desktop.
A spec for a filter
class PrivacyFilter
class << self
def filter(controller)
@controller = controller
first_article?(controller) or administrator?(controller) or user?(controller)
end
def first_article?(controller)
controller.params[:id] == 1
end
def administrator?(controller)
controller.authenticate_administrator!
end
def user?(controller)
controller.authenticate_user!
end
end
end
require "app/models/privacy_filter"
describe PrivacyFilter do
let(:controller) do
stub(:params => {:id => 2},
:authenticate_administrator! => false,
:authenticate_user! => true)
end
it "allows access to the root article" do
controller.stub(:params => {:id => 1})
PrivacyFilter.filter(controller).should be_true
end
it "allows access for administrators" do
controller.stub(:authenticate_administrator! => true)
PrivacyFilter.filter(controller).should be_true
end
it "allows access to users" do
controller.stub(:authenticate_user! => true)
PrivacyFilter.filter(controller).should be_true
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment