some
[appoyo.git] / vendor / plugins / facebooker / test / facebooker / models / user_test.rb
blobbc465669fb442b135fcdeeec440562a8975c8d47
1 require File.expand_path(File.dirname(__FILE__) + '/../../test_helper')
2 require 'active_support'
4 class Facebooker::UserTest < Test::Unit::TestCase
6   def setup
7     @session = Facebooker::Session.create('apikey', 'secretkey')
8     @user = Facebooker::User.new(1234, @session)
9     @other_user = Facebooker::User.new(4321, @session)
10     ENV['FACEBOOK_CANVAS_PATH'] ='facebook_app_name'
11     ENV['FACEBOOK_API_KEY'] = '1234567'
12     ENV['FACEBOOK_SECRET_KEY'] = '7654321'
14     @user.friends = [@other_user]
15   end
17   def test_has_permission
18     expect_http_posts_with_responses(has_app_permission_response_xml)
19     assert @user.has_permission?("status_update")    
20   end
22   def test_has_permissions
23     expect_http_posts_with_responses(has_app_permission_response_xml, has_app_permission_response_xml)    
24     assert @user.has_permissions?(["status_update", "read_stream"])    
25   end 
27   def test_can_ask_user_if_he_or_she_is_friends_with_another_user
28     assert(@user.friends_with?(@other_user))
29   end
31   def test_can_ask_user_if_he_or_she_is_friends_with_another_user_by_user_id
32     assert(@user.friends_with?(@other_user.id))
33   end
35   def test_does_not_query_facebook_for_uid
36     @session.expects(:post).never
37     assert_equal 1234, Facebooker::User.new(1234, @session).uid
38   end
40   def test_uid_is_always_an_integer
41     assert_equal 1234, Facebooker::User.new(:uid => "1234").uid
42     assert_equal 1234, Facebooker::User.new(:id  => "1234").uid
44     assert_equal 1234, Facebooker::User.new(:uid => "1234").id
45     assert_equal 1234, Facebooker::User.new(:id  => "1234").id
47     assert_equal 1234, Facebooker::User.new(:uid => "1234").facebook_id
48     assert_equal 1234, Facebooker::User.new(:id  => "1234").facebook_id
49   end
51   def test_cast_to_friend_list_id_with_nil
52     assert_nil @user.cast_to_friend_list_id(nil)
53   end
54   def test_cast_to_friend_list_id_with_integer
55     assert_equal 14,@user.cast_to_friend_list_id(14)
56   end
57   def test_cast_to_friend_list_id_with_string
58     @user.expects(:friend_lists).returns([Facebooker::FriendList.new(:flid=>199,:name=>"Work")])
59     assert_equal 199,@user.cast_to_friend_list_id("Work")
60   end
61   def test_cast_to_friend_list_id_with_friend_list
62     assert_equal 199,@user.cast_to_friend_list_id(Facebooker::FriendList.new(:flid=>199,:name=>"Work"))
63   end
65   def test_cast_to_friend_list_id_with_invalid_string_raises
66     @user.expects(:friend_lists).returns([Facebooker::FriendList.new(:flid=>1,:name=>"Not Picked")])
67     assert_nil @user.cast_to_friend_list_id("Something")
68     fail("No exception raised, Expected Facebooker::Session::InvalidFriendList")
69   rescue   Facebooker::Session::InvalidFriendList
70     nil
71   end
73   def test_can_create_from_current_session
74     Facebooker::Session.expects(:current).returns("current")
75     user=Facebooker::User.new(1)
76     assert_equal("current",user.session)
77   end
79   def test_raises_when_no_session_bound
80     assert_raises(Facebooker::Model::UnboundSessionException) { Facebooker::User.new(1, nil).populate }
81   end
83   def test_can_set_mobile_fbml
84     @user.expects(:set_profile_fbml).with(nil,"test",nil,nil)
85     @user.mobile_fbml="test"
86   end
87   def test_can_set_profile_action
88     @user.expects(:set_profile_fbml).with(nil,nil,"test",nil)
89     @user.profile_action="test"
90   end
91   def test_can_set_profile_fbml
92     @user.expects(:set_profile_fbml).with("test",nil,nil,nil)
93     @user.profile_fbml="test"
94   end
96   def test_can_set_profile_main
97     @user.expects(:set_profile_fbml).with(nil,nil,nil,"test")
98     @user.profile_main="test"
99   end
101   def test_can_call_set_profile_fbml
102     @session.expects(:post).with('facebook.profile.setFBML', {:uid=>1234,:profile=>"profile",:profile_action=>"action",:mobile_profile=>"mobile"},false)
103     @user.set_profile_fbml("profile","mobile","action")
104   end
106   def test_can_call_set_profile_fbml_with_profile_main
107     @session.expects(:post).with('facebook.profile.setFBML', {:uid=>1234,:profile=>"profile",:profile_action=>"action",:mobile_profile=>"mobile", :profile_main => 'profile_main'},false)
108     @user.set_profile_fbml("profile","mobile","action",'profile_main')
109   end
111   def test_can_get_profile_photos
112     @user.expects(:profile_photos)
113     @user.profile_photos
114   end
116   def test_can_set_cookie
117     @user.expects(:set_cookie).with('name', 'value')
118     @user.set_cookie('name', 'value')
119   end
121   def test_can_get_cookies
122     @user.expects(:get_cookies).with('name')
123     @user.get_cookies('name')
124   end
126   def test_get_profile_photos
127     @user = Facebooker::User.new(548871286, @session)
128     expect_http_posts_with_responses(example_profile_photos_get_xml)
129     photos = @user.profile_photos
130     assert_equal "2357384227378429949", photos.first.aid
131   end
133   def test_publish_to
134     @user = Facebooker::User.new(548871286, @session)
135     expect_http_posts_with_responses(example_profile_publish_to_get_xml)
136     @user.publish_to(@other_user, :message => 'i love you man')
137   end
139   def test_comment_on
140     @user = Facebooker::User.new(548871286, @session)
141     expect_http_posts_with_responses(example_comment_on_response)
142     assert_equal('703826862_78463536863', @user.comment_on('703826862_78463536862', :message => 'that was hilarious!'))
143   end
144   
145   def test_can_send_email
146     @user.expects(:send_email).with("subject", "body text")
147     @user.send_email("subject", "body text")
149     @user.expects(:send_email).with("subject", nil, "body fbml")
150     @user.send_email("subject", nil, "body fbml")
151   end
153   def test_doesnt_post_to_facebook_when_assigning_status
154     @session.expects(:post).never
155     @user.status="my status"
156   end
157   def test_can_set_status_with_string
158     @session.expects(:post).with('facebook.users.setStatus', {:status=>"my status",:status_includes_verb=>1, :uid => @user.uid}, false)
159     @user.set_status("my status")
160   end
162   def test_get_events
163     @user = Facebooker::User.new(9507801, @session)
164     expect_http_posts_with_responses(example_events_get_xml)
165     events = @user.events
166     assert_equal 29511517904, events.first.eid
167   end
169   def test_events_caching_honors_params
170     @user = Facebooker::User.new(9507801, @session)
171     @session.expects(:post).returns([{:eid=>1}])
172     assert_equal 1,@user.events.first.eid
173     @session.expects(:post).returns([{:eid=>2}])
174     assert_equal 2,@user.events(:start_time=>1.day.ago).first.eid
175     @session.expects(:post).never
176     assert_equal 1,@user.events.first.eid
177   end
180   def test_to_s
181     assert_equal("1234",@user.to_s)
182   end
184   def test_equality_with_same_id
185     assert_equal @user, @user.dup
186     assert_equal @user, Facebooker::User.new(:id => @user.id)
187   end
189   def test_not_equal_to_differnt_class
190     assert_not_equal @user, flexmock(:id => @user.id)
191   end
193   def test_hash_email
194     assert_equal "4228600737_c96da02bba97aedfd26136e980ae3761", Facebooker::User.hash_email("mary@example.com")
195   end
196   def test_hash_email_not_normalized
197     assert_equal "4228600737_c96da02bba97aedfd26136e980ae3761", Facebooker::User.hash_email(" MaRy@example.com  ")
198   end
200   def test_register_with_array
201     expect_http_posts_with_responses(register_response_xml)
202     assert_equal ["4228600737_c96da02bba97aedfd26136e980ae3761"],Facebooker::User.register([{:email=>"mary@example.com",:account_id=>1}])
203   end
205   def test_unregister_with_array
206     expect_http_posts_with_responses(unregister_response_xml)
207     assert_equal ["4228600737_c96da02bba97aedfd26136e980ae3761"],Facebooker::User.unregister(["4228600737_c96da02bba97aedfd26136e980ae3761"])
208   end
210   def test_unregister_emails_with_array
211     expect_http_posts_with_responses(unregister_response_xml)
212     assert_equal ["mary@example.com"],Facebooker::User.unregister_emails(["mary@example.com"])
213   end
215   def test_register_with_array_raises_if_not_all_success
216     expect_http_posts_with_responses(register_response_xml)
217     assert_equal ["4228600737_c96da02bba97aedfd26136e980ae3761"],Facebooker::User.register([{:email=>"mary@example.com",:account_id=>1},{:email=>"mike@example.com",:account_id=>2}])
218     fail "Should have raised Facebooker::Session::UserRegistrationFailed"
219   rescue Facebooker::Session::UserRegistrationFailed => e
220     assert_equal({:email=>"mike@example.com",:account_id=>2},e.failed_users.first)
221   end
223   def test_unregister_with_array_raises_if_not_all_success
224     expect_http_posts_with_responses(unregister_response_xml)
225     assert_equal ["4228600737_c96da02bba97aedfd26136e980ae3761"],Facebooker::User.unregister(["4228600737_c96da02bba97aedfd26136e980ae3761","3587916587_791214eb452bf4de30e957d65a0234d4"])
226     fail "Should have raised Facebooker::Session::UserUnRegistrationFailed"
227   rescue Facebooker::Session::UserUnRegistrationFailed => e
228     assert_equal("3587916587_791214eb452bf4de30e957d65a0234d4",e.failed_users.first)
229   end
231   def test_unregister_emails_with_array_raises_if_not_all_success
232     expect_http_posts_with_responses(unregister_response_xml)
233     assert_equal ["mary@example.com"],Facebooker::User.unregister_emails(["mary@example.com","mike@example.com"])
234     fail "Should have raised Facebooker::Session::UserUnRegistrationFailed"
235   rescue Facebooker::Session::UserUnRegistrationFailed => e
236     assert_equal("mike@example.com",e.failed_users.first)
237   end
240   def test_get_locale
241     @user = Facebooker::User.new(9507801, @session)
242     expect_http_posts_with_responses(example_users_get_info_xml)
243     assert_equal "en_US", @user.locale
244   end
246   def test_get_profile_url
247     @user = Facebooker::User.new(8055, @session)
248     expect_http_posts_with_responses(example_users_get_info_xml)
249     assert_equal "http://www.facebook.com/profile.php?id=8055", @user.profile_url
250   end
252   private
253   def example_profile_photos_get_xml
254     "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
255     <photos_get_response xmlns=\"http://api.facebook.com/1.0/\"
256       xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
257       xsi:schemaLocation=\"http://api.facebook.com/1.0/ http://api.facebook.com/1.0/facebook.xsd\" list=\"true\">
258        <photo>
259          <pid>34585991612804</pid>
260          <aid>2357384227378429949</aid>
261          <owner>1240077</owner>
262          <src>http://ip002.facebook.com/v11/135/18/8055/s1240077_30043524_2020.jpg</src>
263          <src_big>http://ip002.facebook.com/v11/135/18/8055/n1240077_30043524_2020.jpg</src_big>
264          <src_small>http://ip002.facebook.com/v11/135/18/8055/t1240077_30043524_2020.jpg</src_small>
265          <link>http://www.facebook.com/photo.php?pid=30043524&id=8055</link>
266          <caption>From The Deathmatch (Trailer) (1999)</caption>
267          <created>1132553361</created>
268        </photo>
269        <photo>
270          <pid>34585991612805</pid>
271          <aid>2357384227378429949</aid>
272          <owner>1240077</owner>
273          <src>http://ip002.facebook.com/v11/135/18/8055/s1240077_30043525_2184.jpg</src>
274          <src_big>http://ip002.facebook.com/v11/135/18/8055/n1240077_30043525_2184.jpg</src_big>
275          <src_small>http://ip002.facebook.com/v11/135/18/8055/t1240077_30043525_2184.jpg</src_small>
276          <link>http://www.facebook.com/photo.php?pid=30043525&id=8055</link>
277          <caption>Mexico City, back cover of the CYHS Student Underground 1999.</caption>
278          <created>1132553362</created>
279        </photo>
280     </photos_get_response>"
281   end
283   def example_events_get_xml
284     "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
285     <events_get_response xmlns=\"http://api.facebook.com/1.0/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://api.facebook.com/1.0/ http://api.facebook.com/1.0/facebook.xsd\" list=\"true\">
286       <event>
287         <eid>29511517904</eid>
288         <name>PUMA SALE</name>
289         <tagline/>
290         <nid>0</nid>
291         <pic>http://profile.ak.facebook.com/object3/370/66/s29511517904_6952.jpg</pic>
292         <pic_big>http://profile.ak.facebook.com/object3/370/66/n29511517904_6952.jpg</pic_big>
293         <pic_small>http://profile.ak.facebook.com/object3/370/66/t29511517904_6952.jpg</pic_small>
294         <host>PUMA</host>
295         <description>PUMA SALE</description>
296         <event_type>Education</event_type>
297         <event_subtype>Study Group</event_subtype>
298         <start_time>1212166800</start_time>
299         <end_time>1212364800</end_time>
300         <creator>1234261165</creator>
301         <update_time>1209768148</update_time>
302         <location>PUMA LOT</location>
303         <venue>
304           <street>5 LYBERTY WAY</street>
305           <city>Westford</city>
306           <state>Massachusetts</state>
307           <country>United States</country>
308           <latitude>42.5792</latitude>
309           <longitude>-71.4383</longitude>
310         </venue>
311       </event>
312     </events_get_response>"
313   end
315   def example_users_get_info_xml
316     <<-XML
317     <?xml version="1.0" encoding="UTF-8"?> <users_getInfo_response xmlns="http://api.facebook.com/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://api.facebook.com/1.0/ http://api.facebook.com/1.0/facebook.xsd" list="true"> <user> <uid>8055</uid> <about_me>This field perpetuates the glorification of the ego. Also, it has a character limit.</about_me> <activities>Here: facebook, etc. There: Glee Club, a capella, teaching.</activities> <affiliations list="true"> <affiliation> <nid>50453093</nid> <name>Facebook Developers</name> <type>work</type> <status/> <year/> </affiliation> </affiliations> <birthday>November 3</birthday> <books>The Brothers K, GEB, Ken Wilber, Zen and the Art, Fitzgerald, The Emporer's New Mind, The Wonderful Story of Henry Sugar</books> <current_location> <city>Palo Alto</city> <state>CA</state> <country>United States</country> <zip>94303</zip> </current_location> <education_history list="true"> <education_info> <name>Harvard</name> <year>2003</year> <concentrations list="true"> <concentration>Applied Mathematics</concentration> <concentration>Computer Science</concentration> </concentrations> </education_info> </education_history> <first_name>Dave</first_name> <hometown_location> <city>York</city> <state>PA</state> <country>United States</country> </hometown_location> <hs_info> <hs1_name>Central York High School</hs1_name> <hs2_name/> <grad_year>1999</grad_year> <hs1_id>21846</hs1_id> <hs2_id>0</hs2_id> </hs_info> <is_app_user>1</is_app_user> <has_added_app>1</has_added_app> <interests>coffee, computers, the funny, architecture, code breaking,snowboarding, philosophy, soccer, talking to strangers</interests> <last_name>Fetterman</last_name> <locale>en_US</locale> <meeting_for list="true"> <seeking>Friendship</seeking> </meeting_for> <meeting_sex list="true"> <sex>female</sex> </meeting_sex> <movies>Tommy Boy, Billy Madison, Fight Club, Dirty Work, Meet the Parents, My Blue Heaven, Office Space </movies> <music>New Found Glory, Daft Punk, Weezer, The Crystal Method, Rage, the KLF, Green Day, Live, Coldplay, Panic at the Disco, Family Force 5</music> <name>Dave Fetterman</name> <notes_count>0</notes_count> <pic>http://photos-055.facebook.com/ip007/profile3/1271/65/s8055_39735.jpg</pic> <pic_big>http://photos-055.facebook.com/ip007/profile3/1271/65/n8055_39735.jpg</pic_big> <pic_small>http://photos-055.facebook.com/ip007/profile3/1271/65/t8055_39735.jpg</pic_small> <pic_square>http://photos-055.facebook.com/ip007/profile3/1271/65/q8055_39735.jpg</pic_square> <political>Moderate</political> <profile_update_time>1170414620</profile_update_time> <profile_url>http://www.facebook.com/profile.php?id=8055</profile_url> <quotes/> <relationship_status>In a Relationship</relationship_status> <religion/> <sex>male</sex> <significant_other_id xsi:nil="true"/> <status> <message>Fast Company, November issue, page 84</message> <time>1193075616</time> </status> <timezone>-8</timezone> <tv>cf. Bob Trahan</tv> <wall_count>121</wall_count> <work_history list="true"> <work_info> <location> <city>Palo Alto</city> <state>CA</state> <country>United States</country> </location> <company_name>Facebook</company_name> <position>Software Engineer</position> <description>Tech Lead, Facebook Platform</description> <start_date>2006-01</start_date> <end_date/> </work_info> </work_history> </user> </users_getInfo_response>
318     XML
319   end
321   def register_response_xml
322     <<-XML
323     <?xml version="1.0" encoding="UTF-8"?>
324     <connect_registerUsers_response xmlns="http://api.facebook.com/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://api.facebook.com/1.0/facebook.xsd" list="true">
325       <connect_registerUsers_response_elt>4228600737_c96da02bba97aedfd26136e980ae3761</connect_registerUsers_response_elt>
326     </connect_registerUsers_response>
327     XML
328   end
330   def unregister_response_xml
331     <<-XML
332     <?xml version="1.0" encoding="UTF-8"?>
333     <connect_unregisterUsers_response xmlns="http://api.facebook.com/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://api.facebook.com/1.0/facebook.xsd" list="true">
334       <connect_unregisterUsers_response_elt>4228600737_c96da02bba97aedfd26136e980ae3761</connect_unregisterUsers_response_elt>
335     </connect_unregisterUsers_response>
336     XML
337   end
339   def has_app_permission_response_xml
340     <<-XML
341     <?xml version="1.0" encoding="UTF-8"?>
342     <users_hasAppPermission_response xmlns="http://api.facebook.com/1.0/"
343       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
344       xsi:schemaLocation="http://api.facebook.com/1.0/ http://api.facebook.com/1.0/facebook.xsd">1</users_hasAppPermission_response>
345     XML
346   end
348   def example_profile_publish_to_get_xml
349     <<-eoxml
350 <?xml version="1.0" encoding="UTF-8"?>
351 <stream_publish_response xmlns="http://api.facebook.com/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://api.facebook.com/1.0/ http://api.facebook.com/1.0/facebook.xsd">703826862_78463536862</stream_publish_response>
352     eoxml
353   end
354   
355   def example_comment_on_response
356     <<-eoxml
357 <?xml version="1.0" encoding="UTF-8"?>
358 <stream_addComment_response xmlns="http://api.facebook.com/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://api.facebook.com/1.0/ http://api.facebook.com/1.0/facebook.xsd">703826862_78463536863</stream_addComment_response>
359     eoxml
360   end