Skip to content

Commit c946569

Browse files
authored
Merge pull request #331 from woodylan/master
support impersonation tokens
2 parents 5a3ef56 + d427844 commit c946569

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed

lib/Gitlab/Api/Users.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,4 +253,49 @@ public function email($id)
253253
{
254254
return $this->get('user/emails/'.$this->encodePath($id));
255255
}
256+
257+
/**
258+
* @param int $user_id
259+
* @return mixed
260+
*/
261+
public function userImpersonationTokens($user_id)
262+
{
263+
return $this->get('users/'.$this->encodePath($user_id).'/impersonation_tokens');
264+
}
265+
266+
/**
267+
* @param int $user_id
268+
* @param int $impersonation_token_id
269+
* @return mixed
270+
*/
271+
public function userImpersonationToken($user_id, $impersonation_token_id)
272+
{
273+
return $this->get('users/'.$this->encodePath($user_id).'/impersonation_tokens/'.$this->encodePath($impersonation_token_id));
274+
}
275+
276+
/**
277+
* @param int $user_id
278+
* @param string $name
279+
* @param array $scopes
280+
* @param null $expires_at
281+
* @return mixed
282+
*/
283+
public function createImpersonationToken($user_id, $name, array $scopes, $expires_at = null)
284+
{
285+
return $this->post('users/'.$this->encodePath($user_id).'/impersonation_tokens', array(
286+
'name' => $name,
287+
'scopes' => $scopes,
288+
'expires_at' => $expires_at
289+
));
290+
}
291+
292+
/**
293+
* @param int $user_id
294+
* @param int $impersonation_token_id
295+
* @return mixed
296+
*/
297+
public function removeImpersonationToken($user_id, $impersonation_token_id)
298+
{
299+
return $this->delete('users/'.$this->encodePath($user_id).'/impersonation_tokens/'.$this->encodePath($impersonation_token_id));
300+
}
256301
}

test/Gitlab/Tests/Api/UsersTest.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,77 @@ public function shouldGetSpecificUserEmail()
406406
$this->assertEquals($expectedArray, $api->email(1));
407407
}
408408

409+
/**
410+
* @test
411+
*/
412+
public function shouldGetCurrentUserImpersonationTokens()
413+
{
414+
$expectedArray = array(
415+
array('id' => 1, 'name' => 'A Name', 'revoked' => false),
416+
array('id' => 2, 'name' => 'A Name', 'revoked' => false),
417+
);
418+
419+
$api = $this->getApiMock();
420+
$api->expects($this->once())
421+
->method('get')
422+
->with('users/1/impersonation_tokens')
423+
->will($this->returnValue($expectedArray))
424+
;
425+
426+
$this->assertEquals($expectedArray, $api->userImpersonationTokens(1));
427+
}
428+
429+
/**
430+
* @test
431+
*/
432+
public function shouldGetUserImpersonationToken()
433+
{
434+
$expectedArray = array('id' => 2, 'name' => 'name');
435+
436+
$api = $this->getApiMock();
437+
$api->expects($this->once())
438+
->method('get')
439+
->with('users/1/impersonation_tokens/1')
440+
->will($this->returnValue($expectedArray))
441+
;
442+
443+
$this->assertEquals($expectedArray, $api->userImpersonationToken(1, 1));
444+
}
445+
446+
/**
447+
* @test
448+
*/
449+
public function shouldCreateImpersonationTokenForUser()
450+
{
451+
$expectedArray = array('id' => 1, 'name' => 'name');
452+
453+
$api = $this->getApiMock();
454+
$api->expects($this->once())
455+
->method('post')
456+
->with('users/1/impersonation_tokens', array('name' => 'name', 'scopes' => ['api'] ,'expires_at' => null))
457+
->will($this->returnValue($expectedArray))
458+
;
459+
460+
$this->assertEquals($expectedArray, $api->createImpersonationToken(1, 'name', ['api']));
461+
}
462+
463+
/**
464+
* @test
465+
*/
466+
public function shouldDeleteImpersonationTokenForUser()
467+
{
468+
$expectedBool = true;
469+
470+
$api = $this->getApiMock();
471+
$api->expects($this->once())
472+
->method('delete')
473+
->with('users/1/impersonation_tokens/1')
474+
->will($this->returnValue($expectedBool))
475+
;
476+
477+
$this->assertEquals($expectedBool, $api->removeImpersonationToken(1, 1));
478+
}
479+
409480
protected function getApiClass()
410481
{
411482
return 'Gitlab\Api\Users';

0 commit comments

Comments
 (0)