Skip to content

Commit

Permalink
Adding things to 1.0_dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Bleigh committed Mar 16, 2009
1 parent 152d76c commit 2e4be42
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 6 deletions.
8 changes: 4 additions & 4 deletions README
Expand Up @@ -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.

Expand All @@ -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
20 changes: 20 additions & 0 deletions 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
4 changes: 4 additions & 0 deletions 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
3 changes: 1 addition & 2 deletions init.rb
@@ -1,2 +1 @@
# Gem Dependencies
config.gem 'oauth'
require File.dirname(__FILE__) + '/rails/init'
4 changes: 4 additions & 0 deletions rails/init.rb
@@ -0,0 +1,4 @@
# Gem Dependencies
config.gem 'oauth'

require 'twitter_auth'
69 changes: 69 additions & 0 deletions 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
14 changes: 14 additions & 0 deletions 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')
9 changes: 9 additions & 0 deletions spec/spec_helper.rb
Expand Up @@ -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

0 comments on commit 2e4be42

Please sign in to comment.