Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions lib/gitlab/client/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,16 @@ def ssh_key(id)
#
# @param [String] title The title of an SSH key.
# @param [String] key The SSH key body.
# @param [Hash] options A customizable set of options.
# @option options [Integer] :user_id id of the user to associate the key with
# @return [Gitlab::ObjectifiedHash] Information about created SSH key.
def create_ssh_key(title, key)
post('/user/keys', body: { title: title, key: key })
def create_ssh_key(title, key, options = {})
user_id = options.delete :user_id
if user_id.to_i.zero?
post('/user/keys', body: { title: title, key: key })
else
post("/users/#{user_id}/keys", body: { title: title, key: key })
end
end

# Deletes an SSH key.
Expand All @@ -174,9 +181,16 @@ def create_ssh_key(title, key)
# Gitlab.delete_ssh_key(1)
#
# @param [Integer] id The ID of a user's SSH key.
# @param [Hash] options A customizable set of options.
# @option options [Integer] :user_id id of the user to associate the key with
# @return [Gitlab::ObjectifiedHash] Information about deleted SSH key.
def delete_ssh_key(id)
delete("/user/keys/#{id}")
def delete_ssh_key(id, options = {})
user_id = options.delete :user_id
if user_id.to_i.zero?
delete("/user/keys/#{id}")
else
delete("/users/#{user_id}/keys/#{id}")
end
end

# Gets user emails.
Expand Down
71 changes: 54 additions & 17 deletions spec/gitlab/client/users_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -252,33 +252,70 @@
end

describe '.create_ssh_key' do
before do
stub_post('/user/keys', 'key')
@key = Gitlab.create_ssh_key('title', 'body')
end
describe 'without user ID' do
before do
stub_post('/user/keys', 'key')
@key = Gitlab.create_ssh_key('title', 'body')
end

it 'gets the correct resource' do
body = { title: 'title', key: 'body' }
expect(a_post('/user/keys').with(body: body)).to have_been_made
it 'gets the correct resource' do
body = { title: 'title', key: 'body' }
expect(a_post('/user/keys').with(body: body)).to have_been_made
end

it 'returns information about a created SSH key' do
expect(@key.title).to eq('narkoz@helium')
end
end

it 'returns information about a created SSH key' do
expect(@key.title).to eq('narkoz@helium')
describe 'with user ID' do
before do
stub_post('/users/1/keys', 'key')
@options = { user_id: 1 }
@key = Gitlab.create_ssh_key('title', 'body', @options)
end

it 'gets the correct resource' do
body = { title: 'title', key: 'body' }
expect(a_post('/users/1/keys').with(body: body)).to have_been_made
end

it 'returns information about a created SSH key' do
expect(@key.title).to eq('narkoz@helium')
end
end
end

describe '.delete_ssh_key' do
before do
stub_delete('/user/keys/1', 'key')
@key = Gitlab.delete_ssh_key(1)
end
describe 'without user ID' do
before do
stub_delete('/user/keys/1', 'key')
@key = Gitlab.delete_ssh_key(1)
end

it 'gets the correct resource' do
expect(a_delete('/user/keys/1')).to have_been_made
it 'gets the correct resource' do
expect(a_delete('/user/keys/1')).to have_been_made
end

it 'returns information about a deleted SSH key' do
expect(@key.title).to eq('narkoz@helium')
end
end

it 'returns information about a deleted SSH key' do
expect(@key.title).to eq('narkoz@helium')
describe 'with user ID' do
before do
stub_delete('/users/1/keys/1', 'key')
@options = { user_id: 1 }
@key = Gitlab.delete_ssh_key(1, @options)
end

it 'gets the correct resource' do
expect(a_delete('/users/1/keys/1')).to have_been_made
end

it 'returns information about a deleted SSH key' do
expect(@key.title).to eq('narkoz@helium')
end
end
end

Expand Down