Skip to content

Commit

Permalink
re-implement handling of sudo requests
Browse files Browse the repository at this point in the history
  • Loading branch information
NARKOZ committed Oct 16, 2013
1 parent 49c54a6 commit 3fca003
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 72 deletions.
2 changes: 1 addition & 1 deletion lib/gitlab/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def initialize(options={})
Configuration::VALID_OPTIONS_KEYS.each do |key|
send("#{key}=", options[key])
end
set_request_defaults @endpoint, @private_token
set_request_defaults @endpoint, @private_token, @sudo
end
end
end
21 changes: 8 additions & 13 deletions lib/gitlab/client/projects.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ def projects(options={})
#
# @param [Integer, String] id The ID or name of a project.
# @return [Gitlab::ObjectifiedHash]
def project(id, options ={})
get("/projects/#{id}", :query => options)
def project(id)
get("/projects/#{id}")
end

# Creates a new project.
Expand Down Expand Up @@ -87,11 +87,9 @@ def team_member(project, id)
# @param [Integer] id The ID of a user.
# @param [Integer] access_level The access level to project.
# @param [Hash] options A customizable set of options.
# @option options [Integer, String] :sudo The user id/username to perform the request as (admin only)
# @return [Gitlab::ObjectifiedHash] Information about added team member.
def add_team_member(project, id, access_level, options = {})
body = {:user_id => id, :access_level => access_level}.merge(options)
post("/projects/#{project}/members", :body => body)
def add_team_member(project, id, access_level)
post("/projects/#{project}/members", :body => {:user_id => id, :access_level => access_level})
end

# Updates a team member's project access level.
Expand All @@ -103,11 +101,9 @@ def add_team_member(project, id, access_level, options = {})
# @param [Integer] id The ID of a user.
# @param [Integer] access_level The access level to project.
# @param [Hash] options A customizable set of options.
# @option options [Integer, String] :sudo The user id/username to perform the request as (admin only)
# @return [Array<Gitlab::ObjectifiedHash>] Information about updated team member.
def edit_team_member(project, id, access_level, options = {})
body = {:access_level => access_level}.merge(options)
put("/projects/#{project}/members/#{id}", :body => body)
def edit_team_member(project, id, access_level)
put("/projects/#{project}/members/#{id}", :body => {:access_level => access_level})
end

# Removes a user from project team.
Expand All @@ -118,10 +114,9 @@ def edit_team_member(project, id, access_level, options = {})
# @param [Integer, String] project The ID or name of a project.
# @param [Integer] id The ID of a user.
# @param [Hash] options A customizable set of options.
# @option options [Integer, String] :sudo The user id/username to perform the request as (admin only)
# @return [Gitlab::ObjectifiedHash] Information about removed team member.
def remove_team_member(project, id, options= {})
delete("/projects/#{project}/members/#{id}", :body => options)
def remove_team_member(project, id)
delete("/projects/#{project}/members/#{id}")
end

# Gets a list of project hooks.
Expand Down
11 changes: 6 additions & 5 deletions lib/gitlab/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ def put(path, options={})
validate self.class.put(path, options)
end

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

# Checks the response code for common errors.
Expand Down Expand Up @@ -76,14 +76,15 @@ def error_message(response)
"Request URI: #{response.request.base_uri}#{response.request.path}"
end

# Sets a base_uri and private_token parameter for requests.
# Sets a base_uri and default_params for requests.
# @raise [Error::MissingCredentials] if endpoint or private_token not set.
def set_request_defaults(endpoint, private_token)
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

self.class.base_uri endpoint
self.class.default_params :private_token => private_token
self.class.default_params :private_token => private_token, :sudo => sudo
self.class.default_params.delete(:sudo) if sudo.nil?
end
end
end
74 changes: 21 additions & 53 deletions spec/gitlab/client/projects_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,19 @@
end

describe ".project" do
context 'normal execution' do
before do
stub_get("/projects/3", "project")
@project = Gitlab.project(3)
end

it "should get the correct resource" do
a_get("/projects/3").should have_been_made
end

it "should return information about a project" do
@project.name.should == "Gitlab"
@project.owner.name.should == "John Smith"
end
end

context 'supports sudo' do
it "should support the additional argument" do
stub_request(:get, "#{Gitlab.endpoint}/projects/3").
with(:query => {:private_token => Gitlab.private_token, :sudo => "foo"}).
to_return(:body => load_fixture("project"))
@project = Gitlab.project(3, {sudo:'foo'})
a_get("/projects/3?sudo=foo").should have_been_made
@project.name.should == "Gitlab"
@project.owner.name.should == "John Smith"
end
before do
stub_get("/projects/3", "project")
@project = Gitlab.project(3)
end

it "should get the correct resource" do
a_get("/projects/3").should have_been_made
end

it "should return information about a project" do
@project.name.should == "Gitlab"
@project.owner.name.should == "John Smith"
end
end

describe ".create_project" do
Expand Down Expand Up @@ -143,35 +128,18 @@
end

describe ".remove_team_member" do
context 'as a user' do
before do
stub_delete("/projects/3/members/1", "team_member")
@team_member = Gitlab.remove_team_member(3, 1)
end

it "should get the correct resource" do
a_delete("/projects/3/members/1").should have_been_made
end

it "should return information about a removed team member" do
@team_member.name.should == "John Smith"
end
before do
stub_delete("/projects/3/members/1", "team_member")
@team_member = Gitlab.remove_team_member(3, 1)
end
context 'as sudo-ing to a user' do
before do
stub_delete("/projects/3/members/1", "team_member")
@team_member = Gitlab.remove_team_member(3, 1, :sudo => 1)
end

it "should get the correct resource" do
a_delete("/projects/3/members/1").should have_been_made
end

it "should return information about a removed team member" do
@team_member.name.should == "John Smith"
end
it "should get the correct resource" do
a_delete("/projects/3/members/1").should have_been_made
end

it "should return information about a removed team member" do
@team_member.name.should == "John Smith"
end
end

describe ".project_hooks" do
Expand Down Expand Up @@ -297,7 +265,7 @@
@deploy_keys.should be_an Array
@deploy_keys.first.id.should eq 2
@deploy_keys.first.title.should eq "Key Title"
@deploy_keys.first.key.should match /ssh-rsa/
@deploy_keys.first.key.should match(/ssh-rsa/)
end
end

Expand All @@ -314,7 +282,7 @@
it "should return project deploy key" do
@deploy_key.id.should eq 2
@deploy_key.title.should eq "Key Title"
@deploy_key.key.should match /ssh-rsa/
@deploy_key.key.should match(/ssh-rsa/)
end
end

Expand Down
7 changes: 7 additions & 0 deletions spec/gitlab_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@
end
end

describe ".sudo=" do
it "should set sudo" do
Gitlab.sudo = 'user'
Gitlab.sudo.should == 'user'
end
end

describe ".user_agent" do
it "should return default user_agent" do
Gitlab.user_agent.should == Gitlab::Configuration::DEFAULT_USER_AGENT
Expand Down

0 comments on commit 3fca003

Please sign in to comment.