diff --git a/lib/gitlab/client/users.rb b/lib/gitlab/client/users.rb index 5d5b0425a..bc0c03afb 100644 --- a/lib/gitlab/client/users.rb +++ b/lib/gitlab/client/users.rb @@ -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. @@ -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. diff --git a/spec/gitlab/client/users_spec.rb b/spec/gitlab/client/users_spec.rb index 01a37d123..eaeb11556 100644 --- a/spec/gitlab/client/users_spec.rb +++ b/spec/gitlab/client/users_spec.rb @@ -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