facebooker-lite 1.0.67

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