authlogic_facebook_shim 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. data/LICENSE +20 -0
  2. data/README.rdoc +76 -0
  3. data/Rakefile +39 -0
  4. data/VERSION +1 -0
  5. data/authlogic_facebook_shim.gemspec +30 -0
  6. data/init.rb +1 -0
  7. data/lib/authlogic_facebook_shim/acts_as_authentic.rb +20 -0
  8. data/lib/authlogic_facebook_shim/helper.rb +4 -0
  9. data/lib/authlogic_facebook_shim/session/adapter.rb +1 -0
  10. data/lib/authlogic_facebook_shim/session/adapters/koala_adapter.rb +35 -0
  11. data/lib/authlogic_facebook_shim/session/config.rb +82 -0
  12. data/lib/authlogic_facebook_shim/session/facebook.rb +72 -0
  13. data/lib/authlogic_facebook_shim/session.rb +12 -0
  14. data/lib/authlogic_facebook_shim.rb +15 -0
  15. data/rails/init.rb +1 -0
  16. data/test/rails_root/app/controllers/application_controller.rb +10 -0
  17. data/test/rails_root/app/helpers/application_helper.rb +3 -0
  18. data/test/rails_root/app/models/user.rb +7 -0
  19. data/test/rails_root/app/models/user_session.rb +7 -0
  20. data/test/rails_root/config/boot.rb +110 -0
  21. data/test/rails_root/config/database.yml +10 -0
  22. data/test/rails_root/config/environment.rb +31 -0
  23. data/test/rails_root/config/environments/development.rb +0 -0
  24. data/test/rails_root/config/environments/test.rb +0 -0
  25. data/test/rails_root/config/facebook.yml +7 -0
  26. data/test/rails_root/config/initializers/authlogic_facebook_koala.rb +5 -0
  27. data/test/rails_root/config/initializers/new_rails_defaults.rb +21 -0
  28. data/test/rails_root/config/initializers/session_store.rb +15 -0
  29. data/test/rails_root/config/locales/en.yml +5 -0
  30. data/test/rails_root/config/routes.rb +43 -0
  31. data/test/rails_root/db/migrate/20101217000008_create_users.rb +37 -0
  32. data/test/rails_root/db/seeds.rb +7 -0
  33. data/test/rails_root/log/test.log +159 -0
  34. data/test/rails_root/script/console +3 -0
  35. data/test/rails_root/script/dbconsole +3 -0
  36. data/test/rails_root/script/generate +3 -0
  37. data/test/test_helper.rb +43 -0
  38. data/test/units/session/adapter_test.rb +182 -0
  39. data/test/units/session/config_test.rb +145 -0
  40. data/test/units/session/facebook_test.rb +221 -0
  41. metadata +234 -0
@@ -0,0 +1,145 @@
1
+ require File.expand_path( '../../test_helper.rb', File.dirname(__FILE__) )
2
+
3
+ class AuthlogicFacebookShim::Session::ConfigTest < ActiveSupport::TestCase
4
+
5
+ context "Session::Config" do
6
+
7
+ setup do
8
+ @session_class = Class.new(Authlogic::Session::Base)
9
+ end
10
+
11
+ context "facebook_config_file" do
12
+
13
+ should "have a default 'facebook.yml'" do
14
+ assert_equal 'facebook.yml', @session_class.facebook_config_file
15
+ end
16
+
17
+ should "have a setter method" do
18
+ fb_config = 'fbconf.yml'
19
+ @session_class.facebook_config_file = fb_config
20
+ assert_equal fb_config, @session_class.facebook_config_file
21
+ end
22
+
23
+ end
24
+
25
+ context "facebook_app_id" do
26
+
27
+ should "default to default_config.app_id" do
28
+ default_from_config = 'defaultappid'
29
+ flexmock(@session_class).should_receive('default_config.app_id').and_return(default_from_config).once
30
+ assert_equal default_from_config, @session_class.facebook_app_id
31
+ end
32
+
33
+ should "have a setter method" do
34
+ fb_app_id = '234234234'
35
+ @session_class.facebook_app_id = fb_app_id
36
+ assert_equal fb_app_id, @session_class.facebook_app_id
37
+ end
38
+
39
+ end
40
+
41
+ context "facebook_secret_key" do
42
+
43
+ should "default to default_config.secret_key" do
44
+ default_from_config = 'defaultsecretkey'
45
+ flexmock(@session_class).should_receive('default_config.secret_key').and_return(default_from_config).once
46
+ assert_equal default_from_config, @session_class.facebook_secret_key
47
+ end
48
+
49
+ should "have a setter method" do
50
+ fb_secret = '553246736447566b583138525a716e693950736'
51
+ @session_class.facebook_secret_key = fb_secret
52
+ assert_equal fb_secret, @session_class.facebook_secret_key
53
+ end
54
+
55
+ end
56
+
57
+ context "facebook_api_key" do
58
+
59
+ should "default to default_config.api_key" do
60
+ default_from_config = 'defaultapikey'
61
+ flexmock(@session_class).should_receive('default_config.api_key').and_return(default_from_config).once
62
+ assert_equal default_from_config, @session_class.facebook_api_key
63
+ end
64
+
65
+ should "have a setter method" do
66
+ fb_api_key = '25a366a46366451636933676978776a45585734'
67
+ @session_class.facebook_api_key = fb_api_key
68
+ assert_equal fb_api_key, @session_class.facebook_api_key
69
+ end
70
+
71
+ end
72
+
73
+ context "facebook_uid_field" do
74
+
75
+ should "have a default of :facebook_uid" do
76
+ assert_equal :facebook_uid, @session_class.facebook_uid_field
77
+ end
78
+
79
+ should "have a setter method" do
80
+ fb_uid_field = 'fb_uid'
81
+ @session_class.facebook_uid_field = fb_uid_field
82
+ assert_equal fb_uid_field, @session_class.facebook_uid_field
83
+ end
84
+
85
+ end
86
+
87
+ context "facebook_finder" do
88
+
89
+ should 'have a default nil' do
90
+ assert_nil @session_class.facebook_finder
91
+ end
92
+
93
+ should "have a setter method" do
94
+ fb_finder = 'find_by_fb_uid'
95
+ @session_class.facebook_finder = fb_finder
96
+ assert_equal fb_finder, @session_class.facebook_finder
97
+ end
98
+
99
+ end
100
+
101
+ context "facebook_auto_register" do
102
+
103
+ should 'have a default false' do
104
+ assert_false @session_class.facebook_auto_register
105
+ end
106
+
107
+ should "have a setter method" do
108
+ fb_auto_reg = true
109
+ @session_class.facebook_auto_register = fb_auto_reg
110
+ assert_equal fb_auto_reg, @session_class.facebook_auto_register
111
+ end
112
+
113
+ end
114
+
115
+ context "default_config" do
116
+
117
+ should "be a class method" do
118
+ assert @session_class.respond_to?(:default_config)
119
+ end
120
+
121
+ should "return an OpenStruct" do
122
+ assert @session_class.default_config.is_a?(OpenStruct)
123
+ end
124
+
125
+ should "return the app_id from the default config file" do
126
+ assert_equal 'appidfromfile', @session_class.default_config.app_id
127
+ end
128
+
129
+ should "return the api_key from the default config file" do
130
+ assert_equal 'apikeyfromfile', @session_class.default_config.api_key
131
+ end
132
+
133
+ should "return the secret_key from the default config file" do
134
+ assert_equal 'secretkeyfromfile', @session_class.default_config.secret_key
135
+ end
136
+
137
+ should "return an empty OpenStruct if the file isn't found" do
138
+ flexmock(@session_class).should_receive(:facebook_config_file).and_return('notthere.yml').once
139
+ assert_equal OpenStruct.new({}), @session_class.default_config
140
+ end
141
+ end
142
+
143
+ end
144
+
145
+ end
@@ -0,0 +1,221 @@
1
+ require File.expand_path( '../../test_helper.rb', File.dirname(__FILE__) )
2
+
3
+ class AuthlogicFacebookShim::Session::FacebookTest < ActiveSupport::TestCase
4
+
5
+ setup :activate_authlogic
6
+
7
+ context "Session::Facebook" do
8
+
9
+ setup do
10
+ @mock_cookies = MockCookieJar.new
11
+ @mock_cookies['fbs_mockappid'] = {:value => 'access_token=mockaccesstoken&expires=0&secret=mocksecret&session_key=mocksessionkey&sig=cbd80b97f124bf392f76e2ee61168990&uid=mockuid'}
12
+
13
+ flexmock(controller).should_receive(:cookies).and_return(@mock_cookies).by_default
14
+
15
+ @session = flexmock(UserSession.new)
16
+ @session.should_receive(:controller).and_return(controller).by_default
17
+ end
18
+
19
+ context "setup - for my own sanity" do
20
+
21
+ should "set the controller" do
22
+ assert_equal controller, @session.controller
23
+ end
24
+
25
+ should "set the cookies" do
26
+ assert_equal @mock_cookies, @session.controller.cookies
27
+ end
28
+
29
+ end
30
+
31
+ context "config accessors" do
32
+
33
+ should "return facebook_app_id" do
34
+ mockappid = 'mockappid'
35
+ flexmock(UserSession).should_receive(:facebook_app_id).and_return(mockappid).once
36
+ assert_equal mockappid, @session.send(:facebook_app_id)
37
+ end
38
+
39
+ should "return facebook_api_key" do
40
+ mockapikey = 'mockapikey'
41
+ flexmock(UserSession).should_receive(:facebook_api_key).and_return(mockapikey).once
42
+ assert_equal mockapikey, @session.send(:facebook_api_key)
43
+ end
44
+
45
+ should "return facebook_secret_key" do
46
+ mocksecret = 'mocksecret'
47
+ flexmock(UserSession).should_receive(:facebook_secret_key).and_return(mocksecret).once
48
+ assert_equal mocksecret, @session.send(:facebook_secret_key)
49
+ end
50
+
51
+ should "return facebook_uid_field" do
52
+ mockuidfield = 'mockuidfield'
53
+ flexmock(UserSession).should_receive(:facebook_uid_field).and_return(mockuidfield).once
54
+ assert_equal mockuidfield, @session.send(:facebook_uid_field)
55
+ end
56
+
57
+ context "facebook_finder" do
58
+
59
+ should "delegate to the class" do
60
+ mockfinder = 'mockfinder'
61
+ flexmock(UserSession).should_receive(:facebook_finder).and_return(mockfinder).once
62
+ assert_equal mockfinder, @session.send(:facebook_finder)
63
+ end
64
+
65
+ should "default if the class returns nil" do
66
+ flexmock(UserSession).should_receive(:facebook_finder).and_return(nil).once
67
+ @session.should_receive(:facebook_uid_field).and_return('mockuidfield').once
68
+ assert_equal "find_by_mockuidfield", @session.send(:facebook_finder)
69
+ end
70
+
71
+ end
72
+
73
+ should "return facebook_auto_register?" do
74
+ flexmock(UserSession).should_receive(:facebook_auto_register).and_return(true).once
75
+ assert @session.send(:facebook_auto_register?)
76
+ end
77
+
78
+ end
79
+
80
+ context "validating with facebook" do
81
+
82
+ context "with a valid facebook session" do
83
+
84
+ setup do
85
+ @facebook_session = flexmock('facebook session', :uid => 'mockuid')
86
+ @session.should_receive(:facebook_session).and_return(@facebook_session).by_default
87
+ end
88
+
89
+ context "with an existing facebook uid" do
90
+
91
+ setup do
92
+ @session.should_receive(:facebook_finder).and_return('finder_method').by_default
93
+
94
+ @user = User.create
95
+ flexmock(User).should_receive('finder_method').with('mockuid').and_return(@user).by_default
96
+
97
+ @session.save
98
+ end
99
+
100
+ should "return true for logged_in_with_facebook?" do
101
+ assert @session.logged_in_with_facebook?
102
+ end
103
+
104
+ should "set attempted_record" do
105
+ assert_equal @user, @session.attempted_record
106
+ end
107
+
108
+ end
109
+
110
+ context "without an existing facebook uid" do
111
+
112
+ setup do
113
+ @session.should_receive(:facebook_finder).and_return('finder_method').by_default
114
+ flexmock(User).should_receive('finder_method').with('mockuid').and_return(nil).by_default
115
+
116
+ @user = flexmock(User.new)
117
+ flexmock(User).should_receive(:new).and_return(@user).by_default
118
+ end
119
+
120
+ context "and facebook_auto_register? true" do
121
+
122
+ setup do
123
+ @session.should_receive(:facebook_auto_register?).and_return(true).by_default
124
+ end
125
+
126
+ should "build a new user on attempted_record" do
127
+ flexmock(User).should_receive(:new).and_return(@user).once
128
+ @session.save
129
+ assert_equal @user, @session.attempted_record
130
+ end
131
+
132
+ should "attempt to call before_connect on the new user" do
133
+ # TODO this is a bit flakey because I can't get flexmock to mock with(@facebook_session)
134
+ @user.should_receive(:before_connect).with(any).and_return(true).once
135
+ assert @session.save
136
+ end
137
+
138
+ should "save the new user" do
139
+ @user.should_receive(:save).with(false).and_return(true).at_least.once
140
+ assert @session.save
141
+ end
142
+
143
+ end
144
+
145
+ context "and facebook_auto_register? false" do
146
+
147
+ should "return false for logged_in_with_facebook?" do
148
+ @session.should_receive(:facebook_auto_register?).and_return(false).once
149
+ @session.save
150
+
151
+ assert_equal false, @session.logged_in_with_facebook?
152
+ end
153
+
154
+ should "not set attempted record" do
155
+ @session.should_receive(:facebook_auto_register?).and_return(false).once
156
+ @session.save
157
+
158
+ assert_nil @session.attempted_record
159
+ end
160
+
161
+ end
162
+
163
+ end
164
+
165
+ end
166
+
167
+ context "when skip_facebook_authentication is true" do
168
+
169
+ should "not attempt to validate with facebook" do
170
+ @session.should_receive(:skip_facebook_authentication).and_return(true).once
171
+ @session.should_receive(:validate_by_facebook).never
172
+
173
+ assert_equal false, @session.save
174
+ end
175
+
176
+ should "return false for logged_in_with_facebook?" do
177
+ @session.should_receive(:skip_facebook_authentication).and_return(true).once
178
+
179
+ assert_equal false, @session.save
180
+ assert_nil @session.logged_in_with_facebook?
181
+ end
182
+
183
+ should "not set attempted record" do
184
+ @session.should_receive(:skip_facebook_authentication).and_return(true).once
185
+
186
+ assert_equal false, @session.save
187
+ assert_nil @session.attempted_record
188
+ end
189
+ end
190
+
191
+ context "when authenticating_with_unauthorized_record? is false" do
192
+
193
+ should "not attempt to validate with facebook" do
194
+ @session.should_receive(:authenticating_with_unauthorized_record?).and_return(false).at_least.once
195
+ @session.should_receive(:validate_by_facebook).never
196
+
197
+ assert_equal false, @session.save
198
+ end
199
+
200
+ should "return false for logged_in_with_facebook?" do
201
+ @session.should_receive(:authenticating_with_unauthorized_record?).and_return(true).at_least.once
202
+
203
+ assert_equal false, @session.save
204
+ assert_nil @session.logged_in_with_facebook?
205
+ end
206
+
207
+ should "not set attempted record" do
208
+ @session.should_receive(:authenticating_with_unauthorized_record?).and_return(true).at_least.once
209
+
210
+ assert_equal false, @session.save
211
+ assert_nil @session.attempted_record
212
+ end
213
+
214
+ end
215
+
216
+
217
+ end
218
+
219
+ end
220
+
221
+ end
metadata ADDED
@@ -0,0 +1,234 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: authlogic_facebook_shim
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 3
9
+ - 2
10
+ version: 0.3.2
11
+ platform: ruby
12
+ authors:
13
+ - James McCarthy
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-05-27 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: authlogic
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 13
30
+ segments:
31
+ - 2
32
+ - 1
33
+ - 3
34
+ version: 2.1.3
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: rails
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ hash: 9
46
+ segments:
47
+ - 2
48
+ - 3
49
+ - 5
50
+ version: 2.3.5
51
+ type: :development
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: flexmock
55
+ prerelease: false
56
+ requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ hash: 3
62
+ segments:
63
+ - 0
64
+ version: "0"
65
+ type: :development
66
+ version_requirements: *id003
67
+ - !ruby/object:Gem::Dependency
68
+ name: jeweler
69
+ prerelease: false
70
+ requirement: &id004 !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ hash: 3
76
+ segments:
77
+ - 0
78
+ version: "0"
79
+ type: :development
80
+ version_requirements: *id004
81
+ - !ruby/object:Gem::Dependency
82
+ name: shoulda
83
+ prerelease: false
84
+ requirement: &id005 !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ hash: 3
90
+ segments:
91
+ - 0
92
+ version: "0"
93
+ type: :development
94
+ version_requirements: *id005
95
+ - !ruby/object:Gem::Dependency
96
+ name: sqlite3-ruby
97
+ prerelease: false
98
+ requirement: &id006 !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ hash: 3
104
+ segments:
105
+ - 0
106
+ version: "0"
107
+ type: :development
108
+ version_requirements: *id006
109
+ - !ruby/object:Gem::Dependency
110
+ name: test-unit
111
+ prerelease: false
112
+ requirement: &id007 !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ hash: 3
118
+ segments:
119
+ - 0
120
+ version: "0"
121
+ type: :development
122
+ version_requirements: *id007
123
+ description: Authlogic plugin to support Facebook OAuth2 javascript sessions. Currently requires koala but is easily extended for other facebook gems
124
+ email: james2mccarthy@gmail.com
125
+ executables: []
126
+
127
+ extensions: []
128
+
129
+ extra_rdoc_files:
130
+ - LICENSE
131
+ - README.rdoc
132
+ files:
133
+ - authlogic_facebook_shim.gemspec
134
+ - init.rb
135
+ - lib/authlogic_facebook_shim/acts_as_authentic.rb
136
+ - lib/authlogic_facebook_shim/helper.rb
137
+ - lib/authlogic_facebook_shim/session/adapter.rb
138
+ - lib/authlogic_facebook_shim/session/adapters/koala_adapter.rb
139
+ - lib/authlogic_facebook_shim/session/config.rb
140
+ - lib/authlogic_facebook_shim/session/facebook.rb
141
+ - lib/authlogic_facebook_shim/session.rb
142
+ - lib/authlogic_facebook_shim.rb
143
+ - LICENSE
144
+ - rails/init.rb
145
+ - Rakefile
146
+ - README.rdoc
147
+ - test/rails_root/app/controllers/application_controller.rb
148
+ - test/rails_root/app/helpers/application_helper.rb
149
+ - test/rails_root/app/models/user.rb
150
+ - test/rails_root/app/models/user_session.rb
151
+ - test/rails_root/config/boot.rb
152
+ - test/rails_root/config/database.yml
153
+ - test/rails_root/config/environment.rb
154
+ - test/rails_root/config/environments/development.rb
155
+ - test/rails_root/config/environments/test.rb
156
+ - test/rails_root/config/facebook.yml
157
+ - test/rails_root/config/initializers/authlogic_facebook_koala.rb
158
+ - test/rails_root/config/initializers/new_rails_defaults.rb
159
+ - test/rails_root/config/initializers/session_store.rb
160
+ - test/rails_root/config/locales/en.yml
161
+ - test/rails_root/config/routes.rb
162
+ - test/rails_root/db/migrate/20101217000008_create_users.rb
163
+ - test/rails_root/db/seeds.rb
164
+ - test/rails_root/log/test.log
165
+ - test/rails_root/script/console
166
+ - test/rails_root/script/dbconsole
167
+ - test/rails_root/script/generate
168
+ - test/test_helper.rb
169
+ - test/units/session/adapter_test.rb
170
+ - test/units/session/config_test.rb
171
+ - test/units/session/facebook_test.rb
172
+ - VERSION
173
+ has_rdoc: true
174
+ homepage: http://github.com/james2m/authlogic_facebook_shim
175
+ licenses: []
176
+
177
+ post_install_message:
178
+ rdoc_options:
179
+ - --charset=UTF-8
180
+ require_paths:
181
+ - lib
182
+ required_ruby_version: !ruby/object:Gem::Requirement
183
+ none: false
184
+ requirements:
185
+ - - ">="
186
+ - !ruby/object:Gem::Version
187
+ hash: 3
188
+ segments:
189
+ - 0
190
+ version: "0"
191
+ required_rubygems_version: !ruby/object:Gem::Requirement
192
+ none: false
193
+ requirements:
194
+ - - ">="
195
+ - !ruby/object:Gem::Version
196
+ hash: 31
197
+ segments:
198
+ - 1
199
+ - 2
200
+ - 0
201
+ version: 1.2.0
202
+ requirements: []
203
+
204
+ rubyforge_project:
205
+ rubygems_version: 1.4.2
206
+ signing_key:
207
+ specification_version: 3
208
+ summary: Authlogic plugin to support Facebook Javascript OAuth2 Sessions.
209
+ test_files:
210
+ - test/rails_root/app/controllers/application_controller.rb
211
+ - test/rails_root/app/helpers/application_helper.rb
212
+ - test/rails_root/app/models/user.rb
213
+ - test/rails_root/app/models/user_session.rb
214
+ - test/rails_root/config/boot.rb
215
+ - test/rails_root/config/database.yml
216
+ - test/rails_root/config/environment.rb
217
+ - test/rails_root/config/environments/development.rb
218
+ - test/rails_root/config/environments/test.rb
219
+ - test/rails_root/config/facebook.yml
220
+ - test/rails_root/config/initializers/authlogic_facebook_koala.rb
221
+ - test/rails_root/config/initializers/new_rails_defaults.rb
222
+ - test/rails_root/config/initializers/session_store.rb
223
+ - test/rails_root/config/locales/en.yml
224
+ - test/rails_root/config/routes.rb
225
+ - test/rails_root/db/migrate/20101217000008_create_users.rb
226
+ - test/rails_root/db/seeds.rb
227
+ - test/rails_root/log/test.log
228
+ - test/rails_root/script/console
229
+ - test/rails_root/script/dbconsole
230
+ - test/rails_root/script/generate
231
+ - test/test_helper.rb
232
+ - test/units/session/adapter_test.rb
233
+ - test/units/session/config_test.rb
234
+ - test/units/session/facebook_test.rb