PageRenderTime 66ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/plugins/facebooker/test/facebooker/rails_integration_test.rb

http://13wins.googlecode.com/
Ruby | 1310 lines | 1291 code | 19 blank | 0 comment | 9 complexity | 59c3f2b2713112eb9c16102ada36be5b MD5 | raw file

Large files files are truncated, but you can click here to view the full file

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

Large files files are truncated, but you can click here to view the full file