tenderlove-facebooker 1.0.16.20090319151701

Sign up to get free protection for your applications and to get access to all the features.
Files changed (124) hide show
  1. data/CHANGELOG.rdoc +17 -0
  2. data/COPYING.rdoc +19 -0
  3. data/Manifest.txt +123 -0
  4. data/README.rdoc +103 -0
  5. data/Rakefile +86 -0
  6. data/TODO.rdoc +4 -0
  7. data/examples/desktop_login.rb +14 -0
  8. data/facebooker.gemspec +38 -0
  9. data/generators/facebook/facebook_generator.rb +14 -0
  10. data/generators/facebook/templates/config/facebooker.yml +46 -0
  11. data/generators/facebook/templates/public/javascripts/facebooker.js +99 -0
  12. data/generators/facebook_controller/USAGE +33 -0
  13. data/generators/facebook_controller/facebook_controller_generator.rb +40 -0
  14. data/generators/facebook_controller/templates/controller.rb +7 -0
  15. data/generators/facebook_controller/templates/functional_test.rb +11 -0
  16. data/generators/facebook_controller/templates/helper.rb +2 -0
  17. data/generators/facebook_controller/templates/view.fbml.erb +2 -0
  18. data/generators/facebook_controller/templates/view.html.erb +2 -0
  19. data/generators/facebook_publisher/facebook_publisher_generator.rb +14 -0
  20. data/generators/facebook_publisher/templates/create_facebook_templates.rb +15 -0
  21. data/generators/facebook_publisher/templates/publisher.rb +3 -0
  22. data/generators/facebook_scaffold/USAGE +27 -0
  23. data/generators/facebook_scaffold/facebook_scaffold_generator.rb +118 -0
  24. data/generators/facebook_scaffold/templates/controller.rb +93 -0
  25. data/generators/facebook_scaffold/templates/facebook_style.css +2579 -0
  26. data/generators/facebook_scaffold/templates/functional_test.rb +89 -0
  27. data/generators/facebook_scaffold/templates/helper.rb +2 -0
  28. data/generators/facebook_scaffold/templates/layout.fbml.erb +6 -0
  29. data/generators/facebook_scaffold/templates/layout.html.erb +17 -0
  30. data/generators/facebook_scaffold/templates/style.css +74 -0
  31. data/generators/facebook_scaffold/templates/view_edit.fbml.erb +13 -0
  32. data/generators/facebook_scaffold/templates/view_edit.html.erb +18 -0
  33. data/generators/facebook_scaffold/templates/view_index.fbml.erb +24 -0
  34. data/generators/facebook_scaffold/templates/view_index.html.erb +24 -0
  35. data/generators/facebook_scaffold/templates/view_new.fbml.erb +12 -0
  36. data/generators/facebook_scaffold/templates/view_new.html.erb +17 -0
  37. data/generators/facebook_scaffold/templates/view_show.fbml.erb +10 -0
  38. data/generators/facebook_scaffold/templates/view_show.html.erb +10 -0
  39. data/generators/publisher/publisher_generator.rb +14 -0
  40. data/generators/xd_receiver/templates/xd_receiver.html +10 -0
  41. data/generators/xd_receiver/xd_receiver_generator.rb +9 -0
  42. data/init.rb +72 -0
  43. data/install.rb +12 -0
  44. data/lib/facebooker/adapters/adapter_base.rb +87 -0
  45. data/lib/facebooker/adapters/bebo_adapter.rb +75 -0
  46. data/lib/facebooker/adapters/facebook_adapter.rb +52 -0
  47. data/lib/facebooker/admin.rb +42 -0
  48. data/lib/facebooker/batch_request.rb +44 -0
  49. data/lib/facebooker/data.rb +57 -0
  50. data/lib/facebooker/feed.rb +78 -0
  51. data/lib/facebooker/logging.rb +51 -0
  52. data/lib/facebooker/mobile.rb +20 -0
  53. data/lib/facebooker/mock/service.rb +50 -0
  54. data/lib/facebooker/mock/session.rb +18 -0
  55. data/lib/facebooker/model.rb +135 -0
  56. data/lib/facebooker/models/affiliation.rb +10 -0
  57. data/lib/facebooker/models/album.rb +11 -0
  58. data/lib/facebooker/models/applicationproperties.rb +39 -0
  59. data/lib/facebooker/models/applicationrestrictions.rb +10 -0
  60. data/lib/facebooker/models/cookie.rb +10 -0
  61. data/lib/facebooker/models/education_info.rb +11 -0
  62. data/lib/facebooker/models/event.rb +28 -0
  63. data/lib/facebooker/models/friend_list.rb +16 -0
  64. data/lib/facebooker/models/group.rb +36 -0
  65. data/lib/facebooker/models/info_item.rb +10 -0
  66. data/lib/facebooker/models/info_section.rb +10 -0
  67. data/lib/facebooker/models/location.rb +8 -0
  68. data/lib/facebooker/models/notifications.rb +17 -0
  69. data/lib/facebooker/models/page.rb +27 -0
  70. data/lib/facebooker/models/photo.rb +12 -0
  71. data/lib/facebooker/models/tag.rb +12 -0
  72. data/lib/facebooker/models/user.rb +426 -0
  73. data/lib/facebooker/models/video.rb +9 -0
  74. data/lib/facebooker/models/work_info.rb +9 -0
  75. data/lib/facebooker/parser.rb +589 -0
  76. data/lib/facebooker/rails/controller.rb +304 -0
  77. data/lib/facebooker/rails/cucumber/world.rb +46 -0
  78. data/lib/facebooker/rails/cucumber.rb +28 -0
  79. data/lib/facebooker/rails/facebook_form_builder.rb +112 -0
  80. data/lib/facebooker/rails/facebook_pretty_errors.rb +22 -0
  81. data/lib/facebooker/rails/facebook_request_fix.rb +24 -0
  82. data/lib/facebooker/rails/facebook_session_handling.rb +69 -0
  83. data/lib/facebooker/rails/facebook_url_helper.rb +192 -0
  84. data/lib/facebooker/rails/facebook_url_rewriting.rb +39 -0
  85. data/lib/facebooker/rails/helpers/fb_connect.rb +89 -0
  86. data/lib/facebooker/rails/helpers.rb +762 -0
  87. data/lib/facebooker/rails/integration_session.rb +38 -0
  88. data/lib/facebooker/rails/profile_publisher_extensions.rb +42 -0
  89. data/lib/facebooker/rails/publisher.rb +526 -0
  90. data/lib/facebooker/rails/routing.rb +49 -0
  91. data/lib/facebooker/rails/test_helpers.rb +68 -0
  92. data/lib/facebooker/rails/utilities.rb +22 -0
  93. data/lib/facebooker/server_cache.rb +24 -0
  94. data/lib/facebooker/service.rb +94 -0
  95. data/lib/facebooker/session.rb +584 -0
  96. data/lib/facebooker/version.rb +9 -0
  97. data/lib/facebooker.rb +174 -0
  98. data/lib/net/http_multipart_post.rb +123 -0
  99. data/lib/tasks/facebooker.rake +18 -0
  100. data/lib/tasks/tunnel.rake +46 -0
  101. data/rails/init.rb +1 -0
  102. data/setup.rb +1585 -0
  103. data/templates/layout.erb +24 -0
  104. data/test/facebooker/adapters_test.rb +96 -0
  105. data/test/facebooker/admin_test.rb +102 -0
  106. data/test/facebooker/batch_request_test.rb +83 -0
  107. data/test/facebooker/data_test.rb +86 -0
  108. data/test/facebooker/logging_test.rb +43 -0
  109. data/test/facebooker/mobile_test.rb +45 -0
  110. data/test/facebooker/model_test.rb +123 -0
  111. data/test/facebooker/models/event_test.rb +15 -0
  112. data/test/facebooker/models/user_test.rb +295 -0
  113. data/test/facebooker/rails/publisher_test.rb +452 -0
  114. data/test/facebooker/rails_integration_test.rb +1312 -0
  115. data/test/facebooker/server_cache_test.rb +44 -0
  116. data/test/facebooker/session_test.rb +614 -0
  117. data/test/facebooker_test.rb +925 -0
  118. data/test/fixtures/multipart_post_body_with_only_parameters.txt +33 -0
  119. data/test/fixtures/multipart_post_body_with_single_file.txt +38 -0
  120. data/test/fixtures/multipart_post_body_with_single_file_that_has_nil_key.txt +38 -0
  121. data/test/net/http_multipart_post_test.rb +52 -0
  122. data/test/rails_test_helper.rb +11 -0
  123. data/test/test_helper.rb +66 -0
  124. metadata +217 -0
@@ -0,0 +1,1312 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../rails_test_helper')
2
+
3
+ ActionController::Routing::Routes.draw do |map|
4
+ map.connect '', :controller=>"facebook",:conditions=>{:canvas=>true}
5
+ map.connect '', :controller=>"plain_old_rails"
6
+ map.resources :comments, :controller=>"plain_old_rails"
7
+ map.connect 'require_auth/:action', :controller => "controller_which_requires_facebook_authentication"
8
+ map.connect 'require_install/:action', :controller => "controller_which_requires_application_installation"
9
+ map.connect ':controller/:action/:id', :controller => "plain_old_rails"
10
+ end
11
+
12
+ module FBConnectTestHelpers
13
+ def setup_fb_connect_cookies(params=cookie_hash_for_auth)
14
+ params.each {|k,v| @request.cookies[ENV['FACEBOOK_API_KEY']+k] = CGI::Cookie.new(ENV['FACEBOOK_API_KEY']+k,v)}
15
+ end
16
+
17
+ def expired_cookie_hash_for_auth
18
+ {"_ss" => "not_used", "_session_key"=> "whatever", "_user"=>"77777", "_expires"=>"#{1.day.ago.to_i}"}
19
+ end
20
+
21
+ def cookie_hash_for_auth
22
+ {"_ss" => "not_used", "_session_key"=> "whatever", "_user"=>"77777", "_expires"=>"#{1.day.from_now.to_i}"}
23
+ end
24
+ end
25
+ class NoisyController < ActionController::Base
26
+ include Facebooker::Rails::Controller
27
+ def rescue_action(e) raise e end
28
+ end
29
+
30
+
31
+ class ControllerWhichRequiresExtendedPermissions< NoisyController
32
+ ensure_authenticated_to_facebook
33
+ before_filter :ensure_has_status_update
34
+ before_filter :ensure_has_photo_upload
35
+ before_filter :ensure_has_video_upload
36
+ before_filter :ensure_has_create_listing
37
+ def index
38
+ render :text => 'score!'
39
+ end
40
+ end
41
+
42
+ class FBConnectController < NoisyController
43
+ before_filter :create_facebook_session
44
+ def index
45
+ render :text => 'score!'
46
+ end
47
+ end
48
+
49
+ class ControllerWhichRequiresFacebookAuthentication < NoisyController
50
+ ensure_authenticated_to_facebook
51
+ def index
52
+ render :text => 'score!'
53
+ end
54
+ def link_test
55
+ options = {}
56
+ options[:canvas] = true if params[:canvas] == true
57
+ options[:canvas] = false if params[:canvas] == false
58
+ render :text=>url_for(options)
59
+ end
60
+
61
+ def named_route_test
62
+ render :text=>comments_url()
63
+ end
64
+
65
+ def image_test
66
+ render :inline=>"<%=image_tag 'image.png'%>"
67
+ end
68
+
69
+ def fb_params_test
70
+ render :text=>facebook_params['user']
71
+ end
72
+
73
+ def publisher_test
74
+ if wants_interface?
75
+ render :text=>"interface"
76
+ else
77
+ render :text=>"not interface"
78
+ end
79
+ end
80
+
81
+ def publisher_test_interface
82
+ render_publisher_interface("This is a test",false,true)
83
+ end
84
+
85
+ def publisher_test_response
86
+ ua=Facebooker::Rails::Publisher::UserAction.new
87
+ ua.data = {:params=>true}
88
+ ua.template_name = "template_name"
89
+ ua.template_id = 1234
90
+ render_publisher_response(ua)
91
+ end
92
+
93
+ def publisher_test_error
94
+ render_publisher_error("Title","Body")
95
+ end
96
+
97
+ end
98
+ class ControllerWhichRequiresApplicationInstallation < NoisyController
99
+ ensure_application_is_installed_by_facebook_user
100
+ def index
101
+ render :text => 'installed!'
102
+ end
103
+ end
104
+ class FacebookController < ActionController::Base
105
+ def index
106
+ end
107
+ end
108
+
109
+ class PlainOldRailsController < ActionController::Base
110
+ def index
111
+ end
112
+ def link_test
113
+ options = {}
114
+ options[:canvas] = true if params[:canvas] == true
115
+ options[:canvas] = false if params[:canvas] == false
116
+ render :text => url_for(options)
117
+ end
118
+
119
+ def named_route_test
120
+ render :text=>comments_url()
121
+ end
122
+ def canvas_false_test
123
+ render :text=>comments_url(:canvas=>false)
124
+ end
125
+ def canvas_true_test
126
+ render :text=>comments_url(:canvas=>true)
127
+ end
128
+ end
129
+
130
+ class Test::Unit::TestCase
131
+ include Facebooker::Rails::TestHelpers
132
+ end
133
+
134
+
135
+ # you can't use asset_recognize, because it can't pass parameters in to the requests
136
+ class UrlRecognitionTests < Test::Unit::TestCase
137
+ def test_detects_in_canvas
138
+ request = ActionController::TestRequest.new({"fb_sig_in_canvas"=>"1"}, {}, nil)
139
+ request.path = "/"
140
+ ActionController::Routing::Routes.recognize(request)
141
+ assert_equal({"controller"=>"facebook","action"=>"index"},request.path_parameters)
142
+ end
143
+
144
+ def test_routes_when_not_in_canvas
145
+ request = ActionController::TestRequest.new({}, {}, nil)
146
+ request.path = "/"
147
+ ActionController::Routing::Routes.recognize(request)
148
+ assert_equal({"controller"=>"plain_old_rails","action"=>"index"},request.path_parameters)
149
+ end
150
+ end
151
+
152
+ class RailsIntegrationTestForFBConnect < Test::Unit::TestCase
153
+ include FBConnectTestHelpers
154
+
155
+ def setup
156
+ ENV['FACEBOOK_CANVAS_PATH'] ='facebook_app_name'
157
+ ENV['FACEBOOK_API_KEY'] = '1234567'
158
+ ENV['FACEBOOK_SECRET_KEY'] = '7654321'
159
+ @controller = FBConnectController.new
160
+ @request = ActionController::TestRequest.new
161
+ @response = ActionController::TestResponse.new
162
+ @controller.stubs(:verify_signature).returns(true)
163
+
164
+ end
165
+
166
+ def test_doesnt_set_cookie_but_facebook_session_is_available
167
+ setup_fb_connect_cookies
168
+ get :index
169
+ assert_not_nil @controller.facebook_session
170
+ assert_nil @response.cookies[:facebook_session]
171
+
172
+ end
173
+ end
174
+
175
+ class RailsIntegrationTestForNonFacebookControllers < Test::Unit::TestCase
176
+ def setup
177
+ ENV['FACEBOOK_CANVAS_PATH'] ='facebook_app_name'
178
+ ENV['FACEBOOK_API_KEY'] = '1234567'
179
+ ENV['FACEBOOK_SECRET_KEY'] = '7654321'
180
+ @controller = PlainOldRailsController.new
181
+ @request = ActionController::TestRequest.new
182
+ @response = ActionController::TestResponse.new
183
+ end
184
+
185
+ def test_url_for_links_to_callback_if_canvas_is_false_and_in_canvas
186
+ get :link_test
187
+ assert_match /test.host/,@response.body
188
+ end
189
+
190
+ def test_named_route_doesnt_include_canvas_path_when_not_in_canvas
191
+ get :named_route_test
192
+ assert_equal "http://test.host/comments",@response.body
193
+ end
194
+ def test_named_route_includes_canvas_path_when_in_canvas
195
+ get :named_route_test, facebook_params
196
+ assert_equal "http://apps.facebook.com/facebook_app_name/comments",@response.body
197
+ end
198
+
199
+ def test_named_route_doesnt_include_canvas_path_when_in_canvas_with_canvas_equals_false
200
+ get :canvas_false_test, facebook_params
201
+ assert_equal "http://test.host/comments",@response.body
202
+ end
203
+ def test_named_route_does_include_canvas_path_when_not_in_canvas_with_canvas_equals_true
204
+ get :canvas_true_test
205
+ assert_equal "http://apps.facebook.com/facebook_app_name/comments",@response.body
206
+ end
207
+
208
+ end
209
+
210
+ class RailsIntegrationTestForExtendedPermissions < Test::Unit::TestCase
211
+ def setup
212
+ ENV['FACEBOOK_API_KEY'] = '1234567'
213
+ ENV['FACEBOOK_SECRET_KEY'] = '7654321'
214
+ @controller = ControllerWhichRequiresExtendedPermissions.new
215
+ @request = ActionController::TestRequest.new
216
+ @response = ActionController::TestResponse.new
217
+ @controller.stubs(:verify_signature).returns(true)
218
+ end
219
+
220
+ def test_redirects_without_set_status
221
+ post :index, facebook_params
222
+ assert_response :success
223
+ assert_equal("<fb:redirect url=\"http://www.facebook.com/authorize.php?api_key=1234567&v=1.0&ext_perm=status_update\" />", @response.body)
224
+ end
225
+ def test_redirects_without_photo_upload
226
+ post :index, facebook_params(:fb_sig_ext_perms=>"status_update")
227
+ assert_response :success
228
+ assert_equal("<fb:redirect url=\"http://www.facebook.com/authorize.php?api_key=1234567&v=1.0&ext_perm=photo_upload\" />", @response.body)
229
+ end
230
+ def test_redirects_without_video_upload
231
+ post :index, facebook_params(:fb_sig_ext_perms=>"status_update,photo_upload")
232
+ assert_response :success
233
+ assert_equal("<fb:redirect url=\"http://www.facebook.com/authorize.php?api_key=1234567&v=1.0&ext_perm=video_upload\" />", @response.body)
234
+ end
235
+ def test_redirects_without_create_listing
236
+ post :index, facebook_params(:fb_sig_ext_perms=>"status_update,photo_upload,video_upload")
237
+ assert_response :success
238
+ assert_equal("<fb:redirect url=\"http://www.facebook.com/authorize.php?api_key=1234567&v=1.0&ext_perm=create_listing\" />", @response.body)
239
+ end
240
+
241
+ def test_renders_with_permission
242
+ post :index, facebook_params(:fb_sig_ext_perms=>"status_update,photo_upload,create_listing,video_upload")
243
+ assert_response :success
244
+ assert_equal("score!", @response.body)
245
+
246
+ end
247
+ end
248
+
249
+ class RailsIntegrationTestForApplicationInstallation < Test::Unit::TestCase
250
+ def setup
251
+ ENV['FACEBOOK_API_KEY'] = '1234567'
252
+ ENV['FACEBOOK_SECRET_KEY'] = '7654321'
253
+ @controller = ControllerWhichRequiresApplicationInstallation.new
254
+ @request = ActionController::TestRequest.new
255
+ @response = ActionController::TestResponse.new
256
+ @controller.stubs(:verify_signature).returns(true)
257
+ end
258
+
259
+ def test_if_controller_requires_application_installation_unauthenticated_requests_will_redirect_to_install_page
260
+ get :index
261
+ assert_response :redirect
262
+ assert_equal("http://www.facebook.com/install.php?api_key=1234567&v=1.0", @response.headers['Location'])
263
+ end
264
+
265
+ def test_if_controller_requires_application_installation_authenticated_requests_without_installation_will_redirect_to_install_page
266
+ get :index, facebook_params(:fb_sig_added => nil)
267
+ assert_response :success
268
+ assert_equal("<fb:redirect url=\"http://www.facebook.com/install.php?api_key=1234567&v=1.0\" />", @response.body)
269
+ end
270
+
271
+ def test_if_controller_requires_application_installation_authenticated_requests_with_installation_will_render
272
+ get :index, facebook_params('fb_sig_added' => "1")
273
+ assert_response :success
274
+ assert_equal("installed!", @response.body)
275
+ end
276
+ end
277
+
278
+ class RailsIntegrationTest < Test::Unit::TestCase
279
+ include FBConnectTestHelpers
280
+ def setup
281
+ ENV['FACEBOOK_CANVAS_PATH'] ='root'
282
+ ENV['FACEBOOK_API_KEY'] = '1234567'
283
+ ENV['FACEBOOK_SECRET_KEY'] = '7654321'
284
+ ActionController::Base.asset_host="http://root.example.com"
285
+ @controller = ControllerWhichRequiresFacebookAuthentication.new
286
+ @request = ActionController::TestRequest.new
287
+ @response = ActionController::TestResponse.new
288
+ @controller.stubs(:verify_signature).returns(true)
289
+
290
+ end
291
+
292
+ def test_named_route_includes_new_canvas_path_when_in_new_canvas
293
+ get :named_route_test, facebook_params("fb_sig_in_new_facebook"=>"1")
294
+ assert_equal "http://apps.facebook.com/root/comments",@response.body
295
+ end
296
+
297
+ def test_if_controller_requires_facebook_authentication_unauthenticated_requests_will_redirect
298
+ get :index
299
+ assert_response :redirect
300
+ assert_equal("http://www.facebook.com/login.php?api_key=1234567&v=1.0", @response.headers['Location'])
301
+ end
302
+
303
+ def test_facebook_params_are_parsed_into_a_separate_hash
304
+ get :index, facebook_params(:fb_sig_user => '9')
305
+ assert_not_nil @controller.facebook_params['time']
306
+ end
307
+
308
+ def test_facebook_params_convert_in_canvas_to_boolean
309
+ get :index, facebook_params
310
+ assert_equal(true, @controller.facebook_params['in_canvas'])
311
+ end
312
+
313
+ def test_facebook_params_convert_added_to_boolean_false
314
+ get :index, facebook_params(:fb_sig_added => '0')
315
+ assert_equal(false, @controller.facebook_params['added'])
316
+ end
317
+
318
+ def test_facebook_params_convert_added_to_boolean_true
319
+ get :index, facebook_params('fb_sig_added' => "1")
320
+ assert_equal(true, @controller.facebook_params['added'])
321
+ end
322
+
323
+ def test_facebook_params_convert_expirey_into_nil
324
+ get :index, facebook_params(:fb_sig_expires => '0')
325
+ assert_nil(@controller.facebook_params['expires'])
326
+ end
327
+
328
+ def test_facebook_params_convert_expirey_into_time
329
+ get :index, facebook_params(:fb_sig_expires => 5.minutes.from_now.to_f)
330
+ assert_instance_of Time, @controller.facebook_params['expires']
331
+ end
332
+
333
+ def test_facebook_params_convert_friend_list_to_parsed_array_of_friend_ids
334
+ get :index, facebook_params(:fb_sig_friends => '1,2,3,4,5')
335
+ assert_kind_of(Array, @controller.facebook_params['friends'])
336
+ assert_equal(5, @controller.facebook_params['friends'].size)
337
+ end
338
+
339
+ def test_session_can_be_resecured_from_facebook_params
340
+ get :index, facebook_params(:fb_sig_user => 10)
341
+ assert_equal(10, @controller.facebook_session.user.id)
342
+ end
343
+
344
+ def test_existing_secured_session_is_used_if_available
345
+ session = Facebooker::Session.create(Facebooker::Session.api_key, Facebooker::Session.secret_key)
346
+ session.secure_with!("session_key", "111", Time.now.to_i + 60)
347
+ get :index, facebook_params(:fb_sig_session_key => 'session_key', :fb_sig_user => '987'), {:facebook_session => session}
348
+ assert_equal(111, @controller.facebook_session.user.id)
349
+ end
350
+
351
+ def test_facebook_params_used_if_existing_secured_session_key_does_not_match
352
+ session = Facebooker::Session.create(Facebooker::Session.api_key, Facebooker::Session.secret_key)
353
+ session.secure_with!("different session key", "111", Time.now.to_i + 60)
354
+ get :index, facebook_params(:fb_sig_session_key => '', :fb_sig_user => '123'), {:facebook_session => session}
355
+ assert_equal(123, @controller.facebook_session.user.id)
356
+ end
357
+
358
+ def test_existing_secured_session_is_NOT_used_if_available_and_facebook_params_session_key_is_nil_and_in_canvas
359
+ session = Facebooker::Session.create(Facebooker::Session.api_key, Facebooker::Session.secret_key)
360
+ session.secure_with!("session_key", "111", Time.now.to_i + 60)
361
+ session.secure_with!("a session key", "1111111", Time.now.to_i + 60)
362
+ get :index, facebook_params(:fb_sig_session_key => '', :fb_sig_user => '987'), {:facebook_session => session}
363
+ assert_equal(987, @controller.facebook_session.user.id)
364
+ end
365
+
366
+ def test_existing_secured_session_IS_used_if_available_and_facebook_params_session_key_is_nil_and_NOT_in_canvas
367
+ @contoller = PlainOldRailsController.new
368
+ session = Facebooker::Session.create(ENV['FACEBOOK_API_KEY'], ENV['FACEBOOK_SECRET_KEY'])
369
+ session.secure_with!("a session key", "1111111", Time.now.to_i + 60)
370
+ get :index,{}, {:facebook_session => session}
371
+
372
+ assert_equal(1111111, @controller.facebook_session.user.id)
373
+ end
374
+
375
+ def test_session_can_be_secured_with_auth_token
376
+ auth_token = 'ohaiauthtokenhere111'
377
+ modified_params = facebook_params
378
+ modified_params.delete('fb_sig_session_key')
379
+ modified_params['auth_token'] = auth_token
380
+ session_mock = flexmock(session = Facebooker::Session.create(ENV['FACEBOOK_API_KEY'], ENV['FACEBOOK_SECRET_KEY']))
381
+ session_params = { 'session_key' => '123', 'uid' => '321' }
382
+ session_mock.should_receive(:post).with('facebook.auth.getSession', :auth_token => auth_token).once.and_return(session_params).ordered
383
+ flexmock(@controller).should_receive(:new_facebook_session).once.and_return(session).ordered
384
+ get :index, modified_params
385
+ end
386
+
387
+ def test_session_secured_with_auth_token_if_cookies_expired
388
+ auth_token = 'ohaiauthtokenhere111'
389
+ modified_params = facebook_params
390
+ modified_params.delete('fb_sig_session_key')
391
+ modified_params['auth_token'] = auth_token
392
+ session_mock = flexmock(session = Facebooker::Session.create(ENV['FACEBOOK_API_KEY'], ENV['FACEBOOK_SECRET_KEY']))
393
+ session_params = { 'session_key' => '123', 'uid' => '321' }
394
+ session_mock.should_receive(:post).with('facebook.auth.getSession', :auth_token => auth_token).once.and_return(session_params).ordered
395
+ flexmock(@controller).should_receive(:new_facebook_session).once.and_return(session).ordered
396
+ setup_fb_connect_cookies(expired_cookie_hash_for_auth)
397
+ get :index, modified_params
398
+ assert_equal(321, @controller.facebook_session.user.id)
399
+ end
400
+
401
+ def test_session_can_be_secured_with_cookies
402
+ setup_fb_connect_cookies
403
+ get :index
404
+ assert_equal(77777, @controller.facebook_session.user.id)
405
+ end
406
+
407
+ def test_session_does_NOT_secure_with_expired_cookies
408
+ setup_fb_connect_cookies(expired_cookie_hash_for_auth)
409
+ get :index
410
+ assert_nil(@controller.facebook_session)
411
+ end
412
+
413
+ def test_user_friends_can_be_populated_from_facebook_params_if_available
414
+ get :index, facebook_params(:fb_sig_friends => '1,2,3,4')
415
+ friends = @controller.facebook_session.user.friends
416
+ assert_not_nil(friends)
417
+ assert_equal(4, friends.size)
418
+ end
419
+
420
+ def test_fbml_redirect_tag_handles_hash_parameters_correctly
421
+ get :index, facebook_params
422
+ assert_equal "<fb:redirect url=\"http://apps.facebook.com/root/require_auth\" />", @controller.send(:fbml_redirect_tag, :action => :index,:canvas=>true)
423
+ end
424
+
425
+ def test_redirect_to_renders_fbml_redirect_tag_if_request_is_for_a_facebook_canvas
426
+ get :index, facebook_params(:fb_sig_user => nil)
427
+ assert_response :success
428
+ assert_equal("<fb:redirect url=\"http://www.facebook.com/login.php?api_key=1234567&v=1.0\" />", @response.body)
429
+ end
430
+
431
+ def test_url_for_links_to_canvas_if_canvas_is_true_and_not_in_canvas
432
+ get :link_test, facebook_params(:fb_sig_in_canvas=>0,:canvas=>true)
433
+ assert_match /apps.facebook.com/,@response.body
434
+ end
435
+
436
+ def test_includes_relative_url_root_when_linked_to_canvas
437
+ get :link_test,facebook_params(:fb_sig_in_canvas=>0,:canvas=>true)
438
+ assert_match /root/,@response.body
439
+ end
440
+
441
+ def test_url_for_links_to_callback_if_canvas_is_false_and_in_canvas
442
+ get :link_test,facebook_params(:fb_sig_in_canvas=>0,:canvas=>false)
443
+ assert_match /test.host/,@response.body
444
+ end
445
+
446
+ def test_url_for_doesnt_include_url_root_when_not_linked_to_canvas
447
+ get :link_test,facebook_params(:fb_sig_in_canvas=>0,:canvas=>false)
448
+ assert !@response.body.match(/root/)
449
+ end
450
+
451
+ def test_url_for_links_to_canvas_if_canvas_is_not_set
452
+ get :link_test,facebook_params
453
+ assert_match /apps.facebook.com/,@response.body
454
+ end
455
+
456
+ def test_image_tag
457
+ get :image_test, facebook_params
458
+ assert_equal "<img alt=\"Image\" src=\"http://root.example.com/images/image.png\" />",@response.body
459
+ end
460
+
461
+ def test_wants_interface_is_available_and_detects_method
462
+ get :publisher_test, facebook_params(:method=>"publisher_getInterface")
463
+ assert_equal "interface",@response.body
464
+ end
465
+ def test_wants_interface_is_available_and_detects_not_interface
466
+ get :publisher_test, facebook_params(:method=>"publisher_getFeedStory")
467
+ assert_equal "not interface",@response.body
468
+ end
469
+
470
+ def test_publisher_test_error
471
+ get :publisher_test_error, facebook_params
472
+ assert_equal Facebooker.json_decode("{\"errorCode\": 1, \"errorTitle\": \"Title\", \"errorMessage\": \"Body\"}"), Facebooker.json_decode(@response.body)
473
+ end
474
+
475
+ def test_publisher_test_interface
476
+ get :publisher_test_interface, facebook_params
477
+ assert_equal Facebooker.json_decode("{\"method\": \"publisher_getInterface\", \"content\": {\"fbml\": \"This is a test\", \"publishEnabled\": false, \"commentEnabled\": true}}"), Facebooker.json_decode(@response.body)
478
+ end
479
+
480
+ def test_publisher_test_reponse
481
+ get :publisher_test_response, facebook_params
482
+ assert_equal Facebooker.json_decode("{\"method\": \"publisher_getFeedStory\", \"content\": {\"feed\": {\"template_data\": {\"params\": true}, \"template_id\": 1234}}}"), Facebooker.json_decode(@response.body)
483
+
484
+ end
485
+
486
+ private
487
+
488
+ def expired_cookie_hash_for_auth
489
+ {"_ss" => "not_used", "_session_key"=> "whatever", "_user"=>"77777", "_expires"=>"#{1.day.ago.to_i}"}
490
+ end
491
+
492
+ def cookie_hash_for_auth
493
+ {"_ss" => "not_used", "_session_key"=> "whatever", "_user"=>"77777", "_expires"=>"#{1.day.from_now.to_i}"}
494
+ end
495
+
496
+ end
497
+
498
+
499
+ class RailsSignatureTest < Test::Unit::TestCase
500
+ def setup
501
+ ENV['FACEBOOKER_RELATIVE_URL_ROOT'] ='root'
502
+ ENV['FACEBOOK_API_KEY'] = '1234567'
503
+ ENV['FACEBOOK_SECRET_KEY'] = '7654321'
504
+ @controller = ControllerWhichRequiresFacebookAuthentication.new
505
+ @request = ActionController::TestRequest.new
506
+ @response = ActionController::TestResponse.new
507
+
508
+ end
509
+
510
+ def test_should_raise_too_old_for_replayed_session
511
+ begin
512
+ get :fb_params_test, facebook_params('fb_sig_time' => Time.now.to_i - 49.hours)
513
+ fail "No SignatureTooOld raised"
514
+ rescue Facebooker::Session::SignatureTooOld=>e
515
+ end
516
+ end
517
+
518
+ def test_should_raise_on_bad_sig
519
+ begin
520
+ get :fb_params_test, facebook_params.merge('fb_sig' => 'incorrect')
521
+ fail "No IncorrectSignature raised"
522
+ rescue Facebooker::Session::IncorrectSignature=>e
523
+ end
524
+ end
525
+
526
+ def test_valid_signature
527
+ @controller.expects(:earliest_valid_session).returns(Time.at(1186588275.5988)-1)
528
+ get :fb_params_test, facebook_params
529
+
530
+ end
531
+
532
+ end
533
+ class RailsHelperTest < Test::Unit::TestCase
534
+ class HelperClass
535
+ include ActionView::Helpers::TextHelper
536
+ include ActionView::Helpers::CaptureHelper
537
+ include ActionView::Helpers::TagHelper
538
+ include ActionView::Helpers::AssetTagHelper
539
+ include ActionView::Helpers::JavaScriptHelper
540
+ include Facebooker::Rails::Helpers
541
+ attr_accessor :flash, :output_buffer
542
+ def initialize
543
+ @flash={}
544
+ @template = self
545
+ @content_for_test_param="Test Param"
546
+ @output_buffer = ""
547
+ end
548
+ #used for stubbing out the form builder
549
+ def url_for(arg)
550
+ arg
551
+ end
552
+ def request
553
+ ActionController::TestRequest.new
554
+ end
555
+ def fields_for(*args)
556
+ ""
557
+ end
558
+
559
+ end
560
+
561
+ # used for capturing the contents of some of the helper tests
562
+ # this duplicates the rails template system
563
+ attr_accessor :_erbout
564
+
565
+ def setup
566
+ ENV['FACEBOOK_CANVAS_PATH'] ='facebook'
567
+ ENV['FACEBOOK_API_KEY'] = '1234567'
568
+ ENV['FACEBOOK_SECRET_KEY'] = '7654321'
569
+
570
+ @_erbout = ""
571
+ @h = HelperClass.new
572
+ #use an asset path where the canvas path equals the hostname to make sure we handle that case right
573
+ ActionController::Base.asset_host='http://facebook.host.com'
574
+ end
575
+
576
+ def test_fb_profile_pic
577
+ assert_equal "<fb:profile-pic uid=\"1234\"></fb:profile-pic>", @h.fb_profile_pic("1234")
578
+ end
579
+
580
+ def test_fb_profile_pic_with_valid_size
581
+ assert_equal "<fb:profile-pic size=\"small\" uid=\"1234\"></fb:profile-pic>", @h.fb_profile_pic("1234", :size => :small)
582
+ end
583
+
584
+ def test_fb_profile_pic_with_invalid_size
585
+ assert_raises(ArgumentError) {@h.fb_profile_pic("1234", :size => :mediumm)}
586
+ end
587
+
588
+ def test_fb_photo
589
+ assert_equal "<fb:photo pid=\"1234\"></fb:photo>",@h.fb_photo("1234")
590
+ end
591
+
592
+ def test_fb_photo_with_object_responding_to_photo_id
593
+ photo = flexmock("photo", :photo_id => "5678")
594
+ assert_equal "<fb:photo pid=\"5678\"></fb:photo>", @h.fb_photo(photo)
595
+ end
596
+
597
+ def test_fb_photo_with_invalid_size
598
+ assert_raises(ArgumentError) {@h.fb_photo("1234", :size => :medium)}
599
+ end
600
+
601
+ def test_fb_photo_with_invalid_size_value
602
+ assert_raises(ArgumentError) {@h.fb_photo("1234", :size => :mediumm)}
603
+ end
604
+
605
+ def test_fb_photo_with_invalid_align_value
606
+ assert_raises(ArgumentError) {@h.fb_photo("1234", :align => :rightt)}
607
+ end
608
+
609
+ def test_fb_photo_with_valid_align_value
610
+ assert_equal "<fb:photo align=\"right\" pid=\"1234\"></fb:photo>",@h.fb_photo("1234", :align => :right)
611
+ end
612
+
613
+ def test_fb_photo_with_class
614
+ assert_equal "<fb:photo class=\"picky\" pid=\"1234\"></fb:photo>",@h.fb_photo("1234", :class => :picky)
615
+ end
616
+ def test_fb_photo_with_style
617
+ assert_equal "<fb:photo pid=\"1234\" style=\"some=css;put=here;\"></fb:photo>",@h.fb_photo("1234", :style => "some=css;put=here;")
618
+ end
619
+
620
+ def test_fb_prompt_permission_valid_no_callback
621
+ assert_equal "<fb:prompt-permission perms=\"email\">Can I email you?</fb:prompt-permission>",@h.fb_prompt_permission("email","Can I email you?")
622
+ end
623
+
624
+ def test_fb_prompt_permission_valid_with_callback
625
+ assert_equal "<fb:prompt-permission next_fbjs=\"do_stuff()\" perms=\"email\">a message</fb:prompt-permission>",@h.fb_prompt_permission("email","a message","do_stuff()")
626
+ end
627
+
628
+ def test_fb_prompt_permission_invalid_option
629
+ assert_raises(ArgumentError) {@h.fb_prompt_permission("invliad", "a message")}
630
+
631
+ end
632
+
633
+ def test_fb_add_profile_section
634
+ assert_equal "<fb:add-section-button section=\"profile\" />",@h.fb_add_profile_section
635
+ end
636
+
637
+ def test_fb_add_info_section
638
+ assert_equal "<fb:add-section-button section=\"info\" />",@h.fb_add_info_section
639
+ end
640
+
641
+ def test_fb_name_with_invalid_key
642
+ assert_raises(ArgumentError) {@h.fb_name(1234, :sizee => false)}
643
+ end
644
+
645
+ def test_fb_name
646
+ assert_equal "<fb:name uid=\"1234\"></fb:name>",@h.fb_name("1234")
647
+ end
648
+
649
+ def test_fb_name_with_transformed_key
650
+ assert_equal "<fb:name uid=\"1234\" useyou=\"true\"></fb:name>", @h.fb_name(1234, :use_you => true)
651
+ end
652
+
653
+ def test_fb_name_with_user_responding_to_facebook_id
654
+ user = flexmock("user", :facebook_id => "5678")
655
+ assert_equal "<fb:name uid=\"5678\"></fb:name>", @h.fb_name(user)
656
+ end
657
+
658
+ def test_fb_name_with_invalid_key
659
+ assert_raises(ArgumentError) {@h.fb_name(1234, :linkd => false)}
660
+ end
661
+
662
+ def test_fb_tabs
663
+ assert_equal "<fb:tabs></fb:tabs>", @h.fb_tabs{}
664
+ end
665
+
666
+ def test_fb_tab_item
667
+ assert_equal "<fb:tab-item href=\"http://www.google.com\" title=\"Google\" />", @h.fb_tab_item("Google", "http://www.google.com")
668
+ end
669
+
670
+ def test_fb_tab_item_raises_exception_for_invalid_option
671
+ assert_raises(ArgumentError) {@h.fb_tab_item("Google", "http://www.google.com", :alignn => :right)}
672
+ end
673
+
674
+ def test_fb_tab_item_raises_exception_for_invalid_align_value
675
+ assert_raises(ArgumentError) {@h.fb_tab_item("Google", "http://www.google.com", :align => :rightt)}
676
+ end
677
+
678
+ def test_fb_req_choice
679
+ assert_equal "<fb:req-choice label=\"label\" url=\"url\" />", @h.fb_req_choice("label","url")
680
+ end
681
+
682
+ def test_fb_multi_friend_selector
683
+ assert_equal "<fb:multi-friend-selector actiontext=\"This is a message\" max=\"20\" showborder=\"false\" />", @h.fb_multi_friend_selector("This is a message")
684
+ end
685
+ def test_fb_multi_friend_selector_with_options
686
+ assert_equal "<fb:multi-friend-selector actiontext=\"This is a message\" exclude_ids=\"1,2\" max=\"20\" showborder=\"false\" />", @h.fb_multi_friend_selector("This is a message",:exclude_ids=>"1,2")
687
+ end
688
+
689
+ def test_fb_comments
690
+ assert_equal "<fb:comments candelete=\"false\" canpost=\"true\" numposts=\"7\" showform=\"true\" xid=\"a:1\" />", @h.fb_comments("a:1",true,false,7,:showform=>true)
691
+ end
692
+
693
+ def test_fb_title
694
+ assert_equal "<fb:title>This is the canvas page window title</fb:title>", @h.fb_title("This is the canvas page window title")
695
+ end
696
+
697
+ def test_fb_google_analytics
698
+ assert_equal "<fb:google-analytics uacct=\"UA-9999999-99\" />", @h.fb_google_analytics("UA-9999999-99")
699
+ end
700
+
701
+ def test_fb_if_is_user_with_single_object
702
+ user = flexmock("user", :facebook_id => "5678")
703
+ assert_equal "<fb:if-is-user uid=\"5678\"></fb:if-is-user>", @h.fb_if_is_user(user){}
704
+ end
705
+
706
+ def test_fb_if_is_user_with_array
707
+ user1 = flexmock("user", :facebook_id => "5678")
708
+ user2 = flexmock("user", :facebook_id => "1234")
709
+ assert_equal "<fb:if-is-user uid=\"5678,1234\"></fb:if-is-user>", @h.fb_if_is_user([user1,user2]){}
710
+ end
711
+
712
+ def test_fb_else
713
+ assert_equal "<fb:else></fb:else>", @h.fb_else{}
714
+ end
715
+
716
+ def test_fb_about_url
717
+ ENV["FACEBOOK_API_KEY"]="1234"
718
+ assert_equal "http://www.facebook.com/apps/application.php?api_key=1234", @h.fb_about_url
719
+ end
720
+
721
+ def test_fb_ref_with_url
722
+ assert_equal "<fb:ref url=\"A URL\" />", @h.fb_ref(:url => "A URL")
723
+ end
724
+
725
+ def test_fb_ref_with_handle
726
+ assert_equal "<fb:ref handle=\"A Handle\" />", @h.fb_ref(:handle => "A Handle")
727
+ end
728
+
729
+ def test_fb_ref_with_invalid_attribute
730
+ assert_raises(ArgumentError) {@h.fb_ref(:handlee => "A HANLDE")}
731
+ end
732
+
733
+ def test_fb_ref_with_handle_and_url
734
+ assert_raises(ArgumentError) {@h.fb_ref(:url => "URL", :handle => "HANDLE")}
735
+ end
736
+
737
+ def test_facebook_messages_notice
738
+ @h.flash[:notice]="A message"
739
+ assert_equal "<fb:success message=\"A message\" />",@h.facebook_messages
740
+ end
741
+
742
+ def test_facebook_messages_error
743
+ @h.flash[:error]="An error"
744
+ assert_equal "<fb:error message=\"An error\" />",@h.facebook_messages
745
+ end
746
+ def test_fb_wall_post
747
+ assert_equal "<fb:wallpost uid=\"1234\">A wall post</fb:wallpost>",@h.fb_wall_post("1234","A wall post")
748
+ end
749
+
750
+ def test_fb_pronoun
751
+ assert_equal "<fb:pronoun uid=\"1234\"></fb:pronoun>", @h.fb_pronoun(1234)
752
+ end
753
+
754
+ def test_fb_pronoun_with_transformed_key
755
+ assert_equal "<fb:pronoun uid=\"1234\" usethey=\"true\"></fb:pronoun>", @h.fb_pronoun(1234, :use_they => true)
756
+ end
757
+
758
+ def test_fb_pronoun_with_user_responding_to_facebook_id
759
+ user = flexmock("user", :facebook_id => "5678")
760
+ assert_equal "<fb:pronoun uid=\"5678\"></fb:pronoun>", @h.fb_pronoun(user)
761
+ end
762
+
763
+ def test_fb_pronoun_with_invalid_key
764
+ assert_raises(ArgumentError) {@h.fb_pronoun(1234, :posessive => true)}
765
+ end
766
+
767
+ def test_fb_wall
768
+ @h.expects(:capture).returns("wall content")
769
+ @h.fb_wall do
770
+ end
771
+ assert_equal "<fb:wall>wall content</fb:wall>",@h.output_buffer
772
+ end
773
+
774
+ def test_fb_multi_friend_request
775
+ @h.expects(:capture).returns("body")
776
+ @h.expects(:protect_against_forgery?).returns(false)
777
+ @h.expects(:fb_multi_friend_selector).returns("friend selector")
778
+ assert_equal "<fb:request-form action=\"action\" content=\"body\" invite=\"true\" method=\"post\" type=\"invite\">friend selector</fb:request-form>",
779
+ (@h.fb_multi_friend_request("invite","ignored","action") {})
780
+ end
781
+
782
+ def test_fb_multi_friend_request_with_protection_against_forgery
783
+ @h.expects(:capture).returns("body")
784
+ @h.expects(:protect_against_forgery?).returns(true)
785
+ @h.expects(:request_forgery_protection_token).returns('forgery_token')
786
+ @h.expects(:form_authenticity_token).returns('form_token')
787
+
788
+ @h.expects(:fb_multi_friend_selector).returns("friend selector")
789
+ assert_equal "<fb:request-form action=\"action\" content=\"body\" invite=\"true\" method=\"post\" type=\"invite\">friend selector<input name=\"forgery_token\" type=\"hidden\" value=\"form_token\" /></fb:request-form>",
790
+ (@h.fb_multi_friend_request("invite","ignored","action") {})
791
+ end
792
+
793
+ def test_fb_dialog
794
+ @h.expects(:capture).returns("dialog content")
795
+ @h.fb_dialog( "my_dialog", "1" ) do
796
+ end
797
+ assert_equal '<fb:dialog cancel_button="1" id="my_dialog">dialog content</fb:dialog>', @h.output_buffer
798
+ end
799
+ def test_fb_dialog_title
800
+ assert_equal '<fb:dialog-title>My Little Dialog</fb:dialog-title>', @h.fb_dialog_title("My Little Dialog")
801
+ end
802
+ def test_fb_dialog_content
803
+ @h.expects(:capture).returns("dialog content content")
804
+ @h.fb_dialog_content do
805
+ end
806
+ assert_equal '<fb:dialog-content>dialog content content</fb:dialog-content>', @h.output_buffer
807
+ end
808
+ def test_fb_dialog_button
809
+ assert_equal '<fb:dialog-button clickrewriteform="my_form" clickrewriteid="my_dialog" clickrewriteurl="http://www.some_url_here.com/dialog_return.php" type="submit" value="Yes" />',
810
+ @h.fb_dialog_button("submit", "Yes", {:clickrewriteurl => "http://www.some_url_here.com/dialog_return.php",
811
+ :clickrewriteid => "my_dialog", :clickrewriteform => "my_form" } )
812
+ end
813
+
814
+ def test_fb_request_form
815
+ @h.expects(:capture).returns("body")
816
+ @h.expects(:protect_against_forgery?).returns(false)
817
+ assert_equal "<fb:request-form action=\"action\" content=\"Test Param\" invite=\"true\" method=\"post\" type=\"invite\">body</fb:request-form>",
818
+ (@h.fb_request_form("invite","test_param","action") {})
819
+ end
820
+
821
+ def test_fb_request_form_with_protect_against_forgery
822
+ @h.expects(:capture).returns("body")
823
+ @h.expects(:protect_against_forgery?).returns(true)
824
+ @h.expects(:request_forgery_protection_token).returns('forgery_token')
825
+ @h.expects(:form_authenticity_token).returns('form_token')
826
+ assert_equal "<fb:request-form action=\"action\" content=\"Test Param\" invite=\"true\" method=\"post\" type=\"invite\">body<input name=\"forgery_token\" type=\"hidden\" value=\"form_token\" /></fb:request-form>",
827
+ (@h.fb_request_form("invite","test_param","action") {})
828
+ end
829
+
830
+ def test_fb_error_with_only_message
831
+ assert_equal "<fb:error message=\"Errors have occurred!!\" />", @h.fb_error("Errors have occurred!!")
832
+ end
833
+
834
+ def test_fb_error_with_message_and_text
835
+ assert_equal "<fb:error><fb:message>Errors have occurred!!</fb:message>Label can't be blank!!</fb:error>", @h.fb_error("Errors have occurred!!", "Label can't be blank!!")
836
+ end
837
+
838
+ def test_fb_explanation_with_only_message
839
+ assert_equal "<fb:explanation message=\"This is an explanation\" />", @h.fb_explanation("This is an explanation")
840
+ end
841
+
842
+ def test_fb_explanation_with_message_and_text
843
+ assert_equal "<fb:explanation><fb:message>This is an explanation</fb:message>You have a match</fb:explanation>", @h.fb_explanation("This is an explanation", "You have a match")
844
+ end
845
+
846
+ def test_fb_success_with_only_message
847
+ assert_equal "<fb:success message=\"Woot!!\" />", @h.fb_success("Woot!!")
848
+ end
849
+
850
+ def test_fb_success_with_message_and_text
851
+ assert_equal "<fb:success><fb:message>Woot!!</fb:message>You Rock!!</fb:success>", @h.fb_success("Woot!!", "You Rock!!")
852
+ end
853
+
854
+ def test_facebook_form_for
855
+ @h.expects(:protect_against_forgery?).returns(false)
856
+ form_body=@h.facebook_form_for(:model,:url=>"action") do
857
+ end
858
+ assert_equal "<fb:editor action=\"action\"></fb:editor>",form_body
859
+ end
860
+
861
+ def test_facebook_form_for_with_authenticity_token
862
+ @h.expects(:protect_against_forgery?).returns(true)
863
+ @h.expects(:request_forgery_protection_token).returns('forgery_token')
864
+ @h.expects(:form_authenticity_token).returns('form_token')
865
+ assert_equal "<fb:editor action=\"action\"><input name=\"forgery_token\" type=\"hidden\" value=\"form_token\" /></fb:editor>",
866
+ (@h.facebook_form_for(:model, :url => "action") {})
867
+ end
868
+
869
+ def test_fb_friend_selector
870
+ assert_equal("<fb:friend-selector />",@h.fb_friend_selector)
871
+ end
872
+
873
+ def test_fb_request_form_submit
874
+ assert_equal("<fb:request-form-submit />",@h.fb_request_form_submit)
875
+ end
876
+
877
+ def test_fb_request_form_submit_with_uid
878
+ assert_equal("<fb:request-form-submit uid=\"123456789\" />",@h.fb_request_form_submit({:uid => "123456789"}))
879
+ end
880
+
881
+ def test_fb_request_form_submit_with_label
882
+ assert_equal("<fb:request-form-submit label=\"Send Invite to Joel\" />",@h.fb_request_form_submit({:label => "Send Invite to Joel"}))
883
+ end
884
+
885
+ def test_fb_request_form_submit_with_uid_and_label
886
+ assert_equal("<fb:request-form-submit label=\"Send Invite to Joel\" uid=\"123456789\" />",@h.fb_request_form_submit({:uid =>"123456789", :label => "Send Invite to Joel"}))
887
+ end
888
+
889
+ def test_fb_action
890
+ assert_equal "<fb:action href=\"/growingpets/rub\">Rub my pet</fb:action>", @h.fb_action("Rub my pet", "/growingpets/rub")
891
+ end
892
+
893
+ def test_fb_help
894
+ assert_equal "<fb:help href=\"http://www.facebook.com/apps/application.php?id=6236036681\">Help</fb:help>", @h.fb_help("Help", "http://www.facebook.com/apps/application.php?id=6236036681")
895
+ end
896
+
897
+ def test_fb_create_button
898
+ assert_equal "<fb:create-button href=\"/growingpets/invite\">Invite Friends</fb:create-button>", @h.fb_create_button('Invite Friends', '/growingpets/invite')
899
+ end
900
+ def test_fb_comments
901
+ assert_equal "<fb:comments candelete=\"false\" canpost=\"true\" numposts=\"4\" optional=\"false\" xid=\"xxx\"></fb:comments>", @h.fb_comments("xxx",true,false,4,:optional=>false)
902
+ end
903
+ def test_fb_comments_with_title
904
+ assert_equal "<fb:comments candelete=\"false\" canpost=\"true\" numposts=\"4\" optional=\"false\" xid=\"xxx\"><fb:title>TITLE</fb:title></fb:comments>", @h.fb_comments("xxx",true,false,4,:optional=>false, :title => "TITLE")
905
+ end
906
+ def test_fb_board
907
+ assert_equal "<fb:board optional=\"false\" xid=\"xxx\"></fb:board>", @h.fb_board("xxx",:optional => false)
908
+ end
909
+ def test_fb_board_with_title
910
+ assert_equal "<fb:board optional=\"false\" xid=\"xxx\"><fb:title>TITLE</fb:title></fb:board>", @h.fb_board("xxx",:optional=>false, :title => "TITLE")
911
+ end
912
+
913
+ def test_fb_dashboard
914
+ @h.expects(:capture).returns("dashboard content")
915
+ @h.fb_dashboard do
916
+ end
917
+ assert_equal "<fb:dashboard>dashboard content</fb:dashboard>", @h.output_buffer
918
+ end
919
+ def test_fb_dashboard_non_block
920
+ assert_equal "<fb:dashboard></fb:dashboard>", @h.fb_dashboard
921
+ end
922
+
923
+ def test_fb_wide
924
+ @h.expects(:capture).returns("wide profile content")
925
+ @h.fb_wide do
926
+ end
927
+ assert_equal "<fb:wide>wide profile content</fb:wide>", @h.output_buffer
928
+ end
929
+
930
+ def test_fb_narrow
931
+ @h.expects(:capture).returns("narrow profile content")
932
+ @h.fb_narrow do
933
+ end
934
+ assert_equal "<fb:narrow>narrow profile content</fb:narrow>", @h.output_buffer
935
+ end
936
+
937
+ def test_fb_login_button
938
+ assert_equal "<fb:login-button onlogin=\"somejs\"></fb:login-button>",@h.fb_login_button("somejs")
939
+ end
940
+
941
+ def test_init_fb_connect_no_features
942
+ assert ! @h.init_fb_connect.match(/XFBML/)
943
+ end
944
+
945
+ def test_init_fb_connect_with_features
946
+ assert @h.init_fb_connect("XFBML").match(/XFBML/)
947
+ end
948
+
949
+ def test_init_fb_connect_with_features_and_body
950
+ @h.expects(:capture).returns("Body Content")
951
+
952
+ @h.init_fb_connect("XFBML") do
953
+ end
954
+ assert @h.output_buffer =~ /Body Content/
955
+ end
956
+
957
+ def test_fb_login_and_redirect
958
+ assert_equal @h.fb_login_and_redirect("/path"),"<fb:login-button onlogin=\"window.location.href = &quot;/path&quot;;\"></fb:login-button>"
959
+ end
960
+
961
+ def test_fb_logout_link
962
+ assert_equal @h.fb_logout_link("Logout","My URL"),"<a href=\"#\" onclick=\"FB.Connect.logoutAndRedirect(&quot;My URL&quot;);; return false;\">Logout</a>"
963
+ end
964
+
965
+
966
+ def test_fb_connect_javascript_tag
967
+ assert_equal "<script src=\"http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php\" type=\"text/javascript\"></script>",
968
+ @h.fb_connect_javascript_tag
969
+ end
970
+
971
+ def test_fb_container
972
+ @h.expects(:capture).returns("Inner Stuff")
973
+ @h.fb_container(:condition=>"somejs") do
974
+ end
975
+ assert_equal "<fb:container condition=\"somejs\">Inner Stuff</fb:container>",@h.output_buffer
976
+ end
977
+
978
+ def test_fb_eventlink
979
+ assert_equal '<fb:eventlink eid="21150032416"></fb:eventlink>',@h.fb_eventlink("21150032416")
980
+ end
981
+
982
+ def test_fb_grouplink
983
+ assert_equal '<fb:grouplink gid="2541896821"></fb:grouplink>',@h.fb_grouplink("2541896821")
984
+ end
985
+
986
+ def test_fb_serverfbml
987
+ @h.expects(:capture).returns("Inner Stuff")
988
+ @h.fb_serverfbml(:condition=>"somejs") do
989
+ end
990
+ assert_equal "<fb:serverfbml condition=\"somejs\">Inner Stuff</fb:serverfbml>",@h.output_buffer
991
+ end
992
+
993
+ def test_fb_share_button
994
+ assert_equal "<fb:share-button class=\"url\" href=\"http://www.elevatedrails.com\"></fb:share-button>",@h.fb_share_button("http://www.elevatedrails.com")
995
+ end
996
+
997
+ def test_fb_unconnected_friends_count_without_condition
998
+ assert_equal "<fb:unconnected-friends-count></fb:unconnected-friends-count>",@h.fb_unconnected_friends_count
999
+ end
1000
+
1001
+ def test_fb_user_status
1002
+ user=flexmock("user", :facebook_id => "5678")
1003
+ assert_equal '<fb:user-status linked="false" uid="5678"></fb:user-status>',@h.fb_user_status(user,false)
1004
+ end
1005
+
1006
+ def test_fb_time
1007
+ time = Time.now
1008
+ assert_equal %Q{<fb:time preposition="true" t="#{time.to_i}" tz="America/New York" />}, @h.fb_time(time, :tz => 'America/New York', :preposition => true)
1009
+ end
1010
+
1011
+ def test_fb_time_defaults
1012
+ time = Time.now
1013
+ assert_equal %Q{<fb:time t="#{time.to_i}" />}, @h.fb_time(time)
1014
+ end
1015
+ end
1016
+ class TestModel
1017
+ attr_accessor :name,:facebook_id
1018
+ end
1019
+
1020
+ class RailsFacebookFormbuilderTest < Test::Unit::TestCase
1021
+ class TestTemplate
1022
+ include ActionView::Helpers::TextHelper
1023
+ include ActionView::Helpers::CaptureHelper
1024
+ include ActionView::Helpers::TagHelper
1025
+ include Facebooker::Rails::Helpers
1026
+ attr_accessor :output_buffer
1027
+ def initialize
1028
+ @output_buffer=""
1029
+ end
1030
+ end
1031
+ def setup
1032
+ @_erbout = ""
1033
+ @test_model = TestModel.new
1034
+ @test_model.name="Mike"
1035
+ @template = TestTemplate.new
1036
+ @proc = Proc.new {}
1037
+ @form_builder = Facebooker::Rails::FacebookFormBuilder.new(:test_model,@test_model,@template,{},@proc)
1038
+ def @form_builder._erbout
1039
+ ""
1040
+ end
1041
+
1042
+ end
1043
+
1044
+ def test_text_field
1045
+ assert_equal "<fb:editor-text id=\"test_model_name\" label=\"Name\" name=\"test_model[name]\" value=\"Mike\"></fb:editor-text>",
1046
+ @form_builder.text_field(:name)
1047
+ end
1048
+ def test_text_area
1049
+ assert_equal "<fb:editor-textarea id=\"test_model_name\" label=\"Name\" name=\"test_model[name]\">Mike</fb:editor-textarea>",
1050
+ @form_builder.text_area(:name)
1051
+ end
1052
+
1053
+ def test_default_name_and_id
1054
+ assert_equal "<fb:editor-text id=\"different_id\" label=\"Name\" name=\"different_name\" value=\"Mike\"></fb:editor-text>",
1055
+ @form_builder.text_field(:name, {:name => 'different_name', :id => 'different_id'})
1056
+ end
1057
+
1058
+ def test_collection_typeahead
1059
+ flexmock(@form_builder) do |fb|
1060
+ fb.should_receive(:collection_typeahead_internal).with(:name,["ABC"],:size,:to_s,{})
1061
+ end
1062
+ @form_builder.collection_typeahead(:name,["ABC"],:size,:to_s)
1063
+ end
1064
+
1065
+ def test_collection_typeahead_internal
1066
+ assert_equal "<fb:typeahead-input id=\"test_model_name\" name=\"test_model[name]\" value=\"Mike\"><fb:typeahead-option value=\"3\">ABC</fb:typeahead-option></fb:typeahead-input>",
1067
+ @form_builder.collection_typeahead_internal(:name,["ABC"],:size,:to_s)
1068
+ end
1069
+
1070
+ def test_buttons
1071
+ @form_builder.expects(:create_button).with(:first).returns("first")
1072
+ @form_builder.expects(:create_button).with(:second).returns("second")
1073
+ @template.expects(:content_tag).with("fb:editor-buttonset","firstsecond")
1074
+ @form_builder.buttons(:first,:second)
1075
+ end
1076
+
1077
+ def test_create_button
1078
+ assert_equal "<fb:editor-button name=\"commit\" value=\"first\"></fb:editor-button>",@form_builder.create_button(:first)
1079
+ end
1080
+
1081
+ def test_custom
1082
+ @template.expects(:password_field).returns("password_field")
1083
+ assert_equal "<fb:editor-custom label=\"Name\">password_field</fb:editor-custom>",@form_builder.password_field(:name)
1084
+ end
1085
+
1086
+ def test_text
1087
+ assert_equal "<fb:editor-custom label=\"custom\">Mike</fb:editor-custom>",@form_builder.text("Mike",:label=>"custom")
1088
+ end
1089
+
1090
+ def test_multi_friend_input
1091
+ assert_equal "<fb:editor-custom label=\"Friends\"><fb:multi-friend-input></fb:multi-friend-input></fb:editor-custom>",@form_builder.multi_friend_input
1092
+ end
1093
+
1094
+
1095
+ end
1096
+
1097
+ class RailsPrettyErrorsTest < Test::Unit::TestCase
1098
+ class ControllerWhichFails < ActionController::Base
1099
+ def pass
1100
+ render :text=>''
1101
+ end
1102
+ def fail
1103
+ raise "I'm failing"
1104
+ end
1105
+ end
1106
+
1107
+ def setup
1108
+ @controller = ControllerWhichFails.new
1109
+ @request = ActionController::TestRequest.new
1110
+ @response = ActionController::TestResponse.new
1111
+ end
1112
+
1113
+ def test_pretty_errors_disabled_success
1114
+ Facebooker.facebooker_config.stubs(:[]).with('pretty_errors').returns(false)
1115
+ post :pass, facebook_params
1116
+ assert_response 200
1117
+ end
1118
+
1119
+ def test_pretty_errors_disabled_error
1120
+ Facebooker.facebooker_config.stubs(:[]).with('pretty_errors').returns(false)
1121
+ post :fail, facebook_params
1122
+ assert_response :error
1123
+ end
1124
+
1125
+ def test_pretty_errors_enabled_success
1126
+ Facebooker.facebooker_config.stubs(:[]).with('pretty_errors').returns(true)
1127
+ post :pass, facebook_params
1128
+ assert_response 200
1129
+ end
1130
+ def test_pretty_errors_enabled_error
1131
+ Facebooker.facebooker_config.stubs(:[]).with('pretty_errors').returns(true)
1132
+ post :fail, facebook_params
1133
+ assert_response 200
1134
+ end
1135
+ end
1136
+
1137
+ class RailsUrlHelperExtensionsTest < Test::Unit::TestCase
1138
+ class UrlHelperExtensionsClass
1139
+ include ActionView::Helpers::UrlHelper
1140
+ include ActionView::Helpers::TagHelper
1141
+ def initialize(controller)
1142
+ @controller = controller
1143
+ end
1144
+
1145
+ def protect_against_forgery?
1146
+ false
1147
+ end
1148
+
1149
+ def request_comes_from_facebook?
1150
+ @request_comes_from_facebook
1151
+ end
1152
+
1153
+ def request_comes_from_facebook=(val)
1154
+ @request_comes_from_facebook = val
1155
+ end
1156
+
1157
+ end
1158
+ class UrlHelperExtensionsController < NoisyController
1159
+ def index
1160
+ render :nothing => true
1161
+ end
1162
+ def do_it
1163
+ render :nothing => true
1164
+ end
1165
+ end
1166
+
1167
+ class FacebookRequest < ActionController::TestRequest
1168
+ end
1169
+
1170
+ def setup
1171
+ @controller = UrlHelperExtensionsController.new
1172
+ @request = FacebookRequest.new
1173
+ @response = ActionController::TestResponse.new
1174
+
1175
+ @u = UrlHelperExtensionsClass.new(@controller)
1176
+ @u.request_comes_from_facebook = true
1177
+
1178
+ @non_canvas_u = UrlHelperExtensionsClass.new(@controller)
1179
+ @non_canvas_u.request_comes_from_facebook = false
1180
+
1181
+ @label = "Testing"
1182
+ @url = "test.host"
1183
+ @prompt = "Are you sure?"
1184
+ @default_title = "Please Confirm"
1185
+ @title = "Confirm Request"
1186
+ @style = {:color => 'black', :background => 'white'}
1187
+ @verbose_style = "{background: 'white', color: 'black'}"
1188
+ @default_okay = "Okay"
1189
+ @default_cancel = "Cancel"
1190
+ @default_style = "" #"'width','200px'"
1191
+ end
1192
+
1193
+ def test_link_to
1194
+ assert_equal "<a href=\"#{@url}\">Testing</a>", @u.link_to(@label, @url)
1195
+ end
1196
+
1197
+ def test_link_to_with_popup
1198
+ assert_raises(ActionView::ActionViewError) {@u.link_to(@label,@url, :popup=>true)}
1199
+ end
1200
+
1201
+ def test_link_to_with_confirm
1202
+ assert_dom_equal( "<a href=\"#{@url}\" onclick=\"var dlg = new Dialog().showChoice(\'#{@default_title}\',\'#{@prompt}\',\'#{@default_okay}\',\'#{@default_cancel}\').setStyle(#{@default_style});"+
1203
+ "var a=this;dlg.onconfirm = function() { " +
1204
+ "document.setLocation(a.getHref()); };return false;\">#{@label}</a>",
1205
+ @u.link_to(@label, @url, :confirm => @prompt) )
1206
+ end
1207
+ def test_link_to_with_confirm_with_title
1208
+ assert_dom_equal( "<a href=\"#{@url}\" onclick=\"var dlg = new Dialog().showChoice(\'#{@title}\',\'#{@prompt}\',\'#{@default_okay}\',\'#{@default_cancel}\').setStyle(#{@default_style});"+
1209
+ "var a=this;dlg.onconfirm = function() { " +
1210
+ "document.setLocation(a.getHref()); };return false;\">#{@label}</a>",
1211
+ @u.link_to(@label, @url, :confirm => {:title=>@title,:content=>@prompt}) )
1212
+ end
1213
+ def test_link_to_with_confirm_with_title_and_style
1214
+ assert_dom_equal( "<a href=\"#{@url}\" onclick=\"var dlg = new Dialog().showChoice(\'#{@title}\',\'#{@prompt}\',\'#{@default_okay}\',\'#{@default_cancel}\').setStyle(#{@verbose_style});"+
1215
+ "var a=this;dlg.onconfirm = function() { " +
1216
+ "document.setLocation(a.getHref()); };return false;\">#{@label}</a>",
1217
+ @u.link_to(@label, @url, :confirm => {:title=>@title,:content=>@prompt}.merge!(@style)) )
1218
+ end
1219
+
1220
+ def test_link_to_with_method
1221
+ assert_dom_equal( "<a href=\"#{@url}\" onclick=\"var a=this;var f = document.createElement('form'); f.setStyle('display','none'); "+
1222
+ "a.getParentNode().appendChild(f); f.setMethod('POST'); f.setAction(a.getHref());" +
1223
+ "var m = document.createElement('input'); m.setType('hidden'); "+
1224
+ "m.setName('_method'); m.setValue('delete'); f.appendChild(m);"+
1225
+ "f.submit();return false;\">#{@label}</a>", @u.link_to(@label,@url, :method=>:delete))
1226
+ end
1227
+
1228
+ def test_link_to_with_confirm_and_method
1229
+ assert_dom_equal( "<a href=\"#{@url}\" onclick=\"var dlg = new Dialog().showChoice(\'#{@default_title}\',\'#{@prompt}\',\'#{@default_okay}\',\'#{@default_cancel}\').setStyle(#{@default_style});"+
1230
+ "var a=this;dlg.onconfirm = function() { " +
1231
+ "var f = document.createElement('form'); f.setStyle('display','none'); "+
1232
+ "a.getParentNode().appendChild(f); f.setMethod('POST'); f.setAction(a.getHref());" +
1233
+ "var m = document.createElement('input'); m.setType('hidden'); "+
1234
+ "m.setName('_method'); m.setValue('delete'); f.appendChild(m);"+
1235
+ "f.submit(); };return false;\">#{@label}</a>", @u.link_to(@label,@url, :confirm=>@prompt, :method=>:delete) )
1236
+ end
1237
+ def test_link_to_with_confirm_and_method_for_non_canvas_page
1238
+ assert_dom_equal( "<a href=\"#{@url}\" onclick=\"if (confirm(\'#{@prompt}\')) { var f = document.createElement('form'); f.style.display = 'none'; "+
1239
+ "this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;var m = document.createElement('input'); "+
1240
+ "m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method'); m.setAttribute('value', 'delete'); "+
1241
+ "f.appendChild(m);f.submit(); };return false;\">#{@label}</a>",
1242
+ @non_canvas_u.link_to(@label,@url, :confirm=>@prompt, :method=>:delete) )
1243
+ end
1244
+
1245
+ def test_button_to
1246
+ assert_equal "<form method=\"post\" action=\"#{@url}\" class=\"button-to\"><div>" +
1247
+ "<input type=\"submit\" value=\"#{@label}\" /></div></form>", @u.button_to(@label,@url)
1248
+ end
1249
+
1250
+ def test_button_to_with_confirm
1251
+ assert_equal "<form method=\"post\" action=\"#{@url}\" class=\"button-to\"><div>" +
1252
+ "<input onclick=\"var dlg = new Dialog().showChoice(\'#{@default_title}\',\'#{@prompt}\',\'#{@default_okay}\',\'#{@default_cancel}\').setStyle(#{@default_style});"+
1253
+ "var a=this;dlg.onconfirm = function() { "+
1254
+ "a.getForm().submit(); };return false;\" type=\"submit\" value=\"#{@label}\" /></div></form>",
1255
+ @u.button_to(@label,@url, :confirm=>@prompt)
1256
+ end
1257
+ def test_button_to_with_confirm_for_non_canvas_page
1258
+ assert_equal "<form method=\"post\" action=\"#{@url}\" class=\"button-to\"><div>"+
1259
+ "<input onclick=\"return confirm(\'#{@prompt}\');\" type=\"submit\" value=\"#{@label}\" /></div></form>",
1260
+ @non_canvas_u.button_to(@label,@url, :confirm=>@prompt)
1261
+ end
1262
+
1263
+ def test_link_to_unless_with_true
1264
+ assert_equal @label, @u.link_to_unless(true,@label,@url)
1265
+ end
1266
+ def test_link_to_unless_with_false
1267
+ assert_equal @u.link_to(@label,@url), @u.link_to_unless(false,@label,@url)
1268
+ end
1269
+
1270
+ def test_link_to_if_with_true
1271
+ assert_equal @u.link_to(@label,@url), @u.link_to_if(true,@label,@url)
1272
+ end
1273
+ def test_link_to_if_with_false
1274
+ assert_equal @label, @u.link_to_if(false,@label,@url)
1275
+ end
1276
+
1277
+ end
1278
+
1279
+ class RailsRequestFormatTest < Test::Unit::TestCase
1280
+ class FacebookController < NoisyController
1281
+ def index
1282
+ respond_to do |format|
1283
+ format.html { render :text => 'html' }
1284
+ format.fbml { render :text => 'fbml' }
1285
+ format.fbjs { render :text => 'fbjs' }
1286
+ end
1287
+ end
1288
+ end
1289
+
1290
+ def setup
1291
+ @controller = FacebookController.new
1292
+ @request = ActionController::TestRequest.new
1293
+ @response = ActionController::TestResponse.new
1294
+ @controller.stubs(:verify_signature).returns(true)
1295
+ end
1296
+
1297
+ def test_responds_to_html_without_canvas
1298
+ get :index
1299
+ assert_equal 'html', @response.body
1300
+ end
1301
+
1302
+ def test_responds_to_fbml_in_canvas
1303
+ get :index, facebook_params(:fb_sig_in_canvas => '1')
1304
+ assert_equal 'fbml', @response.body
1305
+ end
1306
+
1307
+ def test_responds_to_fbjs_when_is_ajax
1308
+ get :index, facebook_params(:fb_sig_is_ajax => '1')
1309
+ assert_equal 'fbjs', @response.body
1310
+ end
1311
+
1312
+ end