Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Move expired token logic to a module

  • Loading branch information...
commit 218748b2aa686d517eaa2aadceb954314d3b0efe 1 parent 2cdf1b5
@felipeelias felipeelias authored
View
9 app/models/access_grant.rb
@@ -1,5 +1,6 @@
class AccessGrant < ActiveRecord::Base
include Doorkeeper::OAuth::RandomString
+ include Doorkeeper::Models::Expirable
self.table_name = :oauth_access_grants
@@ -9,10 +10,6 @@ class AccessGrant < ActiveRecord::Base
before_validation :generate_token, :on => :create
- def expired?
- expires_in.present? && Time.now > expired_time
- end
-
def accessible?
!expired? && !revoked?
end
@@ -35,10 +32,6 @@ def scopes_string
private
- def expired_time
- self.created_at + expires_in.seconds
- end
-
def generate_token
self.token = unique_random_string_for(:token)
end
View
14 app/models/access_token.rb
@@ -1,6 +1,7 @@
class AccessToken < ActiveRecord::Base
include Doorkeeper::OAuth::RandomString
include Doorkeeper::OAuth::Helpers
+ include Doorkeeper::Models::Expirable
self.table_name = :oauth_access_tokens
@@ -41,15 +42,6 @@ def revoked?
self.revoked_at.present?
end
- def expired?
- expires_in.present? && Time.now > expired_time
- end
-
- def time_left
- time_left = (expired_time - Time.now)
- time_left > 0 ? time_left : 0
- end
-
def accessible?
!expired? && !revoked?
end
@@ -69,10 +61,6 @@ def use_refresh_token?
private
- def expired_time
- self.created_at + expires_in.seconds
- end
-
def generate_refresh_token
self.refresh_token = unique_random_string_for(:refresh_token)
end
View
4 lib/doorkeeper.rb
@@ -16,4 +16,8 @@ module Helpers
autoload :URIChecker, "doorkeeper/oauth/helpers/uri_checker"
end
end
+
+ module Models
+ autoload :Expirable, "doorkeeper/models/expirable"
+ end
end
View
18 lib/doorkeeper/models/expirable.rb
@@ -0,0 +1,18 @@
+module Doorkeeper
+ module Models
+ module Expirable
+ def expired?
+ expires_in && Time.now > expired_time
+ end
+
+ def time_left
+ expired? ? 0 : expired_time - Time.now
+ end
+
+ def expired_time
+ created_at + expires_in.seconds
+ end
+ private :expired_time
+ end
+ end
+end
View
49 spec/lib/models/expirable_spec.rb
@@ -0,0 +1,49 @@
+require 'spec_helper'
+require 'timecop'
+require 'active_support/time'
+require 'doorkeeper/models/expirable'
+
+describe 'Expirable' do
+ subject do
+ Class.new do
+ include Doorkeeper::Models::Expirable
+ end.new
+ end
+
+ before do
+ subject.stub :created_at => 1.minute.ago
+ end
+
+ describe :expired? do
+ it "is not expired if time has not passed" do
+ subject.stub :expires_in => 2.minutes
+ subject.should_not be_expired
+ end
+
+ it "is expired if time has passed" do
+ subject.stub :expires_in => 10.seconds
+ subject.should be_expired
+ end
+
+ it "is not expired if expires_in is not set" do
+ subject.stub :expires_in => nil
+ subject.should_not be_expired
+ end
+ end
+
+ describe :time_left do
+ it "returns the time in seconds since it was created" do
+ Timecop.freeze(Time.now) do
+ subject.stub :created_at => Time.now, :expires_in => 10.seconds
+ subject.time_left.should == 10.seconds
+ end
+ end
+
+ it "returns 0 if token has expired" do
+ Timecop.freeze(Time.now + 1.minute) do
+ subject.stub :created_at => 1.minutes.ago, :expires_in => 10.seconds
+ subject.time_left.should == 0
+ end
+ end
+ end
+end
View
18 spec/models/access_grant_spec.rb
@@ -5,6 +5,8 @@
it { should be_valid }
+ it_behaves_like "an accessible token"
+
describe "validations" do
it "is invalid without resource_owner_id" do
subject.resource_owner_id = nil
@@ -42,22 +44,6 @@
end
end
- describe "expired?" do
- it "is not expired when" do
- grant = Factory(:access_grant, :expires_in => 1000)
- grant.should_not be_expired
- grant.should be_accessible
- end
-
- it "is true if expired" do
- grant = Factory(:access_grant, :expires_in => 400)
- Timecop.freeze(Time.now + 600.seconds) do
- grant.should be_expired
- grant.should_not be_accessible
- end
- end
- end
-
describe "revoke token" do
before { subject.save! }
View
21 spec/models/access_token_spec.rb
@@ -5,6 +5,8 @@
it { should be_valid }
+ it_behaves_like "an accessible token"
+
describe "validations" do
it "is invalid without resource_owner_id" do
subject.resource_owner_id = nil
@@ -46,25 +48,6 @@
end
end
- describe "token expiration" do
- subject { Factory(:access_token, :expires_in => 2.hours) }
-
- context "when expiration time has not passed" do
- it { should_not be_expired }
- it { should be_accessible }
- end
-
- context "when expiration time is over" do
- around do |example|
- subject # force creation
- Timecop.freeze(Time.now + 3.hours) { example.call }
- end
-
- it { should be_expired }
- it { should_not be_accessible }
- end
- end
-
describe ".has_authorized_token_for?" do
let(:resource_owner) { stub(:id => 1) }
let(:application) { Factory :application }
View
13 spec/support/shared/models_shared_examples.rb
@@ -0,0 +1,13 @@
+shared_examples "an accessible token" do
+ describe :accessible? do
+ it "is accessible if token is not expired" do
+ subject.stub :expired? => false
+ should be_accessible
+ end
+
+ it "is not accessible if token is expired" do
+ subject.stub :expired? => true
+ should_not be_accessible
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.