facebooker 0.9.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. data/CHANGELOG.txt +0 -0
  2. data/COPYING +19 -0
  3. data/History.txt +3 -0
  4. data/Manifest.txt +60 -0
  5. data/README +44 -0
  6. data/README.txt +79 -0
  7. data/Rakefile +38 -0
  8. data/TODO.txt +10 -0
  9. data/facebooker.yml.tpl +36 -0
  10. data/init.rb +52 -0
  11. data/install.rb +5 -0
  12. data/lib/facebooker.rb +63 -0
  13. data/lib/facebooker/affiliation.rb +10 -0
  14. data/lib/facebooker/album.rb +11 -0
  15. data/lib/facebooker/cookie.rb +10 -0
  16. data/lib/facebooker/data.rb +38 -0
  17. data/lib/facebooker/education_info.rb +11 -0
  18. data/lib/facebooker/event.rb +26 -0
  19. data/lib/facebooker/feed.rb +65 -0
  20. data/lib/facebooker/group.rb +35 -0
  21. data/lib/facebooker/location.rb +8 -0
  22. data/lib/facebooker/model.rb +118 -0
  23. data/lib/facebooker/notifications.rb +17 -0
  24. data/lib/facebooker/parser.rb +386 -0
  25. data/lib/facebooker/photo.rb +9 -0
  26. data/lib/facebooker/rails/controller.rb +174 -0
  27. data/lib/facebooker/rails/facebook_asset_path.rb +18 -0
  28. data/lib/facebooker/rails/facebook_form_builder.rb +112 -0
  29. data/lib/facebooker/rails/facebook_request_fix.rb +14 -0
  30. data/lib/facebooker/rails/facebook_session_handling.rb +58 -0
  31. data/lib/facebooker/rails/facebook_url_rewriting.rb +31 -0
  32. data/lib/facebooker/rails/helpers.rb +535 -0
  33. data/lib/facebooker/rails/routing.rb +49 -0
  34. data/lib/facebooker/rails/test_helpers.rb +11 -0
  35. data/lib/facebooker/rails/utilities.rb +22 -0
  36. data/lib/facebooker/server_cache.rb +24 -0
  37. data/lib/facebooker/service.rb +25 -0
  38. data/lib/facebooker/session.rb +385 -0
  39. data/lib/facebooker/tag.rb +12 -0
  40. data/lib/facebooker/user.rb +200 -0
  41. data/lib/facebooker/version.rb +9 -0
  42. data/lib/facebooker/work_info.rb +9 -0
  43. data/lib/net/http_multipart_post.rb +123 -0
  44. data/lib/tasks/facebooker.rake +16 -0
  45. data/lib/tasks/tunnel.rake +39 -0
  46. data/setup.rb +1585 -0
  47. data/test/event_test.rb +15 -0
  48. data/test/facebook_cache_test.rb +43 -0
  49. data/test/facebook_data_test.rb +50 -0
  50. data/test/facebooker_test.rb +766 -0
  51. data/test/fixtures/multipart_post_body_with_only_parameters.txt +33 -0
  52. data/test/fixtures/multipart_post_body_with_single_file.txt +38 -0
  53. data/test/fixtures/multipart_post_body_with_single_file_that_has_nil_key.txt +38 -0
  54. data/test/http_multipart_post_test.rb +54 -0
  55. data/test/model_test.rb +79 -0
  56. data/test/rails_integration_test.rb +732 -0
  57. data/test/session_test.rb +396 -0
  58. data/test/test_helper.rb +54 -0
  59. data/test/user_test.rb +101 -0
  60. metadata +130 -0
@@ -0,0 +1,396 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class SessionTest < Test::Unit::TestCase
4
+
5
+
6
+ def setup
7
+ ENV['FACEBOOK_API_KEY'] = '1234567'
8
+ ENV['FACEBOOK_SECRET_KEY'] = '7654321'
9
+ @session = Facebooker::Session.create('whatever', 'doesnotmatterintest')
10
+ end
11
+
12
+ def teardown
13
+ flexmock_close
14
+ end
15
+
16
+ def test_install_url_escapes_optional_next_parameter
17
+ session = Facebooker::CanvasSession.create(ENV['FACEBOOK_API_KEY'], ENV['FACEBOOK_SECRET_KEY'])
18
+ assert_equal("http://www.facebook.com/install.php?api_key=1234567&v=1.0&next=next_url%3Fa%3D1%26b%3D2", session.install_url(:next => "next_url?a=1&b=2"))
19
+ end
20
+
21
+ def test_can_get_api_and_secret_key_from_environment
22
+ assert_equal('1234567', Facebooker::Session.api_key)
23
+ assert_equal('7654321', Facebooker::Session.secret_key)
24
+ end
25
+
26
+ def test_if_keys_are_not_available_via_environment_then_they_are_gotten_from_a_file
27
+ ENV['FACEBOOK_API_KEY'], ENV['FACEBOOK_SECRET_KEY'] = nil
28
+ flexmock(File).should_receive(:read).with(File.expand_path("~/.facebookerrc")).once.and_return('{:api => "foo"}')
29
+ assert_equal('foo', Facebooker::Session.api_key)
30
+ end
31
+
32
+ def test_if_environment_and_file_fail_to_match_then_an_exception_is_raised
33
+ ENV['FACEBOOK_API_KEY'], ENV['FACEBOOK_SECRET_KEY'] = nil
34
+ flexmock(File).should_receive(:read).with(File.expand_path("~/.facebookerrc")).once.and_return {raise Errno::ENOENT, "No such file"}
35
+ assert_raises(Facebooker::Session::ConfigurationMissing) {
36
+ Facebooker::Session.api_key
37
+ }
38
+ end
39
+
40
+ def test_marshal_stores_api_key
41
+ data = Marshal.dump(@session)
42
+ loaded_session = Marshal.load(data)
43
+ assert_equal 'whatever', loaded_session.instance_variable_get("@api_key")
44
+ end
45
+
46
+ def test_marshal_stores_secret_key
47
+ data = Marshal.dump(@session)
48
+ loaded_session = Marshal.load(data)
49
+ assert_equal 'doesnotmatterintest', loaded_session.instance_variable_get("@secret_key")
50
+ end
51
+
52
+ def test_configuration_file_path_can_be_set_explicitly
53
+ Facebooker::Session.configuration_file_path = '/tmp/foo'
54
+ assert_equal('/tmp/foo', Facebooker::Session.configuration_file_path)
55
+ end
56
+
57
+ def test_session_can_be_secured_with_existing_values
58
+ session = Facebooker::Session.create(ENV['FACEBOOK_API_KEY'], ENV['FACEBOOK_SECRET_KEY'])
59
+ session.secure_with!("a session key", "123456", Time.now.to_i + 60)
60
+ assert(session.secured?)
61
+ end
62
+
63
+ # The Facebook API for this is hideous. Oh well.
64
+ def test_can_ask_session_to_check_friendship_between_pairs_of_users
65
+ @session = Facebooker::Session.create(ENV['FACEBOOK_API_KEY'], ENV['FACEBOOK_SECRET_KEY'])
66
+ mock_http = establish_session
67
+ mock_http.should_receive(:post_form).and_return(example_check_friendship_xml).once.ordered(:posts)
68
+ assert_equal({[222332, 222333] => true, [1240077, 1240079] => false}, @session.check_friendship([[222332, 222333], [1240077, 1240079]]))
69
+ end
70
+
71
+ def test_facebook_can_claim_ignorance_as_to_friend_relationships
72
+ mock_http = establish_session
73
+ mock_http.should_receive(:post_form).and_return(example_check_friendship_with_unknown_result).once.ordered(:posts)
74
+ assert_equal({[1240077, 1240079] => nil}, @session.check_friendship([[1240077, 1240079]]))
75
+ end
76
+
77
+ def test_can_query_with_fql
78
+ @session = Facebooker::Session.create(ENV['FACEBOOK_API_KEY'], ENV['FACEBOOK_SECRET_KEY'])
79
+ expect_http_posts_with_responses(example_fql_for_multiple_photos_xml)
80
+ response = @session.fql_query('Lets be frank. We are not testing the query here')
81
+ assert_kind_of(Facebooker::Photo, response.first)
82
+ end
83
+
84
+ def test_anonymous_fql_results_get_put_in_a_positioned_array_on_the_model
85
+ @session = Facebooker::Session.create(ENV['FACEBOOK_API_KEY'], ENV['FACEBOOK_SECRET_KEY'])
86
+ expect_http_posts_with_responses(example_fql_for_multiple_photos_with_anon_xml)
87
+ response = @session.fql_query('Lets be frank. We are not testing the query here')
88
+ assert_kind_of(Facebooker::Photo, response.first)
89
+ response.each do |photo|
90
+ assert_equal(['first', 'second'], photo.anonymous_fields)
91
+ end
92
+ end
93
+
94
+ def test_can_fql_query_for_event_members
95
+ @session = Facebooker::Session.create(ENV['FACEBOOK_API_KEY'], ENV['FACEBOOK_SECRET_KEY'])
96
+ expect_http_posts_with_responses(example_fql_query_event_members_xml)
97
+ response = @session.fql_query("DOES NOT REALLY MATTER FOR TEST")
98
+ assert_kind_of(Facebooker::Event::Attendance, response.first)
99
+ assert_equal('attending', response.first.rsvp_status)
100
+ end
101
+
102
+ def test_can_query_for_event_members
103
+ expect_http_posts_with_responses(example_event_members_xml)
104
+ event_attendances = @session.event_members(69)
105
+ assert_equal Facebooker::Event::Attendance, event_attendances.first.class
106
+ assert_equal 'attending', event_attendances.first.rsvp_status
107
+ assert_equal(["1240077", "222332", "222333", "222335", "222336"], event_attendances.map{|ea| ea.uid}.sort)
108
+ assert_equal 5, event_attendances.size
109
+ end
110
+
111
+ def test_can_query_for_events
112
+ expect_http_posts_with_responses(example_events_get_xml)
113
+ events = @session.events
114
+ assert_equal 'Technology Tasting', events.first.name
115
+ end
116
+
117
+ def test_can_query_for_groups
118
+ expect_http_posts_with_responses(example_groups_get_xml)
119
+ groups = @session.user.groups
120
+ assert_equal 'Donald Knuth Is My Homeboy', groups.first.name
121
+ end
122
+
123
+ def test_can_query_for_group_memberships
124
+ expect_http_posts_with_responses(example_group_members_xml)
125
+ example_group = Facebooker::Group.new({:gid => 123, :session => @session})
126
+ group_memberships = example_group.memberships
127
+ assert_equal('officers', group_memberships.last.position)
128
+ assert_equal(123, group_memberships.last.gid)
129
+ assert_equal(1240078, example_group.members.last.id)
130
+ end
131
+
132
+ def test_can_fql_query_for_users_and_pictures
133
+ @session = Facebooker::Session.create(ENV['FACEBOOK_API_KEY'], ENV['FACEBOOK_SECRET_KEY'])
134
+ mock_http = establish_session
135
+ mock_http.should_receive(:post_form).and_return(example_fql_for_multiple_users_and_pics).once.ordered(:posts)
136
+ response = @session.fql_query('SELECT name, pic FROM user WHERE uid=211031 OR uid=4801660')
137
+ assert_kind_of Array, response
138
+ assert_kind_of Facebooker::User, response.first
139
+ assert_equal "Ari Steinberg", response.first.name
140
+ end
141
+
142
+
143
+ def test_can_send_notification_with_object
144
+ @session = Facebooker::Session.create(ENV['FACEBOOK_API_KEY'], ENV['FACEBOOK_SECRET_KEY'])
145
+ @session.expects(:post).with('facebook.notifications.send',{:to_ids=>"1",:notification=>"a"})
146
+ user=flexmock("user")
147
+ user.should_receive(:facebook_id).and_return("1").once
148
+ @session.send_notification([user],"a")
149
+ end
150
+ def test_can_send_notification_with_string
151
+ @session = Facebooker::Session.create(ENV['FACEBOOK_API_KEY'], ENV['FACEBOOK_SECRET_KEY'])
152
+ @session.expects(:post).with('facebook.notifications.send',{:to_ids=>"1",:notification=>"a"})
153
+ @session.send_notification(["1"],"a")
154
+ end
155
+ def teardown
156
+ Facebooker::Session.configuration_file_path = nil
157
+ end
158
+
159
+ private
160
+
161
+ def example_groups_get_xml
162
+ <<-XML
163
+ <?xml version="1.0" encoding="UTF-8"?>
164
+ <groups_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">
165
+ <group>
166
+ <gid>2206609142</gid>
167
+ <name>Donald Knuth Is My Homeboy</name>
168
+ <nid>0</nid>
169
+ <description>Donald Ervin Knuth (born January 10, 1938) is a renowned computer scientist and professor emeritus at Stanford University.
170
+
171
+ Knuth is best known as the author of the multi-volume The Art of Computer Programming, one of the most highly respected references in the computer science field. He practically created the field of rigorous analysis of algorithms, and made many seminal contributions to several branches of theoretical computer science. He is also the creator of the TeX typesetting system and of the METAFONT font design system, and pioneered the concept of literate programming.
172
+
173
+ That's how he ROLLS, y0.</description>
174
+ <group_type>Just for Fun</group_type>
175
+ <group_subtype>Fan Clubs</group_subtype>
176
+ <recent_news/>
177
+ <pic>http://photos-142.facebook.com/ip006/object/543/95/s2206609142_32530.jpg</pic>
178
+ <pic_big>http://photos-142.facebook.com/ip006/object/543/95/n2206609142_32530.jpg</pic_big>
179
+ <pic_small>http://photos-142.facebook.com/ip006/object/543/95/t2206609142_32530.jpg</pic_small>
180
+ <creator>1240077</creator>
181
+ <update_time>1156543965</update_time>
182
+ <office/>
183
+ <website/>
184
+ <venue>
185
+ <street/>
186
+ <city/>
187
+ <state>CA</state>
188
+ <country>United States</country>
189
+ </venue>
190
+ </group>
191
+ </groups_get_response>
192
+ XML
193
+ end
194
+
195
+ def example_events_get_xml
196
+ <<-XML
197
+ <?xml version="1.0" encoding="UTF-8"?>
198
+ <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">
199
+ <event>
200
+ <eid>1037629024</eid>
201
+ <name>Technology Tasting</name>
202
+ <tagline>Who said Engineering can't be delicious?</tagline>
203
+ <nid>12409987</nid>
204
+ <pic>http://photos-628.facebook.com/ip006/object/1345/48/s1037629024_30775.jpg</pic>
205
+ <pic_big>http://photos-628.facebook.com/ip006/object/1345/48/n1037629024_30775.jpg</pic_big>
206
+ <pic_small>http://photos-628.facebook.com/ip006/object/1345/48/t1037629024_30775.jpg</pic_small>
207
+ <host>Facebook</host>
208
+ <description>Facebook will be hosting technology thought leaders and avid software engineers for a social evening of technology tasting. We invite you to connect with some of our newest technologies and innovative people over hors d'oeuvres and wine. Come share ideas, ask questions, and challenge existing technology paradigms in the spirit of the open source community.</description>
209
+ <event_type>Party</event_type>
210
+ <event_subtype>Cocktail Party</event_subtype>
211
+ <start_time>1172107800</start_time>
212
+ <end_time>1172115000</end_time>
213
+ <creator>1078</creator>
214
+ <update_time>1170096157</update_time>
215
+ <location>Facebook's New Office</location>
216
+ <venue>
217
+ <city>Palo Alto</city>
218
+ <state>CA</state>
219
+ <country>United States</country>
220
+ </venue>
221
+ </event>
222
+ </events_get_response>
223
+ XML
224
+ end
225
+
226
+ def example_fql_query_event_members_xml
227
+ <<-XML
228
+ <?xml version="1.0" encoding="UTF-8"?>
229
+ <fql_query_response xmlns="http://api.facebook.com/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" list="true">
230
+ <event_member>
231
+ <uid>517961878</uid>
232
+ <eid>2454827764</eid>
233
+ <rsvp_status>attending</rsvp_status>
234
+ </event_member>
235
+ <event_member>
236
+ <uid>744961110</uid>
237
+ <eid>2454827764</eid>
238
+ <rsvp_status>declined</rsvp_status>
239
+ </event_member>
240
+ </fql_query_response>
241
+ XML
242
+ end
243
+ def example_check_friendship_xml
244
+ <<-XML
245
+ <?xml version="1.0" encoding="UTF-8"?>
246
+ <friends_areFriends_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">
247
+ <friend_info>
248
+ <uid1>222332</uid1>
249
+ <uid2>222333</uid2>
250
+ <are_friends>1</are_friends>
251
+ </friend_info>
252
+ <friend_info>
253
+ <uid1>1240077</uid1>
254
+ <uid2>1240079</uid2>
255
+ <are_friends>0</are_friends>
256
+ </friend_info>
257
+ </friends_areFriends_response>
258
+ XML
259
+ end
260
+
261
+ def example_check_friendship_with_unknown_result
262
+ <<-XML
263
+ <?xml version="1.0" encoding="UTF-8"?>
264
+ <friends_areFriends_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">
265
+ <friend_info>
266
+ <uid1>1240077</uid1>
267
+ <uid2>1240079</uid2>
268
+ <are_friends xsi:nil="true"/>
269
+ </friend_info>
270
+ </friends_areFriends_response>
271
+ XML
272
+ end
273
+
274
+ def example_fql_for_multiple_users_and_pics
275
+ <<-XML
276
+ <?xml version="1.0" encoding="UTF-8"?>
277
+ <fql_query_response xmlns="http://api.facebook.com/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" list="true">
278
+ <user>
279
+ <name>Ari Steinberg</name>
280
+ <pic>http://profile.ak.facebook.com/profile2/1805/47/s211031_26434.jpg</pic>
281
+ </user>
282
+ <user>
283
+ <name>Ruchi Sanghvi</name>
284
+ <pic>http://profile.ak.facebook.com/v52/870/125/s4801660_2498.jpg</pic>
285
+ </user>
286
+ </fql_query_response>
287
+ XML
288
+ end
289
+
290
+ def example_fql_for_multiple_photos_xml
291
+ <<-XML
292
+ <?xml version="1.0" encoding="UTF-8"?>
293
+ <fql_query_response xmlns="http://api.facebook.com/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" list="true">
294
+ <photo>
295
+ <src>http://photos-c.ak.facebook.com/photos-ak-sf2p/v108/212/118/22700225/s22700225_30345986_2713.jpg</src>
296
+ <caption>Nottttt. get ready for some museumz</caption>
297
+ <caption>Nottttt. get ready for some museumz</caption>
298
+ </photo>
299
+ <photo>
300
+ <src>http://photos-c.ak.facebook.com/photos-ak-sf2p/v77/74/112/22701786/s22701786_30324934_7816.jpg</src>
301
+ <caption>Rooftop barbecues make me act funny</caption>
302
+ <caption>Rooftop barbecues make me act funny</caption>
303
+ </photo>
304
+ <photo>
305
+ <src>http://photos-c.ak.facebook.com/photos-ak-sctm/v96/154/56/22700188/s22700188_30321538_17.jpg</src>
306
+ <caption>An epic shot of Patrick getting ready for a run to second.</caption>
307
+ <caption>An epic shot of Patrick getting ready for a run to second.</caption>
308
+ </photo>
309
+ </fql_query_response>
310
+ XML
311
+ end
312
+
313
+ def example_fql_for_multiple_photos_with_anon_xml
314
+ <<-XML
315
+ <?xml version="1.0" encoding="UTF-8"?>
316
+ <fql_query_response xmlns="http://api.facebook.com/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" list="true">
317
+ <photo>
318
+ <src>http://photos-c.ak.facebook.com/photos-ak-sf2p/v108/212/118/22700225/s22700225_30345986_2713.jpg</src>
319
+ <caption>Nottttt. get ready for some museumz</caption>
320
+ <caption>Nottttt. get ready for some museumz</caption>
321
+ <anon>first</anon>
322
+ <anon>second</anon>
323
+ </photo>
324
+ <photo>
325
+ <src>http://photos-c.ak.facebook.com/photos-ak-sf2p/v77/74/112/22701786/s22701786_30324934_7816.jpg</src>
326
+ <caption>Rooftop barbecues make me act funny</caption>
327
+ <caption>Rooftop barbecues make me act funny</caption>
328
+ <anon>first</anon>
329
+ <anon>second</anon>
330
+ </photo>
331
+ <photo>
332
+ <src>http://photos-c.ak.facebook.com/photos-ak-sctm/v96/154/56/22700188/s22700188_30321538_17.jpg</src>
333
+ <caption>An epic shot of Patrick getting ready for a run to second.</caption>
334
+ <caption>An epic shot of Patrick getting ready for a run to second.</caption>
335
+ <anon>first</anon>
336
+ <anon>second</anon>
337
+ </photo>
338
+ </fql_query_response>
339
+ XML
340
+ end
341
+
342
+ def example_group_members_xml
343
+ <<-XML
344
+ <?xml version="1.0" encoding="UTF-8"?>
345
+ <groups_getMembers_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">
346
+ <members list="true">
347
+ <uid>1240077</uid>
348
+ <uid>1240078</uid>
349
+ <uid>222332</uid>
350
+ <uid>222333</uid>
351
+ </members>
352
+ <admins list="true">
353
+ <uid>1240077</uid>
354
+ <uid>222333</uid>
355
+ </admins>
356
+ <officers list="true">
357
+ <uid>1240078</uid>
358
+ </officers>
359
+ <not_replied list="true"/>
360
+ </groups_getMembers_response>
361
+ XML
362
+ end
363
+
364
+ def example_event_members_xml
365
+ <<-XML
366
+ <?xml version="1.0" encoding="UTF-8"?>
367
+ <events_getMembers_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">
368
+ <attending list="true">
369
+ <uid>222332</uid>
370
+ <uid>222333</uid>
371
+ </attending>
372
+ <unsure list="true">
373
+ <uid>1240077</uid>
374
+ </unsure>
375
+ <declined list="true"/>
376
+ <not_replied list="true">
377
+ <uid>222335</uid>
378
+ <uid>222336</uid>
379
+ </not_replied>
380
+ </events_getMembers_response>
381
+ XML
382
+ end
383
+
384
+ end
385
+
386
+ class CanvasSessionTest < Test::Unit::TestCase
387
+ def setup
388
+ ENV['FACEBOOK_API_KEY'] = '1234567'
389
+ ENV['FACEBOOK_SECRET_KEY'] = '7654321'
390
+ end
391
+
392
+ def test_login_url_will_display_callback_url_in_canvas
393
+ session = Facebooker::CanvasSession.create(ENV['FACEBOOK_API_KEY'], ENV['FACEBOOK_SECRET_KEY'])
394
+ assert_equal("http://www.facebook.com/login.php?api_key=1234567&v=1.0&canvas=true", session.login_url)
395
+ end
396
+ end
@@ -0,0 +1,54 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ require 'flexmock/test_unit'
4
+ require 'pp'
5
+
6
+ require File.dirname(__FILE__)+'/../lib/facebooker/rails/test_helpers'
7
+
8
+ $: << File.join(File.dirname(__FILE__), '..', 'lib')
9
+
10
+ RAILS_ROOT=File.join(File.dirname(__FILE__),'..','..')
11
+ require 'facebooker'
12
+
13
+ class Test::Unit::TestCase
14
+ include Facebooker::Rails::TestHelpers
15
+
16
+ private
17
+
18
+ def expect_http_posts_with_responses(*responses_xml)
19
+ mock_http = establish_session
20
+ responses_xml.each do |xml_string|
21
+ mock_http.should_receive(:post_form).and_return(xml_string).once.ordered(:posts)
22
+ end
23
+ end
24
+
25
+ def establish_session(session = @session)
26
+ mock = flexmock(Net::HTTP).should_receive(:post_form).and_return(example_auth_token_xml).once.ordered(:posts)
27
+ mock.should_receive(:post_form).and_return(example_get_session_xml).once.ordered(:posts)
28
+ session.secure!
29
+ mock
30
+ end
31
+
32
+ def example_auth_token_xml
33
+ <<-XML
34
+ <?xml version="1.0" encoding="UTF-8"?>
35
+ <auth_createToken_response xmlns="http://api.facebook.com/1.0/"
36
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
37
+ xsi:schemaLocation="http://api.facebook.com/1.0/ http://api.facebook.com/1.0/facebook.xsd">
38
+ 3e4a22bb2f5ed75114b0fc9995ea85f1
39
+ </auth_createToken_response>
40
+ XML
41
+ end
42
+
43
+ def example_get_session_xml
44
+ <<-XML
45
+ <?xml version="1.0" encoding="UTF-8"?>
46
+ <auth_getSession_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">
47
+ <session_key>5f34e11bfb97c762e439e6a5-8055</session_key>
48
+ <uid>8055</uid>
49
+ <expires>1173309298</expires>
50
+ <secret>ohairoflamao12345</secret>
51
+ </auth_getSession_response>
52
+ XML
53
+ end
54
+ end
@@ -0,0 +1,101 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+ require 'rubygems'
3
+ require 'flexmock/test_unit'
4
+
5
+ class UserTest < Test::Unit::TestCase
6
+
7
+ def setup
8
+ @session = Facebooker::Session.create('apikey', 'secretkey')
9
+ @user = Facebooker::User.new(1234, @session)
10
+ @other_user = Facebooker::User.new(4321, @session)
11
+ @user.friends = [@other_user]
12
+ end
13
+
14
+ def test_can_ask_user_if_he_or_she_is_friends_with_another_user
15
+ assert(@user.friends_with?(@other_user))
16
+ end
17
+
18
+ def test_can_ask_user_if_he_or_she_is_friends_with_another_user_by_user_id
19
+ assert(@user.friends_with?(@other_user.id))
20
+ end
21
+
22
+ def test_can_create_from_current_session
23
+ Facebooker::Session.expects(:current).returns("current")
24
+ user=Facebooker::User.new(1)
25
+ assert_equal("current",user.session)
26
+ end
27
+
28
+ def test_can_set_mobile_fbml
29
+ @user.expects(:set_profile_fbml).with(nil,"test",nil)
30
+ @user.mobile_fbml="test"
31
+ end
32
+ def test_can_set_profile_action
33
+ @user.expects(:set_profile_fbml).with(nil,nil,"test")
34
+ @user.profile_action="test"
35
+ end
36
+ def test_can_set_profile_fbml
37
+ @user.expects(:set_profile_fbml).with("test",nil,nil)
38
+ @user.profile_fbml="test"
39
+ end
40
+
41
+ def test_can_call_set_profile_fbml
42
+ @session.expects(:post).with('facebook.profile.setFBML', :uid=>1234,:profile=>"profile",:profile_action=>"action",:mobile_fbml=>"mobile")
43
+ @user.set_profile_fbml("profile","mobile","action")
44
+ end
45
+
46
+ def test_can_get_profile_photos
47
+ @user.expects(:profile_photos)
48
+ @user.profile_photos
49
+ end
50
+
51
+ def test_get_profile_photos
52
+ @user = Facebooker::User.new(548871286, @session)
53
+ expect_http_posts_with_responses(example_profile_photos_get_xml)
54
+ photos = @user.profile_photos
55
+ assert_equal "2357384227378429949", photos.first.aid
56
+ end
57
+
58
+ def test_can_send_email
59
+ @user.expects(:send_email).with("subject", "body text")
60
+ @user.send_email("subject", "body text")
61
+
62
+ @user.expects(:send_email).with("subject", nil, "body fbml")
63
+ @user.send_email("subject", nil, "body fbml")
64
+ end
65
+
66
+ def test_to_s
67
+ assert_equal("1234",@user.to_s)
68
+ end
69
+
70
+ private
71
+ def example_profile_photos_get_xml
72
+ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
73
+ <photos_get_response xmlns=\"http://api.facebook.com/1.0/\"
74
+ xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
75
+ xsi:schemaLocation=\"http://api.facebook.com/1.0/ http://api.facebook.com/1.0/facebook.xsd\" list=\"true\">
76
+ <photo>
77
+ <pid>34585991612804</pid>
78
+ <aid>2357384227378429949</aid>
79
+ <owner>1240077</owner>
80
+ <src>http://ip002.facebook.com/v11/135/18/8055/s1240077_30043524_2020.jpg</src>
81
+ <src_big>http://ip002.facebook.com/v11/135/18/8055/n1240077_30043524_2020.jpg</src_big>
82
+ <src_small>http://ip002.facebook.com/v11/135/18/8055/t1240077_30043524_2020.jpg</src_small>
83
+ <link>http://www.facebook.com/photo.php?pid=30043524&id=8055</link>
84
+ <caption>From The Deathmatch (Trailer) (1999)</caption>
85
+ <created>1132553361</created>
86
+ </photo>
87
+ <photo>
88
+ <pid>34585991612805</pid>
89
+ <aid>2357384227378429949</aid>
90
+ <owner>1240077</owner>
91
+ <src>http://ip002.facebook.com/v11/135/18/8055/s1240077_30043525_2184.jpg</src>
92
+ <src_big>http://ip002.facebook.com/v11/135/18/8055/n1240077_30043525_2184.jpg</src_big>
93
+ <src_small>http://ip002.facebook.com/v11/135/18/8055/t1240077_30043525_2184.jpg</src_small>
94
+ <link>http://www.facebook.com/photo.php?pid=30043525&id=8055</link>
95
+ <caption>Mexico City, back cover of the CYHS Student Underground 1999.</caption>
96
+ <created>1132553362</created>
97
+ </photo>
98
+ </photos_get_response>"
99
+ end
100
+
101
+ end