Created
July 24, 2008 14:44
-
-
Save agile/2161 to your computer and use it in GitHub Desktop.
This file contains 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 AuthenticatedTestHelper | |
def login_as(role, opts={}) | |
user = users(role) if role.is_a?(Symbol) && self.respond_to(:users) | |
user ||= create_user(role.to_s, opts) | |
controller.send(:current_user=, user) | |
# so we'll have access to it from the specs.. | |
@current_user = user | |
end | |
def authorize_as(user) | |
request.env["HTTP_AUTHORIZATION"] = user ? "Basic #{Base64.encode64("#{users(user).login}:test")}" : nil | |
end | |
def create_user(user, opts={}) | |
options = { | |
:name => "#{user.to_s.titleize} Jones", | |
:email => "#{user.to_s.downcase.gsub(/\W/,'_')}@example.com", | |
:phone => '2145551111', | |
:role => mock_model(Role, :name => user, :to_s => user), | |
:dealer => nil, | |
:active? => true, | |
:password_required? => false | |
}.merge(opts) | |
mock_model(User, options) | |
end | |
end |
This file contains 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
describe SomeController do | |
before(:each) do | |
login as 'user' | |
end | |
... | |
end |
This file contains 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
Spec::Runner.configure do |config| | |
config.inlcude AuthenticatedTestHelper | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment