public
Last active

  • Download Gist
test_spec.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
RSpec::Matchers.define :redirect_to do |expected|
match do |actual|
actual.should be_redirect
actual.location.should include expected
end
end
 
describe "Twitter route" do
include TwitterOAuth
 
let(:app) { Sinatra::Application }
let(:request_token) { double("request_token", authorize_url: "http://api.twitter.com/oauth/authenticate?oauth_token") }
let(:access_token) { double("token", token: "token", secret: "secret") }
let(:user_manager) { UserManager.new }
 
context "/login/twitter" do
context "with an authorized token" do
before do
TwitterService.any_instance.stub(:authentication_request_token).and_return(request_token)
TwitterService.any_instance.stub(:authorize).with(anything(), anything()).and_return(access_token)
TwitterService.any_instance.stub(:verify_credentials).and_return({"id" => "id1"})
get '/login/twitter'
end
 
it "should redirect to twitter authorized url" do
last_response.should redirect_to "http://api.twitter.com/oauth/authenticate?oauth_token"
end
 
it "should set a the request token in the session" do
session[:request_token_twitter].authorize_url.should == "http://api.twitter.com/oauth/authenticate?oauth_token"
end
 
context "after a success callback" do
let(:user) { user_manager.find_by_id("id1") }
context "when there is not an existing user" do
before do
get '/login/twitter/success'
end
 
it "should redirect to /signup" do
last_response.should redirect_to "/signup"
end
 
it "should set an auth_token cookie" do
rack_mock_session.cookie_jar["auth_token"].should == "id1"
end
 
it "should set an s_flag cookie" do
rack_mock_session.cookie_jar["s_flag"].should == "t"
end
 
context "the authenticated user" do
subject { user }
its(:id) { should == "id1" }
its(:token) { should == "token" }
its(:secret) { should == "secret" }
end
end
 
context "when there is an existing user" do
let(:access_token) { double("token", token: "newtoken", secret: "newsecret") }
before do
User.new("id1", "oldtoken", "oldsecret", "t").save
get '/login/twitter/success'
end
 
it "should set an auth_token cookie" do
rack_mock_session.cookie_jar["auth_token"].should == "id1"
end
 
it "should set an s_flag cookie" do
rack_mock_session.cookie_jar["s_flag"].should == "t"
end
 
it "should redirect to /t" do
last_response.should redirect_to "/t"
end
 
context "the authenticated user" do
subject { user }
its(:id) { should == "id1" }
its(:token) { should == "newtoken" }
its(:secret) { should == "newsecret" }
end
end
end
end
 
context "with an invalid token" do
before do
TwitterService.any_instance.stub(:authentication_request_token).and_raise("Unauthorized")
get '/login/twitter'
end
 
it "should redirect back to home page if error occurs" do
last_response.should redirect_to "http://example.org/"
end
 
it "should not set a session value" do
session[:request_token_twitter].should be_nil
end
end
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.