2 module OAuthControllerTestHelper
4 # Some custom stuff since we're using Mocha
5 def mock_model(model_class, options_and_stubs = {})
7 options_and_stubs.reverse_merge! :id => id,
10 :errors => stub("errors", :count => 0)
12 m = stub("#{model_class.name}_#{options_and_stubs[:id]}", options_and_stubs)
13 m.instance_eval <<-CODE
15 #{model_class}.ancestors.include?(other)
18 #{model_class}.ancestors.include?(other)
20 def instance_of?(other)
21 other == #{model_class}
27 yield m if block_given?
31 def mock_full_client_application
32 mock_model(ClientApplication,
34 :url => "http://app.com",
35 :callback_url => "http://app.com/callback",
36 :support_url => "http://app.com/support",
39 :oauth_server => OAuth::Server.new("http://kowabunga.com")
44 @controller.stubs(:local_request?).returns(true)
45 @user = mock_model(User, :login => "ron")
46 @controller.stubs(:current_user).returns(@user)
48 @tokens.stubs(:find).returns(@tokens)
49 @user.stubs(:tokens).returns(@tokens)
50 User.stubs(:find_by_id).returns(@user)
53 def login_as_application_owner
55 @client_application = mock_full_client_application
56 @client_applications = [@client_application]
58 @user.stubs(:client_applications).returns(@client_applications)
59 @client_applications.stubs(:find).returns(@client_application)
63 @controller.stubs(:local_request?).returns(true)
64 @user||=mock_model(User)
66 User.stubs(:find_by_id).returns(@user)
68 @server=OAuth::Server.new "http://test.host"
69 @consumer=OAuth::Consumer.new('key','secret',{:site=>"http://test.host"})
71 @client_application = mock_full_client_application
72 @controller.stubs(:current_client_application).returns(@client_application)
73 ClientApplication.stubs(:find_by_key).returns(@client_application)
74 @client_application.stubs(:key).returns(@consumer.key)
75 @client_application.stubs(:secret).returns(@consumer.secret)
76 @client_application.stubs(:name).returns("Client Application name")
77 @client_application.stubs(:callback_url).returns("http://application/callback")
78 @request_token=mock_model(RequestToken,:token=>'request_token',:client_application=>@client_application,:secret=>"request_secret",:user=>@user)
79 @request_token.stubs(:invalidated?).returns(false)
80 ClientApplication.stubs(:find_token).returns(@request_token)
82 @request_token_string="oauth_token=request_token&oauth_token_secret=request_secret"
83 @request_token.stubs(:to_query).returns(@request_token_string)
85 @access_token=mock_model(AccessToken,:token=>'access_token',:client_application=>@client_application,:secret=>"access_secret",:user=>@user)
86 @access_token.stubs(:invalidated?).returns(false)
87 @access_token.stubs(:authorized?).returns(true)
88 @access_token_string="oauth_token=access_token&oauth_token_secret=access_secret"
89 @access_token.stubs(:to_query).returns(@access_token_string)
91 @client_application.stubs(:authorize_request?).returns(true)
92 # @client_application.stubs(:sign_request_with_oauth_token).returns(@request_token)
93 @client_application.stubs(:exchange_for_access_token).returns(@access_token)
96 def setup_oauth_for_user
99 @tokens=[@request_token]
100 @tokens.stubs(:find).returns(@tokens)
101 @tokens.stubs(:find_by_token).returns(@request_token)
102 @user.stubs(:tokens).returns(@tokens)
105 def sign_request_with_oauth(token=nil)
106 ActionController::TestRequest.use_oauth=true
107 @request.configure_oauth(@consumer, token)
110 def setup_to_authorize_request
112 OauthToken.stubs(:find_by_token).with( @access_token.token).returns(@access_token)
113 @access_token.stubs(:is_a?).returns(true)