Created
July 12, 2010 13:05
-
-
Save kawaguchi/472440 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ControllerMacros | |
def current_user | |
@current_user ||= current_session && current_session.record | |
end | |
def current_session | |
@current_session ||= UserSession.find | |
end | |
def login_as(user) | |
@current_user = user | |
@current_session = mock_model(UserSession, { :user => @current_user }) | |
activate_authlogic | |
UserSession.stub!(:find).and_return(@current_session) | |
controller.stub!(:current_user).and_return @current_user | |
controller.stub!(:current_session).and_return @current_session | |
end | |
def logout | |
controller.stub!(:current_user).and_return nil | |
controller.stub!(:current_session).and_return nil | |
end | |
def self.included(receiver) | |
receiver.extend ClassMethods | |
end | |
module ClassMethods | |
def when_logged_in_as(name, stubs = {}, &block) | |
context "when logged in as #{name}" do | |
let (name) { mock(stubs) } | |
before { login_as __send__(name) } | |
block.bind(self).call | |
end | |
end | |
def when_logged_out(&block) | |
context "when logged out" do | |
before { logout } | |
block.bind(self).call | |
end | |
end | |
def it_should_require_login(method, action, params = {}) | |
before do | |
logout | |
__send__(method, action, params) | |
end | |
it "should require login" do | |
response.should redirect_to login_url | |
end | |
end | |
def it_should_require_logout(method, action, params = {}) | |
before do | |
login_as(mock) | |
__send__(method, action, params) | |
end | |
it "should require logout" do | |
response.should redirect_to root_url | |
end | |
end | |
def it_should_require_admin(method, action, params = {}) | |
it_should_require_login | |
it "should require admin" do | |
login_as(mock(:admin? => false)) | |
__send__(method, action, params) | |
response.status.should =~ /403/ | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment