Skip to content
Browse files

Introduce authenticate method to AccessToken

- Does not rely on active record dynamic finders
  • Loading branch information...
1 parent 462677c commit 5e8b0e7c66d0b9b7459fdc489822b2e31f8ef5a9 @felipeelias felipeelias committed
View
2 lib/doorkeeper/models/access_token.rb
@@ -18,7 +18,7 @@ class AccessToken
before_validation :generate_refresh_token, :on => :create, :if => :use_refresh_token?
def self.authenticate(token)
- find_by_token token
+ where(:token => token).first
end
def self.revoke_all_for(application_id, resource_owner)
View
3 lib/doorkeeper/models/mongoid/access_token.rb
@@ -12,9 +12,6 @@ class AccessToken
field :revoked_at, :type => DateTime
field :scopes, :type => Array
- def self.find_by_token(token)
- where(:token => token).first
- end
def self.find_by_refresh_token(refresh_token)
where(:refresh_token => refresh_token).first
View
14 spec/controllers/protected_resources_controller_spec.rb
@@ -79,29 +79,29 @@ def index
end
it "access_token param" do
- Doorkeeper::AccessToken.should_receive(:find_by_token).with(token_string)
+ Doorkeeper::AccessToken.should_receive(:authenticate).with(token_string)
get :index, :access_token => token_string
end
it "bearer_token param" do
- Doorkeeper::AccessToken.should_receive(:find_by_token).with(token_string)
+ Doorkeeper::AccessToken.should_receive(:authenticate).with(token_string)
get :index, :bearer_token => token_string
end
it "Authorization header" do
- Doorkeeper::AccessToken.should_receive(:find_by_token).with(token_string)
+ Doorkeeper::AccessToken.should_receive(:authenticate).with(token_string)
request.env["HTTP_AUTHORIZATION"] = "Bearer #{token_string}"
get :index
end
it "different kind of Authorization header" do
- Doorkeeper::AccessToken.should_not_receive(:find_by_token)
+ Doorkeeper::AccessToken.should_not_receive(:authenticate)
request.env["HTTP_AUTHORIZATION"] = "Basic #{Base64.encode64("foo:bar")}"
get :index
end
it "doesn't change Authorization header value" do
- Doorkeeper::AccessToken.should_receive(:find_by_token).exactly(2).times
+ Doorkeeper::AccessToken.should_receive(:authenticate).exactly(2).times
request.env["HTTP_AUTHORIZATION"] = "Bearer #{token_string}"
get :index
get :index
@@ -172,14 +172,14 @@ def index
it "allows if the token has particular scopes" do
token = double(Doorkeeper::AccessToken, :accessible? => true, :scopes => [:write, :public])
- Doorkeeper::AccessToken.should_receive(:find_by_token).with(token_string).and_return(token)
+ Doorkeeper::AccessToken.should_receive(:authenticate).with(token_string).and_return(token)
get :index, :access_token => token_string
response.should be_success
end
it "does not allow if the token does not include given scope" do
token = double(Doorkeeper::AccessToken, :accessible? => true, :scopes => [:public])
- Doorkeeper::AccessToken.should_receive(:find_by_token).with(token_string).and_return(token)
+ Doorkeeper::AccessToken.should_receive(:authenticate).with(token_string).and_return(token)
get :index, :access_token => token_string
response.status.should == 401
end
View
4 spec/support/shared/controllers_shared_context.rb
@@ -8,7 +8,7 @@
end
before :each do
- Doorkeeper::AccessToken.stub(:find_by_token).with(token_string).and_return(token)
+ Doorkeeper::AccessToken.stub(:authenticate).with(token_string).and_return(token)
end
end
@@ -22,7 +22,7 @@
end
before :each do
- Doorkeeper::AccessToken.stub(:find_by_token).with(token_string).and_return(token)
+ Doorkeeper::AccessToken.stub(:authenticate).with(token_string).and_return(token)
end
end

0 comments on commit 5e8b0e7

Please sign in to comment.
Something went wrong with that request. Please try again.