Skip to content

Instantly share code, notes, and snippets.

@dennyabraham
Forked from steveklabnik/privacy_filter.rb
Created December 12, 2011 04:13
Show Gist options
  • Select an option

  • Save dennyabraham/1464804 to your computer and use it in GitHub Desktop.

Select an option

Save dennyabraham/1464804 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? or administrator? or user?
end
def first_article?
@controller.params[:id] == 1
end
def administrator?
@controller.authenticate_administrator!
end
def user?
@controller.authenticate_user!
end
end
end
require "app/models/privacy_filter"
describe PrivacyFilter do
before do
PrivacyFilter.stub!(:first_article? => false,
:administrator? => false,
:user? => false)
end
it "allows access to the root article" do
PrivacyFilter.unstub!(:first_article?)
PrivacyFilter.filter(stub(:params => {:id => 1})).should be_true
end
it "allows access for administrators" do
PrivacyFilter.unstub!(:administrator?)
PrivacyFilter.filter(stub(:authenticate_administrator! => true)).should be_true
end
it "allows access to users" do
PrivacyFilter.unstub!(:user?)
PrivacyFilter.filter(stub(:authenticate_user! => true)).should be_true
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment