Skip to content

Commit

Permalink
Fix account.refresh to work even the second time
Browse files Browse the repository at this point in the history
Previously, refreshing the token would not work the second time
because the @refreshed_authentications was caching the old token.
  • Loading branch information
claudiob committed Jun 6, 2014
1 parent c79a1da commit 3f1dce7
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
yt (0.6.0)
yt (0.6.1)
activesupport

GEM
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -326,7 +326,7 @@ To install on your system, run

To use inside a bundled Ruby project, add this line to the Gemfile:

gem 'yt', '~> 0.6.0'
gem 'yt', '~> 0.6.1'

Since the gem follows [Semantic Versioning](http://semver.org),
indicating the full version in your Gemfile (~> *major*.*minor*.*patch*)
Expand Down
5 changes: 2 additions & 3 deletions lib/yt/associations/authentications.rb
Expand Up @@ -25,9 +25,8 @@ def authentication_url
# Returns true if the new access token is different from the previous one
def refresh
old_access_token = authentication.access_token
authentication.expire
new_access_token = authentication.access_token
old_access_token != new_access_token
@authentication = @access_token = @refreshed_authentications = nil
old_access_token != authentication.access_token
end

private
Expand Down
4 changes: 0 additions & 4 deletions lib/yt/models/authentication.rb
Expand Up @@ -13,10 +13,6 @@ def expired?
@expires_at && @expires_at.past?
end

def expire
@expires_at = 10.years.ago
end

private

def expiration_date(options = {})
Expand Down
2 changes: 1 addition & 1 deletion lib/yt/version.rb
@@ -1,3 +1,3 @@
module Yt
VERSION = '0.6.0'
VERSION = '0.6.1'
end
13 changes: 13 additions & 0 deletions spec/associations/device_auth/authentications_spec.rb
Expand Up @@ -4,6 +4,19 @@
describe Yt::Associations::Authentications, :device_app do
subject(:account) { Yt::Account.new attrs }

describe '#refresh' do
context 'given a valid refresh token' do
let(:attrs) { {refresh_token: ENV['YT_TEST_DEVICE_REFRESH_TOKEN']} }

# NOTE: When the token is refreshed, YouTube *might* actually return
# the *same* access token if it is still valid. Typically, within the
# same second, refreshing the token returns the same token. Still,
# testing that *expires_at* changes is a guarantee that we attempted
# to get a new token, which is what refresh is meant to do.
it { expect{account.refresh}.to change{account.expires_at} }
end
end

describe '#authentication' do
context 'given a refresh token' do
let(:attrs) { {refresh_token: refresh_token} }
Expand Down

0 comments on commit 3f1dce7

Please sign in to comment.