require File.dirname(__FILE__) + '/test_helper.rb' require 'rubygems' require 'flexmock/test_unit' class UserTest < Test::Unit::TestCase def setup @session = Facebooker::Session.create('apikey', 'secretkey') @user = Facebooker::User.new(1234, @session) @other_user = Facebooker::User.new(4321, @session) ENV['FACEBOOK_CANVAS_PATH'] ='facebook_app_name' ENV['FACEBOOK_API_KEY'] = '1234567' ENV['FACEBOOK_SECRET_KEY'] = '7654321' @user.friends = [@other_user] end def test_can_ask_user_if_he_or_she_is_friends_with_another_user assert(@user.friends_with?(@other_user)) end def test_can_ask_user_if_he_or_she_is_friends_with_another_user_by_user_id assert(@user.friends_with?(@other_user.id)) end def test_cast_to_friend_list_id_with_nil assert_nil @user.cast_to_friend_list_id(nil) end def test_cast_to_friend_list_id_with_integer assert_equal 14,@user.cast_to_friend_list_id(14) end def test_cast_to_friend_list_id_with_string @user.expects(:friend_lists).returns([Facebooker::FriendList.new(:flid=>199,:name=>"Work")]) assert_equal 199,@user.cast_to_friend_list_id("Work") end def test_cast_to_friend_list_id_with_friend_list assert_equal 199,@user.cast_to_friend_list_id(Facebooker::FriendList.new(:flid=>199,:name=>"Work")) end def test_cast_to_friend_list_id_with_invalid_string_raises @user.expects(:friend_lists).returns([Facebooker::FriendList.new(:flid=>1,:name=>"Not Picked")]) assert_nil @user.cast_to_friend_list_id("Something") fail("No exception raised, Expected Facebooker::Session::InvalidFriendList") rescue Facebooker::Session::InvalidFriendList nil end def test_can_create_from_current_session Facebooker::Session.expects(:current).returns("current") user=Facebooker::User.new(1) assert_equal("current",user.session) end def test_can_set_mobile_fbml @user.expects(:set_profile_fbml).with(nil,"test",nil) @user.mobile_fbml="test" end def test_can_set_profile_action @user.expects(:set_profile_fbml).with(nil,nil,"test") @user.profile_action="test" end def test_can_set_profile_fbml @user.expects(:set_profile_fbml).with("test",nil,nil) @user.profile_fbml="test" end def test_can_set_profile_main @user.expects(:set_profile_fbml).with(nil,nil,nil,"test") @user.profile_main="test" end def test_can_call_set_profile_fbml @session.expects(:post).with('facebook.profile.setFBML', {:uid=>1234,:profile=>"profile",:profile_action=>"action",:mobile_profile=>"mobile"},false) @user.set_profile_fbml("profile","mobile","action") end def test_can_call_set_profile_fbml_with_profile_main @session.expects(:post).with('facebook.profile.setFBML', {:uid=>1234,:profile=>"profile",:profile_action=>"action",:mobile_profile=>"mobile", :profile_main => 'profile_main'},false) @user.set_profile_fbml("profile","mobile","action",'profile_main') end def test_can_get_profile_photos @user.expects(:profile_photos) @user.profile_photos end def test_can_set_cookie @user.expects(:set_cookie).with('name', 'value') @user.set_cookie('name', 'value') end def test_can_get_cookies @user.expects(:get_cookies).with('name') @user.get_cookies('name') end def test_get_profile_photos @user = Facebooker::User.new(548871286, @session) expect_http_posts_with_responses(example_profile_photos_get_xml) photos = @user.profile_photos assert_equal "2357384227378429949", photos.first.aid end def test_can_send_email @user.expects(:send_email).with("subject", "body text") @user.send_email("subject", "body text") @user.expects(:send_email).with("subject", nil, "body fbml") @user.send_email("subject", nil, "body fbml") end def test_can_set_status_with_string @session.expects(:post).with('facebook.users.setStatus', :status=>"my status",:status_includes_verb=>1) @user.status="my status" end def test_get_events @user = Facebooker::User.new(9507801, @session) expect_http_posts_with_responses(example_events_get_xml) events = @user.events assert_equal "29511517904", events.first.eid end def test_can_get_events @user.expects(:events) @user.events end def test_to_s assert_equal("1234",@user.to_s) end def test_equality assert_equal @user, @user.dup end def test_hash_email assert_equal "4228600737_c96da02bba97aedfd26136e980ae3761", Facebooker::User.hash_email("mary@example.com") end def test_hash_email_not_normalized assert_equal "4228600737_c96da02bba97aedfd26136e980ae3761", Facebooker::User.hash_email(" MaRy@example.com ") end def test_register_with_array expect_http_posts_with_responses(["4228600737_c96da02bba97aedfd26136e980ae3761"].to_json) assert_equal ["4228600737_c96da02bba97aedfd26136e980ae3761"],Facebooker::User.register([{:email=>"mary@example.com",:account_id=>1}]) end def test_failed_registration expect_http_posts_with_responses([""].to_json) Facebooker::User.register([{:email=>"mary@example.com",:account_id=>1}]) fail "Expected UserRegistrationFailed to be raised but it wasn't" rescue Facebooker::Session::UserRegistrationFailed=>e assert_equal({:email=>"mary@example.com",:account_id=>1},e.failed_users.first) end private def example_profile_photos_get_xml " 34585991612804 2357384227378429949 1240077 http://ip002.facebook.com/v11/135/18/8055/s1240077_30043524_2020.jpg http://ip002.facebook.com/v11/135/18/8055/n1240077_30043524_2020.jpg http://ip002.facebook.com/v11/135/18/8055/t1240077_30043524_2020.jpg http://www.facebook.com/photo.php?pid=30043524&id=8055 From The Deathmatch (Trailer) (1999) 1132553361 34585991612805 2357384227378429949 1240077 http://ip002.facebook.com/v11/135/18/8055/s1240077_30043525_2184.jpg http://ip002.facebook.com/v11/135/18/8055/n1240077_30043525_2184.jpg http://ip002.facebook.com/v11/135/18/8055/t1240077_30043525_2184.jpg http://www.facebook.com/photo.php?pid=30043525&id=8055 Mexico City, back cover of the CYHS Student Underground 1999. 1132553362 " end def example_events_get_xml " 29511517904 PUMA SALE 0 http://profile.ak.facebook.com/object3/370/66/s29511517904_6952.jpg http://profile.ak.facebook.com/object3/370/66/n29511517904_6952.jpg http://profile.ak.facebook.com/object3/370/66/t29511517904_6952.jpg PUMA PUMA SALE Education Study Group 1212166800 1212364800 1234261165 1209768148 PUMA LOT 5 LYBERTY WAY Westford Massachusetts United States 42.5792 -71.4383 " end end