diff --git a/lib/gitlab/client/users.rb b/lib/gitlab/client/users.rb index 500a0145b..b84aa127b 100644 --- a/lib/gitlab/client/users.rb +++ b/lib/gitlab/client/users.rb @@ -329,5 +329,58 @@ def add_user_custom_attribute(key, value, user_id) def delete_user_custom_attribute(key, user_id) delete("/users/#{user_id}/custom_attributes/#{key}") end + + # Get all impersonation tokens for a user + # + # @example + # Gitlab.user_impersonation_tokens(1) + # + # @param [Integer] user_id The ID of the user. + # @param [String] state Filter impersonation tokens by state {} + # @return [Array] + def user_impersonation_tokens(user_id) + get("/users/#{user_id}/impersonation_tokens") + end + + # Get impersonation token information + # + # @example + # Gitlab.user_impersonation_token(1, 1) + # + # @param [Integer] user_id The ID of the user. + # @param [Integer] impersonation_token_id ID of the impersonation token. + # @return [Gitlab::ObjectifiedHash] + def user_impersonation_token(user_id, impersonation_token_id) + get("/users/#{user_id}/impersonation_tokens/#{impersonation_token_id}") + end + + # Create impersonation token + # + # @example + # Gitlab.create_user_impersonation_token(2, "token", ["api", "read_user"]) + # Gitlab.create_user_impersonation_token(2, "token", ["api", "read_user"], "1970-01-01") + # + # @param [Integer] user_id The ID of the user. + # @param [String] name Name for impersonation token. + # @param [Array] scopes Array of scopes for the impersonation token + # @param [String] expires_at Date for impersonation token expiration in ISO format. + # @return [Gitlab::ObjectifiedHash] + def create_user_impersonation_token(user_id, name, scopes, expires_at = nil) + body = { name: name, scopes: scopes } + body[:expires_at] = expires_at if expires_at + post("/users/#{user_id}/impersonation_tokens", body: body) + end + + # Revoke an impersonation token + # + # @example + # Gitlab.revoke_user_impersonation_token(1, 1) + # + # @param [Integer] user_id The ID of the user. + # @param [Integer] impersonation_token_id ID of the impersonation token. + # @return [Gitlab::ObjectifiedHash] + def revoke_user_impersonation_token(user_id, impersonation_token_id) + delete("/users/#{user_id}/impersonation_tokens/#{impersonation_token_id}") + end end end diff --git a/spec/fixtures/impersonation_create.json b/spec/fixtures/impersonation_create.json new file mode 100644 index 000000000..8b4a674ce --- /dev/null +++ b/spec/fixtures/impersonation_create.json @@ -0,0 +1,14 @@ +{ + "id" : 2, + "revoked" : false, + "user_id" : 2, + "scopes" : [ + "api" + ], + "token" : "EsMo-vhKfXGwX9RKrwiy", + "active" : true, + "impersonation" : true, + "name" : "mytoken", + "created_at" : "2017-03-17T17:18:09.283Z", + "expires_at" : "2017-04-04" +} diff --git a/spec/fixtures/impersonation_get.json b/spec/fixtures/impersonation_get.json new file mode 100644 index 000000000..936d21ba2 --- /dev/null +++ b/spec/fixtures/impersonation_get.json @@ -0,0 +1,13 @@ +{ + "active" : true, + "user_id" : 2, + "scopes" : [ + "api" + ], + "revoked" : false, + "name" : "mytoken", + "id" : 2, + "created_at" : "2017-03-17T17:18:09.283Z", + "impersonation" : true, + "expires_at" : "2017-04-04" +} diff --git a/spec/fixtures/impersonation_get_all.json b/spec/fixtures/impersonation_get_all.json new file mode 100644 index 000000000..158cf0072 --- /dev/null +++ b/spec/fixtures/impersonation_get_all.json @@ -0,0 +1,28 @@ +[ + { + "active": true, + "user_id": 2, + "scopes": [ + "api" + ], + "revoked": false, + "name": "mytoken", + "id": 2, + "created_at": "2017-03-17T17:18:09.283Z", + "impersonation": true, + "expires_at": "2017-04-04" + }, + { + "active": false, + "user_id": 2, + "scopes": [ + "read_user" + ], + "revoked": true, + "name": "mytoken2", + "created_at": "2017-03-17T17:19:28.697Z", + "id": 3, + "impersonation": true, + "expires_at": "2017-04-14" + } +] \ No newline at end of file diff --git a/spec/gitlab/client/users_spec.rb b/spec/gitlab/client/users_spec.rb index 5f97e4304..4abdbe7e9 100644 --- a/spec/gitlab/client/users_spec.rb +++ b/spec/gitlab/client/users_spec.rb @@ -547,4 +547,74 @@ end end end + + describe 'impersonation tokens' do + describe 'get all' do + before do + stub_get('/users/2/impersonation_tokens', 'impersonation_get_all') + @tokens = Gitlab.user_impersonation_tokens(2) + end + + it 'gets the correct resource' do + expect(a_get('/users/2/impersonation_tokens')).to have_been_made + end + + it 'gets an array of user impersonation tokens' do + expect(@tokens.first.id).to eq(2) + expect(@tokens.last.id).to eq(3) + expect(@tokens.first.impersonation).to be_truthy + expect(@tokens.last.impersonation).to be_truthy + end + end + end + + describe 'get one' do + before do + stub_get('/users/2/impersonation_tokens/2', 'impersonation_get') + @token = Gitlab.user_impersonation_token(2, 2) + end + + it 'gets the correct resource' do + expect(a_get('/users/2/impersonation_tokens/2')).to have_been_made + end + + it 'gets a user impersonation token' do + expect(@token.user_id).to eq(2) + expect(@token.id).to eq(2) + expect(@token.impersonation).to be_truthy + end + end + + describe 'create' do + before do + stub_post('/users/2/impersonation_tokens', 'impersonation_create') + @token = Gitlab.create_user_impersonation_token(2, 'mytoken', scopes) + end + + it 'gets the correct resource' do + expect(a_post('/users/2/impersonation_tokens').with(body: 'name=mytoken&scopes%5B%5D=api')).to have_been_made + end + + it 'returns a valid impersonation token' do + expect(@token.user_id).to eq(2) + expect(@token.id).to eq(2) + expect(@token.impersonation).to be_truthy + expect(@token.active).to be_truthy + expect(@token.token).to eq('EsMo-vhKfXGwX9RKrwiy') + end + end + + describe 'revoke' do + before do + stub_request(:delete, "#{Gitlab.endpoint}/users/2/impersonation_tokens/2") + .with(headers: { 'PRIVATE-TOKEN' => Gitlab.private_token }) + .to_return(status: 204) + @token = Gitlab.revoke_user_impersonation_token(2, 2) + end + + it 'removes a token' do + expect(a_delete('/users/2/impersonation_tokens/2')).to have_been_made + expect(@token).to be_empty + end + end end