1 require File.expand_path(File.dirname(__FILE__) + '/../rails_test_helper')
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)}
8 def expired_cookie_hash_for_auth
9 {"_ss" => "not_used", "_session_key"=> "whatever", "_user"=>"77777", "_expires"=>"#{1.day.ago.to_i}"}
12 def cookie_hash_for_auth
13 {"_ss" => "not_used", "_session_key"=> "whatever", "_user"=>"77777", "_expires"=>"#{1.day.from_now.to_i}"}
16 class NoisyController < ActionController::Base
17 include Facebooker::Rails::Controller
18 def rescue_action(e) raise e end
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
29 render :text => 'score!'
33 class FBConnectController < NoisyController
34 before_filter :create_facebook_session
36 render :text => 'score!'
40 class FBConnectControllerProxy < NoisyController
41 before_filter :create_facebook_session_with_secret
43 render :text => 'score!'
47 class ControllerWhichRequiresFacebookAuthentication < NoisyController
48 ensure_authenticated_to_facebook
50 render :text => 'score!'
54 options[:canvas] = true if params[:canvas] == true
55 options[:canvas] = false if params[:canvas] == false
56 render :text=>url_for(options)
60 render :text=>comments_url()
64 render :inline=>"<%=image_tag 'image.png'%>"
68 render :text=>facebook_params['user']
73 render :text=>"interface"
75 render :text=>"not interface"
79 def publisher_test_interface
80 render_publisher_interface("This is a test",false,true)
83 def publisher_test_response
84 ua=Facebooker::Rails::Publisher::UserAction.new
85 ua.data = {:params=>true}
86 ua.template_name = "template_name"
88 render_publisher_response(ua)
91 def publisher_test_error
92 render_publisher_error("Title","Body")
96 class ControllerWhichRequiresApplicationInstallation < NoisyController
97 ensure_application_is_installed_by_facebook_user
99 render :text => 'installed!'
102 class FacebookController < ActionController::Base
107 class PlainOldRailsController < ActionController::Base
112 options[:canvas] = true if params[:canvas] == true
113 options[:canvas] = false if params[:canvas] == false
114 render :text => url_for(options)
118 render :text=>comments_url()
120 def canvas_false_test
121 render :text=>comments_url(:canvas=>false)
124 render :text=>comments_url(:canvas=>true)
128 class Test::Unit::TestCase
129 include Facebooker::Rails::TestHelpers
133 # you can't use asset_recognize, because it can't pass parameters in to the requests
134 class UrlRecognitionTests < Test::Unit::TestCase
135 def test_detects_in_canvas
136 if Rails.version < '2.3'
137 request = ActionController::TestRequest.new({"fb_sig_in_canvas"=>"1"}, {}, nil)
139 request = ActionController::TestRequest.new
140 request.query_parameters[:fb_sig_in_canvas] = "1"
143 ActionController::Routing::Routes.recognize(request)
144 assert_equal({"controller"=>"facebook","action"=>"index"},request.path_parameters)
147 def test_routes_when_not_in_canvas
148 if Rails.version < '2.3'
149 request = ActionController::TestRequest.new({}, {}, nil)
151 request = ActionController::TestRequest.new
154 ActionController::Routing::Routes.recognize(request)
155 assert_equal({"controller"=>"plain_old_rails","action"=>"index"},request.path_parameters)
159 class RailsIntegrationTestForFBConnect < Test::Unit::TestCase
160 include FBConnectTestHelpers
163 ENV['FACEBOOK_CANVAS_PATH'] ='facebook_app_name'
164 ENV['FACEBOOK_API_KEY'] = '1234567'
165 ENV['FACEBOOK_SECRET_KEY'] = '7654321'
166 @controller = FBConnectController.new
167 @request = ActionController::TestRequest.new
168 @response = ActionController::TestResponse.new
169 @controller.stubs(:verify_signature).returns(true)
173 def test_doesnt_set_cookie_but_facebook_session_is_available
174 setup_fb_connect_cookies
176 assert_not_nil @controller.facebook_session
177 assert_nil @response.cookies[:facebook_session]
182 class RailsIntegrationTestForNonFacebookControllers < Test::Unit::TestCase
184 ENV['FACEBOOK_CANVAS_PATH'] ='facebook_app_name'
185 ENV['FACEBOOK_API_KEY'] = '1234567'
186 ENV['FACEBOOK_SECRET_KEY'] = '7654321'
187 @controller = PlainOldRailsController.new
188 @request = ActionController::TestRequest.new
189 @response = ActionController::TestResponse.new
192 def test_url_for_links_to_callback_if_canvas_is_false_and_in_canvas
194 assert_match(/test.host/, @response.body)
197 def test_named_route_doesnt_include_canvas_path_when_not_in_canvas
198 get :named_route_test
199 assert_equal "http://test.host/comments",@response.body
201 def test_named_route_includes_canvas_path_when_in_canvas
202 get :named_route_test, facebook_params
203 assert_equal "http://apps.facebook.com/facebook_app_name/comments",@response.body
206 def test_named_route_doesnt_include_canvas_path_when_in_canvas_with_canvas_equals_false
207 get :canvas_false_test, facebook_params
208 assert_equal "http://test.host/comments",@response.body
210 def test_named_route_does_include_canvas_path_when_not_in_canvas_with_canvas_equals_true
211 get :canvas_true_test
212 assert_equal "http://apps.facebook.com/facebook_app_name/comments",@response.body
217 class RailsIntegrationTestForExtendedPermissions < Test::Unit::TestCase
219 ENV['FACEBOOK_API_KEY'] = '1234567'
220 ENV['FACEBOOK_SECRET_KEY'] = '7654321'
221 @controller = ControllerWhichRequiresExtendedPermissions.new
222 @request = ActionController::TestRequest.new
223 @response = ActionController::TestResponse.new
224 @controller.stubs(:verify_signature).returns(true)
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)
232 def test_redirects_without_photo_upload
233 post :index, facebook_params(:fb_sig_ext_perms=>"status_update")
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)
237 def test_redirects_without_video_upload
238 post :index, facebook_params(:fb_sig_ext_perms=>"status_update,photo_upload")
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)
242 def test_redirects_without_create_listing
243 post :index, facebook_params(:fb_sig_ext_perms=>"status_update,photo_upload,video_upload")
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)
248 def test_renders_with_permission
249 post :index, facebook_params(:fb_sig_ext_perms=>"status_update,photo_upload,create_listing,video_upload")
250 assert_response :success
251 assert_equal("score!", @response.body)
256 class RailsIntegrationTestForApplicationInstallation < Test::Unit::TestCase
258 ENV['FACEBOOK_API_KEY'] = '1234567'
259 ENV['FACEBOOK_SECRET_KEY'] = '7654321'
260 @controller = ControllerWhichRequiresApplicationInstallation.new
261 @request = ActionController::TestRequest.new
262 @response = ActionController::TestResponse.new
263 @controller.stubs(:verify_signature).returns(true)
266 def test_if_controller_requires_application_installation_unauthenticated_requests_will_redirect_to_install_page
268 assert_response :redirect
269 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'])
272 def test_if_controller_requires_application_installation_authenticated_requests_without_installation_will_redirect_to_install_page
273 get :index, facebook_params(:fb_sig_added => nil)
274 assert_response :success
275 assert(@response.body =~ /fb:redirect/)
278 def test_if_controller_requires_application_installation_authenticated_requests_with_installation_will_render
279 get :index, facebook_params('fb_sig_added' => "1")
280 assert_response :success
281 assert_equal("installed!", @response.body)
285 class RailsIntegrationTest < Test::Unit::TestCase
286 include FBConnectTestHelpers
288 ENV['FACEBOOK_CANVAS_PATH'] ='root'
289 ENV['FACEBOOK_API_KEY'] = '1234567'
290 ENV['FACEBOOK_SECRET_KEY'] = '7654321'
291 ActionController::Base.asset_host="http://root.example.com"
292 @controller = ControllerWhichRequiresFacebookAuthentication.new
293 @request = ActionController::TestRequest.new
294 @response = ActionController::TestResponse.new
295 @controller.stubs(:verify_signature).returns(true)
299 def test_named_route_includes_new_canvas_path_when_in_new_canvas
300 get :named_route_test, facebook_params("fb_sig_in_new_facebook"=>"1")
301 assert_equal "http://apps.facebook.com/root/comments",@response.body
304 def test_if_controller_requires_facebook_authentication_unauthenticated_requests_will_redirect
306 assert_response :redirect
307 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'])
310 def test_facebook_params_are_parsed_into_a_separate_hash
311 get :index, facebook_params(:fb_sig_user => '9')
312 assert_not_nil @controller.facebook_params['time']
315 def test_facebook_params_convert_in_canvas_to_boolean
316 get :index, facebook_params
317 assert_equal(true, @controller.facebook_params['in_canvas'])
320 def test_facebook_params_convert_added_to_boolean_false
321 get :index, facebook_params(:fb_sig_added => '0')
322 assert_equal(false, @controller.facebook_params['added'])
325 def test_facebook_params_convert_added_to_boolean_true
326 get :index, facebook_params('fb_sig_added' => "1")
327 assert_equal(true, @controller.facebook_params['added'])
330 def test_facebook_params_convert_expirey_into_nil
331 get :index, facebook_params(:fb_sig_expires => '0')
332 assert_nil(@controller.facebook_params['expires'])
335 def test_facebook_params_convert_expirey_into_time
336 get :index, facebook_params(:fb_sig_expires => 5.minutes.from_now.to_f)
337 assert_instance_of Time, @controller.facebook_params['expires']
340 def test_facebook_params_convert_friend_list_to_parsed_array_of_friend_ids
341 get :index, facebook_params(:fb_sig_friends => '1,2,3,4,5')
342 assert_kind_of(Array, @controller.facebook_params['friends'])
343 assert_equal(5, @controller.facebook_params['friends'].size)
346 def test_session_can_be_resecured_from_facebook_params
347 get :index, facebook_params(:fb_sig_user => 10)
348 assert_equal(10, @controller.facebook_session.user.id)
351 def test_existing_secured_session_is_used_if_available
352 session = Facebooker::Session.create(Facebooker::Session.api_key, Facebooker::Session.secret_key)
353 session.secure_with!("session_key", "111", Time.now.to_i + 60)
354 get :index, facebook_params(:fb_sig_session_key => 'session_key', :fb_sig_user => '987'), {:facebook_session => session}
355 assert_equal(111, @controller.facebook_session.user.id)
358 def test_facebook_params_used_if_existing_secured_session_key_does_not_match
359 session = Facebooker::Session.create(Facebooker::Session.api_key, Facebooker::Session.secret_key)
360 session.secure_with!("different session key", "111", Time.now.to_i + 60)
361 get :index, facebook_params(:fb_sig_session_key => '', :fb_sig_user => '123'), {:facebook_session => session}
362 assert_equal(123, @controller.facebook_session.user.id)
365 def test_existing_secured_session_is_NOT_used_if_available_and_facebook_params_session_key_is_nil_and_in_canvas
366 session = Facebooker::Session.create(Facebooker::Session.api_key, Facebooker::Session.secret_key)
367 session.secure_with!("session_key", "111", Time.now.to_i + 60)
368 session.secure_with!("a session key", "1111111", Time.now.to_i + 60)
369 get :index, facebook_params(:fb_sig_session_key => '', :fb_sig_user => '987'), {:facebook_session => session}
370 assert_equal(987, @controller.facebook_session.user.id)
373 def test_existing_secured_session_IS_used_if_available_and_facebook_params_session_key_is_nil_and_NOT_in_canvas
374 @contoller = PlainOldRailsController.new
375 session = Facebooker::Session.create(ENV['FACEBOOK_API_KEY'], ENV['FACEBOOK_SECRET_KEY'])
376 session.secure_with!("a session key", "1111111", Time.now.to_i + 60)
377 get :index,{}, {:facebook_session => session}
379 assert_equal(1111111, @controller.facebook_session.user.id)
382 def test_session_can_be_secured_with_secret
383 @controller = FBConnectControllerProxy.new
384 auth_token = 'ohaiauthtokenhere111'
385 modified_params = facebook_params
386 modified_params.delete('fb_sig_session_key')
387 modified_params['auth_token'] = auth_token
388 modified_params['generate_session_secret'] = true
389 session_mock = flexmock(session = Facebooker::Session.create(ENV['FACEBOOK_API_KEY'], ENV['FACEBOOK_SECRET_KEY']))
390 session_params = { 'session_key' => '123', 'uid' => '321' }
391 session_mock.should_receive(:post).with('facebook.auth.getSession', :auth_token => auth_token, :generate_session_secret => "1").once.and_return(session_params).ordered
392 flexmock(@controller).should_receive(:new_facebook_session).once.and_return(session).ordered
393 get :index, modified_params
396 def test_session_can_be_secured_with_auth_token
397 auth_token = 'ohaiauthtokenhere111'
398 modified_params = facebook_params
399 modified_params.delete('fb_sig_session_key')
400 modified_params['auth_token'] = auth_token
401 session_mock = flexmock(session = Facebooker::Session.create(ENV['FACEBOOK_API_KEY'], ENV['FACEBOOK_SECRET_KEY']))
402 session_params = { 'session_key' => '123', 'uid' => '321' }
403 session_mock.should_receive(:post).with('facebook.auth.getSession', :auth_token => auth_token, :generate_session_secret => "0").once.and_return(session_params).ordered
404 flexmock(@controller).should_receive(:new_facebook_session).once.and_return(session).ordered
405 get :index, modified_params
408 def test_session_secured_with_auth_token_if_cookies_expired
409 auth_token = 'ohaiauthtokenhere111'
410 modified_params = facebook_params
411 modified_params.delete('fb_sig_session_key')
412 modified_params['auth_token'] = auth_token
413 session_mock = flexmock(session = Facebooker::Session.create(ENV['FACEBOOK_API_KEY'], ENV['FACEBOOK_SECRET_KEY']))
414 session_params = { 'session_key' => '123', 'uid' => '321' }
415 session_mock.should_receive(:post).with('facebook.auth.getSession', :auth_token => auth_token, :generate_session_secret => "0").once.and_return(session_params).ordered
416 flexmock(@controller).should_receive(:new_facebook_session).once.and_return(session).ordered
417 setup_fb_connect_cookies(expired_cookie_hash_for_auth)
418 get :index, modified_params
419 assert_equal(321, @controller.facebook_session.user.id)
422 def test_session_can_be_secured_with_cookies
423 setup_fb_connect_cookies
425 assert_equal(77777, @controller.facebook_session.user.id)
428 def test_session_does_NOT_secure_with_expired_cookies
429 setup_fb_connect_cookies(expired_cookie_hash_for_auth)
431 assert_nil(@controller.facebook_session)
434 def test_user_friends_can_be_populated_from_facebook_params_if_available
435 get :index, facebook_params(:fb_sig_friends => '1,2,3,4')
436 friends = @controller.facebook_session.user.friends
437 assert_not_nil(friends)
438 assert_equal(4, friends.size)
441 def test_fbml_redirect_tag_handles_hash_parameters_correctly
442 get :index, facebook_params
443 assert_equal "<fb:redirect url=\"http://apps.facebook.com/root/require_auth\" />", @controller.send(:fbml_redirect_tag, :action => :index,:canvas=>true)
446 def test_redirect_to_renders_fbml_redirect_tag_if_request_is_for_a_facebook_canvas
447 get :index, facebook_params(:fb_sig_user => nil)
448 assert_response :success
449 assert @response.body =~ /fb:redirect/
452 def test_redirect_to_renders_javascript_redirect_if_request_is_for_a_facebook_iframe
453 get :index, facebook_params(:fb_sig_user => nil, :fb_sig_in_iframe => 1)
454 assert_response :success
455 assert_match "javascript", @response.body
456 assert_match "http-equiv", @response.body
457 assert_match "http://www.facebook.com/login.php?api_key=1234567&v=1.0", @response.body
460 def test_url_for_links_to_canvas_if_canvas_is_true_and_not_in_canvas
461 get :link_test, facebook_params(:fb_sig_in_canvas=>0,:canvas=>true)
462 assert_match(/apps.facebook.com/, @response.body)
465 def test_includes_relative_url_root_when_linked_to_canvas
466 get :link_test,facebook_params(:fb_sig_in_canvas=>0,:canvas=>true)
467 assert_match(/root/,@response.body)
470 def test_url_for_links_to_callback_if_canvas_is_false_and_in_canvas
471 get :link_test,facebook_params(:fb_sig_in_canvas=>0,:canvas=>false)
472 assert_match(/test.host/,@response.body)
475 def test_url_for_doesnt_include_url_root_when_not_linked_to_canvas
476 get :link_test,facebook_params(:fb_sig_in_canvas=>0,:canvas=>false)
477 assert !@response.body.match(/root/)
480 def test_url_for_links_to_canvas_if_canvas_is_not_set
481 get :link_test,facebook_params
482 assert_match(/apps.facebook.com/,@response.body)
486 get :image_test, facebook_params
487 assert_equal "<img alt=\"Image\" src=\"http://root.example.com/images/image.png\" />",@response.body
490 def test_wants_interface_is_available_and_detects_method
491 get :publisher_test, facebook_params(:method=>"publisher_getInterface")
492 assert_equal "interface",@response.body
494 def test_wants_interface_is_available_and_detects_not_interface
495 get :publisher_test, facebook_params(:method=>"publisher_getFeedStory")
496 assert_equal "not interface",@response.body
499 def test_publisher_test_error
500 get :publisher_test_error, facebook_params
501 assert_equal Facebooker.json_decode("{\"errorCode\": 1, \"errorTitle\": \"Title\", \"errorMessage\": \"Body\"}"), Facebooker.json_decode(@response.body)
504 def test_publisher_test_interface
505 get :publisher_test_interface, facebook_params
506 assert_equal Facebooker.json_decode("{\"method\": \"publisher_getInterface\", \"content\": {\"fbml\": \"This is a test\", \"publishEnabled\": false, \"commentEnabled\": true}}"), Facebooker.json_decode(@response.body)
509 def test_publisher_test_reponse
510 get :publisher_test_response, facebook_params
511 assert_equal Facebooker.json_decode("{\"method\": \"publisher_getFeedStory\", \"content\": {\"feed\": {\"template_data\": {\"params\": true}, \"template_id\": 1234}}}"), Facebooker.json_decode(@response.body)
517 def expired_cookie_hash_for_auth
518 {"_ss" => "not_used", "_session_key"=> "whatever", "_user"=>"77777", "_expires"=>"#{1.day.ago.to_i}"}
521 def cookie_hash_for_auth
522 {"_ss" => "not_used", "_session_key"=> "whatever", "_user"=>"77777", "_expires"=>"#{1.day.from_now.to_i}"}
528 class RailsSignatureTest < Test::Unit::TestCase
530 ENV['FACEBOOKER_RELATIVE_URL_ROOT'] ='root'
531 ENV['FACEBOOK_API_KEY'] = '1234567'
532 ENV['FACEBOOK_SECRET_KEY'] = '7654321'
533 @controller = ControllerWhichRequiresFacebookAuthentication.new
534 @request = ActionController::TestRequest.new
535 @response = ActionController::TestResponse.new
539 def test_should_raise_too_old_for_replayed_session
541 get :fb_params_test, facebook_params('fb_sig_time' => Time.now.to_i - 49.hours)
542 fail "No SignatureTooOld raised"
543 rescue Facebooker::Session::SignatureTooOld=>e
547 def test_should_raise_on_bad_sig
549 get :fb_params_test, facebook_params.merge('fb_sig' => 'incorrect')
550 fail "No IncorrectSignature raised"
551 rescue Facebooker::Session::IncorrectSignature=>e
555 def test_valid_signature
556 @controller.expects(:earliest_valid_session).returns(Time.at(1186588275.5988)-1)
557 get :fb_params_test, facebook_params
562 class RailsHelperTest < Test::Unit::TestCase
564 include ActionView::Helpers::TextHelper
565 include ActionView::Helpers::CaptureHelper
566 include ActionView::Helpers::TagHelper
567 include ActionView::Helpers::AssetTagHelper
568 include ActionView::Helpers::JavaScriptHelper
569 include Facebooker::Rails::Helpers
570 attr_accessor :flash, :output_buffer
574 @content_for_test_param="Test Param"
577 #used for stubbing out the form builder
582 ActionController::TestRequest.new
584 def fields_for(*args)
590 # used for capturing the contents of some of the helper tests
591 # this duplicates the rails template system
592 attr_accessor :_erbout
595 ENV['FACEBOOK_CANVAS_PATH'] ='facebook'
596 ENV['FACEBOOK_API_KEY'] = '1234567'
597 ENV['FACEBOOK_SECRET_KEY'] = '7654321'
601 #use an asset path where the canvas path equals the hostname to make sure we handle that case right
602 ActionController::Base.asset_host='http://facebook.host.com'
605 def test_fb_profile_pic
606 assert_equal "<fb:profile-pic uid=\"1234\"></fb:profile-pic>", @h.fb_profile_pic("1234")
609 def test_fb_profile_pic_with_valid_size
610 assert_equal "<fb:profile-pic size=\"small\" uid=\"1234\"></fb:profile-pic>", @h.fb_profile_pic("1234", :size => :small)
613 def test_fb_profile_pic_with_width_and_height
614 assert_equal "<fb:profile-pic height=\"200\" uid=\"1234\" width=\"100\"></fb:profile-pic>", @h.fb_profile_pic("1234", :width => 100, :height => 200)
617 def test_fb_profile_pic_with_invalid_size
618 assert_raises(ArgumentError) {@h.fb_profile_pic("1234", :size => :mediumm)}
622 assert_equal "<fb:photo pid=\"1234\"></fb:photo>",@h.fb_photo("1234")
625 def test_fb_photo_with_object_responding_to_photo_id
626 photo = flexmock("photo", :photo_id => "5678")
627 assert_equal "<fb:photo pid=\"5678\"></fb:photo>", @h.fb_photo(photo)
630 def test_fb_photo_with_invalid_size
631 assert_raises(ArgumentError) {@h.fb_photo("1234", :size => :medium)}
634 def test_fb_photo_with_invalid_size_value
635 assert_raises(ArgumentError) {@h.fb_photo("1234", :size => :mediumm)}
638 def test_fb_photo_with_invalid_align_value
639 assert_raises(ArgumentError) {@h.fb_photo("1234", :align => :rightt)}
642 def test_fb_photo_with_valid_align_value
643 assert_equal "<fb:photo align=\"right\" pid=\"1234\"></fb:photo>",@h.fb_photo("1234", :align => :right)
646 def test_fb_photo_with_class
647 assert_equal "<fb:photo class=\"picky\" pid=\"1234\"></fb:photo>",@h.fb_photo("1234", :class => :picky)
649 def test_fb_photo_with_style
650 assert_equal "<fb:photo pid=\"1234\" style=\"some=css;put=here;\"></fb:photo>",@h.fb_photo("1234", :style => "some=css;put=here;")
653 def test_fb_prompt_permission_valid_no_callback
654 assert_equal "<fb:prompt-permission perms=\"email\">Can I email you?</fb:prompt-permission>",@h.fb_prompt_permission("email","Can I email you?")
657 def test_fb_prompt_permission_valid_with_callback
658 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()")
661 def test_fb_prompt_permission_invalid_option
662 assert_raises(ArgumentError) {@h.fb_prompt_permission("invliad", "a message")}
666 def test_fb_prompt_permissions_valid_no_callback
667 assert_equal "<fb:prompt-permission perms=\"publish_stream,read_stream\">Can I read and write your streams?</fb:prompt-permission>",
668 @h.fb_prompt_permissions(['publish_stream', 'read_stream'],"Can I read and write your streams?")
671 def test_fb_prompt_permissions_valid_with_callback
672 assert_equal "<fb:prompt-permission next_fbjs=\"do_stuff()\" perms=\"publish_stream,read_stream\">Can I read and write your streams?</fb:prompt-permission>",
673 @h.fb_prompt_permissions(['publish_stream', 'read_stream'],"Can I read and write your streams?", "do_stuff()")
676 def test_fb_prompt_permissions_invalid_option
677 assert_raises(ArgumentError) {@h.fb_prompt_permissions(["invliad", "read_stream"], "a message")}
682 def test_fb_add_profile_section
683 assert_equal "<fb:add-section-button section=\"profile\" />",@h.fb_add_profile_section
686 def test_fb_add_info_section
687 assert_equal "<fb:add-section-button section=\"info\" />",@h.fb_add_info_section
690 def test_fb_name_with_invalid_key_sizee
691 assert_raises(ArgumentError) {@h.fb_name(1234, :sizee => false)}
695 assert_equal "<fb:name uid=\"1234\"></fb:name>",@h.fb_name("1234")
698 def test_fb_name_with_transformed_key
699 assert_equal "<fb:name uid=\"1234\" useyou=\"true\"></fb:name>", @h.fb_name(1234, :use_you => true)
702 def test_fb_name_with_user_responding_to_facebook_id
703 user = flexmock("user", :facebook_id => "5678")
704 assert_equal "<fb:name uid=\"5678\"></fb:name>", @h.fb_name(user)
707 def test_fb_name_with_invalid_key_linkd
708 assert_raises(ArgumentError) {@h.fb_name(1234, :linkd => false)}
712 assert_equal "<fb:tabs></fb:tabs>", @h.fb_tabs{}
716 assert_equal "<fb:tab-item href=\"http://www.google.com\" title=\"Google\" />", @h.fb_tab_item("Google", "http://www.google.com")
719 def test_fb_tab_item_raises_exception_for_invalid_option
720 assert_raises(ArgumentError) {@h.fb_tab_item("Google", "http://www.google.com", :alignn => :right)}
723 def test_fb_tab_item_raises_exception_for_invalid_align_value
724 assert_raises(ArgumentError) {@h.fb_tab_item("Google", "http://www.google.com", :align => :rightt)}
727 def test_fb_req_choice
728 assert_equal "<fb:req-choice label=\"label\" url=\"url\" />", @h.fb_req_choice("label","url")
731 def test_fb_multi_friend_selector
732 assert_equal "<fb:multi-friend-selector actiontext=\"This is a message\" max=\"20\" showborder=\"false\" />", @h.fb_multi_friend_selector("This is a message")
734 def test_fb_multi_friend_selector_with_options
735 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")
739 assert_equal "<fb:title>This is the canvas page window title</fb:title>", @h.fb_title("This is the canvas page window title")
742 def test_fb_google_analytics
743 assert_equal "<fb:google-analytics uacct=\"UA-9999999-99\" />", @h.fb_google_analytics("UA-9999999-99")
746 def test_fb_if_is_user_with_single_object
747 user = flexmock("user", :facebook_id => "5678")
748 assert_equal "<fb:if-is-user uid=\"5678\"></fb:if-is-user>", @h.fb_if_is_user(user){}
751 def test_fb_if_is_user_with_array
752 user1 = flexmock("user", :facebook_id => "5678")
753 user2 = flexmock("user", :facebook_id => "1234")
754 assert_equal "<fb:if-is-user uid=\"5678,1234\"></fb:if-is-user>", @h.fb_if_is_user([user1,user2]){}
758 assert_equal "<fb:else></fb:else>", @h.fb_else{}
761 def test_fb_about_url
762 ENV["FACEBOOK_API_KEY"]="1234"
763 assert_equal "http://www.facebook.com/apps/application.php?api_key=1234", @h.fb_about_url
766 def test_fb_ref_with_url
767 assert_equal "<fb:ref url=\"A URL\" />", @h.fb_ref(:url => "A URL")
770 def test_fb_ref_with_handle
771 assert_equal "<fb:ref handle=\"A Handle\" />", @h.fb_ref(:handle => "A Handle")
774 def test_fb_ref_with_invalid_attribute
775 assert_raises(ArgumentError) {@h.fb_ref(:handlee => "A HANLDE")}
778 def test_fb_ref_with_handle_and_url
779 assert_raises(ArgumentError) {@h.fb_ref(:url => "URL", :handle => "HANDLE")}
782 def test_facebook_messages_notice
783 @h.flash[:notice]="A message"
784 assert_equal "<fb:success message=\"A message\" />",@h.facebook_messages
787 def test_facebook_messages_error
788 @h.flash[:error]="An error"
789 assert_equal "<fb:error message=\"An error\" />",@h.facebook_messages
791 def test_fb_wall_post
792 assert_equal "<fb:wallpost uid=\"1234\">A wall post</fb:wallpost>",@h.fb_wall_post("1234","A wall post")
796 assert_equal "<fb:pronoun uid=\"1234\"></fb:pronoun>", @h.fb_pronoun(1234)
799 def test_fb_pronoun_with_transformed_key
800 assert_equal "<fb:pronoun uid=\"1234\" usethey=\"true\"></fb:pronoun>", @h.fb_pronoun(1234, :use_they => true)
803 def test_fb_pronoun_with_user_responding_to_facebook_id
804 user = flexmock("user", :facebook_id => "5678")
805 assert_equal "<fb:pronoun uid=\"5678\"></fb:pronoun>", @h.fb_pronoun(user)
808 def test_fb_pronoun_with_invalid_key
809 assert_raises(ArgumentError) {@h.fb_pronoun(1234, :posessive => true)}
813 @h.expects(:capture).returns("wall content")
816 assert_equal "<fb:wall>wall content</fb:wall>",@h.output_buffer
819 def test_fb_multi_friend_request
820 @h.expects(:capture).returns("body")
821 @h.expects(:protect_against_forgery?).returns(false)
822 @h.expects(:fb_multi_friend_selector).returns("friend selector")
823 assert_equal "<fb:request-form action=\"action\" content=\"body\" invite=\"true\" method=\"post\" type=\"invite\">friend selector</fb:request-form>",
824 (@h.fb_multi_friend_request("invite","ignored","action") {})
827 def test_fb_multi_friend_request_with_protection_against_forgery
828 @h.expects(:capture).returns("body")
829 @h.expects(:protect_against_forgery?).returns(true)
830 @h.expects(:request_forgery_protection_token).returns('forgery_token')
831 @h.expects(:form_authenticity_token).returns('form_token')
833 @h.expects(:fb_multi_friend_selector).returns("friend selector")
834 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>",
835 (@h.fb_multi_friend_request("invite","ignored","action") {})
839 @h.expects(:capture).returns("dialog content")
840 @h.fb_dialog( "my_dialog", "1" ) do
842 assert_equal '<fb:dialog cancel_button="1" id="my_dialog">dialog content</fb:dialog>', @h.output_buffer
844 def test_fb_dialog_title
845 assert_equal '<fb:dialog-title>My Little Dialog</fb:dialog-title>', @h.fb_dialog_title("My Little Dialog")
847 def test_fb_dialog_content
848 @h.expects(:capture).returns("dialog content content")
849 @h.fb_dialog_content do
851 assert_equal '<fb:dialog-content>dialog content content</fb:dialog-content>', @h.output_buffer
853 def test_fb_dialog_button
854 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" />',
855 @h.fb_dialog_button("submit", "Yes", {:clickrewriteurl => "http://www.some_url_here.com/dialog_return.php",
856 :clickrewriteid => "my_dialog", :clickrewriteform => "my_form" } )
859 def test_fb_request_form
860 @h.expects(:capture).returns("body")
861 @h.expects(:protect_against_forgery?).returns(false)
862 assert_equal "<fb:request-form action=\"action\" content=\"Test Param\" invite=\"true\" method=\"post\" type=\"invite\">body</fb:request-form>",
863 (@h.fb_request_form("invite","test_param","action") {})
866 def test_fb_request_form_with_protect_against_forgery
867 @h.expects(:capture).returns("body")
868 @h.expects(:protect_against_forgery?).returns(true)
869 @h.expects(:request_forgery_protection_token).returns('forgery_token')
870 @h.expects(:form_authenticity_token).returns('form_token')
871 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>",
872 (@h.fb_request_form("invite","test_param","action") {})
875 def test_fb_error_with_only_message
876 assert_equal "<fb:error message=\"Errors have occurred!!\" />", @h.fb_error("Errors have occurred!!")
879 def test_fb_error_with_message_and_text
880 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!!")
883 def test_fb_explanation_with_only_message
884 assert_equal "<fb:explanation message=\"This is an explanation\" />", @h.fb_explanation("This is an explanation")
887 def test_fb_explanation_with_message_and_text
888 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")
891 def test_fb_success_with_only_message
892 assert_equal "<fb:success message=\"Woot!!\" />", @h.fb_success("Woot!!")
895 def test_fb_success_with_message_and_text
896 assert_equal "<fb:success><fb:message>Woot!!</fb:message>You Rock!!</fb:success>", @h.fb_success("Woot!!", "You Rock!!")
899 def test_facebook_form_for
900 @h.expects(:protect_against_forgery?).returns(false)
901 form_body=@h.facebook_form_for(:model,:url=>"action") do
903 assert_equal "<fb:editor action=\"action\"></fb:editor>",form_body
906 def test_facebook_form_for_with_authenticity_token
907 @h.expects(:protect_against_forgery?).returns(true)
908 @h.expects(:request_forgery_protection_token).returns('forgery_token')
909 @h.expects(:form_authenticity_token).returns('form_token')
910 assert_equal "<fb:editor action=\"action\"><input name=\"forgery_token\" type=\"hidden\" value=\"form_token\" /></fb:editor>",
911 (@h.facebook_form_for(:model, :url => "action") {})
914 def test_fb_friend_selector
915 assert_equal("<fb:friend-selector />",@h.fb_friend_selector)
918 def test_fb_request_form_submit
919 assert_equal("<fb:request-form-submit />",@h.fb_request_form_submit)
922 def test_fb_request_form_submit_with_uid
923 assert_equal("<fb:request-form-submit uid=\"123456789\" />",@h.fb_request_form_submit({:uid => "123456789"}))
926 def test_fb_request_form_submit_with_label
927 assert_equal("<fb:request-form-submit label=\"Send Invite to Joel\" />",@h.fb_request_form_submit({:label => "Send Invite to Joel"}))
930 def test_fb_request_form_submit_with_uid_and_label
931 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"}))
935 assert_equal "<fb:action href=\"/growingpets/rub\">Rub my pet</fb:action>", @h.fb_action("Rub my pet", "/growingpets/rub")
939 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")
942 def test_fb_create_button
943 assert_equal "<fb:create-button href=\"/growingpets/invite\">Invite Friends</fb:create-button>", @h.fb_create_button('Invite Friends', '/growingpets/invite')
946 def test_fb_comments_a_1
947 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)
950 def test_fb_comments_xxx
951 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)
954 def test_fb_comments_with_title
955 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")
958 assert_equal "<fb:board optional=\"false\" xid=\"xxx\"></fb:board>", @h.fb_board("xxx",:optional => false)
960 def test_fb_board_with_title
961 assert_equal "<fb:board optional=\"false\" xid=\"xxx\"><fb:title>TITLE</fb:title></fb:board>", @h.fb_board("xxx",:optional=>false, :title => "TITLE")
964 def test_fb_dashboard
965 @h.expects(:capture).returns("dashboard content")
968 assert_equal "<fb:dashboard>dashboard content</fb:dashboard>", @h.output_buffer
970 def test_fb_dashboard_non_block
971 assert_equal "<fb:dashboard></fb:dashboard>", @h.fb_dashboard
975 @h.expects(:capture).returns("wide profile content")
978 assert_equal "<fb:wide>wide profile content</fb:wide>", @h.output_buffer
982 @h.expects(:capture).returns("narrow profile content")
985 assert_equal "<fb:narrow>narrow profile content</fb:narrow>", @h.output_buffer
988 def test_fb_login_button
989 assert_equal "<fb:login-button onlogin=\"somejs\"></fb:login-button>",@h.fb_login_button("somejs")
992 def test_init_fb_connect_no_features
993 assert ! @h.init_fb_connect.match(/XFBML/)
996 def test_init_fb_connect_with_features
997 assert @h.init_fb_connect("XFBML").match(/XFBML/)
1000 def test_init_fb_connect_receiver_path
1001 assert @h.init_fb_connect.match(/xd_receiver.html/)
1004 def test_init_fb_connect_receiver_path_ssl
1007 ssl_request = ActionController::TestRequest.new
1008 ssl_request.stubs(:ssl?).returns(true)
1013 assert @h.init_fb_connect.match(/xd_receiver_ssl.html/)
1016 def test_init_fb_connect_with_features_and_body
1017 @h.expects(:capture).returns("Body Content")
1019 __in_erb_template = true
1021 @h.init_fb_connect("XFBML") do
1023 assert @h.output_buffer =~ /Body Content/
1026 def test_init_fb_connect_no_options
1027 assert ! @h.init_fb_connect.match(/Element.observe\(window,'load',/)
1030 def test_init_fb_connect_with_options_js_jquery
1031 assert ! @h.init_fb_connect(:js => :jquery).match(/\$\(document\).ready\(/)
1034 def test_init_fb_connect_with_features_and_options_js_jquery
1035 assert @h.init_fb_connect("XFBML", :js => :jquery).match(/XFBML.*/)
1036 assert @h.init_fb_connect("XFBML", :js => :jquery).match(/\$\(document\).ready\(/)
1039 def test_init_fb_connect_without_options_app_settings
1040 assert @h.init_fb_connect().match(/, \{\}\)/)
1043 def test_init_fb_connect_with_options_app_settings
1044 assert @h.init_fb_connect(:app_settings => "{foo: bar}").match(/, \{foo: bar\}\)/)
1048 def test_fb_login_and_redirect
1049 assert_equal @h.fb_login_and_redirect("/path"),"<fb:login-button onlogin=\"window.location.href = "/path";\"></fb:login-button>"
1052 def test_fb_logout_link
1053 assert_equal @h.fb_logout_link("Logout","My URL"),"<a href=\"#\" onclick=\"FB.Connect.logoutAndRedirect("My URL");; return false;\">Logout</a>"
1055 def test_fb_user_action
1056 action = Facebooker::Rails::Publisher::UserAction.new
1057 assert_equal @h.fb_user_action(action,"message","prompt"),"FB.Connect.showFeedDialog(null, null, null, null, null, FB.RequireConnect.promptConnect, null, \"prompt\", {\"value\": \"message\"});"
1061 def test_fb_connect_javascript_tag
1063 assert_equal "<script src=\"http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php\" type=\"text/javascript\"></script>",
1064 @h.fb_connect_javascript_tag
1068 def test_fb_connect_javascript_tag_ssl
1071 ssl_request = ActionController::TestRequest.new
1072 ssl_request.stubs(:ssl?).returns(true)
1078 assert_equal "<script src=\"https://www.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php\" type=\"text/javascript\"></script>",
1079 @h.fb_connect_javascript_tag
1083 def test_fb_container
1084 @h.expects(:capture).returns("Inner Stuff")
1085 @h.fb_container(:condition=>"somejs") do
1087 assert_equal "<fb:container condition=\"somejs\">Inner Stuff</fb:container>",@h.output_buffer
1090 def test_fb_eventlink
1091 assert_equal '<fb:eventlink eid="21150032416"></fb:eventlink>',@h.fb_eventlink("21150032416")
1094 def test_fb_grouplink
1095 assert_equal '<fb:grouplink gid="2541896821"></fb:grouplink>',@h.fb_grouplink("2541896821")
1098 def test_fb_serverfbml
1099 @h.expects(:capture).returns("Inner Stuff")
1100 @h.fb_serverfbml(:condition=>"somejs") do
1102 assert_equal "<fb:serverfbml condition=\"somejs\">Inner Stuff</fb:serverfbml>",@h.output_buffer
1105 def test_fb_share_button
1106 assert_equal "<fb:share-button class=\"url\" href=\"http://www.elevatedrails.com\"></fb:share-button>",@h.fb_share_button("http://www.elevatedrails.com")
1109 def test_fb_unconnected_friends_count_without_condition
1110 assert_equal "<fb:unconnected-friends-count></fb:unconnected-friends-count>",@h.fb_unconnected_friends_count
1113 def test_fb_user_status
1114 user=flexmock("user", :facebook_id => "5678")
1115 assert_equal '<fb:user-status linked="false" uid="5678"></fb:user-status>',@h.fb_user_status(user,false)
1120 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)
1123 def test_fb_time_defaults
1125 assert_equal %Q{<fb:time t="#{time.to_i}" />}, @h.fb_time(time)
1129 attr_accessor :name,:facebook_id
1132 class RailsFacebookFormbuilderTest < Test::Unit::TestCase
1134 include ActionView::Helpers::TextHelper
1135 include ActionView::Helpers::CaptureHelper
1136 include ActionView::Helpers::TagHelper
1137 include Facebooker::Rails::Helpers
1138 attr_accessor :output_buffer
1145 @test_model = TestModel.new
1146 @test_model.name="Mike"
1147 @template = TestTemplate.new
1149 @form_builder = Facebooker::Rails::FacebookFormBuilder.new(:test_model,@test_model,@template,{},@proc)
1150 def @form_builder._erbout
1157 assert_equal "<fb:editor-text id=\"test_model_name\" label=\"Name\" name=\"test_model[name]\" value=\"Mike\"></fb:editor-text>",
1158 @form_builder.text_field(:name)
1161 assert_equal "<fb:editor-textarea id=\"test_model_name\" label=\"Name\" name=\"test_model[name]\">Mike</fb:editor-textarea>",
1162 @form_builder.text_area(:name)
1165 def test_default_name_and_id
1166 assert_equal "<fb:editor-text id=\"different_id\" label=\"Name\" name=\"different_name\" value=\"Mike\"></fb:editor-text>",
1167 @form_builder.text_field(:name, {:name => 'different_name', :id => 'different_id'})
1170 def test_collection_typeahead
1171 flexmock(@form_builder) do |fb|
1172 fb.should_receive(:collection_typeahead_internal).with(:name,["ABC"],:size,:to_s,{})
1174 @form_builder.collection_typeahead(:name,["ABC"],:size,:to_s)
1177 def test_collection_typeahead_internal
1178 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>",
1179 @form_builder.collection_typeahead_internal(:name,["ABC"],:size,:to_s)
1183 @form_builder.expects(:create_button).with(:first).returns("first")
1184 @form_builder.expects(:create_button).with(:second).returns("second")
1185 @template.expects(:content_tag).with("fb:editor-buttonset","firstsecond")
1186 @form_builder.buttons(:first,:second)
1189 def test_create_button
1190 assert_equal "<fb:editor-button name=\"commit\" value=\"first\"></fb:editor-button>",@form_builder.create_button(:first)
1194 @template.expects(:password_field).returns("password_field")
1195 assert_equal "<fb:editor-custom label=\"Name\">password_field</fb:editor-custom>",@form_builder.password_field(:name)
1199 assert_equal "<fb:editor-custom label=\"custom\">Mike</fb:editor-custom>",@form_builder.text("Mike",:label=>"custom")
1202 def test_multi_friend_input
1203 assert_equal "<fb:editor-custom label=\"Friends\"><fb:multi-friend-input></fb:multi-friend-input></fb:editor-custom>",@form_builder.multi_friend_input
1209 class RailsPrettyErrorsTest < Test::Unit::TestCase
1210 class ControllerWhichFails < ActionController::Base
1220 Facebooker.apply_configuration('api_key'=>"1234", 'secret_key'=>"34278",'canvas_page_name'=>'mike','pretty_errors'=>true)
1221 @controller = ControllerWhichFails.new
1222 @request = ActionController::TestRequest.new
1223 @response = ActionController::TestResponse.new
1226 def test_pretty_errors_disabled_success
1227 post :pass, facebook_params
1231 def test_pretty_errors_disabled_error
1232 Facebooker.apply_configuration('api_key'=>"1234", 'secret_key'=>"34278",'canvas_page_name'=>'mike','pretty_errors'=>false)
1234 post :fail, facebook_params
1236 assert_response :error
1239 def test_pretty_errors_enabled_success
1240 post :pass, facebook_params
1243 def test_pretty_errors_enabled_error
1245 post :fail, facebook_params
1251 class RailsUrlHelperExtensionsTest < Test::Unit::TestCase
1252 class UrlHelperExtensionsClass
1253 include ActionView::Helpers::UrlHelper
1254 include ActionView::Helpers::TagHelper
1255 def initialize(controller)
1256 @controller = controller
1259 def protect_against_forgery?
1263 def request_comes_from_facebook?
1264 @request_comes_from_facebook
1267 def request_comes_from_facebook=(val)
1268 @request_comes_from_facebook = val
1272 class UrlHelperExtensionsController < NoisyController
1274 render :nothing => true
1277 render :nothing => true
1281 class FacebookRequest < ActionController::TestRequest
1285 @controller = UrlHelperExtensionsController.new
1286 @request = FacebookRequest.new
1287 @response = ActionController::TestResponse.new
1289 @u = UrlHelperExtensionsClass.new(@controller)
1290 @u.request_comes_from_facebook = true
1292 @non_canvas_u = UrlHelperExtensionsClass.new(@controller)
1293 @non_canvas_u.request_comes_from_facebook = false
1297 @prompt = "Are you sure?"
1298 @default_title = "Please Confirm"
1299 @title = "Confirm Request"
1300 @style = {:color => 'black', :background => 'white'}
1301 @verbose_style = "{background: 'white', color: 'black'}"
1302 @default_okay = "Okay"
1303 @default_cancel = "Cancel"
1304 @default_style = "" #"'width','200px'"
1308 assert_equal "<a href=\"#{@url}\">Testing</a>", @u.link_to(@label, @url)
1311 def test_link_to_with_popup
1312 assert_raises(ActionView::ActionViewError) {@u.link_to(@label,@url, :popup=>true)}
1315 def test_link_to_with_confirm
1316 assert_dom_equal( "<a href=\"#{@url}\" onclick=\"var dlg = new Dialog().showChoice(\'#{@default_title}\',\'#{@prompt}\',\'#{@default_okay}\',\'#{@default_cancel}\').setStyle(#{@default_style});"+
1317 "var a=this;dlg.onconfirm = function() { " +
1318 "document.setLocation(a.getHref()); };return false;\">#{@label}</a>",
1319 @u.link_to(@label, @url, :confirm => @prompt) )
1321 def test_link_to_with_confirm_with_title
1322 assert_dom_equal( "<a href=\"#{@url}\" onclick=\"var dlg = new Dialog().showChoice(\'#{@title}\',\'#{@prompt}\',\'#{@default_okay}\',\'#{@default_cancel}\').setStyle(#{@default_style});"+
1323 "var a=this;dlg.onconfirm = function() { " +
1324 "document.setLocation(a.getHref()); };return false;\">#{@label}</a>",
1325 @u.link_to(@label, @url, :confirm => {:title=>@title,:content=>@prompt}) )
1327 def test_link_to_with_confirm_with_title_and_style
1328 assert_dom_equal( "<a href=\"#{@url}\" onclick=\"var dlg = new Dialog().showChoice(\'#{@title}\',\'#{@prompt}\',\'#{@default_okay}\',\'#{@default_cancel}\').setStyle(#{@verbose_style});"+
1329 "var a=this;dlg.onconfirm = function() { " +
1330 "document.setLocation(a.getHref()); };return false;\">#{@label}</a>",
1331 @u.link_to(@label, @url, :confirm => {:title=>@title,:content=>@prompt}.merge!(@style)) )
1334 def test_link_to_with_method
1335 assert_dom_equal( "<a href=\"#{@url}\" onclick=\"var a=this;var f = document.createElement('form'); f.setStyle('display','none'); "+
1336 "a.getParentNode().appendChild(f); f.setMethod('POST'); f.setAction(a.getHref());" +
1337 "var m = document.createElement('input'); m.setType('hidden'); "+
1338 "m.setName('_method'); m.setValue('delete'); f.appendChild(m);"+
1339 "f.submit();return false;\">#{@label}</a>", @u.link_to(@label,@url, :method=>:delete))
1342 def test_link_to_with_confirm_and_method
1343 assert_dom_equal( "<a href=\"#{@url}\" onclick=\"var dlg = new Dialog().showChoice(\'#{@default_title}\',\'#{@prompt}\',\'#{@default_okay}\',\'#{@default_cancel}\').setStyle(#{@default_style});"+
1344 "var a=this;dlg.onconfirm = function() { " +
1345 "var f = document.createElement('form'); f.setStyle('display','none'); "+
1346 "a.getParentNode().appendChild(f); f.setMethod('POST'); f.setAction(a.getHref());" +
1347 "var m = document.createElement('input'); m.setType('hidden'); "+
1348 "m.setName('_method'); m.setValue('delete'); f.appendChild(m);"+
1349 "f.submit(); };return false;\">#{@label}</a>", @u.link_to(@label,@url, :confirm=>@prompt, :method=>:delete) )
1351 def test_link_to_with_confirm_and_method_for_non_canvas_page
1352 assert_dom_equal( "<a href=\"#{@url}\" onclick=\"if (confirm(\'#{@prompt}\')) { var f = document.createElement('form'); f.style.display = 'none'; "+
1353 "this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;var m = document.createElement('input'); "+
1354 "m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method'); m.setAttribute('value', 'delete'); "+
1355 "f.appendChild(m);f.submit(); };return false;\">#{@label}</a>",
1356 @non_canvas_u.link_to(@label,@url, :confirm=>@prompt, :method=>:delete) )
1360 assert_equal "<form method=\"post\" action=\"#{@url}\" class=\"button-to\"><div>" +
1361 "<input type=\"submit\" value=\"#{@label}\" /></div></form>", @u.button_to(@label,@url)
1364 def test_button_to_with_confirm
1365 assert_equal "<form method=\"post\" action=\"#{@url}\" class=\"button-to\"><div>" +
1366 "<input onclick=\"var dlg = new Dialog().showChoice(\'#{@default_title}\',\'#{@prompt}\',\'#{@default_okay}\',\'#{@default_cancel}\').setStyle(#{@default_style});"+
1367 "var a=this;dlg.onconfirm = function() { "+
1368 "a.getForm().submit(); };return false;\" type=\"submit\" value=\"#{@label}\" /></div></form>",
1369 @u.button_to(@label,@url, :confirm=>@prompt)
1371 def test_button_to_with_confirm_for_non_canvas_page
1372 assert_equal "<form method=\"post\" action=\"#{@url}\" class=\"button-to\"><div>"+
1373 "<input onclick=\"return confirm(\'#{@prompt}\');\" type=\"submit\" value=\"#{@label}\" /></div></form>",
1374 @non_canvas_u.button_to(@label,@url, :confirm=>@prompt)
1377 def test_link_to_unless_with_true
1378 assert_equal @label, @u.link_to_unless(true,@label,@url)
1380 def test_link_to_unless_with_false
1381 assert_equal @u.link_to(@label,@url), @u.link_to_unless(false,@label,@url)
1384 def test_link_to_if_with_true
1385 assert_equal @u.link_to(@label,@url), @u.link_to_if(true,@label,@url)
1387 def test_link_to_if_with_false
1388 assert_equal @label, @u.link_to_if(false,@label,@url)
1393 class RailsRequestFormatTest < Test::Unit::TestCase
1394 class FacebookController < NoisyController
1396 respond_to do |format|
1397 format.html { render :text => 'html' }
1398 format.fbml { render :text => 'fbml' }
1399 format.fbjs { render :text => 'fbjs' }
1405 ENV['FACEBOOK_CANVAS_PATH'] ='facebook_app_name'
1406 ENV['FACEBOOK_API_KEY'] = '1234567'
1407 ENV['FACEBOOK_SECRET_KEY'] = '7654321'
1408 @controller = FacebookController.new
1409 @request = ActionController::TestRequest.new
1410 @response = ActionController::TestResponse.new
1411 @controller.stubs(:verify_signature).returns(true)
1414 def test_responds_to_html_without_canvas
1416 assert_equal 'html', @response.body
1419 def test_responds_to_fbml_in_canvas
1420 get :index, facebook_params(:fb_sig_in_canvas => '1')
1421 assert_equal 'fbml', @response.body
1424 def test_responds_to_fbjs_when_is_ajax
1425 get :index, facebook_params(:fb_sig_is_ajax => '1')
1426 assert_equal 'fbjs', @response.body
1429 def test_responds_to_html_when_iframe
1430 get :index, facebook_params(:fb_sig_in_iframe => '1')
1431 assert_equal 'html', @response.body