PageRenderTime 76ms CodeModel.GetById 8ms RepoModel.GetById 2ms app.codeStats 0ms

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

http://13wins.googlecode.com/
Ruby | 1126 lines | 1112 code | 14 blank | 0 comment | 9 complexity | d0988dff7e82b4680eef1b61584d76a0 MD5 | raw file

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

  1. require File.dirname(__FILE__) + '/test_helper.rb'
  2. begin
  3. require 'action_controller'
  4. require 'action_controller/test_process'
  5. require 'facebooker/rails/controller'
  6. require 'facebooker/rails/helpers'
  7. require 'facebooker/rails/facebook_form_builder'
  8. require File.dirname(__FILE__)+'/../init'
  9. require 'mocha'
  10. ActionController::Routing::Routes.draw do |map|
  11. map.connect '', :controller=>"facebook",:conditions=>{:canvas=>true}
  12. map.connect '', :controller=>"plain_old_rails"
  13. map.resources :comments, :controller=>"plain_old_rails"
  14. map.connect 'require_auth/:action', :controller => "controller_which_requires_facebook_authentication"
  15. map.connect 'require_install/:action', :controller => "controller_which_requires_application_installation"
  16. map.connect ':controller/:action/:id', :controller => "plain_old_rails"
  17. end
  18. class NoisyController < ActionController::Base
  19. include Facebooker::Rails::Controller
  20. def rescue_action(e) raise e end
  21. 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_create_listing
  27. def index
  28. render :text => 'score!'
  29. end
  30. end
  31. class ControllerWhichRequiresFacebookAuthentication < NoisyController
  32. ensure_authenticated_to_facebook
  33. def index
  34. render :text => 'score!'
  35. end
  36. def link_test
  37. options = {}
  38. options[:canvas] = true if params[:canvas] == true
  39. options[:canvas] = false if params[:canvas] == false
  40. render :text=>url_for(options)
  41. end
  42. def named_route_test
  43. render :text=>comments_url()
  44. end
  45. def image_test
  46. render :inline=>"<%=image_tag 'image.png'%>"
  47. end
  48. def fb_params_test
  49. render :text=>facebook_params['user']
  50. end
  51. def publisher_test
  52. if wants_interface?
  53. render :text=>"interface"
  54. else
  55. render :text=>"not interface"
  56. end
  57. end
  58. def publisher_test_interface
  59. render_publisher_interface("This is a test",false,true)
  60. end
  61. def publisher_test_response
  62. ua=Facebooker::Rails::Publisher::UserAction.new
  63. ua.data = {:params=>true}
  64. ua.template_name = "template_name"
  65. ua.template_id = 1234
  66. render_publisher_response(ua)
  67. end
  68. def publisher_test_error
  69. render_publisher_error("Title","Body")
  70. end
  71. end
  72. class ControllerWhichRequiresApplicationInstallation < NoisyController
  73. ensure_application_is_installed_by_facebook_user
  74. def index
  75. render :text => 'installed!'
  76. end
  77. end
  78. class FacebookController < ActionController::Base
  79. def index
  80. end
  81. end
  82. class PlainOldRailsController < ActionController::Base
  83. def index
  84. end
  85. def link_test
  86. options = {}
  87. options[:canvas] = true if params[:canvas] == true
  88. options[:canvas] = false if params[:canvas] == false
  89. render :text => url_for(options)
  90. end
  91. def named_route_test
  92. render :text=>comments_url()
  93. end
  94. def canvas_false_test
  95. render :text=>comments_url(:canvas=>false)
  96. end
  97. def canvas_true_test
  98. render :text=>comments_url(:canvas=>true)
  99. end
  100. end
  101. class ControllerWhichFails < ActionController::Base
  102. def pass
  103. render :text=>''
  104. end
  105. def fail
  106. raise "I'm failing"
  107. end
  108. end
  109. # you can't use asset_recognize, because it can't pass parameters in to the requests
  110. class UrlRecognitionTests < Test::Unit::TestCase
  111. def test_detects_in_canvas
  112. request = ActionController::TestRequest.new({"fb_sig_in_canvas"=>"1"}, {}, nil)
  113. request.path = "/"
  114. ActionController::Routing::Routes.recognize(request)
  115. assert_equal({"controller"=>"facebook","action"=>"index"},request.path_parameters)
  116. end
  117. def test_routes_when_not_in_canvas
  118. request = ActionController::TestRequest.new({}, {}, nil)
  119. request.path = "/"
  120. ActionController::Routing::Routes.recognize(request)
  121. assert_equal({"controller"=>"plain_old_rails","action"=>"index"},request.path_parameters)
  122. end
  123. end
  124. class RailsIntegrationTestForNonFacebookControllers < Test::Unit::TestCase
  125. def setup
  126. ENV['FACEBOOK_CANVAS_PATH'] ='facebook_app_name'
  127. ENV['FACEBOOK_API_KEY'] = '1234567'
  128. ENV['FACEBOOK_SECRET_KEY'] = '7654321'
  129. @controller = PlainOldRailsController.new
  130. @request = ActionController::TestRequest.new
  131. @response = ActionController::TestResponse.new
  132. end
  133. def test_url_for_links_to_callback_if_canvas_is_false_and_in_canvas
  134. get :link_test, example_rails_params
  135. assert_match /test.host/,@response.body
  136. end
  137. def test_named_route_doesnt_include_canvas_path_when_not_in_canvas
  138. get :named_route_test, example_rails_params
  139. assert_equal "http://test.host/comments",@response.body
  140. end
  141. def test_named_route_includes_canvas_path_when_in_canvas
  142. get :named_route_test, example_rails_params_including_fb
  143. assert_equal "http://apps.facebook.com/facebook_app_name/comments",@response.body
  144. end
  145. def test_named_route_doesnt_include_canvas_path_when_in_canvas_with_canvas_equals_false
  146. get :canvas_false_test, example_rails_params_including_fb
  147. assert_equal "http://test.host/comments",@response.body
  148. end
  149. def test_named_route_does_include_canvas_path_when_not_in_canvas_with_canvas_equals_true
  150. get :canvas_true_test, example_rails_params
  151. assert_equal "http://apps.facebook.com/facebook_app_name/comments",@response.body
  152. end
  153. private
  154. def example_rails_params
  155. { "action"=>"index", "controller"=>"plain_old_rails_controller" }
  156. end
  157. def example_rails_params_including_fb(options={})
  158. {"fb_sig_time"=>"1186588275.5988", "fb_sig"=>"8d9e9dd2cb0742a5a2bfe35563134585", "action"=>"index", "fb_sig_in_canvas"=>"1", "fb_sig_session_key"=>"c452b5d5d60cbd0a0da82021-744961110", "controller"=>"controller_which_requires_facebook_authentication", "fb_sig_expires"=>"0", "fb_sig_friends"=>"417358,702720,1001170,1530839,3300204,3501584,6217936,9627766,9700907,22701786,33902768,38914148,67400422,135301144,157200364,500103523,500104930,500870819,502149612,502664898,502694695,502852293,502985816,503254091,504510130,504611551,505421674,509229747,511075237,512548373,512830487,517893818,517961878,518890403,523589362,523826914,525812984,531555098,535310228,539339781,541137089,549405288,552706617,564393355,564481279,567640762,568091401,570201702,571469972,573863097,574415114,575543081,578129427,578520568,582262836,582561201,586550659,591631962,592318318,596269347,596663221,597405464,599764847,602995438,606661367,609761260,610544224,620049417,626087078,628803637,632686250,641422291,646763898,649678032,649925863,653288975,654395451,659079771,661794253,665861872,668960554,672481514,675399151,678427115,685772348,686821151,687686894,688506532,689275123,695551670,710631572,710766439,712406081,715741469,718976395,719246649,722747311,725327717,725683968,725831016,727580320,734151780,734595181,737944528,748881410,752244947,763868412,768578853,776596978,789728437,873695441", "fb_sig_added"=>"0", "fb_sig_api_key"=>"b6c9c857ac543ca806f4d3187cd05e09", "fb_sig_user"=>"744961110", "fb_sig_profile_update_time"=>"1180712453"}.merge(options)
  159. end
  160. end
  161. class RailsIntegrationTestForExtendedPermissions < Test::Unit::TestCase
  162. def setup
  163. ENV['FACEBOOK_API_KEY'] = '1234567'
  164. ENV['FACEBOOK_SECRET_KEY'] = '7654321'
  165. @controller = ControllerWhichRequiresExtendedPermissions.new
  166. @request = ActionController::TestRequest.new
  167. @response = ActionController::TestResponse.new
  168. @controller.stubs(:verify_signature).returns(true)
  169. end
  170. def test_redirects_without_set_status
  171. post :index,example_rails_params_including_fb
  172. assert_response :success
  173. assert_equal("<fb:redirect url=\"http://www.facebook.com/authorize.php?api_key=1234567&v=1.0&ext_perm=status_update\" />", @response.body)
  174. end
  175. def test_redirects_without_photo_upload
  176. post :index,example_rails_params_including_fb.merge(:fb_sig_ext_perms=>"status_update")
  177. assert_response :success
  178. assert_equal("<fb:redirect url=\"http://www.facebook.com/authorize.php?api_key=1234567&v=1.0&ext_perm=photo_upload\" />", @response.body)
  179. end
  180. def test_redirects_without_create_listing
  181. post :index,example_rails_params_including_fb.merge(:fb_sig_ext_perms=>"status_update,photo_upload")
  182. assert_response :success
  183. assert_equal("<fb:redirect url=\"http://www.facebook.com/authorize.php?api_key=1234567&v=1.0&ext_perm=create_listing\" />", @response.body)
  184. end
  185. def test_renders_with_permission
  186. post :index,example_rails_params_including_fb.merge(:fb_sig_ext_perms=>"status_update,photo_upload,create_listing")
  187. assert_response :success
  188. assert_equal("score!", @response.body)
  189. end
  190. private
  191. def example_rails_params_including_fb
  192. {"fb_sig_time"=>"1186588275.5988", "fb_sig"=>"7371a6400329b229f800a5ecafe03b0a", "action"=>"index", "fb_sig_in_canvas"=>"1", "fb_sig_session_key"=>"c452b5d5d60cbd0a0da82021-744961110", "controller"=>"controller_which_requires_facebook_authentication", "fb_sig_expires"=>"0", "fb_sig_friends"=>"417358,702720,1001170,1530839,3300204,3501584,6217936,9627766,9700907,22701786,33902768,38914148,67400422,135301144,157200364,500103523,500104930,500870819,502149612,502664898,502694695,502852293,502985816,503254091,504510130,504611551,505421674,509229747,511075237,512548373,512830487,517893818,517961878,518890403,523589362,523826914,525812984,531555098,535310228,539339781,541137089,549405288,552706617,564393355,564481279,567640762,568091401,570201702,571469972,573863097,574415114,575543081,578129427,578520568,582262836,582561201,586550659,591631962,592318318,596269347,596663221,597405464,599764847,602995438,606661367,609761260,610544224,620049417,626087078,628803637,632686250,641422291,646763898,649678032,649925863,653288975,654395451,659079771,661794253,665861872,668960554,672481514,675399151,678427115,685772348,686821151,687686894,688506532,689275123,695551670,710631572,710766439,712406081,715741469,718976395,719246649,722747311,725327717,725683968,725831016,727580320,734151780,734595181,737944528,748881410,752244947,763868412,768578853,776596978,789728437,873695441", "fb_sig_added"=>"0", "fb_sig_api_key"=>"b6c9c857ac543ca806f4d3187cd05e09", "fb_sig_user"=>"744961110", "fb_sig_profile_update_time"=>"1180712453"}
  193. end
  194. end
  195. class RailsIntegrationTestForApplicationInstallation < Test::Unit::TestCase
  196. def setup
  197. ENV['FACEBOOK_API_KEY'] = '1234567'
  198. ENV['FACEBOOK_SECRET_KEY'] = '7654321'
  199. @controller = ControllerWhichRequiresApplicationInstallation.new
  200. @request = ActionController::TestRequest.new
  201. @response = ActionController::TestResponse.new
  202. @controller.stubs(:verify_signature).returns(true)
  203. end
  204. def test_if_controller_requires_application_installation_unauthenticated_requests_will_redirect_to_install_page
  205. get :index
  206. assert_response :redirect
  207. assert_equal("http://www.facebook.com/install.php?api_key=1234567&v=1.0", @response.headers['Location'])
  208. end
  209. def test_if_controller_requires_application_installation_authenticated_requests_without_installation_will_redirect_to_install_page
  210. get :index, example_rails_params_including_fb
  211. assert_response :success
  212. assert_equal("<fb:redirect url=\"http://www.facebook.com/install.php?api_key=1234567&v=1.0\" />", @response.body)
  213. end
  214. def test_if_controller_requires_application_installation_authenticated_requests_with_installation_will_render
  215. get :index, example_rails_params_including_fb.merge('fb_sig_added' => "1")
  216. assert_response :success
  217. assert_equal("installed!", @response.body)
  218. end
  219. private
  220. def example_rails_params_including_fb
  221. {"fb_sig_time"=>"1186588275.5988", "fb_sig"=>"7371a6400329b229f800a5ecafe03b0a", "action"=>"index", "fb_sig_in_canvas"=>"1", "fb_sig_session_key"=>"c452b5d5d60cbd0a0da82021-744961110", "controller"=>"controller_which_requires_facebook_authentication", "fb_sig_expires"=>"0", "fb_sig_friends"=>"417358,702720,1001170,1530839,3300204,3501584,6217936,9627766,9700907,22701786,33902768,38914148,67400422,135301144,157200364,500103523,500104930,500870819,502149612,502664898,502694695,502852293,502985816,503254091,504510130,504611551,505421674,509229747,511075237,512548373,512830487,517893818,517961878,518890403,523589362,523826914,525812984,531555098,535310228,539339781,541137089,549405288,552706617,564393355,564481279,567640762,568091401,570201702,571469972,573863097,574415114,575543081,578129427,578520568,582262836,582561201,586550659,591631962,592318318,596269347,596663221,597405464,599764847,602995438,606661367,609761260,610544224,620049417,626087078,628803637,632686250,641422291,646763898,649678032,649925863,653288975,654395451,659079771,661794253,665861872,668960554,672481514,675399151,678427115,685772348,686821151,687686894,688506532,689275123,695551670,710631572,710766439,712406081,715741469,718976395,719246649,722747311,725327717,725683968,725831016,727580320,734151780,734595181,737944528,748881410,752244947,763868412,768578853,776596978,789728437,873695441", "fb_sig_added"=>"0", "fb_sig_api_key"=>"b6c9c857ac543ca806f4d3187cd05e09", "fb_sig_user"=>"744961110", "fb_sig_profile_update_time"=>"1180712453"}
  222. end
  223. end
  224. class RailsIntegrationTest < Test::Unit::TestCase
  225. def setup
  226. ENV['FACEBOOK_CANVAS_PATH'] ='root'
  227. ENV['FACEBOOK_API_KEY'] = '1234567'
  228. ENV['FACEBOOK_SECRET_KEY'] = '7654321'
  229. ActionController::Base.asset_host="http://root.example.com"
  230. @controller = ControllerWhichRequiresFacebookAuthentication.new
  231. @request = ActionController::TestRequest.new
  232. @response = ActionController::TestResponse.new
  233. @controller.stubs(:verify_signature).returns(true)
  234. end
  235. def test_named_route_includes_new_canvas_path_when_in_new_canvas
  236. get :named_route_test, example_rails_params_including_fb.merge("fb_sig_in_new_facebook"=>"1")
  237. assert_equal "http://apps.facebook.com/root/comments",@response.body
  238. end
  239. def test_if_controller_requires_facebook_authentication_unauthenticated_requests_will_redirect
  240. get :index
  241. assert_response :redirect
  242. assert_equal("http://www.facebook.com/login.php?api_key=1234567&v=1.0", @response.headers['Location'])
  243. end
  244. def test_facebook_params_are_parsed_into_a_separate_hash
  245. get :index, example_rails_params_including_fb
  246. facebook_params = @controller.facebook_params
  247. assert_equal([8, 8], [facebook_params['time'].day, facebook_params['time'].mon])
  248. end
  249. def test_facebook_params_convert_in_canvas_to_boolean
  250. get :index, example_rails_params_including_fb
  251. assert_equal(true, @controller.facebook_params['in_canvas'])
  252. end
  253. def test_facebook_params_convert_added_to_boolean_false
  254. get :index, example_rails_params_including_fb
  255. assert_equal(false, @controller.facebook_params['added'])
  256. end
  257. def test_facebook_params_convert_added_to_boolean_true
  258. get :index, example_rails_params_including_fb.merge('fb_sig_added' => "1")
  259. assert_equal(true, @controller.facebook_params['added'])
  260. end
  261. def test_facebook_params_convert_expirey_into_time_or_nil
  262. get :index, example_rails_params_including_fb
  263. assert_nil(@controller.facebook_params['expires'])
  264. modified_params = example_rails_params_including_fb
  265. modified_params['fb_sig_expires'] = modified_params['fb_sig_time']
  266. setup # reset session and cached params
  267. get :index, modified_params
  268. assert_equal([8, 8], [@controller.facebook_params['time'].day, @controller.facebook_params['time'].mon])
  269. end
  270. def test_facebook_params_convert_friend_list_to_parsed_array_of_friend_ids
  271. get :index, example_rails_params_including_fb
  272. assert_kind_of(Array, @controller.facebook_params['friends'])
  273. assert_equal(111, @controller.facebook_params['friends'].size)
  274. end
  275. def test_session_can_be_resecured_from_facebook_params
  276. get :index, example_rails_params_including_fb
  277. assert_equal(744961110, @controller.facebook_session.user.id)
  278. end
  279. def test_existing_secured_session_is_used_if_available
  280. session = Facebooker::Session.create(ENV['FACEBOOK_API_KEY'], ENV['FACEBOOK_SECRET_KEY'])
  281. session.secure_with!("c452b5d5d60cbd0a0da82021-744961110", "1111111", Time.now.to_i + 60)
  282. get :index, example_rails_params_including_fb, {:facebook_session => session}
  283. assert_equal(1111111, @controller.facebook_session.user.id)
  284. end
  285. def test_facebook_params_used_if_existing_secured_session_key_does_not_match
  286. session = Facebooker::Session.create(ENV['FACEBOOK_API_KEY'], ENV['FACEBOOK_SECRET_KEY'])
  287. session.secure_with!("a session key", "1111111", Time.now.to_i + 60)
  288. get :index, example_rails_params_including_fb, {:facebook_session => session}
  289. assert_equal(744961110, @controller.facebook_session.user.id)
  290. end
  291. def test_existing_secured_session_is_NOT_used_if_available_and_facebook_params_session_key_is_nil_and_in_canvas
  292. session = Facebooker::Session.create(ENV['FACEBOOK_API_KEY'], ENV['FACEBOOK_SECRET_KEY'])
  293. session.secure_with!("a session key", "1111111", Time.now.to_i + 60)
  294. get :index, example_rails_params_including_fb.merge("fb_sig_session_key" => ''), {:facebook_session => session}
  295. assert_equal(744961110, @controller.facebook_session.user.id)
  296. end
  297. def test_existing_secured_session_IS_used_if_available_and_facebook_params_session_key_is_nil_and_NOT_in_canvas
  298. @contoller = PlainOldRailsController.new
  299. session = Facebooker::Session.create(ENV['FACEBOOK_API_KEY'], ENV['FACEBOOK_SECRET_KEY'])
  300. session.secure_with!("a session key", "1111111", Time.now.to_i + 60)
  301. get :index,{}, {:facebook_session => session}
  302. assert_equal(1111111, @controller.facebook_session.user.id)
  303. end
  304. def test_session_can_be_secured_with_auth_token
  305. auth_token = 'ohaiauthtokenhere111'
  306. modified_params = example_rails_params_including_fb
  307. modified_params.delete('fb_sig_session_key')
  308. modified_params['auth_token'] = auth_token
  309. session_mock = flexmock(session = Facebooker::Session.create(ENV['FACEBOOK_API_KEY'], ENV['FACEBOOK_SECRET_KEY']))
  310. session_params = { 'session_key' => '123', 'uid' => '321' }
  311. session_mock.should_receive(:post).with('facebook.auth.getSession', :auth_token => auth_token).once.and_return(session_params).ordered
  312. flexmock(@controller).should_receive(:new_facebook_session).once.and_return(session).ordered
  313. get :index, modified_params
  314. end
  315. def test_user_friends_can_be_populated_from_facebook_params_if_available
  316. get :index, example_rails_params_including_fb
  317. assert_not_nil(friends = @controller.facebook_session.user.friends)
  318. assert_equal(111, friends.size)
  319. end
  320. def test_fbml_redirect_tag_handles_hash_parameters_correctly
  321. get :index, example_rails_params_including_fb
  322. assert_equal "<fb:redirect url=\"http://apps.facebook.com/root/require_auth\" />", @controller.send(:fbml_redirect_tag, :action => :index,:canvas=>true)
  323. end
  324. def test_redirect_to_renders_fbml_redirect_tag_if_request_is_for_a_facebook_canvas
  325. get :index, example_rails_params_including_fb_for_user_not_logged_into_application
  326. assert_response :success
  327. assert_equal("<fb:redirect url=\"http://www.facebook.com/login.php?api_key=1234567&v=1.0\" />", @response.body)
  328. end
  329. def test_url_for_links_to_canvas_if_canvas_is_true_and_not_in_canvas
  330. get :link_test,example_rails_params_including_fb.merge(:fb_sig_in_canvas=>0,:canvas=>true)
  331. assert_match /apps.facebook.com/,@response.body
  332. end
  333. def test_includes_relative_url_root_when_linked_to_canvas
  334. get :link_test,example_rails_params_including_fb.merge(:fb_sig_in_canvas=>0,:canvas=>true)
  335. assert_match /root/,@response.body
  336. end
  337. def test_url_for_links_to_callback_if_canvas_is_false_and_in_canvas
  338. get :link_test,example_rails_params_including_fb.merge(:fb_sig_in_canvas=>0,:canvas=>false)
  339. assert_match /test.host/,@response.body
  340. end
  341. def test_url_for_doesnt_include_url_root_when_not_linked_to_canvas
  342. get :link_test,example_rails_params_including_fb.merge(:fb_sig_in_canvas=>0,:canvas=>false)
  343. assert !@response.body.match(/root/)
  344. end
  345. def test_url_for_links_to_canvas_if_canvas_is_not_set
  346. get :link_test,example_rails_params_including_fb
  347. assert_match /apps.facebook.com/,@response.body
  348. end
  349. def test_image_tag
  350. get :image_test, example_rails_params_including_fb
  351. assert_equal "<img alt=\"Image\" src=\"http://root.example.com/images/image.png\" />",@response.body
  352. end
  353. def test_wants_interface_is_available_and_detects_method
  354. get :publisher_test, example_rails_params_including_fb.merge(:method=>"publisher_getInterface")
  355. assert_equal "interface",@response.body
  356. end
  357. def test_wants_interface_is_available_and_detects_not_interface
  358. get :publisher_test, example_rails_params_including_fb.merge(:method=>"publisher_getFeedStory")
  359. assert_equal "not interface",@response.body
  360. end
  361. def test_publisher_test_error
  362. get :publisher_test_error, example_rails_params_including_fb
  363. assert_equal JSON.parse("{\"errorCode\": 1, \"errorTitle\": \"Title\", \"errorMessage\": \"Body\"}"), JSON.parse(@response.body)
  364. end
  365. def test_publisher_test_interface
  366. get :publisher_test_interface, example_rails_params_including_fb
  367. assert_equal JSON.parse("{\"method\": \"publisher_getInterface\", \"content\": {\"fbml\": \"This is a test\", \"publishEnabled\": false, \"commentEnabled\": true}}"), JSON.parse(@response.body)
  368. end
  369. def test_publisher_test_reponse
  370. get :publisher_test_response, example_rails_params_including_fb
  371. assert_equal JSON.parse("{\"method\": \"publisher_getFeedStory\", \"content\": {\"feed\": {\"template_data\": {\"params\": true}, \"template_id\": 1234}}}"), JSON.parse(@response.body)
  372. end
  373. private
  374. def example_rails_params_including_fb_for_user_not_logged_into_application
  375. {"fb_sig_time"=>"1186588275.5988", "fb_sig"=>"7371a6400329b229f800a5ecafe03b0a", "action"=>"index", "fb_sig_in_canvas"=>"1", "controller"=>"controller_which_requires_facebook_authentication", "fb_sig_added"=>"0", "fb_sig_api_key"=>"b6c9c857ac543ca806f4d3187cd05e09"}
  376. end
  377. def example_rails_params_including_fb
  378. {"fb_sig_time"=>"1186588275.5988", "fb_sig"=>"7371a6400329b229f800a5ecafe03b0a", "action"=>"index", "fb_sig_in_canvas"=>"1", "fb_sig_session_key"=>"c452b5d5d60cbd0a0da82021-744961110", "controller"=>"controller_which_requires_facebook_authentication", "fb_sig_expires"=>"0", "fb_sig_friends"=>"417358,702720,1001170,1530839,3300204,3501584,6217936,9627766,9700907,22701786,33902768,38914148,67400422,135301144,157200364,500103523,500104930,500870819,502149612,502664898,502694695,502852293,502985816,503254091,504510130,504611551,505421674,509229747,511075237,512548373,512830487,517893818,517961878,518890403,523589362,523826914,525812984,531555098,535310228,539339781,541137089,549405288,552706617,564393355,564481279,567640762,568091401,570201702,571469972,573863097,574415114,575543081,578129427,578520568,582262836,582561201,586550659,591631962,592318318,596269347,596663221,597405464,599764847,602995438,606661367,609761260,610544224,620049417,626087078,628803637,632686250,641422291,646763898,649678032,649925863,653288975,654395451,659079771,661794253,665861872,668960554,672481514,675399151,678427115,685772348,686821151,687686894,688506532,689275123,695551670,710631572,710766439,712406081,715741469,718976395,719246649,722747311,725327717,725683968,725831016,727580320,734151780,734595181,737944528,748881410,752244947,763868412,768578853,776596978,789728437,873695441", "fb_sig_added"=>"0", "fb_sig_api_key"=>"b6c9c857ac543ca806f4d3187cd05e09", "fb_sig_user"=>"744961110", "fb_sig_profile_update_time"=>"1180712453"}
  379. end
  380. end
  381. class RailsSignatureTest < Test::Unit::TestCase
  382. def setup
  383. ENV['FACEBOOKER_RELATIVE_URL_ROOT'] ='root'
  384. ENV['FACEBOOK_API_KEY'] = '1234567'
  385. ENV['FACEBOOK_SECRET_KEY'] = '7654321'
  386. @controller = ControllerWhichRequiresFacebookAuthentication.new
  387. @request = ActionController::TestRequest.new
  388. @response = ActionController::TestResponse.new
  389. end
  390. def test_should_raise_too_old_for_replayed_session
  391. begin
  392. get :fb_params_test,example_rails_params_including_fb
  393. fail "No SignatureTooOld raised"
  394. rescue Facebooker::Session::SignatureTooOld=>e
  395. end
  396. end
  397. def test_should_raise_on_bad_sig
  398. begin
  399. get :fb_params_test,example_rails_params_including_fb("fb_sig"=>'incorrect')
  400. fail "No IncorrectSignature raised"
  401. rescue Facebooker::Session::IncorrectSignature=>e
  402. end
  403. end
  404. def test_valid_signature
  405. @controller.expects(:earliest_valid_session).returns(Time.at(1186588275.5988)-1)
  406. get :fb_params_test,example_rails_params_including_fb
  407. end
  408. def example_rails_params_including_fb(options={})
  409. {"fb_sig_time"=>"1186588275.5988", "fb_sig"=>"8d9e9dd2cb0742a5a2bfe35563134585", "action"=>"index", "fb_sig_in_canvas"=>"1", "fb_sig_session_key"=>"c452b5d5d60cbd0a0da82021-744961110", "controller"=>"controller_which_requires_facebook_authentication", "fb_sig_expires"=>"0", "fb_sig_friends"=>"417358,702720,1001170,1530839,3300204,3501584,6217936,9627766,9700907,22701786,33902768,38914148,67400422,135301144,157200364,500103523,500104930,500870819,502149612,502664898,502694695,502852293,502985816,503254091,504510130,504611551,505421674,509229747,511075237,512548373,512830487,517893818,517961878,518890403,523589362,523826914,525812984,531555098,535310228,539339781,541137089,549405288,552706617,564393355,564481279,567640762,568091401,570201702,571469972,573863097,574415114,575543081,578129427,578520568,582262836,582561201,586550659,591631962,592318318,596269347,596663221,597405464,599764847,602995438,606661367,609761260,610544224,620049417,626087078,628803637,632686250,641422291,646763898,649678032,649925863,653288975,654395451,659079771,661794253,665861872,668960554,672481514,675399151,678427115,685772348,686821151,687686894,688506532,689275123,695551670,710631572,710766439,712406081,715741469,718976395,719246649,722747311,725327717,725683968,725831016,727580320,734151780,734595181,737944528,748881410,752244947,763868412,768578853,776596978,789728437,873695441", "fb_sig_added"=>"0", "fb_sig_api_key"=>"b6c9c857ac543ca806f4d3187cd05e09", "fb_sig_user"=>"744961110", "fb_sig_profile_update_time"=>"1180712453"}.merge(options)
  410. end
  411. end
  412. class RailsHelperTest < Test::Unit::TestCase
  413. class HelperClass
  414. include ActionView::Helpers::TextHelper
  415. include ActionView::Helpers::CaptureHelper
  416. include ActionView::Helpers::TagHelper
  417. include ActionView::Helpers::AssetTagHelper
  418. include Facebooker::Rails::Helpers
  419. attr_accessor :flash, :output_buffer
  420. def initialize
  421. @flash={}
  422. @template = self
  423. @content_for_test_param="Test Param"
  424. @output_buffer = ""
  425. end
  426. #used for stubbing out the form builder
  427. def url_for(arg)
  428. arg
  429. end
  430. def fields_for(*args)
  431. ""
  432. end
  433. end
  434. # used for capturing the contents of some of the helper tests
  435. # this duplicates the rails template system
  436. attr_accessor :_erbout
  437. def setup
  438. ENV['FACEBOOK_CANVAS_PATH'] ='facebook'
  439. ENV['FACEBOOK_API_KEY'] = '1234567'
  440. ENV['FACEBOOK_SECRET_KEY'] = '7654321'
  441. @_erbout = ""
  442. @h = HelperClass.new
  443. #use an asset path where the canvas path equals the hostname to make sure we handle that case right
  444. ActionController::Base.asset_host='http://facebook.host.com'
  445. end
  446. def test_fb_profile_pic
  447. assert_equal "<fb:profile-pic uid=\"1234\" />", @h.fb_profile_pic("1234")
  448. end
  449. def test_fb_profile_pic_with_valid_size
  450. assert_equal "<fb:profile-pic size=\"small\" uid=\"1234\" />", @h.fb_profile_pic("1234", :size => :small)
  451. end
  452. def test_fb_profile_pic_with_invalid_size
  453. assert_raises(ArgumentError) {@h.fb_profile_pic("1234", :size => :mediumm)}
  454. end
  455. def test_fb_photo
  456. assert_equal "<fb:photo pid=\"1234\" />",@h.fb_photo("1234")
  457. end
  458. def test_fb_photo_with_object_responding_to_photo_id
  459. photo = flexmock("photo", :photo_id => "5678")
  460. assert_equal "<fb:photo pid=\"5678\" />", @h.fb_photo(photo)
  461. end
  462. def test_fb_photo_with_invalid_size
  463. assert_raises(ArgumentError) {@h.fb_photo("1234", :size => :medium)}
  464. end
  465. def test_fb_photo_with_invalid_size_value
  466. assert_raises(ArgumentError) {@h.fb_photo("1234", :size => :mediumm)}
  467. end
  468. def test_fb_photo_with_invalid_align_value
  469. assert_raises(ArgumentError) {@h.fb_photo("1234", :align => :rightt)}
  470. end
  471. def test_fb_photo_with_valid_align_value
  472. assert_equal "<fb:photo align=\"right\" pid=\"1234\" />",@h.fb_photo("1234", :align => :right)
  473. end
  474. def test_fb_photo_with_class
  475. assert_equal "<fb:photo class=\"picky\" pid=\"1234\" />",@h.fb_photo("1234", :class => :picky)
  476. end
  477. def test_fb_photo_with_style
  478. assert_equal "<fb:photo pid=\"1234\" style=\"some=css;put=here;\" />",@h.fb_photo("1234", :style => "some=css;put=here;")
  479. end
  480. def test_fb_prompt_permission_valid_no_callback
  481. assert_equal "<fb:prompt-permission perms=\"email\">Can I email you?</fb:prompt-permission>",@h.fb_prompt_permission("email","Can I email you?")
  482. end
  483. def test_fb_prompt_permission_valid_with_callback
  484. 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()")
  485. end
  486. def test_fb_prompt_permission_invalid_option
  487. assert_raises(ArgumentError) {@h.fb_prompt_permission("invliad", "a message")}
  488. end
  489. def test_fb_add_profile_section
  490. assert_equal "<fb:add-section-button section=\"profile\" />",@h.fb_add_profile_section
  491. end
  492. def test_fb_add_info_section
  493. assert_equal "<fb:add-section-button section=\"info\" />",@h.fb_add_info_section
  494. end
  495. def test_fb_name_with_invalid_key
  496. assert_raises(ArgumentError) {@h.fb_name(1234, :sizee => false)}
  497. end
  498. def test_fb_name
  499. assert_equal "<fb:name uid=\"1234\" />",@h.fb_name("1234")
  500. end
  501. def test_fb_name_with_transformed_key
  502. assert_equal "<fb:name uid=\"1234\" useyou=\"true\" />", @h.fb_name(1234, :use_you => true)
  503. end
  504. def test_fb_name_with_user_responding_to_facebook_id
  505. user = flexmock("user", :facebook_id => "5678")
  506. assert_equal "<fb:name uid=\"5678\" />", @h.fb_name(user)
  507. end
  508. def test_fb_name_with_invalid_key
  509. assert_raises(ArgumentError) {@h.fb_name(1234, :linkd => false)}
  510. end
  511. def test_fb_tabs
  512. assert_equal "<fb:tabs></fb:tabs>", @h.fb_tabs{}
  513. end
  514. def test_fb_tab_item
  515. assert_equal "<fb:tab-item href=\"http://www.google.com\" title=\"Google\" />", @h.fb_tab_item("Google", "http://www.google.com")
  516. end
  517. def test_fb_tab_item_raises_exception_for_invalid_option
  518. assert_raises(ArgumentError) {@h.fb_tab_item("Google", "http://www.google.com", :alignn => :right)}
  519. end
  520. def test_fb_tab_item_raises_exception_for_invalid_align_value
  521. assert_raises(ArgumentError) {@h.fb_tab_item("Google", "http://www.google.com", :align => :rightt)}
  522. end
  523. def test_fb_req_choice
  524. assert_equal "<fb:req-choice label=\"label\" url=\"url\" />", @h.fb_req_choice("label","url")
  525. end
  526. def test_fb_multi_friend_selector
  527. assert_equal "<fb:multi-friend-selector actiontext=\"This is a message\" max=\"20\" showborder=\"false\" />", @h.fb_multi_friend_selector("This is a message")
  528. end
  529. def test_fb_multi_friend_selector_with_options
  530. 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")
  531. end
  532. def test_fb_comments
  533. 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)
  534. end
  535. def test_fb_title
  536. assert_equal "<fb:title>This is the canvas page window title</fb:title>", @h.fb_title("This is the canvas page window title")
  537. end
  538. def test_fb_google_analytics
  539. assert_equal "<fb:google-analytics uacct=\"UA-9999999-99\" />", @h.fb_google_analytics("UA-9999999-99")
  540. end
  541. def test_fb_if_is_user_with_single_object
  542. user = flexmock("user", :facebook_id => "5678")
  543. assert_equal "<fb:if-is-user uid=\"5678\"></fb:if-is-user>", @h.fb_if_is_user(user){}
  544. end
  545. def test_fb_if_is_user_with_array
  546. user1 = flexmock("user", :facebook_id => "5678")
  547. user2 = flexmock("user", :facebook_id => "1234")
  548. assert_equal "<fb:if-is-user uid=\"5678,1234\"></fb:if-is-user>", @h.fb_if_is_user([user1,user2]){}
  549. end
  550. def test_fb_else
  551. assert_equal "<fb:else></fb:else>", @h.fb_else{}
  552. end
  553. def test_fb_about_url
  554. ENV["FACEBOOK_API_KEY"]="1234"
  555. assert_equal "http://www.facebook.com/apps/application.php?api_key=1234", @h.fb_about_url
  556. end
  557. def test_fb_ref_with_url
  558. assert_equal "<fb:ref url=\"A URL\" />", @h.fb_ref(:url => "A URL")
  559. end
  560. def test_fb_ref_with_handle
  561. assert_equal "<fb:ref handle=\"A Handle\" />", @h.fb_ref(:handle => "A Handle")
  562. end
  563. def test_fb_ref_with_invalid_attribute
  564. assert_raises(ArgumentError) {@h.fb_ref(:handlee => "A HANLDE")}
  565. end
  566. def test_fb_ref_with_handle_and_url
  567. assert_raises(ArgumentError) {@h.fb_ref(:url => "URL", :handle => "HANDLE")}
  568. end
  569. def test_facebook_messages_notice
  570. @h.flash[:notice]="A message"
  571. assert_equal "<fb:success message=\"A message\" />",@h.facebook_messages
  572. end
  573. def test_facebook_messages_error
  574. @h.flash[:error]="An error"
  575. assert_equal "<fb:error message=\"An error\" />",@h.facebook_messages
  576. end
  577. def test_fb_wall_post
  578. assert_equal "<fb:wallpost uid=\"1234\">A wall post</fb:wallpost>",@h.fb_wall_post("1234","A wall post")
  579. end
  580. def test_fb_pronoun
  581. assert_equal "<fb:pronoun uid=\"1234\" />", @h.fb_pronoun(1234)
  582. end
  583. def test_fb_pronoun_with_transformed_key
  584. assert_equal "<fb:pronoun uid=\"1234\" usethey=\"true\" />", @h.fb_pronoun(1234, :use_they => true)
  585. end
  586. def test_fb_pronoun_with_user_responding_to_facebook_id
  587. user = flexmock("user", :facebook_id => "5678")
  588. assert_equal "<fb:pronoun uid=\"5678\" />", @h.fb_pronoun(user)
  589. end
  590. def test_fb_pronoun_with_invalid_key
  591. assert_raises(ArgumentError) {@h.fb_pronoun(1234, :posessive => true)}
  592. end
  593. def test_fb_wall
  594. @h.expects(:capture).returns("wall content")
  595. @h.fb_wall do
  596. end
  597. assert_equal "<fb:wall>wall content</fb:wall>",@h.output_buffer
  598. end
  599. def test_fb_multi_friend_request
  600. @h.expects(:capture).returns("body")
  601. @h.expects(:protect_against_forgery?).returns(false)
  602. @h.expects(:fb_multi_friend_selector).returns("friend selector")
  603. assert_equal "<fb:request-form action=\"action\" content=\"body\" invite=\"true\" method=\"post\" type=\"invite\">friend selector</fb:request-form>",
  604. (@h.fb_multi_friend_request("invite","ignored","action") {})
  605. end
  606. def test_fb_multi_friend_request_with_protection_against_forgery
  607. @h.expects(:capture).returns("body")
  608. @h.expects(:protect_against_forgery?).returns(true)
  609. @h.expects(:request_forgery_protection_token).returns('forgery_token')
  610. @h.expects(:form_authenticity_token).returns('form_token')
  611. @h.expects(:fb_multi_friend_selector).returns("friend selector")
  612. 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>",
  613. (@h.fb_multi_friend_request("invite","ignored","action") {})
  614. end
  615. def test_fb_dialog
  616. @h.expects(:capture).returns("dialog content")
  617. @h.fb_dialog( "my_dialog", "1" ) do
  618. end
  619. assert_equal '<fb:dialog cancel_button="1" id="my_dialog">dialog content</fb:dialog>', @h.output_buffer
  620. end
  621. def test_fb_dialog_title
  622. assert_equal '<fb:dialog-title>My Little Dialog</fb:dialog-title>', @h.fb_dialog_title("My Little Dialog")
  623. end
  624. def test_fb_dialog_content
  625. @h.expects(:capture).returns("dialog content content")
  626. @h.fb_dialog_content do
  627. end
  628. assert_equal '<fb:dialog-content>dialog content content</fb:dialog-content>', @h.output_buffer
  629. end
  630. def test_fb_dialog_button
  631. 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" />',
  632. @h.fb_dialog_button("submit", "Yes", {:clickrewriteurl => "http://www.some_url_here.com/dialog_return.php",
  633. :clickrewriteid => "my_dialog", :clickrewriteform => "my_form" } )
  634. end
  635. def test_fb_request_form
  636. @h.expects(:capture).returns("body")
  637. @h.expects(:protect_against_forgery?).returns(false)
  638. assert_equal "<fb:request-form action=\"action\" content=\"Test Param\" invite=\"true\" method=\"post\" type=\"invite\">body</fb:request-form>",
  639. (@h.fb_request_form("invite","test_param","action") {})
  640. end
  641. def test_fb_request_form_with_protect_against_forgery
  642. @h.expects(:capture).returns("body")
  643. @h.expects(:protect_against_forgery?).returns(true)
  644. @h.expects(:request_forgery_protection_token).returns('forgery_token')
  645. @h.expects(:form_authenticity_token).returns('form_token')
  646. 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>",
  647. (@h.fb_request_form("invite","test_param","action") {})
  648. end
  649. def test_fb_error_with_only_message
  650. assert_equal "<fb:error message=\"Errors have occurred!!\" />", @h.fb_error("Errors have occurred!!")
  651. end
  652. def test_fb_error_with_message_and_text
  653. 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!!")
  654. end
  655. def test_fb_explanation_with_only_message
  656. assert_equal "<fb:explanation message=\"This is an explanation\" />", @h.fb_explanation("This is an explanation")
  657. end
  658. def test_fb_explanation_with_message_and_text
  659. 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")
  660. end
  661. def test_fb_success_with_only_message
  662. assert_equal "<fb:success message=\"Woot!!\" />", @h.fb_success("Woot!!")
  663. end
  664. def test_fb_success_with_message_and_text
  665. assert_equal "<fb:success><fb:message>Woot!!</fb:message>You Rock!!</fb:success>", @h.fb_success("Woot!!", "You Rock!!")
  666. end
  667. def test_facebook_form_for
  668. @h.expects(:protect_against_forgery?).returns(false)
  669. form_body=@h.facebook_form_for(:model,:url=>"action") do
  670. end
  671. assert_equal "<fb:editor action=\"action\"></fb:editor>",form_body
  672. end
  673. def test_facebook_form_for_with_authenticity_token
  674. @h.expects(:protect_against_forgery?).returns(true)
  675. @h.expects(:request_forgery_protection_token).returns('forgery_token')
  676. @h.expects(:form_authenticity_token).returns('form_token')
  677. assert_equal "<fb:editor action=\"action\"><input name=\"forgery_token\" type=\"hidden\" value=\"form_token\" /></fb:editor>",
  678. (@h.facebook_form_for(:model, :url => "action") {})
  679. end
  680. def test_fb_friend_selector
  681. assert_equal("<fb:friend-selector />",@h.fb_friend_selector)
  682. end
  683. def test_fb_request_form_submit
  684. assert_equal("<fb:request-form-submit />",@h.fb_request_form_submit)
  685. end
  686. def test_fb_request_form_submit_with_uid
  687. assert_equal("<fb:request-form-submit uid=\"123456789\" />",@h.fb_request_form_submit({:uid => "123456789"}))
  688. end
  689. def test_fb_request_form_submit_with_label
  690. assert_equal("<fb:request-form-submit label=\"Send Invite to Joel\" />",@h.fb_request_form_submit({:label => "Send Invite to Joel"}))
  691. end
  692. def test_fb_request_form_submit_with_uid_and_label
  693. 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"}))
  694. end
  695. def test_fb_action
  696. assert_equal "<fb:action href=\"/growingpets/rub\">Rub my pet</fb:action>", @h.fb_action("Rub my pet", "/growingpets/rub")
  697. end
  698. def test_fb_help
  699. 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")
  700. end
  701. def test_fb_create_button
  702. assert_equal "<fb:create-button href=\"/growingpets/invite\">Invite Friends</fb:create-button>", @h.fb_create_button('Invite Friends', '/growingpets/invite')
  703. end
  704. def test_fb_comments
  705. 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)
  706. end
  707. def test_fb_comments_with_title
  708. 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")
  709. end
  710. def test_fb_board
  711. assert_equal "<fb:board optional=\"false\" xid=\"xxx\" />", @h.fb_board("xxx",:optional => false)
  712. end
  713. def test_fb_dashboard
  714. @h.expects(:capture).returns("dashboard content")
  715. @h.fb_dashboard do
  716. end
  717. assert_equal "<fb:dashboard>dashboard content</fb:dashboard>", @h.output_buffer
  718. end
  719. def test_fb_dashboard_non_block
  720. assert_equal "<fb:dashboard></fb:dashboard>", @h.fb_dashboard
  721. end
  722. def test_fb_wide
  723. @h.expects(:capture).returns("wide profile content")
  724. @h.fb_wide do
  725. end
  726. assert_equal "<fb:wide>wide profile content</fb:wide>", @h.output_buffer
  727. end
  728. def test_fb_narrow
  729. @h.expects(:capture).returns("narrow profile content")
  730. @h.fb_narrow do
  731. end
  732. assert_equal "<fb:narrow>narrow profile content</fb:narrow>", @h.output_buffer
  733. end
  734. end
  735. class TestModel
  736. attr_accessor :name,:facebook_id
  737. end
  738. class RailsFacebookFormbuilderTest < Test::Unit::TestCase
  739. class TestTemplate
  740. include ActionView::Helpers::TextHelper
  741. include ActionView::Helpers::CaptureHelper
  742. include ActionView::Helpers::TagHelper
  743. include Facebooker::Rails::Helpers
  744. attr_accessor :output_buffer
  745. def initialize
  746. @output_buffer=""
  747. end
  748. end
  749. def setup
  750. @_erbout = ""
  751. @test_model = TestModel.new
  752. @test_model.name="Mike"
  753. @template = TestTemplate.new
  754. @proc = Proc.new {}
  755. @form_builder = Facebooker::Rails::FacebookFormBuilder.new(:test_model,@test_model,@template,{},@proc)
  756. def @form_builder._erbout
  757. ""
  758. end
  759. end
  760. def test_text_field
  761. assert_equal "<fb:editor-text id=\"test_model_name\" label=\"Name\" name=\"test_model[name]\" value=\"Mike\"></fb:editor-text>",
  762. @form_builder.text_field(:name)
  763. end
  764. def test_text_area
  765. assert_equal "<fb:editor-textarea id=\"test_model_name\" label=\"Name\" name=\"test_model[name]\">Mike</fb:editor-textarea>",
  766. @form_builder.text_area(:name)
  767. end
  768. def test_default_name_and_id
  769. assert_equal "<fb:editor-text id=\"different_id\" label=\"Name\" name=\"different_name\" value=\"Mike\"></fb:editor-text>",
  770. @form_builder.text_field(:name, {:name => 'different_name', :id => 'different_id'})
  771. end
  772. def test_collection_typeahead
  773. flexmock(@form_builder) do |fb|
  774. fb.should_receive(:collection_typeahead_internal).with(:name,["ABC"],:size,:to_s,{})
  775. end
  776. @form_builder.collection_typeahead(:name,["ABC"],:size,:to_s)
  777. end
  778. def test_collection_typeahead_internal
  779. 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>",
  780. @form_builder.collection_typeahead_internal(:name,["ABC"],:size,:to_s)
  781. end
  782. def test_buttons
  783. @form_builder.expects(:create_button).with(:first).returns("first")
  784. @form_builder.expects(:create_button).with(:second).returns("second")
  785. @template.expects(:content_tag).with("fb:editor-buttonset","firstsecond")
  786. @form_builder.buttons(:first,:second)
  787. end
  788. def test_create_button
  789. assert_equal "<fb:editor-button name=\"commit\" value=\"first\"></fb:editor-button>",@form_builder.create_button(:first)
  790. end
  791. def test_custom
  792. @template.expects(:password_field).returns("password_field")
  793. assert_equal "<fb:editor-custom label=\"Name\">password_field</fb:editor-custom>",@form_builder.password_field(:name)
  794. end
  795. def test_text
  796. assert_equal "<fb:editor-custom label=\"custom\">Mike</fb:editor-custom>",@form_builder.text("Mike",:label=>"custom")
  797. end
  798. def test_multi_friend_input
  799. assert_equal "<fb:editor-custom label=\"Friends\"><fb:multi-friend-input></fb:multi-friend-input></fb:editor-custom>",@form_builder.multi_friend_input
  800. end
  801. end
  802. class RailsPrettyErrorsTest < Test::Unit::TestCase
  803. def setup
  804. ENV['FACEBOOK_API_KEY'] = '1234567'
  805. ENV['FACEBOOK_SECRET_KEY'] = '7654321'
  806. @controller = ControllerWhichFails.new
  807. @request = ActionController::TestRequest.new
  808. @response = ActionController::TestResponse.new
  809. @controller.stubs(:verify_signature).returns(true)
  810. end
  811. def test_pretty_errors
  812. Facebooker.facebooker_config.stubs(:pretty_errors).returns(false)
  813. post :pass, example_rails_params_including_fb
  814. assert_response :success
  815. post :fail, example_rails_params_including_fb
  816. assert_response :error
  817. Facebooker.facebooker_config.stubs(:pretty_errors).returns(true)
  818. post :pass, example_rails_params_including_fb
  819. assert_response :success
  820. post :fail, example_rails_params_including_fb
  821. assert_response :error
  822. end
  823. private
  824. def example_rails_params_including_fb
  825. {"fb_sig_time"=>"1186588275.5988", "fb_sig"=>"7371a6400329b229f800a5ecafe03b0a", "action"=>"index", "fb_sig_in_canvas"=>"1", "fb_sig_session_key"=>"c452b5d5d60cbd0a0da82021-744961110", "controller"=>"controller_which_requires_facebook_authentication", "fb_sig_expires"=>"0", "fb_sig_friends"=>"417358,702720,1001170,1530839,3300204,3501584,6217936,9627766,9700907,22701786,33902768,38914148,67400422,135301144,157200364,500103523,500104930,500870819,502149612,502664898,502694695,502852293,502985816,503254091,504510130,504611551,505421674,509229747,511075237,512548373,512830487,517893818,517961878,518890403,523589362,523826914,525812984,531555098,535310228,539339781,541137089,549405288,552706617,564393355,564481279,567640762,568091401,570201702,571469972,573863097,574415114,575543081,578129427,578520568,582262836,582561201,586550659,591631962,592318318,596269347,596663221,597405464,599764847,602995438,606661367,609761260,610544224,620049417,626087078,628803637,632686250,641422291,646763898,649678032,649925863,653288975,654395451,659079771,661794253,665861872,668960554,672481514,675399151,678427115,685772348,686821151,687686894,688506532,689275123,695551670,710631572,710766439,712406081,715741469,718976395,719246649,722747311,725327717,725683968,725831016,727580320,734151780,734595181,737944528,748881410,752244947,763868412,768578853,776596978,789728437,873695441", "fb_sig_added"=>"0", "fb_sig_api_key"=>"b6c9c857ac543ca806f4d3187cd05e09", "fb_sig_user"=>"744961110", "fb_sig_profile_update_time"=>"1180712453"}
  826. end
  827. end
  828. class RailsUrlHelperExtensionsTest < Test::Unit::TestCase
  829. class UrlHelperExtensionsClass
  830. include ActionView::Helpers::UrlHelper
  831. include ActionView::Helpers::TagHelper
  832. def initialize(controller)
  833. @controller = controller
  834. end
  835. def protect_against_forgery?
  836. false
  837. end
  838. end
  839. class UrlHelperExtensionsController < NoisyController
  840. def index
  841. render :nothing => true
  842. end
  843. def do_it
  844. render :nothing => true
  845. end
  846. end
  847. class FacebookRequest < ActionController::TestRequest
  848. end
  849. def setup
  850. @controller = UrlHelperExtensionsController.new
  851. @request = FacebookRequest.new
  852. @response = ActionController::TestResponse.new
  853. @u = UrlHelperExtensionsClass.new(@controller)
  854. @u.stubs(:request_comes_from_facebook?).returns(true)
  855. @non_canvas_u = UrlHelperExtensionsClass.new(@controller)
  856. @non_canvas_u.stubs(:request_comes_from_facebook?).returns(false)
  857. @label = "Testing"
  858. @url = "test.host"
  859. @prompt = "Are you sure?"
  860. @default_title = "Please Confirm"
  861. @title = "Confirm Request"
  862. @style = {:color => 'black', :background => 'white'}
  863. @verbose_style = "{background: 'white', color: 'black'}"
  864. @default_style = "" #"'width','200px'"
  865. end
  866. def test_link_to
  867. assert_equal "<a href=\"#{@url}\">Testing</a>", @u.link_to(@label, @url)
  868. end
  869. def test_link_to_with_popup
  870. assert_raises(ActionView::ActionViewError) {@u.link_to(@label,@url, :popup=>true)}
  871. end
  872. def test_link_to_with_confirm
  873. assert_dom_equal( "<a href=\"#{@url}\" onclick=\"var dlg = new Dialog().showChoice(\'#{@default_title}\',\'#{@prompt}\').setStyle(#{@default_style});"+
  874. "var a=this;dlg.onconfirm = function() { " +
  875. "document.setLocation(a.getHref()); };return false;\">#{@label}</a>",
  876. @u.link_to(@label, @url, :confirm => @prompt) )
  877. end
  878. def test_link_to_with_confirm_with_title
  879. assert_dom_equal( "<a href=\"#{@url}\" onclick=\"var dlg = new Dialog().showChoice(\'#{@title}\',\'#{@prompt}\').setStyle(#{@default_style});"+
  880. "var a=this;dlg.onconfirm = function() { " +
  881. "document.setLocation(a.getHref()); };return false;\">#{@label}</a>",

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