require File.dirname(__FILE__) + '/test_helper.rb' begin require 'action_controller' require 'action_controller/test_process' require 'facebooker/rails/controller' require 'facebooker/rails/helpers' require 'facebooker/rails/facebook_form_builder' require File.dirname(__FILE__)+'/../init' require 'mocha' ActionController::Routing::Routes.draw do |map| map.connect '', :controller=>"facebook",:conditions=>{:canvas=>true} map.connect '', :controller=>"plain_old_rails" map.resources :comments, :controller=>"plain_old_rails" map.connect 'require_auth/:action', :controller => "controller_which_requires_facebook_authentication" map.connect 'require_install/:action', :controller => "controller_which_requires_application_installation" map.connect ':controller/:action/:id', :controller => "plain_old_rails" end class NoisyController < ActionController::Base include Facebooker::Rails::Controller def rescue_action(e) raise e end end class ControllerWhichRequiresExtendedPermissions< NoisyController ensure_authenticated_to_facebook before_filter :ensure_has_status_update before_filter :ensure_has_photo_upload before_filter :ensure_has_create_listing def index render :text => 'score!' end end class ControllerWhichRequiresFacebookAuthentication < NoisyController ensure_authenticated_to_facebook def index render :text => 'score!' end def link_test options = {} options[:canvas] = true if params[:canvas] == true options[:canvas] = false if params[:canvas] == false render :text=>url_for(options) end def named_route_test render :text=>comments_url() end def image_test render :inline=>"<%=image_tag 'image.png'%>" end def fb_params_test render :text=>facebook_params['user'] end def publisher_test if wants_interface? render :text=>"interface" else render :text=>"not interface" end end def publisher_test_interface render_publisher_interface("This is a test",false,true) end def publisher_test_response ua=Facebooker::Rails::Publisher::UserAction.new ua.data = {:params=>true} ua.template_name = "template_name" ua.template_id = 1234 render_publisher_response(ua) end def publisher_test_error render_publisher_error("Title","Body") end end class ControllerWhichRequiresApplicationInstallation < NoisyController ensure_application_is_installed_by_facebook_user def index render :text => 'installed!' end end class FacebookController < ActionController::Base def index end end class PlainOldRailsController < ActionController::Base def index end def link_test options = {} options[:canvas] = true if params[:canvas] == true options[:canvas] = false if params[:canvas] == false render :text => url_for(options) end def named_route_test render :text=>comments_url() end def canvas_false_test render :text=>comments_url(:canvas=>false) end def canvas_true_test render :text=>comments_url(:canvas=>true) end end class ControllerWhichFails < ActionController::Base def pass render :text=>'' end def fail raise "I'm failing" end end # you can't use asset_recognize, because it can't pass parameters in to the requests class UrlRecognitionTests < Test::Unit::TestCase def test_detects_in_canvas request = ActionController::TestRequest.new({"fb_sig_in_canvas"=>"1"}, {}, nil) request.path = "/" ActionController::Routing::Routes.recognize(request) assert_equal({"controller"=>"facebook","action"=>"index"},request.path_parameters) end def test_routes_when_not_in_canvas request = ActionController::TestRequest.new({}, {}, nil) request.path = "/" ActionController::Routing::Routes.recognize(request) assert_equal({"controller"=>"plain_old_rails","action"=>"index"},request.path_parameters) end end class RailsIntegrationTestForNonFacebookControllers < Test::Unit::TestCase def setup ENV['FACEBOOK_CANVAS_PATH'] ='facebook_app_name' ENV['FACEBOOK_API_KEY'] = '1234567' ENV['FACEBOOK_SECRET_KEY'] = '7654321' @controller = PlainOldRailsController.new @request = ActionController::TestRequest.new @response = ActionController::TestResponse.new end def test_url_for_links_to_callback_if_canvas_is_false_and_in_canvas get :link_test, example_rails_params assert_match /test.host/,@response.body end def test_named_route_doesnt_include_canvas_path_when_not_in_canvas get :named_route_test, example_rails_params assert_equal "http://test.host/comments",@response.body end def test_named_route_includes_canvas_path_when_in_canvas get :named_route_test, example_rails_params_including_fb assert_equal "http://apps.facebook.com/facebook_app_name/comments",@response.body end def test_named_route_doesnt_include_canvas_path_when_in_canvas_with_canvas_equals_false get :canvas_false_test, example_rails_params_including_fb assert_equal "http://test.host/comments",@response.body end def test_named_route_does_include_canvas_path_when_not_in_canvas_with_canvas_equals_true get :canvas_true_test, example_rails_params assert_equal "http://apps.facebook.com/facebook_app_name/comments",@response.body end private def example_rails_params { "action"=>"index", "controller"=>"plain_old_rails_controller" } end def example_rails_params_including_fb(options={}) {"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) end end class RailsIntegrationTestForExtendedPermissions < Test::Unit::TestCase def setup ENV['FACEBOOK_API_KEY'] = '1234567' ENV['FACEBOOK_SECRET_KEY'] = '7654321' @controller = ControllerWhichRequiresExtendedPermissions.new @request = ActionController::TestRequest.new @response = ActionController::TestResponse.new @controller.stubs(:verify_signature).returns(true) end def test_redirects_without_set_status post :index,example_rails_params_including_fb assert_response :success assert_equal("", @response.body) end def test_redirects_without_photo_upload post :index,example_rails_params_including_fb.merge(:fb_sig_ext_perms=>"status_update") assert_response :success assert_equal("", @response.body) end def test_redirects_without_create_listing post :index,example_rails_params_including_fb.merge(:fb_sig_ext_perms=>"status_update,photo_upload") assert_response :success assert_equal("", @response.body) end def test_renders_with_permission post :index,example_rails_params_including_fb.merge(:fb_sig_ext_perms=>"status_update,photo_upload,create_listing") assert_response :success assert_equal("score!", @response.body) end private def example_rails_params_including_fb {"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"} end end class RailsIntegrationTestForApplicationInstallation < Test::Unit::TestCase def setup ENV['FACEBOOK_API_KEY'] = '1234567' ENV['FACEBOOK_SECRET_KEY'] = '7654321' @controller = ControllerWhichRequiresApplicationInstallation.new @request = ActionController::TestRequest.new @response = ActionController::TestResponse.new @controller.stubs(:verify_signature).returns(true) end def test_if_controller_requires_application_installation_unauthenticated_requests_will_redirect_to_install_page get :index assert_response :redirect assert_equal("http://www.facebook.com/install.php?api_key=1234567&v=1.0", @response.headers['Location']) end def test_if_controller_requires_application_installation_authenticated_requests_without_installation_will_redirect_to_install_page get :index, example_rails_params_including_fb assert_response :success assert_equal("", @response.body) end def test_if_controller_requires_application_installation_authenticated_requests_with_installation_will_render get :index, example_rails_params_including_fb.merge('fb_sig_added' => "1") assert_response :success assert_equal("installed!", @response.body) end private def example_rails_params_including_fb {"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"} end end class RailsIntegrationTest < Test::Unit::TestCase def setup ENV['FACEBOOK_CANVAS_PATH'] ='root' ENV['FACEBOOK_API_KEY'] = '1234567' ENV['FACEBOOK_SECRET_KEY'] = '7654321' ActionController::Base.asset_host="http://root.example.com" @controller = ControllerWhichRequiresFacebookAuthentication.new @request = ActionController::TestRequest.new @response = ActionController::TestResponse.new @controller.stubs(:verify_signature).returns(true) end def test_named_route_includes_new_canvas_path_when_in_new_canvas get :named_route_test, example_rails_params_including_fb.merge("fb_sig_in_new_facebook"=>"1") assert_equal "http://apps.facebook.com/root/comments",@response.body end def test_if_controller_requires_facebook_authentication_unauthenticated_requests_will_redirect get :index assert_response :redirect assert_equal("http://www.facebook.com/login.php?api_key=1234567&v=1.0", @response.headers['Location']) end def test_facebook_params_are_parsed_into_a_separate_hash get :index, example_rails_params_including_fb facebook_params = @controller.facebook_params assert_equal([8, 8], [facebook_params['time'].day, facebook_params['time'].mon]) end def test_facebook_params_convert_in_canvas_to_boolean get :index, example_rails_params_including_fb assert_equal(true, @controller.facebook_params['in_canvas']) end def test_facebook_params_convert_added_to_boolean_false get :index, example_rails_params_including_fb assert_equal(false, @controller.facebook_params['added']) end def test_facebook_params_convert_added_to_boolean_true get :index, example_rails_params_including_fb.merge('fb_sig_added' => "1") assert_equal(true, @controller.facebook_params['added']) end def test_facebook_params_convert_expirey_into_time_or_nil get :index, example_rails_params_including_fb assert_nil(@controller.facebook_params['expires']) modified_params = example_rails_params_including_fb modified_params['fb_sig_expires'] = modified_params['fb_sig_time'] setup # reset session and cached params get :index, modified_params assert_equal([8, 8], [@controller.facebook_params['time'].day, @controller.facebook_params['time'].mon]) end def test_facebook_params_convert_friend_list_to_parsed_array_of_friend_ids get :index, example_rails_params_including_fb assert_kind_of(Array, @controller.facebook_params['friends']) assert_equal(111, @controller.facebook_params['friends'].size) end def test_session_can_be_resecured_from_facebook_params get :index, example_rails_params_including_fb assert_equal(744961110, @controller.facebook_session.user.id) end def test_existing_secured_session_is_used_if_available session = Facebooker::Session.create(ENV['FACEBOOK_API_KEY'], ENV['FACEBOOK_SECRET_KEY']) session.secure_with!("c452b5d5d60cbd0a0da82021-744961110", "1111111", Time.now.to_i + 60) get :index, example_rails_params_including_fb, {:facebook_session => session} assert_equal(1111111, @controller.facebook_session.user.id) end def test_facebook_params_used_if_existing_secured_session_key_does_not_match session = Facebooker::Session.create(ENV['FACEBOOK_API_KEY'], ENV['FACEBOOK_SECRET_KEY']) session.secure_with!("a session key", "1111111", Time.now.to_i + 60) get :index, example_rails_params_including_fb, {:facebook_session => session} assert_equal(744961110, @controller.facebook_session.user.id) end def test_existing_secured_session_is_NOT_used_if_available_and_facebook_params_session_key_is_nil_and_in_canvas session = Facebooker::Session.create(ENV['FACEBOOK_API_KEY'], ENV['FACEBOOK_SECRET_KEY']) session.secure_with!("a session key", "1111111", Time.now.to_i + 60) get :index, example_rails_params_including_fb.merge("fb_sig_session_key" => ''), {:facebook_session => session} assert_equal(744961110, @controller.facebook_session.user.id) end def test_existing_secured_session_IS_used_if_available_and_facebook_params_session_key_is_nil_and_NOT_in_canvas @contoller = PlainOldRailsController.new session = Facebooker::Session.create(ENV['FACEBOOK_API_KEY'], ENV['FACEBOOK_SECRET_KEY']) session.secure_with!("a session key", "1111111", Time.now.to_i + 60) get :index,{}, {:facebook_session => session} assert_equal(1111111, @controller.facebook_session.user.id) end def test_session_can_be_secured_with_auth_token auth_token = 'ohaiauthtokenhere111' modified_params = example_rails_params_including_fb modified_params.delete('fb_sig_session_key') modified_params['auth_token'] = auth_token session_mock = flexmock(session = Facebooker::Session.create(ENV['FACEBOOK_API_KEY'], ENV['FACEBOOK_SECRET_KEY'])) session_params = { 'session_key' => '123', 'uid' => '321' } session_mock.should_receive(:post).with('facebook.auth.getSession', :auth_token => auth_token).once.and_return(session_params).ordered flexmock(@controller).should_receive(:new_facebook_session).once.and_return(session).ordered get :index, modified_params end def test_user_friends_can_be_populated_from_facebook_params_if_available get :index, example_rails_params_including_fb assert_not_nil(friends = @controller.facebook_session.user.friends) assert_equal(111, friends.size) end def test_fbml_redirect_tag_handles_hash_parameters_correctly get :index, example_rails_params_including_fb assert_equal "", @controller.send(:fbml_redirect_tag, :action => :index,:canvas=>true) end def test_redirect_to_renders_fbml_redirect_tag_if_request_is_for_a_facebook_canvas get :index, example_rails_params_including_fb_for_user_not_logged_into_application assert_response :success assert_equal("", @response.body) end def test_url_for_links_to_canvas_if_canvas_is_true_and_not_in_canvas get :link_test,example_rails_params_including_fb.merge(:fb_sig_in_canvas=>0,:canvas=>true) assert_match /apps.facebook.com/,@response.body end def test_includes_relative_url_root_when_linked_to_canvas get :link_test,example_rails_params_including_fb.merge(:fb_sig_in_canvas=>0,:canvas=>true) assert_match /root/,@response.body end def test_url_for_links_to_callback_if_canvas_is_false_and_in_canvas get :link_test,example_rails_params_including_fb.merge(:fb_sig_in_canvas=>0,:canvas=>false) assert_match /test.host/,@response.body end def test_url_for_doesnt_include_url_root_when_not_linked_to_canvas get :link_test,example_rails_params_including_fb.merge(:fb_sig_in_canvas=>0,:canvas=>false) assert !@response.body.match(/root/) end def test_url_for_links_to_canvas_if_canvas_is_not_set get :link_test,example_rails_params_including_fb assert_match /apps.facebook.com/,@response.body end def test_image_tag get :image_test, example_rails_params_including_fb assert_equal "\"Image\"",@response.body end def test_wants_interface_is_available_and_detects_method get :publisher_test, example_rails_params_including_fb.merge(:method=>"publisher_getInterface") assert_equal "interface",@response.body end def test_wants_interface_is_available_and_detects_not_interface get :publisher_test, example_rails_params_including_fb.merge(:method=>"publisher_getFeedStory") assert_equal "not interface",@response.body end def test_publisher_test_error get :publisher_test_error, example_rails_params_including_fb assert_equal JSON.parse("{\"errorCode\": 1, \"errorTitle\": \"Title\", \"errorMessage\": \"Body\"}"), JSON.parse(@response.body) end def test_publisher_test_interface get :publisher_test_interface, example_rails_params_including_fb assert_equal JSON.parse("{\"method\": \"publisher_getInterface\", \"content\": {\"fbml\": \"This is a test\", \"publishEnabled\": false, \"commentEnabled\": true}}"), JSON.parse(@response.body) end def test_publisher_test_reponse get :publisher_test_response, example_rails_params_including_fb assert_equal JSON.parse("{\"method\": \"publisher_getFeedStory\", \"content\": {\"feed\": {\"template_data\": {\"params\": true}, \"template_id\": 1234}}}"), JSON.parse(@response.body) end private def example_rails_params_including_fb_for_user_not_logged_into_application {"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"} end def example_rails_params_including_fb {"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"} end end class RailsSignatureTest < Test::Unit::TestCase def setup ENV['FACEBOOKER_RELATIVE_URL_ROOT'] ='root' ENV['FACEBOOK_API_KEY'] = '1234567' ENV['FACEBOOK_SECRET_KEY'] = '7654321' @controller = ControllerWhichRequiresFacebookAuthentication.new @request = ActionController::TestRequest.new @response = ActionController::TestResponse.new end def test_should_raise_too_old_for_replayed_session begin get :fb_params_test,example_rails_params_including_fb fail "No SignatureTooOld raised" rescue Facebooker::Session::SignatureTooOld=>e end end def test_should_raise_on_bad_sig begin get :fb_params_test,example_rails_params_including_fb("fb_sig"=>'incorrect') fail "No IncorrectSignature raised" rescue Facebooker::Session::IncorrectSignature=>e end end def test_valid_signature @controller.expects(:earliest_valid_session).returns(Time.at(1186588275.5988)-1) get :fb_params_test,example_rails_params_including_fb end def example_rails_params_including_fb(options={}) {"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) end end class RailsHelperTest < Test::Unit::TestCase class HelperClass include ActionView::Helpers::TextHelper include ActionView::Helpers::CaptureHelper include ActionView::Helpers::TagHelper include ActionView::Helpers::AssetTagHelper include Facebooker::Rails::Helpers attr_accessor :flash, :output_buffer def initialize @flash={} @template = self @content_for_test_param="Test Param" @output_buffer = "" end #used for stubbing out the form builder def url_for(arg) arg end def fields_for(*args) "" end end # used for capturing the contents of some of the helper tests # this duplicates the rails template system attr_accessor :_erbout def setup ENV['FACEBOOK_CANVAS_PATH'] ='facebook' ENV['FACEBOOK_API_KEY'] = '1234567' ENV['FACEBOOK_SECRET_KEY'] = '7654321' @_erbout = "" @h = HelperClass.new #use an asset path where the canvas path equals the hostname to make sure we handle that case right ActionController::Base.asset_host='http://facebook.host.com' end def test_fb_profile_pic assert_equal "", @h.fb_profile_pic("1234") end def test_fb_profile_pic_with_valid_size assert_equal "", @h.fb_profile_pic("1234", :size => :small) end def test_fb_profile_pic_with_invalid_size assert_raises(ArgumentError) {@h.fb_profile_pic("1234", :size => :mediumm)} end def test_fb_photo assert_equal "",@h.fb_photo("1234") end def test_fb_photo_with_object_responding_to_photo_id photo = flexmock("photo", :photo_id => "5678") assert_equal "", @h.fb_photo(photo) end def test_fb_photo_with_invalid_size assert_raises(ArgumentError) {@h.fb_photo("1234", :size => :medium)} end def test_fb_photo_with_invalid_size_value assert_raises(ArgumentError) {@h.fb_photo("1234", :size => :mediumm)} end def test_fb_photo_with_invalid_align_value assert_raises(ArgumentError) {@h.fb_photo("1234", :align => :rightt)} end def test_fb_photo_with_valid_align_value assert_equal "",@h.fb_photo("1234", :align => :right) end def test_fb_photo_with_class assert_equal "",@h.fb_photo("1234", :class => :picky) end def test_fb_photo_with_style assert_equal "",@h.fb_photo("1234", :style => "some=css;put=here;") end def test_fb_prompt_permission_valid_no_callback assert_equal "Can I email you?",@h.fb_prompt_permission("email","Can I email you?") end def test_fb_prompt_permission_valid_with_callback assert_equal "a message",@h.fb_prompt_permission("email","a message","do_stuff()") end def test_fb_prompt_permission_invalid_option assert_raises(ArgumentError) {@h.fb_prompt_permission("invliad", "a message")} end def test_fb_add_profile_section assert_equal "",@h.fb_add_profile_section end def test_fb_add_info_section assert_equal "",@h.fb_add_info_section end def test_fb_name_with_invalid_key assert_raises(ArgumentError) {@h.fb_name(1234, :sizee => false)} end def test_fb_name assert_equal "",@h.fb_name("1234") end def test_fb_name_with_transformed_key assert_equal "", @h.fb_name(1234, :use_you => true) end def test_fb_name_with_user_responding_to_facebook_id user = flexmock("user", :facebook_id => "5678") assert_equal "", @h.fb_name(user) end def test_fb_name_with_invalid_key assert_raises(ArgumentError) {@h.fb_name(1234, :linkd => false)} end def test_fb_tabs assert_equal "", @h.fb_tabs{} end def test_fb_tab_item assert_equal "", @h.fb_tab_item("Google", "http://www.google.com") end def test_fb_tab_item_raises_exception_for_invalid_option assert_raises(ArgumentError) {@h.fb_tab_item("Google", "http://www.google.com", :alignn => :right)} end def test_fb_tab_item_raises_exception_for_invalid_align_value assert_raises(ArgumentError) {@h.fb_tab_item("Google", "http://www.google.com", :align => :rightt)} end def test_fb_req_choice assert_equal "", @h.fb_req_choice("label","url") end def test_fb_multi_friend_selector assert_equal "", @h.fb_multi_friend_selector("This is a message") end def test_fb_multi_friend_selector_with_options assert_equal "", @h.fb_multi_friend_selector("This is a message",:exclude_ids=>"1,2") end def test_fb_comments assert_equal "", @h.fb_comments("a:1",true,false,7,:showform=>true) end def test_fb_title assert_equal "This is the canvas page window title", @h.fb_title("This is the canvas page window title") end def test_fb_google_analytics assert_equal "", @h.fb_google_analytics("UA-9999999-99") end def test_fb_if_is_user_with_single_object user = flexmock("user", :facebook_id => "5678") assert_equal "", @h.fb_if_is_user(user){} end def test_fb_if_is_user_with_array user1 = flexmock("user", :facebook_id => "5678") user2 = flexmock("user", :facebook_id => "1234") assert_equal "", @h.fb_if_is_user([user1,user2]){} end def test_fb_else assert_equal "", @h.fb_else{} end def test_fb_about_url ENV["FACEBOOK_API_KEY"]="1234" assert_equal "http://www.facebook.com/apps/application.php?api_key=1234", @h.fb_about_url end def test_fb_ref_with_url assert_equal "", @h.fb_ref(:url => "A URL") end def test_fb_ref_with_handle assert_equal "", @h.fb_ref(:handle => "A Handle") end def test_fb_ref_with_invalid_attribute assert_raises(ArgumentError) {@h.fb_ref(:handlee => "A HANLDE")} end def test_fb_ref_with_handle_and_url assert_raises(ArgumentError) {@h.fb_ref(:url => "URL", :handle => "HANDLE")} end def test_facebook_messages_notice @h.flash[:notice]="A message" assert_equal "",@h.facebook_messages end def test_facebook_messages_error @h.flash[:error]="An error" assert_equal "",@h.facebook_messages end def test_fb_wall_post assert_equal "A wall post",@h.fb_wall_post("1234","A wall post") end def test_fb_pronoun assert_equal "", @h.fb_pronoun(1234) end def test_fb_pronoun_with_transformed_key assert_equal "", @h.fb_pronoun(1234, :use_they => true) end def test_fb_pronoun_with_user_responding_to_facebook_id user = flexmock("user", :facebook_id => "5678") assert_equal "", @h.fb_pronoun(user) end def test_fb_pronoun_with_invalid_key assert_raises(ArgumentError) {@h.fb_pronoun(1234, :posessive => true)} end def test_fb_wall @h.expects(:capture).returns("wall content") @h.fb_wall do end assert_equal "wall content",@h.output_buffer end def test_fb_multi_friend_request @h.expects(:capture).returns("body") @h.expects(:protect_against_forgery?).returns(false) @h.expects(:fb_multi_friend_selector).returns("friend selector") assert_equal "friend selector", (@h.fb_multi_friend_request("invite","ignored","action") {}) end def test_fb_multi_friend_request_with_protection_against_forgery @h.expects(:capture).returns("body") @h.expects(:protect_against_forgery?).returns(true) @h.expects(:request_forgery_protection_token).returns('forgery_token') @h.expects(:form_authenticity_token).returns('form_token') @h.expects(:fb_multi_friend_selector).returns("friend selector") assert_equal "friend selector", (@h.fb_multi_friend_request("invite","ignored","action") {}) end def test_fb_dialog @h.expects(:capture).returns("dialog content") @h.fb_dialog( "my_dialog", "1" ) do end assert_equal 'dialog content', @h.output_buffer end def test_fb_dialog_title assert_equal 'My Little Dialog', @h.fb_dialog_title("My Little Dialog") end def test_fb_dialog_content @h.expects(:capture).returns("dialog content content") @h.fb_dialog_content do end assert_equal 'dialog content content', @h.output_buffer end def test_fb_dialog_button assert_equal '', @h.fb_dialog_button("submit", "Yes", {:clickrewriteurl => "http://www.some_url_here.com/dialog_return.php", :clickrewriteid => "my_dialog", :clickrewriteform => "my_form" } ) end def test_fb_request_form @h.expects(:capture).returns("body") @h.expects(:protect_against_forgery?).returns(false) assert_equal "body", (@h.fb_request_form("invite","test_param","action") {}) end def test_fb_request_form_with_protect_against_forgery @h.expects(:capture).returns("body") @h.expects(:protect_against_forgery?).returns(true) @h.expects(:request_forgery_protection_token).returns('forgery_token') @h.expects(:form_authenticity_token).returns('form_token') assert_equal "body", (@h.fb_request_form("invite","test_param","action") {}) end def test_fb_error_with_only_message assert_equal "", @h.fb_error("Errors have occurred!!") end def test_fb_error_with_message_and_text assert_equal "Errors have occurred!!Label can't be blank!!", @h.fb_error("Errors have occurred!!", "Label can't be blank!!") end def test_fb_explanation_with_only_message assert_equal "", @h.fb_explanation("This is an explanation") end def test_fb_explanation_with_message_and_text assert_equal "This is an explanationYou have a match", @h.fb_explanation("This is an explanation", "You have a match") end def test_fb_success_with_only_message assert_equal "", @h.fb_success("Woot!!") end def test_fb_success_with_message_and_text assert_equal "Woot!!You Rock!!", @h.fb_success("Woot!!", "You Rock!!") end def test_facebook_form_for @h.expects(:protect_against_forgery?).returns(false) form_body=@h.facebook_form_for(:model,:url=>"action") do end assert_equal "",form_body end def test_facebook_form_for_with_authenticity_token @h.expects(:protect_against_forgery?).returns(true) @h.expects(:request_forgery_protection_token).returns('forgery_token') @h.expects(:form_authenticity_token).returns('form_token') assert_equal "", (@h.facebook_form_for(:model, :url => "action") {}) end def test_fb_friend_selector assert_equal("",@h.fb_friend_selector) end def test_fb_request_form_submit assert_equal("",@h.fb_request_form_submit) end def test_fb_request_form_submit_with_uid assert_equal("",@h.fb_request_form_submit({:uid => "123456789"})) end def test_fb_request_form_submit_with_label assert_equal("",@h.fb_request_form_submit({:label => "Send Invite to Joel"})) end def test_fb_request_form_submit_with_uid_and_label assert_equal("",@h.fb_request_form_submit({:uid =>"123456789", :label => "Send Invite to Joel"})) end def test_fb_action assert_equal "Rub my pet", @h.fb_action("Rub my pet", "/growingpets/rub") end def test_fb_help assert_equal "Help", @h.fb_help("Help", "http://www.facebook.com/apps/application.php?id=6236036681") end def test_fb_create_button assert_equal "Invite Friends", @h.fb_create_button('Invite Friends', '/growingpets/invite') end def test_fb_comments assert_equal "", @h.fb_comments("xxx",true,false,4,:optional=>false) end def test_fb_comments_with_title assert_equal "TITLE", @h.fb_comments("xxx",true,false,4,:optional=>false, :title => "TITLE") end def test_fb_board assert_equal "", @h.fb_board("xxx",:optional => false) end def test_fb_dashboard @h.expects(:capture).returns("dashboard content") @h.fb_dashboard do end assert_equal "dashboard content", @h.output_buffer end def test_fb_dashboard_non_block assert_equal "", @h.fb_dashboard end def test_fb_wide @h.expects(:capture).returns("wide profile content") @h.fb_wide do end assert_equal "wide profile content", @h.output_buffer end def test_fb_narrow @h.expects(:capture).returns("narrow profile content") @h.fb_narrow do end assert_equal "narrow profile content", @h.output_buffer end end class TestModel attr_accessor :name,:facebook_id end class RailsFacebookFormbuilderTest < Test::Unit::TestCase class TestTemplate include ActionView::Helpers::TextHelper include ActionView::Helpers::CaptureHelper include ActionView::Helpers::TagHelper include Facebooker::Rails::Helpers attr_accessor :output_buffer def initialize @output_buffer="" end end def setup @_erbout = "" @test_model = TestModel.new @test_model.name="Mike" @template = TestTemplate.new @proc = Proc.new {} @form_builder = Facebooker::Rails::FacebookFormBuilder.new(:test_model,@test_model,@template,{},@proc) def @form_builder._erbout "" end end def test_text_field assert_equal "", @form_builder.text_field(:name) end def test_text_area assert_equal "Mike", @form_builder.text_area(:name) end def test_default_name_and_id assert_equal "", @form_builder.text_field(:name, {:name => 'different_name', :id => 'different_id'}) end def test_collection_typeahead flexmock(@form_builder) do |fb| fb.should_receive(:collection_typeahead_internal).with(:name,["ABC"],:size,:to_s,{}) end @form_builder.collection_typeahead(:name,["ABC"],:size,:to_s) end def test_collection_typeahead_internal assert_equal "ABC", @form_builder.collection_typeahead_internal(:name,["ABC"],:size,:to_s) end def test_buttons @form_builder.expects(:create_button).with(:first).returns("first") @form_builder.expects(:create_button).with(:second).returns("second") @template.expects(:content_tag).with("fb:editor-buttonset","firstsecond") @form_builder.buttons(:first,:second) end def test_create_button assert_equal "",@form_builder.create_button(:first) end def test_custom @template.expects(:password_field).returns("password_field") assert_equal "password_field",@form_builder.password_field(:name) end def test_text assert_equal "Mike",@form_builder.text("Mike",:label=>"custom") end def test_multi_friend_input assert_equal "",@form_builder.multi_friend_input end end class RailsPrettyErrorsTest < Test::Unit::TestCase def setup ENV['FACEBOOK_API_KEY'] = '1234567' ENV['FACEBOOK_SECRET_KEY'] = '7654321' @controller = ControllerWhichFails.new @request = ActionController::TestRequest.new @response = ActionController::TestResponse.new @controller.stubs(:verify_signature).returns(true) end def test_pretty_errors Facebooker.facebooker_config.stubs(:pretty_errors).returns(false) post :pass, example_rails_params_including_fb assert_response :success post :fail, example_rails_params_including_fb assert_response :error Facebooker.facebooker_config.stubs(:pretty_errors).returns(true) post :pass, example_rails_params_including_fb assert_response :success post :fail, example_rails_params_including_fb assert_response :error end private def example_rails_params_including_fb {"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"} end end class RailsUrlHelperExtensionsTest < Test::Unit::TestCase class UrlHelperExtensionsClass include ActionView::Helpers::UrlHelper include ActionView::Helpers::TagHelper def initialize(controller) @controller = controller end def protect_against_forgery? false end end class UrlHelperExtensionsController < NoisyController def index render :nothing => true end def do_it render :nothing => true end end class FacebookRequest < ActionController::TestRequest end def setup @controller = UrlHelperExtensionsController.new @request = FacebookRequest.new @response = ActionController::TestResponse.new @u = UrlHelperExtensionsClass.new(@controller) @u.stubs(:request_comes_from_facebook?).returns(true) @non_canvas_u = UrlHelperExtensionsClass.new(@controller) @non_canvas_u.stubs(:request_comes_from_facebook?).returns(false) @label = "Testing" @url = "test.host" @prompt = "Are you sure?" @default_title = "Please Confirm" @title = "Confirm Request" @style = {:color => 'black', :background => 'white'} @verbose_style = "{background: 'white', color: 'black'}" @default_style = "" #"'width','200px'" end def test_link_to assert_equal "Testing", @u.link_to(@label, @url) end def test_link_to_with_popup assert_raises(ActionView::ActionViewError) {@u.link_to(@label,@url, :popup=>true)} end def test_link_to_with_confirm assert_dom_equal( "#{@label}", @u.link_to(@label, @url, :confirm => @prompt) ) end def test_link_to_with_confirm_with_title assert_dom_equal( "#{@label}", @u.link_to(@label, @url, :confirm => {:title=>@title,:content=>@prompt}) ) end def test_link_to_with_confirm_with_title_and_style assert_dom_equal( "#{@label}", @u.link_to(@label, @url, :confirm => {:title=>@title,:content=>@prompt}.merge!(@style)) ) end def test_link_to_with_method assert_dom_equal( "#{@label}", @u.link_to(@label,@url, :method=>:delete)) end def test_link_to_with_confirm_and_method assert_dom_equal( "#{@label}", @u.link_to(@label,@url, :confirm=>@prompt, :method=>:delete) ) end def test_link_to_with_confirm_and_method_for_non_canvas_page assert_dom_equal( "#{@label}", @non_canvas_u.link_to(@label,@url, :confirm=>@prompt, :method=>:delete) ) end def test_button_to assert_equal "
" + "
", @u.button_to(@label,@url) end def test_button_to_with_confirm assert_equal "
" + "
", @u.button_to(@label,@url, :confirm=>@prompt) end def test_button_to_with_confirm_for_non_canvas_page assert_equal "
"+ "
", @non_canvas_u.button_to(@label,@url, :confirm=>@prompt) end def test_link_to_unless_with_true assert_equal @label, @u.link_to_unless(true,@label,@url) end def test_link_to_unless_with_false assert_equal @u.link_to(@label,@url), @u.link_to_unless(false,@label,@url) end def test_link_to_if_with_true assert_equal @u.link_to(@label,@url), @u.link_to_if(true,@label,@url) end def test_link_to_if_with_false assert_equal @label, @u.link_to_if(false,@label,@url) end end # rescue LoadError # $stderr.puts "Couldn't find action controller. That's OK. We'll skip it." end