Last active
December 19, 2015 05:59
-
-
Save cheald/5908093 to your computer and use it in GitHub Desktop.
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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment