Skip to content
Browse files

fake oauth

  • Loading branch information...
1 parent 4b10c1a commit 267f73163e3870e0527d2760b83e52d533a1b4d3 @jasonm jasonm committed
View
28 ...clearance_twitter_features/templates/features/step_definitions/clearance_twitter_steps.rb
@@ -43,3 +43,31 @@
# When /^I am signed into Twitter account "([^\"]*)"$/ do |arg1|
# pending # express the regexp above with the code you wish you had
# end
+
+Given /^Twitter OAuth is faked$/ do
+ FakeTwitter.stub_oauth
+ # [TwitterAuth.config['authorize_path'], '/oauth/request_token', '/oauth/authorize', '/oauth/access_token'].each do |path|
+ # FakeWeb.register_uri(:any, TwitterAuth.config['base_url'] + path, :body => '')
+ # end
+
+ # request_token = stub('request_token',
+ # :authorize_url => TwitterAuth.config['base_url'] + TwitterAuth.config['authorize_path'],
+ # :token => 'token',
+ # :secret => 'secret')
+ # consumer = stub('consumer', :get_request_token => request_token)
+ # TwitterAuth.stubs(:consumer).returns(consumer)
+end
+
+# Then /^I should be directed to Twitter OAuth$/ do
+# assert_redirected_to(TwitterAuth.config['base_url'] + TwitterAuth.config['authorize_path'] + '&oauth_callback=' + CGI.escape(TwitterAuth.config['oauth_callback']))
+# end
+#
+# When /^I return from Twitter OAuth with a valid OAuth verifier for "@([^\"]*)"$/ do |username|
+# user = stub('user', :id => '1', :remember_me => '2', :login => 'boys', :geocoded_location => FakeGeocoder.geocode() )
+# request_token = stub('request_token', :get_access_token => 'access_token')
+# User.stubs(:identify_or_create_from_access_token).returns(user)
+# CrushesController.any_instance.stubs(:current_user).returns(user)
+# OAuth::RequestToken.stubs(:new).returns(request_token)
+# visit oauth_callback_url(:oauth_token => 'token', :oauth_verifier => 'verifier')
+# end
+
View
1 generators/clearance_twitter_features/templates/features/twitter_sign_in.feature
@@ -5,6 +5,7 @@ Feature: Sign in with Twitter OAuth
Scenario: User signs in with Twitter
Given there are no users
+ And Twitter OAuth is faked
And a user exists with a twitter username of "jerkcity"
When I go to the sign in page
And I click the Sign in with Twitter button
View
1 generators/clearance_twitter_features/templates/features/twitter_sign_up.feature
@@ -6,6 +6,7 @@ Feature: Sign up with Twitter OAuth
@wip
Scenario: User successfully signs up with Twitter OAuth
Given there are no users
+ And Twitter OAuth is faked
And I go to the sign up page
And I click the Sign in with Twitter button
And I grant access to the Twitter application for Twitter user "jerkcity"
View
38 lib/clearance_twitter.rb
@@ -5,9 +5,13 @@ module ClearanceTwitter
def self.config(environment=RAILS_ENV)
{
'oauth_consumer_key' => 'key',
- 'oauth_consumer_secret' => 'secret'
+ 'oauth_consumer_secret' => 'secret',
+ 'base_url' => "https://twitter.com",
+ 'authorize_path' => "/oauth/authenticate",
+ 'oauth_callback' => "http://localhost:3000/oauth_callback"
}
+
# TODO
# @config ||= {}
# @config[environment] ||= YAML.load(File.open(RAILS_ROOT + '/config/twitter_auth.yml').read)[environment]
@@ -17,21 +21,25 @@ def self.base_url
config['base_url'] || 'https://twitter.com'
end
+ mattr_accessor :consumer
+
def self.consumer
- options = {:site => ClearanceTwitter.base_url}
- [ :authorize_path,
- :request_token_path,
- :access_token_path,
- :scheme,
- :signature_method ].each do |oauth_option|
- options[oauth_option] = ClearanceTwitter.config[oauth_option.to_s] if ClearanceTwitter.config[oauth_option.to_s]
- end
-
- OAuth::Consumer.new(
- config['oauth_consumer_key'],
- config['oauth_consumer_secret'],
- options
- )
+ @@consumer ||= begin
+ options = {:site => ClearanceTwitter.base_url}
+ [ :authorize_path,
+ :request_token_path,
+ :access_token_path,
+ :scheme,
+ :signature_method ].each do |oauth_option|
+ options[oauth_option] = ClearanceTwitter.config[oauth_option.to_s] if ClearanceTwitter.config[oauth_option.to_s]
+ end
+
+ OAuth::Consumer.new(
+ config['oauth_consumer_key'],
+ config['oauth_consumer_secret'],
+ options
+ )
+ end
end
# class Error < StandardError; end
View
42 lib/clearance_twitter/fake_twitter.rb
@@ -22,6 +22,10 @@ class FakeOAuthConsumer
def initialize(attributes)
attributes.each { |key, value| send(:"#{key}=", value) }
end
+
+ def get_request_token(*args)
+ @get_request_token
+ end
end
require 'webmock'
@@ -39,10 +43,10 @@ def stub_request(method, url, response_options)
def stub_oauth
FakeTwitter.oauth_paths_to_stub.each do |path|
- stub_request(:any, ClearanceFacebook.configuration['base_url'] + path, :body => '')
+ stub_request(:any, ClearanceTwitter.base_url + path, :body => '')
end
- ClearanceFacebook.oauth_consumer = fake_oauth_consumer
+ ClearanceTwitter.consumer = fake_oauth_consumer
end
def add_user(twitter_username)
@@ -57,8 +61,8 @@ def fake_oauth_consumer
end
def fake_request_token
- authorize_url = ClearanceFacebook.config['base_url'] +
- ClearanceFacebook.config['authorize_path']
+ authorize_url = ClearanceTwitter.base_url +
+ ClearanceTwitter.config['authorize_path']
FakeOAuthRequestToken.new({
:authorize_url => authorize_url,
@@ -68,39 +72,11 @@ def fake_request_token
end
def oauth_paths_to_stub
- [ClearanceFacebook.configuration['authorize_path'],
+ [ClearanceTwitter.config['authorize_path'],
'/oauth/request_token',
'/oauth/authorize',
'/oauth/access_token']
end
-
-
- end
-end
-
-Given /^Twitter OAuth is faked$/ do
- [TwitterAuth.config['authorize_path'], '/oauth/request_token', '/oauth/authorize', '/oauth/access_token'].each do |path|
- FakeWeb.register_uri(:any, TwitterAuth.config['base_url'] + path, :body => '')
end
-
- request_token = stub('request_token',
- :authorize_url => TwitterAuth.config['base_url'] + TwitterAuth.config['authorize_path'],
- :token => 'token',
- :secret => 'secret')
- consumer = stub('consumer', :get_request_token => request_token)
- TwitterAuth.stubs(:consumer).returns(consumer)
-end
-
-Then /^I should be directed to Twitter OAuth$/ do
- assert_redirected_to(TwitterAuth.config['base_url'] + TwitterAuth.config['authorize_path'] + '&oauth_callback=' + CGI.escape(TwitterAuth.config['oauth_callback']))
-end
-
-When /^I return from Twitter OAuth with a valid OAuth verifier for "@([^\"]*)"$/ do |username|
- user = stub('user', :id => '1', :remember_me => '2', :login => 'boys', :geocoded_location => FakeGeocoder.geocode() )
- request_token = stub('request_token', :get_access_token => 'access_token')
- User.stubs(:identify_or_create_from_access_token).returns(user)
- CrushesController.any_instance.stubs(:current_user).returns(user)
- OAuth::RequestToken.stubs(:new).returns(request_token)
- visit oauth_callback_url(:oauth_token => 'token', :oauth_verifier => 'verifier')
end

0 comments on commit 267f731

Please sign in to comment.
Something went wrong with that request. Please try again.