Codebase list ruby-omniauth-facebook / 12bd3d6
add raw info and credential expiry to auth hash Mark Dodwell 12 years ago
2 changed file(s) with 81 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
3535 })
3636 end
3737
38 credentials do
39 prune!({
40 'expires' => access_token.expires?,
41 'expires_at' => access_token.expires_at
42 })
43 end
44
45 extra do
46 prune!({
47 'raw_info' => raw_info
48 })
49 end
50
3851 def raw_info
3952 @raw_info ||= access_token.get('/me').parsed
4053 end
5467 def prune!(hash)
5568 hash.delete_if do |_, value|
5669 prune!(value) if value.is_a?(Hash)
57 value.nil? || value.empty?
70 value.nil? || (value.respond_to?(:empty?) && value.empty?)
5871 end
5972 end
6073 end
175175 subject.raw_info['ohai'].should eq('thar')
176176 end
177177 end
178
179 describe '#credentials' do
180 before :each do
181 @access_token = double('OAuth2::AccessToken')
182 @access_token.stub(:token)
183 @access_token.stub(:expires?)
184 @access_token.stub(:expires_at)
185 @access_token.stub(:refresh_token)
186 subject.stub(:access_token) { @access_token }
187 end
188
189 it 'returns a Hash' do
190 subject.credentials.should be_a(Hash)
191 end
192
193 it 'returns the token' do
194 @access_token.stub(:token) { '123' }
195 subject.credentials['token'].should eq('123')
196 end
197
198 it 'returns the expiry status' do
199 @access_token.stub(:expires?) { true }
200 subject.credentials['expires'].should eq(true)
201
202 @access_token.stub(:expires?) { false }
203 subject.credentials['expires'].should eq(false)
204 end
205
206 it 'returns the refresh token and expiry time when expiring' do
207 ten_mins_from_now = (Time.now + 360).to_i
208 @access_token.stub(:expires?) { true }
209 @access_token.stub(:refresh_token) { '321' }
210 @access_token.stub(:expires_at) { ten_mins_from_now }
211 subject.credentials['refresh_token'].should eq('321')
212 subject.credentials['expires_at'].should eq(ten_mins_from_now)
213 end
214
215 # FIXME omniauth does not behave like this atm.
216 pending 'does not return the refresh token when it is nil and expiring' do
217 @access_token.stub(:expires?) { true }
218 @access_token.stub(:refresh_token) { nil }
219 subject.credentials['refresh_token'].should be_nil
220 subject.credentials.should_not have_key('refresh_token')
221 end
222
223 it 'does not return the refresh token when not expiring' do
224 @access_token.stub(:expires?) { false }
225 @access_token.stub(:refresh_token) { 'XXX' }
226 subject.credentials['refresh_token'].should be_nil
227 subject.credentials.should_not have_key('refresh_token')
228 end
229 end
230
231 describe '#extra' do
232 before :each do
233 @raw_info = { 'name' => 'Fred Smith' }
234 subject.stub(:raw_info) { @raw_info }
235 end
236
237 it 'returns a Hash' do
238 subject.extra.should be_a(Hash)
239 end
240
241 it 'contains raw info' do
242 subject.extra.should eq({ 'raw_info' => @raw_info })
243 end
244 end
178245 end