Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Michael Bleigh
committed
Mar 16, 2009
1 parent
152d76c
commit 2e4be42
Showing
8 changed files
with
125 additions
and
6 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
ActionController::Routing::Routes.draw do |map| | ||
map.resource :session | ||
map.oauth_callback '/oauth_callback', :controller => 'sessions', :action => 'oauth_callback' | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
# Gem Dependencies | ||
config.gem 'oauth' | ||
require File.dirname(__FILE__) + '/rails/init' |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Gem Dependencies | ||
config.gem 'oauth' | ||
|
||
require 'twitter_auth' |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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') |
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