authlogic_facebook_koala 0.0.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. data/README.rdoc +60 -2
  2. data/Rakefile +28 -1
  3. data/VERSION +1 -1
  4. data/authlogic_facebook_koala.gemspec +12 -45
  5. data/lib/authlogic_facebook_koala.rb +10 -4
  6. data/lib/authlogic_facebook_koala/adapter.rb +35 -0
  7. data/lib/authlogic_facebook_koala/config.rb +78 -4
  8. data/lib/authlogic_facebook_koala/session.rb +50 -116
  9. data/test/rails_root/app/controllers/application_controller.rb +10 -0
  10. data/test/rails_root/app/helpers/application_helper.rb +3 -0
  11. data/test/rails_root/app/models/user.rb +7 -0
  12. data/test/{libs → rails_root/app/models}/user_session.rb +0 -0
  13. data/test/rails_root/config/boot.rb +110 -0
  14. data/test/rails_root/config/database.yml +10 -0
  15. data/test/rails_root/config/environment.rb +31 -0
  16. data/test/rails_root/config/environments/development.rb +0 -0
  17. data/test/rails_root/config/environments/test.rb +0 -0
  18. data/test/rails_root/config/facebook.yml +7 -0
  19. data/test/rails_root/config/initializers/authlogic_facebook_koala.rb +5 -0
  20. data/test/rails_root/config/initializers/new_rails_defaults.rb +21 -0
  21. data/test/rails_root/config/initializers/session_store.rb +15 -0
  22. data/test/rails_root/config/locales/en.yml +5 -0
  23. data/test/rails_root/config/routes.rb +43 -0
  24. data/test/rails_root/db/migrate/20101217000008_create_users.rb +37 -0
  25. data/test/rails_root/db/seeds.rb +7 -0
  26. data/test/rails_root/script/console +3 -0
  27. data/test/rails_root/script/dbconsole +3 -0
  28. data/test/rails_root/script/generate +3 -0
  29. data/test/test_helper.rb +19 -58
  30. data/test/units/adapter_test.rb +182 -0
  31. data/test/units/config_test.rb +145 -0
  32. data/test/units/session_test.rb +221 -0
  33. metadata +139 -27
  34. data/lib/authlogic_facebook_koala/controller.rb +0 -59
  35. data/test/libs/user.rb +0 -3
  36. data/test/session_test.rb +0 -30
@@ -0,0 +1,182 @@
1
+ require File.expand_path( '../test_helper.rb', File.dirname(__FILE__) )
2
+
3
+ class AdapterTest < ActiveSupport::TestCase
4
+
5
+ setup :activate_authlogic
6
+
7
+ context "Adapter" do
8
+ setup do
9
+ @user_info = {
10
+ 'session_key' => 'mocksessionkey',
11
+ 'expires' => '0',
12
+ 'uid' => 'mockuid',
13
+ 'sig' => 'cbd80b97f124bf392f76e2ee61168990',
14
+ 'secret' => 'mocksecret',
15
+ 'access_token' => 'mockaccesstoken'
16
+ }
17
+ @mock_cookies = MockCookieJar.new
18
+ @mock_cookies['fbs_mockappid'] = {:value => 'access_token=mockaccesstoken&expires=0&secret=mocksecret&session_key=mocksessionkey&sig=cbd80b97f124bf392f76e2ee61168990&uid=mockuid'}
19
+ @session = flexmock(UserSession.new)
20
+ @controller = flexmock('Controller')
21
+
22
+ @session.should_receive(:facebook_app_id).and_return('mockappid').by_default
23
+ @session.should_receive(:facebook_api_key).and_return('mockapikey').by_default
24
+ @session.should_receive(:facebook_secret_key).and_return('mocksecret').by_default
25
+ @session.should_receive(:controller).and_return(@controller).by_default
26
+ @controller.should_receive(:cookies).and_return(@mock_cookies).by_default
27
+ end
28
+
29
+ context "setup - for my own sanity" do
30
+
31
+ should "set the controller" do
32
+ assert_equal @controller, @session.controller
33
+ end
34
+
35
+ should "set the cookies" do
36
+ assert_equal @mock_cookies, @session.controller.cookies
37
+ end
38
+
39
+ should "set the facebook_app_id" do
40
+ assert_equal 'mockappid', @session.facebook_app_id
41
+ end
42
+
43
+ should "set the facebook_secret_key" do
44
+ assert_equal 'mocksecret', @session.facebook_secret_key
45
+ end
46
+
47
+ should "set the facebook_api_key" do
48
+ assert_equal 'mockapikey', @session.facebook_api_key
49
+ end
50
+
51
+ end
52
+
53
+ context "facebook_session" do
54
+
55
+ context "with a valid facebook cookie" do
56
+
57
+ context "and koala support for get_user_info_from_cookie" do
58
+
59
+ should "return a session_key" do
60
+ assert_equal 'mocksessionkey', @session.facebook_session.session_key
61
+ end
62
+
63
+ should "return a uid" do
64
+ assert_equal 'mockuid', @session.facebook_session.uid
65
+ end
66
+
67
+ should "return a secret" do
68
+ assert_equal 'mocksecret', @session.facebook_session.secret
69
+ end
70
+
71
+ should "return a sig" do
72
+ assert_equal 'cbd80b97f124bf392f76e2ee61168990', @session.facebook_session.sig
73
+ end
74
+
75
+ should "return an access_token" do
76
+ assert_equal 'mockaccesstoken', @session.facebook_session.access_token
77
+ end
78
+
79
+ end
80
+
81
+ context "with previous koala api" do
82
+
83
+ should "get user info with the get_user_from_cookie method" do
84
+ @oauth = flexmock('oauth')
85
+ flexmock(Koala::Facebook::OAuth).should_receive(:new).and_return(@oauth).once
86
+ @oauth.should_receive(:respond_to?).with(:get_user_info_from_cookie).and_return(false).once
87
+ @oauth.should_receive(:get_user_from_cookie).with(@mock_cookies).and_return(@user_info).once
88
+
89
+ assert_equal 'mocksessionkey', @session.facebook_session.session_key
90
+ end
91
+
92
+ end
93
+
94
+ end
95
+
96
+ context "with no valid facebook cookie" do
97
+
98
+ should "return nil" do
99
+ @session.should_receive('facebook_app_id').and_return(nil).once
100
+ assert_nil @session.facebook_session
101
+ end
102
+
103
+ end
104
+
105
+ end
106
+
107
+ context "facebook_session?" do
108
+
109
+ context "with a valid facebook session" do
110
+
111
+ should "be true" do
112
+ assert @session.facebook_session?
113
+ end
114
+
115
+ end
116
+
117
+ context "without a valid facebook session" do
118
+
119
+ should "return nil" do
120
+ @session.should_receive('facebook_app_id').and_return(nil).once
121
+ assert_equal false, @session.facebook_session?
122
+ end
123
+
124
+ end
125
+
126
+ end
127
+
128
+ context "facebook_user" do
129
+
130
+ context "with a valid facebook session" do
131
+
132
+ setup do
133
+ @user = {
134
+ "id" => "mockid",
135
+ "name" => "Full name",
136
+ "first_name" => "First name",
137
+ "last_name" => "Last name"
138
+ }
139
+
140
+ @access_token = flexmock('access token')
141
+ @session.should_receive('facebook_session.access_token').and_return(@access_token).by_default
142
+ @session.should_receive('facebook_session?').and_return(true).by_default
143
+
144
+ @graph_api = flexmock('graph api', :get_object => @user)
145
+ flexmock(Koala::Facebook::GraphAPI).should_receive(:new).and_return(@graph_api).by_default
146
+ end
147
+
148
+ should "initialize the graph api" do
149
+ flexmock(Koala::Facebook::GraphAPI).should_receive(:new).with(@access_token).and_return(@graph_api).once
150
+ @session.facebook_user
151
+ end
152
+
153
+ should "return an OpenStruct" do
154
+ assert @session.facebook_user.is_a?(OpenStruct)
155
+ end
156
+
157
+ should "return the user details" do
158
+ assert_equal 'Full name', @session.facebook_user.name
159
+ assert_equal 'First name', @session.facebook_user.first_name
160
+ assert_equal 'Last name', @session.facebook_user.last_name
161
+ end
162
+
163
+ should "return the facebook id as uid" do
164
+ assert_equal 'mockid', @session.facebook_user.uid
165
+ end
166
+
167
+ end
168
+
169
+ context "with no valid facebook session" do
170
+
171
+ should "return nil" do
172
+ @session.should_receive('facebook_session?').and_return(false).once
173
+ assert_nil @session.facebook_user
174
+ end
175
+
176
+ end
177
+
178
+ end
179
+
180
+ end
181
+
182
+ end
@@ -0,0 +1,145 @@
1
+ require File.expand_path( '../test_helper.rb', File.dirname(__FILE__) )
2
+
3
+ class ConfigTest < ActiveSupport::TestCase
4
+
5
+ context "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 SessionTest < ActiveSupport::TestCase
4
+
5
+ setup :activate_authlogic
6
+
7
+ context "Session" 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