diff --git a/README b/README index 221de34..34943a2 100644 --- a/README +++ b/README @@ -8,11 +8,11 @@ Installation You can include TwitterAuth as a gem in your project like so: -config.gem 'mbleigh-twitter-auth', :source => 'http://gems.github.com' + config.gem 'mbleigh-twitter-auth', :source => 'http://gems.github.com' Or you can install it as a traditional Rails plugin: -script/plugin install git://github.com/mbleigh/twitter-auth.git + script/plugin install git://github.com/mbleigh/twitter-auth.git Note that because TwitterAuth utilizes Rails Engines functionality introduced in Rails 2.3, it will not work with earlier versions of Rails. @@ -21,8 +21,8 @@ Usage To utilize TwitterAuth in your application you will need to run the generator: -script/generate twitter_auth --strategy [oauth|basic] + script/generate twitter_auth --strategy [oauth|basic] This will generate a migration as well as set up the stubs needed to use the Rails Engines controllers and models set up by TwitterAuth. It will also create a User class that inherits from TwitterUser, abstracting away all of the Twitter authentication functionality and leaving you a blank slate to work with for your application. -Copyright (c) 2009 (Michael Bleigh)[http://www.mbleigh.com] and (Intridea, Inc.)[http://www.intridea.com/], released under the MIT license +Copyright (c) 2009 (Michael Bleigh)[http://www.mbleigh.com] and (Intridea, Inc.)[http://www.intridea.com/], released under the MIT License diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index b0435f5..6a36804 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -1,3 +1,23 @@ class SessionsController < ApplicationController + def new + if TwitterAuth.oauth? + @request_token = TwitterAuth.consumer.get_request_token + session[:request_token] = @request_token.token + session[:request_token_secret] = @request_token.secret + redirect_to @request_token.authorize_url + end + end + def oauth_callback + unless session[:request_token] && session[:request_token_secret] + flash[:error] = 'No authentication information was found in the session. Please try again.' + redirect_to '/' and return + end + + @request_token = OAuth::RequestToken.new(TwitterAuth.consumer, session[:request_token], session[:request_token_secret]) + + @access_token = @request_token.get_access_token + + render :text => @request_token.inspect + end end diff --git a/config/routes.rb b/config/routes.rb new file mode 100644 index 0000000..d2dc4be --- /dev/null +++ b/config/routes.rb @@ -0,0 +1,4 @@ +ActionController::Routing::Routes.draw do |map| + map.resource :session + map.oauth_callback '/oauth_callback', :controller => 'sessions', :action => 'oauth_callback' +end diff --git a/init.rb b/init.rb index 7892b14..470d490 100644 --- a/init.rb +++ b/init.rb @@ -1,2 +1 @@ -# Gem Dependencies -config.gem 'oauth' +require File.dirname(__FILE__) + '/rails/init' diff --git a/rails/init.rb b/rails/init.rb new file mode 100644 index 0000000..ba24820 --- /dev/null +++ b/rails/init.rb @@ -0,0 +1,4 @@ +# Gem Dependencies +config.gem 'oauth' + +require 'twitter_auth' diff --git a/spec/controllers/sessions_controller_spec.rb b/spec/controllers/sessions_controller_spec.rb index d5d25b2..0f0ae35 100644 --- a/spec/controllers/sessions_controller_spec.rb +++ b/spec/controllers/sessions_controller_spec.rb @@ -1,5 +1,74 @@ require File.dirname(__FILE__) + '/../spec_helper' describe SessionsController do + describe 'routes' do + it 'should route /session/new to SessionsController#new' do + params_from(:get, '/session/new').should == {:controller => 'sessions', :action => 'new'} + end + it 'should route /oauth_callback to SessionsController#oauth_callback' do + params_from(:get, '/oauth_callback').should == {:controller => 'sessions', :action => 'oauth_callback'} + end + end + + describe 'with OAuth strategy' do + before do + stub_oauth! + end + + describe '#new' do + it 'should retrieve a request token' do + get :new + assigns[:request_token].token.should == 'faketoken' + assigns[:request_token].secret.should == 'faketokensecret' + end + + it 'should set session variables for the request token' do + get :new + session[:request_token].should == 'faketoken' + session[:request_token_secret].should == 'faketokensecret' + end + + it 'should redirect to the oauth authorization url' do + get :new + response.should redirect_to('https://twitter.com/oauth/authorize?oauth_token=faketoken') + end + end + + describe '#oauth_callback' do + describe 'with no session info' do + it 'should set the flash[:error]' do + get :oauth_callback + flash[:error].should == 'No authentication information was found in the session. Please try again.' + end + + it 'should redirect to "/"' do + get :oauth_callback + response.should redirect_to('/') + end + end + + describe 'with proper info' do + before do + session.should_receive(:[]).any_number_of_times.with(:request_token).and_return('faketoken') + session.should_receive(:[]).any_number_of_times.with(:request_token_secret).and_return('faketokensecret') + get :oauth_callback, :oauth_token => 'faketoken' + end + + it 'should rebuild the request token' do + correct_token = OAuth::RequestToken.new(TwitterAuth.consumer,'faketoken','faketokensecret') + + %w(token secret).each do |att| + assigns[:request_token].send(att).should == correct_token.send(att) + end + end + + it 'should exchange the request token for an access token' do + assigns[:access_token].should be_a(OAuth::AccessToken) + assigns[:access_token].token.should == 'fakeaccesstoken' + assigns[:access_token].secret.should == 'fakeaccesstokensecret' + end + end + end + end end diff --git a/spec/fixtures/fakeweb.rb b/spec/fixtures/fakeweb.rb new file mode 100644 index 0000000..20b63a4 --- /dev/null +++ b/spec/fixtures/fakeweb.rb @@ -0,0 +1,14 @@ +# This is where we fake out all of the URLs that we +# will be calling as a part of this spec suite. +# You must have the 'fakeweb' gem in order to run +# the tests for TwitterAuth. +# +# gem install 'mbleigh-fakeweb' + +require 'fake_web' + +FakeWeb.allow_net_connect = false + +FakeWeb.register_uri(:post, 'https://twitter.com:443/oauth/request_token', :string => 'oauth_token=faketoken&oauth_token_secret=faketokensecret') + +FakeWeb.register_uri(:post, 'https://twitter.com:443/oauth/access_token', :string => 'oauth_token=fakeaccesstoken&oauth_token_secret=fakeaccesstokensecret') diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 3424846..9732692 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -5,6 +5,15 @@ exit end +require File.dirname(__FILE__) + '/fixtures/fakeweb' + plugin_spec_dir = File.dirname(__FILE__) ActiveRecord::Base.logger = Logger.new(plugin_spec_dir + "/debug.log") +def stub_oauth! + TwitterAuth.stub!(:config).and_return({ + 'strategy' => 'oauth', + 'oauth_consumer_key' => 'testkey', + 'oauth_consumer_secret' => 'testsecret' + }) +end