facebooker 0.9.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. data/CHANGELOG.txt +0 -0
  2. data/COPYING +19 -0
  3. data/History.txt +3 -0
  4. data/Manifest.txt +60 -0
  5. data/README +44 -0
  6. data/README.txt +79 -0
  7. data/Rakefile +38 -0
  8. data/TODO.txt +10 -0
  9. data/facebooker.yml.tpl +36 -0
  10. data/init.rb +52 -0
  11. data/install.rb +5 -0
  12. data/lib/facebooker.rb +63 -0
  13. data/lib/facebooker/affiliation.rb +10 -0
  14. data/lib/facebooker/album.rb +11 -0
  15. data/lib/facebooker/cookie.rb +10 -0
  16. data/lib/facebooker/data.rb +38 -0
  17. data/lib/facebooker/education_info.rb +11 -0
  18. data/lib/facebooker/event.rb +26 -0
  19. data/lib/facebooker/feed.rb +65 -0
  20. data/lib/facebooker/group.rb +35 -0
  21. data/lib/facebooker/location.rb +8 -0
  22. data/lib/facebooker/model.rb +118 -0
  23. data/lib/facebooker/notifications.rb +17 -0
  24. data/lib/facebooker/parser.rb +386 -0
  25. data/lib/facebooker/photo.rb +9 -0
  26. data/lib/facebooker/rails/controller.rb +174 -0
  27. data/lib/facebooker/rails/facebook_asset_path.rb +18 -0
  28. data/lib/facebooker/rails/facebook_form_builder.rb +112 -0
  29. data/lib/facebooker/rails/facebook_request_fix.rb +14 -0
  30. data/lib/facebooker/rails/facebook_session_handling.rb +58 -0
  31. data/lib/facebooker/rails/facebook_url_rewriting.rb +31 -0
  32. data/lib/facebooker/rails/helpers.rb +535 -0
  33. data/lib/facebooker/rails/routing.rb +49 -0
  34. data/lib/facebooker/rails/test_helpers.rb +11 -0
  35. data/lib/facebooker/rails/utilities.rb +22 -0
  36. data/lib/facebooker/server_cache.rb +24 -0
  37. data/lib/facebooker/service.rb +25 -0
  38. data/lib/facebooker/session.rb +385 -0
  39. data/lib/facebooker/tag.rb +12 -0
  40. data/lib/facebooker/user.rb +200 -0
  41. data/lib/facebooker/version.rb +9 -0
  42. data/lib/facebooker/work_info.rb +9 -0
  43. data/lib/net/http_multipart_post.rb +123 -0
  44. data/lib/tasks/facebooker.rake +16 -0
  45. data/lib/tasks/tunnel.rake +39 -0
  46. data/setup.rb +1585 -0
  47. data/test/event_test.rb +15 -0
  48. data/test/facebook_cache_test.rb +43 -0
  49. data/test/facebook_data_test.rb +50 -0
  50. data/test/facebooker_test.rb +766 -0
  51. data/test/fixtures/multipart_post_body_with_only_parameters.txt +33 -0
  52. data/test/fixtures/multipart_post_body_with_single_file.txt +38 -0
  53. data/test/fixtures/multipart_post_body_with_single_file_that_has_nil_key.txt +38 -0
  54. data/test/http_multipart_post_test.rb +54 -0
  55. data/test/model_test.rb +79 -0
  56. data/test/rails_integration_test.rb +732 -0
  57. data/test/session_test.rb +396 -0
  58. data/test/test_helper.rb +54 -0
  59. data/test/user_test.rb +101 -0
  60. metadata +130 -0
@@ -0,0 +1,33 @@
1
+ --MichaelNiessnerIsSuperDuperAwesome
2
+ Content-Disposition: form-data; name="aid"
3
+
4
+ 940915667462717
5
+ --MichaelNiessnerIsSuperDuperAwesome
6
+ Content-Disposition: form-data; name="api_key"
7
+
8
+ 77a52842357422fadd912a2600e6e53c
9
+ --MichaelNiessnerIsSuperDuperAwesome
10
+ Content-Disposition: form-data; name="call_id"
11
+
12
+ 1172623588.023010
13
+ --MichaelNiessnerIsSuperDuperAwesome
14
+ Content-Disposition: form-data; name="caption"
15
+
16
+ Under the sunset
17
+ --MichaelNiessnerIsSuperDuperAwesome
18
+ Content-Disposition: form-data; name="method"
19
+
20
+ facebook.photos.upload
21
+ --MichaelNiessnerIsSuperDuperAwesome
22
+ Content-Disposition: form-data; name="session_key"
23
+
24
+ 489727d0ab2efc6e8003018c-i2LLkn8BDb2s.
25
+ --MichaelNiessnerIsSuperDuperAwesome
26
+ Content-Disposition: form-data; name="sig"
27
+
28
+ dfa724b8a5cd97d9df4baf2b60d3484c
29
+ --MichaelNiessnerIsSuperDuperAwesome
30
+ Content-Disposition: form-data; name="v"
31
+
32
+ 1.0
33
+ --MichaelNiessnerIsSuperDuperAwesome--
@@ -0,0 +1,38 @@
1
+ --MichaelNiessnerIsSuperDuperAwesome
2
+ Content-Disposition: form-data; name="aid"
3
+
4
+ 940915667462717
5
+ --MichaelNiessnerIsSuperDuperAwesome
6
+ Content-Disposition: form-data; name="api_key"
7
+
8
+ 77a52842357422fadd912a2600e6e53c
9
+ --MichaelNiessnerIsSuperDuperAwesome
10
+ Content-Disposition: form-data; name="call_id"
11
+
12
+ 1172623588.023010
13
+ --MichaelNiessnerIsSuperDuperAwesome
14
+ Content-Disposition: form-data; name="caption"
15
+
16
+ Under the sunset
17
+ --MichaelNiessnerIsSuperDuperAwesome
18
+ Content-Disposition: form-data; name="method"
19
+
20
+ facebook.photos.upload
21
+ --MichaelNiessnerIsSuperDuperAwesome
22
+ Content-Disposition: form-data; name="session_key"
23
+
24
+ 489727d0ab2efc6e8003018c-i2LLkn8BDb2s.
25
+ --MichaelNiessnerIsSuperDuperAwesome
26
+ Content-Disposition: form-data; name="sig"
27
+
28
+ dfa724b8a5cd97d9df4baf2b60d3484c
29
+ --MichaelNiessnerIsSuperDuperAwesome
30
+ Content-Disposition: form-data; name="v"
31
+
32
+ 1.0
33
+ --MichaelNiessnerIsSuperDuperAwesome
34
+ Content-Disposition: form-data; name="file"; filename="somefilename.jpg"
35
+ Content-Type: image/jpg
36
+
37
+ [Raw file data here]
38
+ --MichaelNiessnerIsSuperDuperAwesome--
@@ -0,0 +1,38 @@
1
+ --MichaelNiessnerIsSuperDuperAwesome
2
+ Content-Disposition: form-data; name="aid"
3
+
4
+ 940915667462717
5
+ --MichaelNiessnerIsSuperDuperAwesome
6
+ Content-Disposition: form-data; name="api_key"
7
+
8
+ 77a52842357422fadd912a2600e6e53c
9
+ --MichaelNiessnerIsSuperDuperAwesome
10
+ Content-Disposition: form-data; name="call_id"
11
+
12
+ 1172623588.023010
13
+ --MichaelNiessnerIsSuperDuperAwesome
14
+ Content-Disposition: form-data; name="caption"
15
+
16
+ Under the sunset
17
+ --MichaelNiessnerIsSuperDuperAwesome
18
+ Content-Disposition: form-data; name="method"
19
+
20
+ facebook.photos.upload
21
+ --MichaelNiessnerIsSuperDuperAwesome
22
+ Content-Disposition: form-data; name="session_key"
23
+
24
+ 489727d0ab2efc6e8003018c-i2LLkn8BDb2s.
25
+ --MichaelNiessnerIsSuperDuperAwesome
26
+ Content-Disposition: form-data; name="sig"
27
+
28
+ dfa724b8a5cd97d9df4baf2b60d3484c
29
+ --MichaelNiessnerIsSuperDuperAwesome
30
+ Content-Disposition: form-data; name="v"
31
+
32
+ 1.0
33
+ --MichaelNiessnerIsSuperDuperAwesome
34
+ Content-Disposition: form-data; filename="somefilename.jpg"
35
+ Content-Type: image/jpg
36
+
37
+ [Raw file data here]
38
+ --MichaelNiessnerIsSuperDuperAwesome--
@@ -0,0 +1,54 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+ require 'rubygems'
3
+ require 'flexmock/test_unit'
4
+ require 'net/http_multipart_post'
5
+
6
+ class HttpMulitpartPostTest < Test::Unit::TestCase
7
+ def setup
8
+ super
9
+ end
10
+
11
+ def fixture(string)
12
+ File.open(File.dirname(__FILE__) + "/fixtures/#{string}.txt").read
13
+ end
14
+
15
+ def test_multipart_post_with_only_parameters
16
+ params = add_sample_facebook_params({})
17
+ post = Net::HTTP::MultipartPost.new("uri", params)
18
+ assert_equal fixture("multipart_post_body_with_only_parameters"), post.send(:body)
19
+ end
20
+
21
+ def test_multipart_post_with_a_single_file
22
+ params = add_sample_facebook_params({})
23
+ params[:file] = Net::HTTP::MultipartPostFile.new
24
+ params[:file].filename = "somefilename.jpg"
25
+ params[:file].content_type = "image/jpg"
26
+ params[:file].data = "[Raw file data here]"
27
+ post = Net::HTTP::MultipartPost.new("uri", params)
28
+ assert_equal fixture("multipart_post_body_with_single_file"), post.send(:body)
29
+ end
30
+
31
+ def test_multipart_post_with_a_single_file_parameter_that_has_nil_key
32
+ params = add_sample_facebook_params({})
33
+ params[nil] = Net::HTTP::MultipartPostFile.new("somefilename.jpg", "image/jpg", "[Raw file data here]")
34
+ post = Net::HTTP::MultipartPost.new("uri", params)
35
+ assert_equal fixture("multipart_post_body_with_single_file_that_has_nil_key"), post.send(:body)
36
+ end
37
+
38
+ def test_multipart_post_should_have_correct_content_type
39
+ post = Net::HTTP::MultipartPost.new("uri", {})
40
+ assert post.send(:content_type) =~ /multipart\/form-data; boundary=/
41
+ end
42
+
43
+ def add_sample_facebook_params(hash)
44
+ hash[:method] = "facebook.photos.upload"
45
+ hash[:v] = "1.0"
46
+ hash[:api_key] = "77a52842357422fadd912a2600e6e53c"
47
+ hash[:session_key] = "489727d0ab2efc6e8003018c-i2LLkn8BDb2s."
48
+ hash[:call_id] = "1172623588.023010"
49
+ hash[:caption] = "Under the sunset"
50
+ hash[:aid] = "940915667462717"
51
+ hash[:sig] = "dfa724b8a5cd97d9df4baf2b60d3484c"
52
+ hash
53
+ end
54
+ end
@@ -0,0 +1,79 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+ require 'rubygems'
3
+ require 'flexmock/test_unit'
4
+
5
+ class TestFacebooker < Test::Unit::TestCase
6
+
7
+ class ComplexThing
8
+ include Facebooker::Model
9
+ attr_accessor :weight, :height
10
+ end
11
+
12
+ class Thing
13
+ include Facebooker::Model
14
+ attr_accessor :name, :job
15
+ hash_settable_accessor :complex_thing, ComplexThing
16
+ hash_settable_list_accessor :list_of_complex_things, ComplexThing
17
+ end
18
+
19
+ class PopulatingThing
20
+ include Facebooker::Model
21
+ populating_attr_accessor :first_name
22
+ end
23
+
24
+ def test_can_instantiate_an_object_with_a_hash
25
+ h = {:name => "Blob", :job => "Monster"}
26
+ assert_equal("Blob", Thing.from_hash(h).name)
27
+ end
28
+
29
+ def test_if_no_hash_is_given_to_model_constructor_no_attributes_are_set
30
+ assert_nothing_raised {
31
+ t = Thing.new
32
+ assert_nil(t.name)
33
+ }
34
+ end
35
+
36
+ def test_can_declare_hash_settable_attributes
37
+ t = Thing.new({})
38
+ t.complex_thing = {:weight => 123, :height => 5.4}
39
+ assert_equal(123, t.complex_thing.weight)
40
+ t.complex_thing = ComplexThing.new(:weight => 321)
41
+ assert_equal(321, t.complex_thing.weight)
42
+ end
43
+
44
+ def test_can_declare_attributes_which_are_settable_via_a_list_of_hashes
45
+ t = Thing.new
46
+ t.list_of_complex_things = [{:weight => 444, :height => 123.0}, {:weight => 222, :height => 321.1}]
47
+ assert_equal("123.0, 321.1", t.list_of_complex_things.map{|ct| ct.height.to_s}.sort.join(', '))
48
+ t.list_of_complex_things = [ComplexThing.new(:weight => 555), ComplexThing.new(:weight => 111)]
49
+ assert_equal("111, 555", t.list_of_complex_things.map{|ct| ct.weight.to_s}.sort.join(', '))
50
+ end
51
+
52
+ def test_if_you_try_to_use_a_models_session_without_initializing_it_first_you_get_a_descriptive_error
53
+ t = Thing.new
54
+ assert_raises(Facebooker::Model::UnboundSessionException) {
55
+ t.session
56
+ }
57
+ end
58
+
59
+ def test_populating_reader_will_call_populate_if_model_was_not_previously_populated
60
+ t = PopulatingThing.new
61
+ flexmock(t).should_receive(:populate).once
62
+ t.first_name
63
+ end
64
+
65
+ def test_populating_reader_will_not_call_populate_if_model_was_previously_populated
66
+ t = PopulatingThing.new
67
+ flexmock(t).should_receive(:populated?).and_return(true)
68
+ flexmock(t).should_receive(:populate).never
69
+ t.first_name
70
+ end
71
+
72
+ def test_attempting_to_access_a_populating_reader_will_raise_an_exception_if_populate_was_not_defined
73
+ t = PopulatingThing.new
74
+ assert_raises(NotImplementedError) {
75
+ t.first_name
76
+ }
77
+ end
78
+ end
79
+
@@ -0,0 +1,732 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+ require 'ruby-debug'
3
+ begin
4
+ require 'action_controller'
5
+ require 'action_controller/test_process'
6
+ require 'facebooker/rails/controller'
7
+ require 'facebooker/rails/helpers'
8
+ require 'facebooker/rails/facebook_form_builder'
9
+ require File.dirname(__FILE__)+'/../init'
10
+ require 'mocha'
11
+
12
+ ActionController::Routing::Routes.draw do |map|
13
+ map.connect '', :controller=>"facebook",:conditions=>{:canvas=>true}
14
+ map.connect '', :controller=>"plain_old_rails"
15
+ map.resources :comments, :controller=>"plain_old_rails"
16
+ map.connect 'require_auth/:action', :controller => "controller_which_requires_facebook_authentication"
17
+ map.connect 'require_install/:action', :controller => "controller_which_requires_application_installation"
18
+ map.connect ':controller/:action/:id', :controller => "plain_old_rails"
19
+ end
20
+
21
+ class NoisyController < ActionController::Base
22
+ include Facebooker::Rails::Controller
23
+ def rescue_action(e) raise e end
24
+ end
25
+
26
+ class ControllerWhichRequiresFacebookAuthentication < NoisyController
27
+ ensure_authenticated_to_facebook
28
+ def index
29
+ render :text => 'score!'
30
+ end
31
+ def link_test
32
+ options = {}
33
+ options[:canvas] = true if params[:canvas] == true
34
+ options[:canvas] = false if params[:canvas] == false
35
+ render :text=>url_for(options)
36
+ end
37
+
38
+ def image_test
39
+ render :inline=>"<%=image_tag 'image.png'%>"
40
+ end
41
+
42
+ def fb_params_test
43
+ render :text=>facebook_params['user']
44
+ end
45
+
46
+ end
47
+ class ControllerWhichRequiresApplicationInstallation < NoisyController
48
+ ensure_application_is_installed_by_facebook_user
49
+ def index
50
+ render :text => 'installed!'
51
+ end
52
+ end
53
+ class FacebookController < ActionController::Base
54
+ def index
55
+ end
56
+ end
57
+
58
+ class PlainOldRailsController < ActionController::Base
59
+ def index
60
+ end
61
+ def link_test
62
+ options = {}
63
+ options[:canvas] = true if params[:canvas] == true
64
+ options[:canvas] = false if params[:canvas] == false
65
+ render :text => url_for(options)
66
+ end
67
+
68
+ def named_route_test
69
+ render :text=>comments_url()
70
+ end
71
+ def canvas_false_test
72
+ render :text=>comments_url(:canvas=>false)
73
+ end
74
+ def canvas_true_test
75
+ render :text=>comments_url(:canvas=>true)
76
+ end
77
+ end
78
+
79
+ # you can't use asset_recognize, because it can't pass parameters in to the requests
80
+ class UrlRecognitionTests < Test::Unit::TestCase
81
+ def test_detects_in_canvas
82
+ request = ActionController::TestRequest.new({"fb_sig_in_canvas"=>"1"}, {}, nil)
83
+ request.path = "/"
84
+ ActionController::Routing::Routes.recognize(request)
85
+ assert_equal({"controller"=>"facebook","action"=>"index"},request.path_parameters)
86
+ end
87
+
88
+ def test_routes_when_not_in_canvas
89
+ request = ActionController::TestRequest.new({}, {}, nil)
90
+ request.path = "/"
91
+ ActionController::Routing::Routes.recognize(request)
92
+ assert_equal({"controller"=>"plain_old_rails","action"=>"index"},request.path_parameters)
93
+ end
94
+ end
95
+
96
+ class RailsIntegrationTestForNonFacebookControllers < Test::Unit::TestCase
97
+ def setup
98
+ ENV['FACEBOOK_CANVAS_PATH'] ='facebook_app_name'
99
+ ENV['FACEBOOK_API_KEY'] = '1234567'
100
+ ENV['FACEBOOK_SECRET_KEY'] = '7654321'
101
+ @controller = PlainOldRailsController.new
102
+ @request = ActionController::TestRequest.new
103
+ @response = ActionController::TestResponse.new
104
+ end
105
+
106
+ def test_url_for_links_to_callback_if_canvas_is_false_and_in_canvas
107
+ get :link_test, example_rails_params
108
+ assert_match /test.host/,@response.body
109
+ end
110
+
111
+ def test_named_route_doesnt_include_canvas_path_when_not_in_canvas
112
+ get :named_route_test, example_rails_params
113
+ assert_equal "http://test.host/comments",@response.body
114
+ end
115
+ def test_named_route_includes_canvas_path_when_in_canvas
116
+ get :named_route_test, example_rails_params_including_fb
117
+ assert_equal "http://apps.facebook.com/facebook_app_name/comments",@response.body
118
+ end
119
+ def test_named_route_doesnt_include_canvas_path_when_in_canvas_with_canvas_equals_false
120
+ get :canvas_false_test, example_rails_params_including_fb
121
+ assert_equal "http://test.host/comments",@response.body
122
+ end
123
+ def test_named_route_does_include_canvas_path_when_not_in_canvas_with_canvas_equals_true
124
+ get :canvas_true_test, example_rails_params
125
+ assert_equal "http://apps.facebook.com/facebook_app_name/comments",@response.body
126
+ end
127
+
128
+ private
129
+ def example_rails_params
130
+ { "action"=>"index", "controller"=>"plain_old_rails_controller" }
131
+ end
132
+ def example_rails_params_including_fb(options={})
133
+ {"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)
134
+ end
135
+
136
+ end
137
+
138
+
139
+ class RailsIntegrationTestForApplicationInstallation < Test::Unit::TestCase
140
+ def setup
141
+ ENV['FACEBOOK_API_KEY'] = '1234567'
142
+ ENV['FACEBOOK_SECRET_KEY'] = '7654321'
143
+ @controller = ControllerWhichRequiresApplicationInstallation.new
144
+ @request = ActionController::TestRequest.new
145
+ @response = ActionController::TestResponse.new
146
+ @controller.stubs(:verify_signature).returns(true)
147
+ end
148
+
149
+ def test_if_controller_requires_application_installation_unauthenticated_requests_will_redirect_to_install_page
150
+ get :index
151
+ assert_response :redirect
152
+ assert_equal("http://www.facebook.com/install.php?api_key=1234567&v=1.0", @response.headers['Location'])
153
+ end
154
+
155
+ def test_if_controller_requires_application_installation_authenticated_requests_without_installation_will_redirect_to_install_page
156
+ get :index, example_rails_params_including_fb
157
+ assert_response :success
158
+ assert_equal("<fb:redirect url=\"http://www.facebook.com/install.php?api_key=1234567&v=1.0\" />", @response.body)
159
+ end
160
+
161
+ def test_if_controller_requires_application_installation_authenticated_requests_with_installation_will_render
162
+ get :index, example_rails_params_including_fb.merge('fb_sig_added' => "1")
163
+ assert_response :success
164
+ assert_equal("installed!", @response.body)
165
+ end
166
+
167
+ private
168
+ def example_rails_params_including_fb
169
+ {"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"}
170
+ end
171
+ end
172
+
173
+ class RailsIntegrationTest < Test::Unit::TestCase
174
+ def setup
175
+ ENV['FACEBOOK_CANVAS_PATH'] ='root'
176
+ ENV['FACEBOOK_API_KEY'] = '1234567'
177
+ ENV['FACEBOOK_SECRET_KEY'] = '7654321'
178
+ ActionController::Base.asset_host="http://root.example.com"
179
+ @controller = ControllerWhichRequiresFacebookAuthentication.new
180
+ @request = ActionController::TestRequest.new
181
+ @response = ActionController::TestResponse.new
182
+ @controller.stubs(:verify_signature).returns(true)
183
+
184
+ end
185
+
186
+ def test_if_controller_requires_facebook_authentication_unauthenticated_requests_will_redirect
187
+ get :index
188
+ assert_response :redirect
189
+ assert_equal("http://www.facebook.com/login.php?api_key=1234567&v=1.0", @response.headers['Location'])
190
+ end
191
+
192
+ def test_facebook_params_are_parsed_into_a_separate_hash
193
+ get :index, example_rails_params_including_fb
194
+ facebook_params = @controller.facebook_params
195
+ assert_equal([8, 8], [facebook_params['time'].day, facebook_params['time'].mon])
196
+ end
197
+
198
+ def test_facebook_params_convert_in_canvas_to_boolean
199
+ get :index, example_rails_params_including_fb
200
+ assert_equal(true, @controller.facebook_params['in_canvas'])
201
+ end
202
+
203
+ def test_facebook_params_convert_added_to_boolean_false
204
+ get :index, example_rails_params_including_fb
205
+ assert_equal(false, @controller.facebook_params['added'])
206
+ end
207
+
208
+ def test_facebook_params_convert_added_to_boolean_true
209
+ get :index, example_rails_params_including_fb.merge('fb_sig_added' => "1")
210
+ assert_equal(true, @controller.facebook_params['added'])
211
+ end
212
+
213
+ def test_facebook_params_convert_expirey_into_time_or_nil
214
+ get :index, example_rails_params_including_fb
215
+ assert_nil(@controller.facebook_params['expires'])
216
+ modified_params = example_rails_params_including_fb
217
+ modified_params['fb_sig_expires'] = modified_params['fb_sig_time']
218
+ setup # reset session and cached params
219
+ get :index, modified_params
220
+ assert_equal([8, 8], [@controller.facebook_params['time'].day, @controller.facebook_params['time'].mon])
221
+ end
222
+
223
+ def test_facebook_params_convert_friend_list_to_parsed_array_of_friend_ids
224
+ get :index, example_rails_params_including_fb
225
+ assert_kind_of(Array, @controller.facebook_params['friends'])
226
+ assert_equal(111, @controller.facebook_params['friends'].size)
227
+ end
228
+
229
+ def test_session_can_be_resecured_from_facebook_params
230
+ get :index, example_rails_params_including_fb
231
+ assert_equal(744961110, @controller.facebook_session.user.id)
232
+ end
233
+
234
+ def test_existing_secured_session_is_used_if_available
235
+ session = Facebooker::Session.create(ENV['FACEBOOK_API_KEY'], ENV['FACEBOOK_SECRET_KEY'])
236
+ session.secure_with!("a session key", "1111111", Time.now.to_i + 60)
237
+ get :index, example_rails_params_including_fb, {:facebook_session => session}
238
+ assert_equal(1111111, @controller.facebook_session.user.id)
239
+ end
240
+
241
+ def test_session_can_be_secured_with_auth_token
242
+ auth_token = 'ohaiauthtokenhere111'
243
+ modified_params = example_rails_params_including_fb
244
+ modified_params.delete('fb_sig_session_key')
245
+ modified_params['auth_token'] = auth_token
246
+ session_mock = flexmock(session = Facebooker::Session.create(ENV['FACEBOOK_API_KEY'], ENV['FACEBOOK_SECRET_KEY']))
247
+ session_mock.should_receive(:post).with('facebook.auth.getSession', :auth_token => auth_token).once.and_return({}).ordered
248
+ flexmock(@controller).should_receive(:new_facebook_session).once.and_return(session).ordered
249
+ get :index, modified_params
250
+ end
251
+
252
+ def test_user_friends_can_be_populated_from_facebook_params_if_available
253
+ get :index, example_rails_params_including_fb
254
+ assert_not_nil(friends = @controller.facebook_session.user.instance_variable_get("@friends"))
255
+ assert_equal(111, friends.size)
256
+ end
257
+
258
+ def test_fbml_redirect_tag_handles_hash_parameters_correctly
259
+ get :index, example_rails_params_including_fb
260
+ assert_equal "<fb:redirect url=\"http://apps.facebook.com/root/require_auth\" />", @controller.send(:fbml_redirect_tag, :action => :index,:canvas=>true)
261
+ end
262
+
263
+ def test_redirect_to_renders_fbml_redirect_tag_if_request_is_for_a_facebook_canvas
264
+ get :index, example_rails_params_including_fb_for_user_not_logged_into_application
265
+ assert_response :success
266
+ assert_equal("<fb:redirect url=\"http://www.facebook.com/login.php?api_key=1234567&v=1.0\" />", @response.body)
267
+ end
268
+
269
+ def test_url_for_links_to_canvas_if_canvas_is_true_and_not_in_canvas
270
+ get :link_test,example_rails_params_including_fb.merge(:fb_sig_in_canvas=>0,:canvas=>true)
271
+ assert_match /apps.facebook.com/,@response.body
272
+ end
273
+
274
+ def test_includes_relative_url_root_when_linked_to_canvas
275
+ get :link_test,example_rails_params_including_fb.merge(:fb_sig_in_canvas=>0,:canvas=>true)
276
+ assert_match /root/,@response.body
277
+ end
278
+
279
+ def test_url_for_links_to_callback_if_canvas_is_false_and_in_canvas
280
+ get :link_test,example_rails_params_including_fb.merge(:fb_sig_in_canvas=>0,:canvas=>false)
281
+ assert_match /test.host/,@response.body
282
+ end
283
+
284
+ def test_url_for_doesnt_include_url_root_when_not_linked_to_canvas
285
+ get :link_test,example_rails_params_including_fb.merge(:fb_sig_in_canvas=>0,:canvas=>false)
286
+ assert !@response.body.match(/root/)
287
+ end
288
+
289
+ def test_url_for_links_to_canvas_if_canvas_is_not_set
290
+ get :link_test,example_rails_params_including_fb
291
+ assert_match /apps.facebook.com/,@response.body
292
+ end
293
+
294
+ def test_image_tag
295
+ get :image_test, example_rails_params_including_fb
296
+ assert_equal "<img alt=\"Image\" src=\"http://root.example.com/images/image.png\" />",@response.body
297
+ end
298
+
299
+
300
+ private
301
+ def example_rails_params_including_fb_for_user_not_logged_into_application
302
+ {"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"}
303
+ end
304
+
305
+ def example_rails_params_including_fb
306
+ {"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"}
307
+ end
308
+ end
309
+
310
+
311
+ class RailsSignatureTest < Test::Unit::TestCase
312
+ def setup
313
+ ENV['FACEBOOKER_RELATIVE_URL_ROOT'] ='root'
314
+ ENV['FACEBOOK_API_KEY'] = '1234567'
315
+ ENV['FACEBOOK_SECRET_KEY'] = '7654321'
316
+ @controller = ControllerWhichRequiresFacebookAuthentication.new
317
+ @request = ActionController::TestRequest.new
318
+ @response = ActionController::TestResponse.new
319
+
320
+ end
321
+
322
+ def test_should_raise_too_old_for_replayed_session
323
+ begin
324
+ get :fb_params_test,example_rails_params_including_fb
325
+ fail "No SignatureTooOld raised"
326
+ rescue Facebooker::Session::SignatureTooOld=>e
327
+ end
328
+ end
329
+
330
+ def test_should_raise_on_bad_sig
331
+ begin
332
+ get :fb_params_test,example_rails_params_including_fb("fb_sig"=>'incorrect')
333
+ fail "No IncorrectSignature raised"
334
+ rescue Facebooker::Session::IncorrectSignature=>e
335
+ end
336
+ end
337
+
338
+ def test_valid_signature
339
+ @controller.expects(:earliest_valid_session).returns(Time.at(1186588275.5988)-1)
340
+ get :fb_params_test,example_rails_params_including_fb
341
+
342
+ end
343
+
344
+ def example_rails_params_including_fb(options={})
345
+ {"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)
346
+ end
347
+
348
+ end
349
+ class RailsHelperTest < Test::Unit::TestCase
350
+ class HelperClass
351
+ include ActionView::Helpers::TextHelper
352
+ include ActionView::Helpers::CaptureHelper
353
+ include ActionView::Helpers::TagHelper
354
+ include ActionView::Helpers::AssetTagHelper
355
+ include Facebooker::Rails::Helpers
356
+ attr_accessor :flash
357
+ def initialize
358
+ @flash={}
359
+ @template = self
360
+ @content_for_test_param="Test Param"
361
+ end
362
+ #used for stubbing out the form builder
363
+ def url_for(arg)
364
+ arg
365
+ end
366
+ def fields_for(*args)
367
+ ""
368
+ end
369
+
370
+ end
371
+
372
+ # used for capturing the contents of some of the helper tests
373
+ # this duplicates the rails template system
374
+ attr_accessor :_erbout
375
+
376
+ def setup
377
+ ENV['FACEBOOK_CANVAS_PATH'] ='facebook'
378
+ ENV['FACEBOOK_API_KEY'] = '1234567'
379
+ ENV['FACEBOOK_SECRET_KEY'] = '7654321'
380
+
381
+ @_erbout = ""
382
+ @h = HelperClass.new
383
+ #use an asset path where the canvas path equals the hostname to make sure we handle that case right
384
+ ActionController::Base.asset_host='http://facebook.host.com'
385
+ end
386
+
387
+ def test_fb_profile_pic
388
+ assert_equal "<fb:profile-pic uid=\"1234\" />", @h.fb_profile_pic("1234")
389
+ end
390
+
391
+ def test_fb_profile_pic_with_valid_size
392
+ assert_equal "<fb:profile-pic size=\"small\" uid=\"1234\" />", @h.fb_profile_pic("1234", :size => :small)
393
+ end
394
+
395
+ def test_fb_profile_pic_with_invalid_size
396
+ assert_raises(ArgumentError) {@h.fb_profile_pic("1234", :size => :mediumm)}
397
+ end
398
+
399
+ def test_fb_photo
400
+ assert_equal "<fb:photo pid=\"1234\" />",@h.fb_photo("1234")
401
+ end
402
+
403
+ def test_fb_photo_with_object_responding_to_photo_id
404
+ photo = flexmock("photo", :photo_id => "5678")
405
+ assert_equal "<fb:photo pid=\"5678\" />", @h.fb_photo(photo)
406
+ end
407
+
408
+ def test_fb_photo_with_invalid_size
409
+ assert_raises(ArgumentError) {@h.fb_photo("1234", :size => :medium)}
410
+ end
411
+
412
+ def test_fb_photo_with_invalid_size_value
413
+ assert_raises(ArgumentError) {@h.fb_photo("1234", :size => :mediumm)}
414
+ end
415
+
416
+ def test_fb_photo_with_invalid_align_value
417
+ assert_raises(ArgumentError) {@h.fb_photo("1234", :align => :rightt)}
418
+ end
419
+
420
+ def test_fb_photo_with_valid_align_value
421
+ assert_equal "<fb:photo align=\"right\" pid=\"1234\" />",@h.fb_photo("1234", :align => :right)
422
+ end
423
+
424
+ def test_fb_name_with_invalid_key
425
+ assert_raises(ArgumentError) {@h.fb_name(1234, :sizee => false)}
426
+ end
427
+
428
+ def test_fb_name
429
+ assert_equal "<fb:name uid=\"1234\" />",@h.fb_name("1234")
430
+ end
431
+
432
+ def test_fb_name_with_transformed_key
433
+ assert_equal "<fb:name uid=\"1234\" useyou=\"true\" />", @h.fb_name(1234, :use_you => true)
434
+ end
435
+
436
+ def test_fb_name_with_user_responding_to_facebook_id
437
+ user = flexmock("user", :facebook_id => "5678")
438
+ assert_equal "<fb:name uid=\"5678\" />", @h.fb_name(user)
439
+ end
440
+
441
+ def test_fb_name_with_invalid_key
442
+ assert_raises(ArgumentError) {@h.fb_name(1234, :linkd => false)}
443
+ end
444
+
445
+ def test_fb_tabs
446
+ assert_equal "<fb:tabs></fb:tabs>", @h.fb_tabs{}
447
+ end
448
+
449
+ def test_fb_tab_item
450
+ assert_equal "<fb:tab-item href=\"http://www.google.com\" title=\"Google\" />", @h.fb_tab_item("Google", "http://www.google.com")
451
+ end
452
+
453
+ def test_fb_tab_item_raises_exception_for_invalid_option
454
+ assert_raises(ArgumentError) {@h.fb_tab_item("Google", "http://www.google.com", :alignn => :right)}
455
+ end
456
+
457
+ def test_fb_tab_item_raises_exception_for_invalid_align_value
458
+ assert_raises(ArgumentError) {@h.fb_tab_item("Google", "http://www.google.com", :align => :rightt)}
459
+ end
460
+
461
+ def test_fb_req_choice
462
+ assert_equal "<fb:req-choice label=\"label\" url=\"url\" />", @h.fb_req_choice("label","url")
463
+ end
464
+
465
+ def test_fb_multi_friend_selector
466
+ assert_equal "<fb:multi-friend-selector actiontext=\"This is a message\" max=\"20\" />", @h.fb_multi_friend_selector("This is a message")
467
+ end
468
+ def test_fb_multi_friend_selector_with_options
469
+ assert_equal "<fb:multi-friend-selector actiontext=\"This is a message\" exclude_ids=\"1,2\" max=\"20\" />", @h.fb_multi_friend_selector("This is a message",:exclude_ids=>"1,2")
470
+ end
471
+
472
+ def test_fb_comments
473
+ 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)
474
+ end
475
+
476
+ def test_fb_title
477
+ assert_equal "<fb:title>This is the canvas page window title</fb:title>", @h.fb_title("This is the canvas page window title")
478
+ end
479
+
480
+ def test_fb_google_analytics
481
+ assert_equal "<fb:google-analytics uacct=\"UA-9999999-99\" />", @h.fb_google_analytics("UA-9999999-99")
482
+ end
483
+
484
+ def test_fb_if_is_user_with_single_object
485
+ user = flexmock("user", :facebook_id => "5678")
486
+ assert_equal "<fb:if-is-user uid=\"5678\"></fb:if-is-user>", @h.fb_if_is_user(user){}
487
+ end
488
+
489
+ def test_fb_if_is_user_with_array
490
+ user1 = flexmock("user", :facebook_id => "5678")
491
+ user2 = flexmock("user", :facebook_id => "1234")
492
+ assert_equal "<fb:if-is-user uid=\"5678,1234\"></fb:if-is-user>", @h.fb_if_is_user([user1,user2]){}
493
+ end
494
+
495
+ def test_fb_else
496
+ assert_equal "<fb:else></fb:else>", @h.fb_else{}
497
+ end
498
+
499
+ def test_fb_about_url
500
+ ENV["FACEBOOK_API_KEY"]="1234"
501
+ assert_equal "http://www.facebook.com/apps/application.php?api_key=1234", @h.fb_about_url
502
+ end
503
+
504
+ def test_fb_ref_with_url
505
+ assert_equal "<fb:ref url=\"A URL\" />", @h.fb_ref(:url => "A URL")
506
+ end
507
+
508
+ def test_fb_ref_with_handle
509
+ assert_equal "<fb:ref handle=\"A Handle\" />", @h.fb_ref(:handle => "A Handle")
510
+ end
511
+
512
+ def test_fb_ref_with_invalid_attribute
513
+ assert_raises(ArgumentError) {@h.fb_ref(:handlee => "A HANLDE")}
514
+ end
515
+
516
+ def test_fb_ref_with_handle_and_url
517
+ assert_raises(ArgumentError) {@h.fb_ref(:url => "URL", :handle => "HANDLE")}
518
+ end
519
+
520
+ def test_facebook_messages_notice
521
+ @h.flash[:notice]="A message"
522
+ assert_equal "<fb:success message=\"A message\" />",@h.facebook_messages
523
+ end
524
+
525
+ def test_facebook_messages_error
526
+ @h.flash[:error]="An error"
527
+ assert_equal "<fb:error message=\"An error\" />",@h.facebook_messages
528
+ end
529
+ def test_fb_wall_post
530
+ assert_equal "<fb:wallpost uid=\"1234\">A wall post</fb:wallpost>",@h.fb_wall_post("1234","A wall post")
531
+ end
532
+
533
+ def test_fb_pronoun
534
+ assert_equal "<fb:pronoun uid=\"1234\" />", @h.fb_pronoun(1234)
535
+ end
536
+
537
+ def test_fb_pronoun_with_transformed_key
538
+ assert_equal "<fb:pronoun uid=\"1234\" usethey=\"true\" />", @h.fb_pronoun(1234, :use_they => true)
539
+ end
540
+
541
+ def test_fb_pronoun_with_user_responding_to_facebook_id
542
+ user = flexmock("user", :facebook_id => "5678")
543
+ assert_equal "<fb:pronoun uid=\"5678\" />", @h.fb_pronoun(user)
544
+ end
545
+
546
+ def test_fb_pronoun_with_invalid_key
547
+ assert_raises(ArgumentError) {@h.fb_pronoun(1234, :posessive => true)}
548
+ end
549
+
550
+ def test_fb_wall
551
+ @h.expects(:capture).returns("wall content")
552
+ @h.fb_wall do
553
+ end
554
+ assert_equal "<fb:wall>wall content</fb:wall>",_erbout
555
+ end
556
+
557
+ def test_fb_multi_friend_request
558
+ @h.expects(:capture).returns("body")
559
+ @h.expects(:fb_multi_friend_selector).returns("friend selector")
560
+ assert_equal "<fb:request-form action=\"action\" content=\"body\" invite=\"true\" method=\"post\" type=\"invite\">friend selector</fb:request-form>",
561
+ (@h.fb_multi_friend_request("invite","ignored","action") {})
562
+ end
563
+
564
+ def test_fb_request_form
565
+ @h.expects(:capture).returns("body")
566
+ assert_equal "<fb:request-form action=\"action\" content=\"Test Param\" invite=\"true\" method=\"post\" type=\"invite\">body</fb:request-form>",
567
+ (@h.fb_request_form("invite","test_param","action") {})
568
+
569
+ end
570
+
571
+ def test_fb_error_with_only_message
572
+ assert_equal "<fb:error message=\"Errors have occurred!!\" />", @h.fb_error("Errors have occurred!!")
573
+ end
574
+
575
+ def test_fb_error_with_message_and_text
576
+ 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!!")
577
+ end
578
+
579
+ def test_fb_explanation_with_only_message
580
+ assert_equal "<fb:explanation message=\"This is an explanation\" />", @h.fb_explanation("This is an explanation")
581
+ end
582
+
583
+ def test_fb_explanation_with_message_and_text
584
+ 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")
585
+ end
586
+
587
+ def test_fb_success_with_only_message
588
+ assert_equal "<fb:success message=\"Woot!!\" />", @h.fb_success("Woot!!")
589
+ end
590
+
591
+ def test_fb_success_with_message_and_text
592
+ assert_equal "<fb:success><fb:message>Woot!!</fb:message>You Rock!!</fb:success>", @h.fb_success("Woot!!", "You Rock!!")
593
+ end
594
+
595
+ def test_facebook_form_for
596
+ form_body=@h.facebook_form_for(:model,:url=>"action") do
597
+ end
598
+ assert_equal "<fb:editor action=\"action\"></fb:editor>",form_body
599
+ end
600
+
601
+ def test_fb_friend_selector
602
+ assert_equal("<fb:friend-selector />",@h.fb_friend_selector)
603
+ end
604
+
605
+ def test_fb_request_form_submit
606
+ assert_equal("<fb:request-form-submit />",@h.fb_request_form_submit)
607
+ end
608
+
609
+ def test_fb_request_form_submit_with_uid
610
+ assert_equal("<fb:request-form-submit uid=\"123456789\" />",@h.fb_request_form_submit({:uid => "123456789"}))
611
+ end
612
+
613
+ def test_fb_request_form_submit_with_label
614
+ assert_equal("<fb:request-form-submit label=\"Send Invite to Joel\" />",@h.fb_request_form_submit({:label => "Send Invite to Joel"}))
615
+ end
616
+
617
+ def test_fb_request_form_submit_with_uid_and_label
618
+ 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"}))
619
+ end
620
+
621
+ def test_fb_action
622
+ assert_equal "<fb:action href=\"/growingpets/rub\">Rub my pet</fb:action>", @h.fb_action("Rub my pet", "/growingpets/rub")
623
+ end
624
+
625
+ def test_fb_help
626
+ 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")
627
+ end
628
+
629
+ def test_fb_create_button
630
+ assert_equal "<fb:create-button href=\"/growingpets/invite\">Invite Friends</fb:create-button>", @h.fb_create_button('Invite Friends', '/growingpets/invite')
631
+ end
632
+
633
+ def test_fb_dashboard
634
+ @h.expects(:capture).returns("dashboard content")
635
+ @h.fb_dashboard do
636
+ end
637
+ assert_equal "<fb:dashboard>dashboard content</fb:dashboard>", _erbout
638
+ end
639
+ def test_fb_dashboard_non_block
640
+ assert_equal "<fb:dashboard></fb:dashboard>", @h.fb_dashboard
641
+ end
642
+
643
+ def test_fb_wide
644
+ @h.expects(:capture).returns("wide profile content")
645
+ @h.fb_wide do
646
+ end
647
+ assert_equal "<fb:wide>wide profile content</fb:wide>", _erbout
648
+ end
649
+
650
+ def test_fb_narrow
651
+ @h.expects(:capture).returns("narrow profile content")
652
+ @h.fb_narrow do
653
+ end
654
+ assert_equal "<fb:narrow>narrow profile content</fb:narrow>", _erbout
655
+ end
656
+ end
657
+ class TestModel
658
+ attr_accessor :name,:facebook_id
659
+ end
660
+
661
+ class RailsFacebookFormbuilderTest < Test::Unit::TestCase
662
+ class TestTemplate
663
+ include ActionView::Helpers::TextHelper
664
+ include ActionView::Helpers::CaptureHelper
665
+ include ActionView::Helpers::TagHelper
666
+ include Facebooker::Rails::Helpers
667
+ attr_accessor :_erbout
668
+ def initialize
669
+ @_erbout=""
670
+ end
671
+ end
672
+ def setup
673
+ @_erbout = ""
674
+ @test_model = TestModel.new
675
+ @test_model.name="Mike"
676
+ @template = TestTemplate.new
677
+ @proc = Proc.new {}
678
+ @form_builder = Facebooker::Rails::FacebookFormBuilder.new(:test_model,@test_model,@template,{},@proc)
679
+ def @form_builder._erbout
680
+ ""
681
+ end
682
+
683
+ end
684
+
685
+ def test_text_field
686
+ assert_equal "<fb:editor-text id=\"testmodel_name\" label=\"Name\" name=\"testmodel[name]\" value=\"Mike\"></fb:editor-text>",
687
+ @form_builder.text_field(:name)
688
+ end
689
+ def test_text_area
690
+ assert_equal "<fb:editor-textarea id=\"testmodel_name\" label=\"Name\" name=\"testmodel[name]\">Mike</fb:editor-textarea>",
691
+ @form_builder.text_area(:name)
692
+ end
693
+
694
+ def test_collection_typeahead
695
+ flexmock(@form_builder) do |fb|
696
+ fb.should_receive(:collection_typeahead_internal).with(:name,["ABC"],:size,:to_s,{})
697
+ end
698
+ @form_builder.collection_typeahead(:name,["ABC"],:size,:to_s)
699
+ end
700
+
701
+ def test_collection_typeahead_internal
702
+ assert_equal "<fb:typeahead-input id=\"testmodel_name\" name=\"testmodel[name]\" value=\"Mike\"><fb:typeahead-option value=\"3\">ABC</fb:typeahead-option></fb:typeahead-input>",
703
+ @form_builder.collection_typeahead_internal(:name,["ABC"],:size,:to_s)
704
+ end
705
+
706
+ def test_buttons
707
+ @form_builder.expects(:create_button).with(:first).returns("first")
708
+ @form_builder.expects(:create_button).with(:second).returns("second")
709
+ @template.expects(:content_tag).with("fb:editor-buttonset","firstsecond")
710
+ @form_builder.buttons(:first,:second)
711
+ end
712
+
713
+ def test_create_button
714
+ assert_equal "<fb:editor-button value=\"first\"></fb:editor-button>",@form_builder.create_button(:first)
715
+ end
716
+
717
+ def test_custom
718
+ @template.expects(:password_field).returns("password_field")
719
+ assert_equal "<fb:editor-custom label=\"Name\"></fb:editor-custom>",@form_builder.password_field(:name)
720
+ end
721
+
722
+ def test_text
723
+ assert_equal "<fb:editor-custom label=\"custom\">Mike</fb:editor-custom>",@form_builder.text("Mike",:label=>"custom")
724
+ end
725
+
726
+ def test_multi_friend_input
727
+ assert_equal "<fb:editor-custom label=\"Friends\"></fb:editor-custom>",@form_builder.multi_friend_input
728
+ end
729
+ end
730
+ # rescue LoadError
731
+ # $stderr.puts "Couldn't find action controller. That's OK. We'll skip it."
732
+ end