PageRenderTime 71ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

/test/rails_integration_test.rb

https://github.com/yury/facebooker
Ruby | 993 lines | 979 code | 14 blank | 0 comment | 9 complexity | 6cf0aa75ddc082e8b054a32a8a8019d9 MD5 | raw 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
  420. def initialize
  421. @flash={}
  422. @template = self
  423. @content_for_test_param="Test Param"
  424. end
  425. #used for stubbing out the form builder
  426. def url_for(arg)
  427. arg
  428. end
  429. def fields_for(*args)
  430. ""
  431. end
  432. end
  433. # used for capturing the contents of some of the helper tests
  434. # this duplicates the rails template system
  435. attr_accessor :_erbout
  436. def setup
  437. ENV['FACEBOOK_CANVAS_PATH'] ='facebook'
  438. ENV['FACEBOOK_API_KEY'] = '1234567'
  439. ENV['FACEBOOK_SECRET_KEY'] = '7654321'
  440. @_erbout = ""
  441. @h = HelperClass.new
  442. #use an asset path where the canvas path equals the hostname to make sure we handle that case right
  443. ActionController::Base.asset_host='http://facebook.host.com'
  444. end
  445. def test_fb_profile_pic
  446. assert_equal "<fb:profile-pic uid=\"1234\" />", @h.fb_profile_pic("1234")
  447. end
  448. def test_fb_profile_pic_with_valid_size
  449. assert_equal "<fb:profile-pic size=\"small\" uid=\"1234\" />", @h.fb_profile_pic("1234", :size => :small)
  450. end
  451. def test_fb_profile_pic_with_invalid_size
  452. assert_raises(ArgumentError) {@h.fb_profile_pic("1234", :size => :mediumm)}
  453. end
  454. def test_fb_photo
  455. assert_equal "<fb:photo pid=\"1234\" />",@h.fb_photo("1234")
  456. end
  457. def test_fb_photo_with_object_responding_to_photo_id
  458. photo = flexmock("photo", :photo_id => "5678")
  459. assert_equal "<fb:photo pid=\"5678\" />", @h.fb_photo(photo)
  460. end
  461. def test_fb_photo_with_invalid_size
  462. assert_raises(ArgumentError) {@h.fb_photo("1234", :size => :medium)}
  463. end
  464. def test_fb_photo_with_invalid_size_value
  465. assert_raises(ArgumentError) {@h.fb_photo("1234", :size => :mediumm)}
  466. end
  467. def test_fb_photo_with_invalid_align_value
  468. assert_raises(ArgumentError) {@h.fb_photo("1234", :align => :rightt)}
  469. end
  470. def test_fb_photo_with_valid_align_value
  471. assert_equal "<fb:photo align=\"right\" pid=\"1234\" />",@h.fb_photo("1234", :align => :right)
  472. end
  473. def test_fb_photo_with_class
  474. assert_equal "<fb:photo class=\"picky\" pid=\"1234\" />",@h.fb_photo("1234", :class => :picky)
  475. end
  476. def test_fb_photo_with_style
  477. assert_equal "<fb:photo pid=\"1234\" style=\"some=css;put=here;\" />",@h.fb_photo("1234", :style => "some=css;put=here;")
  478. end
  479. def test_fb_prompt_permission_valid_no_callback
  480. assert_equal "<fb:prompt-permission perms=\"email\">Can I email you?</fb:prompt-permission>",@h.fb_prompt_permission("email","Can I email you?")
  481. end
  482. def test_fb_prompt_permission_valid_with_callback
  483. 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()")
  484. end
  485. def test_fb_prompt_permission_invalid_option
  486. assert_raises(ArgumentError) {@h.fb_prompt_permission("invliad", "a message")}
  487. end
  488. def test_fb_add_profile_section
  489. assert_equal "<fb:add-section-button section=\"profile\" />",@h.fb_add_profile_section
  490. end
  491. def test_fb_add_info_section
  492. assert_equal "<fb:add-section-button section=\"info\" />",@h.fb_add_info_section
  493. end
  494. def test_fb_name_with_invalid_key
  495. assert_raises(ArgumentError) {@h.fb_name(1234, :sizee => false)}
  496. end
  497. def test_fb_name
  498. assert_equal "<fb:name uid=\"1234\" />",@h.fb_name("1234")
  499. end
  500. def test_fb_name_with_transformed_key
  501. assert_equal "<fb:name uid=\"1234\" useyou=\"true\" />", @h.fb_name(1234, :use_you => true)
  502. end
  503. def test_fb_name_with_user_responding_to_facebook_id
  504. user = flexmock("user", :facebook_id => "5678")
  505. assert_equal "<fb:name uid=\"5678\" />", @h.fb_name(user)
  506. end
  507. def test_fb_name_with_invalid_key
  508. assert_raises(ArgumentError) {@h.fb_name(1234, :linkd => false)}
  509. end
  510. def test_fb_tabs
  511. assert_equal "<fb:tabs></fb:tabs>", @h.fb_tabs{}
  512. end
  513. def test_fb_tab_item
  514. assert_equal "<fb:tab-item href=\"http://www.google.com\" title=\"Google\" />", @h.fb_tab_item("Google", "http://www.google.com")
  515. end
  516. def test_fb_tab_item_raises_exception_for_invalid_option
  517. assert_raises(ArgumentError) {@h.fb_tab_item("Google", "http://www.google.com", :alignn => :right)}
  518. end
  519. def test_fb_tab_item_raises_exception_for_invalid_align_value
  520. assert_raises(ArgumentError) {@h.fb_tab_item("Google", "http://www.google.com", :align => :rightt)}
  521. end
  522. def test_fb_req_choice
  523. assert_equal "<fb:req-choice label=\"label\" url=\"url\" />", @h.fb_req_choice("label","url")
  524. end
  525. def test_fb_multi_friend_selector
  526. assert_equal "<fb:multi-friend-selector actiontext=\"This is a message\" max=\"20\" showborder=\"false\" />", @h.fb_multi_friend_selector("This is a message")
  527. end
  528. def test_fb_multi_friend_selector_with_options
  529. 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")
  530. end
  531. def test_fb_comments
  532. 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)
  533. end
  534. def test_fb_title
  535. assert_equal "<fb:title>This is the canvas page window title</fb:title>", @h.fb_title("This is the canvas page window title")
  536. end
  537. def test_fb_google_analytics
  538. assert_equal "<fb:google-analytics uacct=\"UA-9999999-99\" />", @h.fb_google_analytics("UA-9999999-99")
  539. end
  540. def test_fb_if_is_user_with_single_object
  541. user = flexmock("user", :facebook_id => "5678")
  542. assert_equal "<fb:if-is-user uid=\"5678\"></fb:if-is-user>", @h.fb_if_is_user(user){}
  543. end
  544. def test_fb_if_is_user_with_array
  545. user1 = flexmock("user", :facebook_id => "5678")
  546. user2 = flexmock("user", :facebook_id => "1234")
  547. assert_equal "<fb:if-is-user uid=\"5678,1234\"></fb:if-is-user>", @h.fb_if_is_user([user1,user2]){}
  548. end
  549. def test_fb_else
  550. assert_equal "<fb:else></fb:else>", @h.fb_else{}
  551. end
  552. def test_fb_about_url
  553. ENV["FACEBOOK_API_KEY"]="1234"
  554. assert_equal "http://www.facebook.com/apps/application.php?api_key=1234", @h.fb_about_url
  555. end
  556. def test_fb_ref_with_url
  557. assert_equal "<fb:ref url=\"A URL\" />", @h.fb_ref(:url => "A URL")
  558. end
  559. def test_fb_ref_with_handle
  560. assert_equal "<fb:ref handle=\"A Handle\" />", @h.fb_ref(:handle => "A Handle")
  561. end
  562. def test_fb_ref_with_invalid_attribute
  563. assert_raises(ArgumentError) {@h.fb_ref(:handlee => "A HANLDE")}
  564. end
  565. def test_fb_ref_with_handle_and_url
  566. assert_raises(ArgumentError) {@h.fb_ref(:url => "URL", :handle => "HANDLE")}
  567. end
  568. def test_facebook_messages_notice
  569. @h.flash[:notice]="A message"
  570. assert_equal "<fb:success message=\"A message\" />",@h.facebook_messages
  571. end
  572. def test_facebook_messages_error
  573. @h.flash[:error]="An error"
  574. assert_equal "<fb:error message=\"An error\" />",@h.facebook_messages
  575. end
  576. def test_fb_wall_post
  577. assert_equal "<fb:wallpost uid=\"1234\">A wall post</fb:wallpost>",@h.fb_wall_post("1234","A wall post")
  578. end
  579. def test_fb_pronoun
  580. assert_equal "<fb:pronoun uid=\"1234\" />", @h.fb_pronoun(1234)
  581. end
  582. def test_fb_pronoun_with_transformed_key
  583. assert_equal "<fb:pronoun uid=\"1234\" usethey=\"true\" />", @h.fb_pronoun(1234, :use_they => true)
  584. end
  585. def test_fb_pronoun_with_user_responding_to_facebook_id
  586. user = flexmock("user", :facebook_id => "5678")
  587. assert_equal "<fb:pronoun uid=\"5678\" />", @h.fb_pronoun(user)
  588. end
  589. def test_fb_pronoun_with_invalid_key
  590. assert_raises(ArgumentError) {@h.fb_pronoun(1234, :posessive => true)}
  591. end
  592. def test_fb_wall
  593. @h.expects(:capture).returns("wall content")
  594. @h.fb_wall do
  595. end
  596. assert_equal "<fb:wall>wall content</fb:wall>",_erbout
  597. end
  598. def test_fb_multi_friend_request
  599. @h.expects(:capture).returns("body")
  600. @h.expects(:protect_against_forgery?).returns(false)
  601. @h.expects(:fb_multi_friend_selector).returns("friend selector")
  602. assert_equal "<fb:request-form action=\"action\" content=\"body\" invite=\"true\" method=\"post\" type=\"invite\">friend selector</fb:request-form>",
  603. (@h.fb_multi_friend_request("invite","ignored","action") {})
  604. end
  605. def test_fb_multi_friend_request_with_protection_against_forgery
  606. @h.expects(:capture).returns("body")
  607. @h.expects(:protect_against_forgery?).returns(true)
  608. @h.expects(:request_forgery_protection_token).returns('forgery_token')
  609. @h.expects(:form_authenticity_token).returns('form_token')
  610. @h.expects(:fb_multi_friend_selector).returns("friend selector")
  611. 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>",
  612. (@h.fb_multi_friend_request("invite","ignored","action") {})
  613. end
  614. def test_fb_dialog
  615. @h.expects(:capture).returns("dialog content")
  616. @h.fb_dialog( "my_dialog", "1" ) do
  617. end
  618. assert_equal '<fb:dialog cancel_button="1" id="my_dialog">dialog content</fb:dialog>', _erbout
  619. end
  620. def test_fb_dialog_title
  621. assert_equal '<fb:dialog-title>My Little Dialog</fb:dialog-title>', @h.fb_dialog_title("My Little Dialog")
  622. end
  623. def test_fb_dialog_content
  624. @h.expects(:capture).returns("dialog content content")
  625. @h.fb_dialog_content do
  626. end
  627. assert_equal '<fb:dialog-content>dialog content content</fb:dialog-content>', _erbout
  628. end
  629. def test_fb_dialog_button
  630. 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" />',
  631. @h.fb_dialog_button("submit", "Yes", {:clickrewriteurl => "http://www.some_url_here.com/dialog_return.php",
  632. :clickrewriteid => "my_dialog", :clickrewriteform => "my_form" } )
  633. end
  634. def test_fb_request_form
  635. @h.expects(:capture).returns("body")
  636. @h.expects(:protect_against_forgery?).returns(false)
  637. assert_equal "<fb:request-form action=\"action\" content=\"Test Param\" invite=\"true\" method=\"post\" type=\"invite\">body</fb:request-form>",
  638. (@h.fb_request_form("invite","test_param","action") {})
  639. end
  640. def test_fb_request_form_with_protect_against_forgery
  641. @h.expects(:capture).returns("body")
  642. @h.expects(:protect_against_forgery?).returns(true)
  643. @h.expects(:request_forgery_protection_token).returns('forgery_token')
  644. @h.expects(:form_authenticity_token).returns('form_token')
  645. 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>",
  646. (@h.fb_request_form("invite","test_param","action") {})
  647. end
  648. def test_fb_error_with_only_message
  649. assert_equal "<fb:error message=\"Errors have occurred!!\" />", @h.fb_error("Errors have occurred!!")
  650. end
  651. def test_fb_error_with_message_and_text
  652. 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!!")
  653. end
  654. def test_fb_explanation_with_only_message
  655. assert_equal "<fb:explanation message=\"This is an explanation\" />", @h.fb_explanation("This is an explanation")
  656. end
  657. def test_fb_explanation_with_message_and_text
  658. 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")
  659. end
  660. def test_fb_success_with_only_message
  661. assert_equal "<fb:success message=\"Woot!!\" />", @h.fb_success("Woot!!")
  662. end
  663. def test_fb_success_with_message_and_text
  664. assert_equal "<fb:success><fb:message>Woot!!</fb:message>You Rock!!</fb:success>", @h.fb_success("Woot!!", "You Rock!!")
  665. end
  666. def test_facebook_form_for
  667. @h.expects(:protect_against_forgery?).returns(false)
  668. form_body=@h.facebook_form_for(:model,:url=>"action") do
  669. end
  670. assert_equal "<fb:editor action=\"action\"></fb:editor>",form_body
  671. end
  672. def test_facebook_form_for_with_authenticity_token
  673. @h.expects(:protect_against_forgery?).returns(true)
  674. @h.expects(:request_forgery_protection_token).returns('forgery_token')
  675. @h.expects(:form_authenticity_token).returns('form_token')
  676. assert_equal "<fb:editor action=\"action\"><input name=\"forgery_token\" type=\"hidden\" value=\"form_token\" /></fb:editor>",
  677. (@h.facebook_form_for(:model, :url => "action") {})
  678. end
  679. def test_fb_friend_selector
  680. assert_equal("<fb:friend-selector />",@h.fb_friend_selector)
  681. end
  682. def test_fb_request_form_submit
  683. assert_equal("<fb:request-form-submit />",@h.fb_request_form_submit)
  684. end
  685. def test_fb_request_form_submit_with_uid
  686. assert_equal("<fb:request-form-submit uid=\"123456789\" />",@h.fb_request_form_submit({:uid => "123456789"}))
  687. end
  688. def test_fb_request_form_submit_with_label
  689. assert_equal("<fb:request-form-submit label=\"Send Invite to Joel\" />",@h.fb_request_form_submit({:label => "Send Invite to Joel"}))
  690. end
  691. def test_fb_request_form_submit_with_uid_and_label
  692. 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"}))
  693. end
  694. def test_fb_action
  695. assert_equal "<fb:action href=\"/growingpets/rub\">Rub my pet</fb:action>", @h.fb_action("Rub my pet", "/growingpets/rub")
  696. end
  697. def test_fb_help
  698. 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")
  699. end
  700. def test_fb_create_button
  701. assert_equal "<fb:create-button href=\"/growingpets/invite\">Invite Friends</fb:create-button>", @h.fb_create_button('Invite Friends', '/growingpets/invite')
  702. end
  703. def test_fb_comments
  704. 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)
  705. end
  706. def test_fb_comments_with_title
  707. 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")
  708. end
  709. def test_fb_board
  710. assert_equal "<fb:board optional=\"false\" xid=\"xxx\" />", @h.fb_board("xxx",:optional => false)
  711. end
  712. def test_fb_dashboard
  713. @h.expects(:capture).returns("dashboard content")
  714. @h.fb_dashboard do
  715. end
  716. assert_equal "<fb:dashboard>dashboard content</fb:dashboard>", _erbout
  717. end
  718. def test_fb_dashboard_non_block
  719. assert_equal "<fb:dashboard></fb:dashboard>", @h.fb_dashboard
  720. end
  721. def test_fb_wide
  722. @h.expects(:capture).returns("wide profile content")
  723. @h.fb_wide do
  724. end
  725. assert_equal "<fb:wide>wide profile content</fb:wide>", _erbout
  726. end
  727. def test_fb_narrow
  728. @h.expects(:capture).returns("narrow profile content")
  729. @h.fb_narrow do
  730. end
  731. assert_equal "<fb:narrow>narrow profile content</fb:narrow>", _erbout
  732. end
  733. end
  734. class TestModel
  735. attr_accessor :name,:facebook_id
  736. end
  737. class RailsFacebookFormbuilderTest < Test::Unit::TestCase
  738. class TestTemplate
  739. include ActionView::Helpers::TextHelper
  740. include ActionView::Helpers::CaptureHelper
  741. include ActionView::Helpers::TagHelper
  742. include Facebooker::Rails::Helpers
  743. attr_accessor :_erbout
  744. def initialize
  745. @_erbout=""
  746. end
  747. end
  748. def setup
  749. @_erbout = ""
  750. @test_model = TestModel.new
  751. @test_model.name="Mike"
  752. @template = TestTemplate.new
  753. @proc = Proc.new {}
  754. @form_builder = Facebooker::Rails::FacebookFormBuilder.new(:test_model,@test_model,@template,{},@proc)
  755. def @form_builder._erbout
  756. ""
  757. end
  758. end
  759. def test_text_field
  760. assert_equal "<fb:editor-text id=\"test_model_name\" label=\"Name\" name=\"test_model[name]\" value=\"Mike\"></fb:editor-text>",
  761. @form_builder.text_field(:name)
  762. end
  763. def test_text_area
  764. assert_equal "<fb:editor-textarea id=\"test_model_name\" label=\"Name\" name=\"test_model[name]\">Mike</fb:editor-textarea>",
  765. @form_builder.text_area(:name)
  766. end
  767. def test_default_name_and_id
  768. assert_equal "<fb:editor-text id=\"different_id\" label=\"Name\" name=\"different_name\" value=\"Mike\"></fb:editor-text>",
  769. @form_builder.text_field(:name, {:name => 'different_name', :id => 'different_id'})
  770. end
  771. def test_collection_typeahead
  772. flexmock(@form_builder) do |fb|
  773. fb.should_receive(:collection_typeahead_internal).with(:name,["ABC"],:size,:to_s,{})
  774. end
  775. @form_builder.collection_typeahead(:name,["ABC"],:size,:to_s)
  776. end
  777. def test_collection_typeahead_internal
  778. 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>",
  779. @form_builder.collection_typeahead_internal(:name,["ABC"],:size,:to_s)
  780. end
  781. def test_buttons
  782. @form_builder.expects(:create_button).with(:first).returns("first")
  783. @form_builder.expects(:create_button).with(:second).returns("second")
  784. @template.expects(:content_tag).with("fb:editor-buttonset","firstsecond")
  785. @form_builder.buttons(:first,:second)
  786. end
  787. def test_create_button
  788. assert_equal "<fb:editor-button name=\"commit\" value=\"first\"></fb:editor-button>",@form_builder.create_button(:first)
  789. end
  790. def test_custom
  791. @template.expects(:password_field).returns("password_field")
  792. assert_equal "<fb:editor-custom label=\"Name\"></fb:editor-custom>",@form_builder.password_field(:name)
  793. end
  794. def test_text
  795. assert_equal "<fb:editor-custom label=\"custom\">Mike</fb:editor-custom>",@form_builder.text("Mike",:label=>"custom")
  796. end
  797. def test_multi_friend_input
  798. assert_equal "<fb:editor-custom label=\"Friends\"></fb:editor-custom>",@form_builder.multi_friend_input
  799. end
  800. end
  801. class RailsPrettyErrorsTest < Test::Unit::TestCase
  802. def setup
  803. ENV['FACEBOOK_API_KEY'] = '1234567'
  804. ENV['FACEBOOK_SECRET_KEY'] = '7654321'
  805. @controller = ControllerWhichFails.new
  806. @request = ActionController::TestRequest.new
  807. @response = ActionController::TestResponse.new
  808. @controller.stubs(:verify_signature).returns(true)
  809. end
  810. def test_pretty_errors
  811. Facebooker.facebooker_config.stubs(:pretty_errors).returns(false)
  812. post :pass, example_rails_params_including_fb
  813. assert_response :success
  814. post :fail, example_rails_params_including_fb
  815. assert_response :error
  816. Facebooker.facebooker_config.stubs(:pretty_errors).returns(true)
  817. post :pass, example_rails_params_including_fb
  818. assert_response :success
  819. post :fail, example_rails_params_including_fb
  820. assert_response :error
  821. end
  822. private
  823. def example_rails_params_including_fb
  824. {"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"}
  825. end
  826. end
  827. # rescue LoadError
  828. # $stderr.puts "Couldn't find action controller. That's OK. We'll skip it."
  829. end