require File.dirname(__FILE__) + '/spec_helper.rb' describe "Tweetsy::Client" do before do @base = Tweetsy::Client.new('foo', 'bar') end describe "authenticated?" do it "should return true on successful authentication (200)" do Curl::Easy.stub!(:perform).and_return(mock(:result, :response_code => 200)) @base.authenticated?.should be_true end it "should return false on unsuccessful authentication (401)" do Curl::Easy.stub!(:perform).and_return(mock(:result, :response_code => 401)) @base.authenticated?.should be_false end it "should raise CantConnect on anything that's not 200 or 401" do Curl::Easy.stub!(:perform).and_return(mock(:result, :response_code => 502)) lambda { @base.authenticated? }.should raise_error(Tweetsy::CantConnect) end end describe "being initialized" do it "should require email and password" do lambda { Tweetsy::Client.new }.should raise_error(ArgumentError) end end describe "timelines" do it "should be able to retrieve friends timeline" do data = open(File.dirname(__FILE__) + '/fixtures/friends_timeline.xml').read @base.should_receive(:request).and_return(Hpricot::XML(data)) @base.friends_timeline.size.should == 3 end end describe "social graphs" do it "should be able to retrieve friend_ids" do data = open(File.dirname(__FILE__) + '/fixtures/friend_ids.xml').read @base.should_receive(:request).and_return(Hpricot::XML(data)) @base.friend_ids.size.should == 3 end end describe "users" do it "should be able to get single user" do data = open(File.dirname(__FILE__) + '/fixtures/user.xml').read @base.should_receive(:request).and_return(Hpricot::XML(data)) @base.user('4243').name.should == 'John Nunemaker' end end describe "request" do it "should raise ArgumentError on bad arguments" do lambda { @base.request(:whoa, 'nada') }.should raise_error(ArgumentError) end it "should raise Unauthorized on 401" do response = mock(:response, :response_code => 401) Curl::Easy.stub!(:perform).and_return(response) lambda { @base.request(:get, 'blah') }.should raise_error(Tweetsy::Unauthorized) lambda { @base.request(:get, 'blah') }.should raise_error(Tweetsy::CantConnect) end it "should raise CantConnect on anything that isn't 200 or 304 response" do [401, 503, 504, 505, nil].each do |code| response = mock(:response, :response_code => code) Curl::Easy.stub!(:perform).and_return(response) lambda { @base.request(:get, 'blah') }.should raise_error(Tweetsy::CantConnect) end end end end