spec/cases/oauth_spec.rb in koala-1.5.0 vs spec/cases/oauth_spec.rb in koala-1.6.0.rc1

- old
+ new

@@ -1,9 +1,9 @@ require 'spec_helper' describe "Koala::Facebook::OAuth" do - before :each do + before :all do # make the relevant test data easily accessible @app_id = KoalaTest.app_id @secret = KoalaTest.secret @code = KoalaTest.code @callback_url = KoalaTest.oauth_test_data["callback_url"] @@ -22,11 +22,13 @@ # we can just test against the same key twice @multiple_session_keys = [KoalaTest.session_key, KoalaTest.session_key] if KoalaTest.session_key @oauth = Koala::Facebook::OAuth.new(@app_id, @secret, @callback_url) + end + before :each do @time = Time.now Time.stub!(:now).and_return(@time) @time.stub!(:to_i).and_return(1273363199) end @@ -115,23 +117,20 @@ @oauth.stub(:get_access_token_info).and_return(nil) @oauth.get_user_info_from_cookies(@cookie).should be_nil end it "returns nil if the call to FB returns an expired code error" do - @oauth.stub(:get_access_token_info).and_raise(Koala::Facebook::APIError.new( - "type" => "OAuthException", - "message" => "Code was invalid or expired. Session has expired at unix time 1324044000. The current unix time is 1324300957." + @oauth.stub(:get_access_token_info).and_raise(Koala::Facebook::OAuthTokenRequestError.new(400, + '{ "error": { "type": "OAuthException", "message": "Code was invalid or expired. Session has expired at unix time 1324044000. The current unix time is 1324300957." } }' )) @oauth.get_user_info_from_cookies(@cookie).should be_nil end it "raises the error if the call to FB returns a different error" do - @oauth.stub(:get_access_token_info).and_raise(Koala::Facebook::APIError.new( - "type" => "OtherError", - "message" => "A Facebook Error" - )) - expect { @oauth.get_user_info_from_cookies(@cookie) }.to raise_exception(Koala::Facebook::APIError) + @oauth.stub(:get_access_token_info).and_raise(Koala::Facebook::OAuthTokenRequestError.new(400, + '{ "error": { "type": "OtherError", "message": "A Facebook Error" } }')) + expect { @oauth.get_user_info_from_cookies(@cookie) }.to raise_exception(Koala::Facebook::OAuthTokenRequestError) end end it "doesn't parse invalid cookies" do # make an invalid string by replacing some values @@ -147,10 +146,11 @@ result.should be_a(Hash) end it "returns all the cookie components from valid cookie string" do cookie_data = KoalaTest.oauth_test_data["valid_cookies"] + puts cookie_data.inspect parsing_results = @oauth.get_user_info_from_cookies(cookie_data) number_of_components = cookie_data["fbs_#{@app_id.to_s}"].scan(/\=/).length parsing_results.length.should == number_of_components end @@ -293,17 +293,17 @@ @code ||= "test_code" end it "generates a properly formatted OAuth token URL when provided a code" do url = @oauth.url_for_access_token(@code) - url.should match_url("https://#{Koala::Facebook::GRAPH_SERVER}/oauth/access_token?client_id=#{@app_id}&code=#{@code}&client_secret=#{@secret}&redirect_uri=#{CGI.escape @callback_url}").should be_true + url.should match_url("https://#{Koala::Facebook::GRAPH_SERVER}/oauth/access_token?client_id=#{@app_id}&code=#{@code}&client_secret=#{@secret}&redirect_uri=#{CGI.escape @callback_url}") end it "generates a properly formatted OAuth token URL when provided a callback" do callback = "foo.com" url = @oauth.url_for_access_token(@code, :callback => callback) - url.should match_url("https://#{Koala::Facebook::GRAPH_SERVER}/oauth/access_token?client_id=#{@app_id}&code=#{@code}&client_secret=#{@secret}&redirect_uri=#{CGI.escape callback}").should be_true + url.should match_url("https://#{Koala::Facebook::GRAPH_SERVER}/oauth/access_token?client_id=#{@app_id}&code=#{@code}&client_secret=#{@secret}&redirect_uri=#{CGI.escape callback}") end it "includes any additional options as URL parameters, appropriately escaped" do params = { :url => "http://foo.bar?c=2", @@ -397,11 +397,11 @@ result = @oauth.get_access_token_info(@code) result["access_token"].should end it "raises an error when get_access_token is called with a bad code" do - lambda { @oauth.get_access_token_info("foo") }.should raise_error(Koala::Facebook::APIError) + lambda { @oauth.get_access_token_info("foo") }.should raise_error(Koala::Facebook::OAuthTokenRequestError) end end end describe "#get_access_token" do @@ -423,11 +423,11 @@ original = @oauth.get_access_token_info(@code) result.should == original["access_token"] end it "raises an error when get_access_token is called with a bad code" do - lambda { @oauth.get_access_token("foo") }.should raise_error(Koala::Facebook::APIError) + lambda { @oauth.get_access_token("foo") }.should raise_error(Koala::Facebook::OAuthTokenRequestError) end end end unless KoalaTest.code @@ -491,11 +491,11 @@ Koala.should_receive(:make_request).with(anything, anything, anything, hash_including(options)).and_return(Koala::HTTPService::Response.new(200, "", {})) @oauth.exchange_access_token_info(KoalaTest.oauth_token, options) end it "raises an error when exchange_access_token_info is called with a bad code" do - lambda { @oauth.exchange_access_token_info("foo") }.should raise_error(Koala::Facebook::APIError) + lambda { @oauth.exchange_access_token_info("foo") }.should raise_error(Koala::Facebook::OAuthTokenRequestError) end end describe "exchange_access_token" do it "uses get_access_token_info to get and parse an access token token results" do @@ -577,12 +577,12 @@ result = @oauth.get_token_info_from_session_keys(["foo"].concat(@multiple_session_keys)) # it should return nil for each of the invalid ones result.each_with_index {|r, index| index > 0 ? r.should(be_a(Hash)) : r.should(be_nil)} end - it "throws an APIError if Facebook returns an empty body (as happens for instance when the API breaks)" do + it "throws a BadFacebookResponse if Facebook returns an empty body (as happens for instance when the API breaks)" do @oauth.should_receive(:fetch_token_string).and_return("") - lambda { @oauth.get_token_info_from_session_keys(@multiple_session_keys) }.should raise_error(Koala::Facebook::APIError) + lambda { @oauth.get_token_info_from_session_keys(@multiple_session_keys) }.should raise_error(Koala::Facebook::BadFacebookResponse) end it "passes on any options provided to make_request" do options = {:a => 2} Koala.should_receive(:make_request).with(anything, anything, anything, hash_including(options)).and_return(Koala::HTTPService::Response.new(200, "[{}]", {}))