Skip to content

Commit

Permalink
set private_token param separately for each request
Browse files Browse the repository at this point in the history
  • Loading branch information
NARKOZ committed May 11, 2014
1 parent 03c8fa6 commit 3bf0363
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 12 deletions.
1 change: 1 addition & 0 deletions lib/gitlab/client/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ def edit_user(user_id, options={})
# @param [String] email The email of a user.
# @param [String] password The password of a user.
# @return [Gitlab::ObjectifiedHash]
# @note This method doesn't require private_token to be set.
def session(email, password)
post("/session", :body => {:email => email, :password => password})
end
Expand Down
28 changes: 22 additions & 6 deletions lib/gitlab/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class Request
headers 'Accept' => 'application/json'
parser Proc.new { |body, _| parse(body) }

attr_accessor :private_token

# Converts the response body to an ObjectifiedHash.
def self.parse(body)
body = decode(body)
Expand All @@ -32,19 +34,23 @@ def self.decode(response)
end

def get(path, options={})
set_private_token_param(options)
validate self.class.get(path, options)
end

def post(path, options={})
set_private_token_param(options, path)
validate self.class.post(path, options)
end

def put(path, options={})
set_private_token_param(options)
validate self.class.put(path, options)
end

def delete(path)
validate self.class.delete(path)
def delete(path, options={})
set_private_token_param(options)
validate self.class.delete(path, options)
end

# Checks the response code for common errors.
Expand All @@ -65,15 +71,25 @@ def validate(response)
response.parsed_response
end

# Sets a private_token parameter for requests.
# @raise [Error::MissingCredentials] if private_token not set.
def set_private_token_param(options, path=nil)
# session doesn't require private_token param
return if path == '/session'

raise Error::MissingCredentials.new("Please set a private_token for user") unless @private_token
private_token_param = {:private_token => @private_token}
options[:query] = options[:query] ? options[:query].merge(private_token_param) : private_token_param
end

# Sets a base_uri and default_params for requests.
# @raise [Error::MissingCredentials] if endpoint or private_token not set.
# @raise [Error::MissingCredentials] if endpoint not set.
def set_request_defaults(endpoint, private_token, sudo=nil)
raise Error::MissingCredentials.new("Please set an endpoint to API") unless endpoint
raise Error::MissingCredentials.new("Please set a private_token for user") unless private_token
@private_token = private_token

self.class.base_uri endpoint
self.class.default_params :private_token => private_token, :sudo => sudo
self.class.default_params.delete(:sudo) if sudo.nil?
self.class.default_params :sudo => sudo unless sudo.nil?
end

private
Expand Down
36 changes: 30 additions & 6 deletions spec/gitlab/client/users_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,42 @@
end

describe ".session" do
after do
Gitlab.endpoint = 'https://api.example.com'
Gitlab.private_token = 'secret'
end

before do
stub_post("/session", "session")
stub_request(:post, "#{Gitlab.endpoint}/session").
to_return(:body => load_fixture('session'), :status => 200)
@session = Gitlab.session("email", "pass")
end

it "should get the correct resource" do
a_post("/session").should have_been_made
context "when endpoint is not set" do
it "should raise Error::MissingCredentials" do
Gitlab.endpoint = nil
expect {
Gitlab.session("email", "pass")
}.to raise_error(Gitlab::Error::MissingCredentials, 'Please set an endpoint to API')
end
end

context "when private_token is not set" do
it "should not raise Error::MissingCredentials" do
Gitlab.private_token = nil
expect { Gitlab.session("email", "pass") }.to_not raise_error
end
end

it "should return information about a created session" do
@session.email.should == "[email protected]"
@session.private_token.should == "qEsq1pt6HJPaNciie3MG"
context "when endpoint is set" do
it "should get the correct resource" do
a_request(:post, "#{Gitlab.endpoint}/session")
end

it "should return information about a created session" do
@session.email.should == "[email protected]"
@session.private_token.should == "qEsq1pt6HJPaNciie3MG"
end
end
end

Expand Down

0 comments on commit 3bf0363

Please sign in to comment.