Skip to content

Commit

Permalink
Update Authorizations API
Browse files Browse the repository at this point in the history
  • Loading branch information
cursedcoder committed May 25, 2015
1 parent 3c36776 commit 2ea5d50
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 28 deletions.
100 changes: 89 additions & 11 deletions lib/Github/Api/Authorizations.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,122 @@

namespace Github\Api;

use Github\Api\AbstractApi;

/**
* Creating, deleting and listing authorizations.
*
* @link http://developer.github.com/v3/oauth/
* @link http://developer.github.com/v3/oauth_authorizations/
* @author Evgeniy Guseletov <[email protected]>
*/
class Authorizations extends AbstractApi
{
/**
* List all authorizations.
*
* @return array
*/
public function all()
{
return $this->get('authorizations');
}

public function show($number)
/**
* Show a single authorization.
*
* @param $clientId
*
* @return array
*/
public function show($clientId)
{
return $this->get('authorizations/'.rawurlencode($number));
return $this->get('authorizations/'.rawurlencode($clientId));
}

/**
* Create an authorization.
*
* @param array $params
* @param null $OTPCode
*
* @return array
*/
public function create(array $params, $OTPCode = null)
{
$headers = null === $OTPCode ? array() : array('X-GitHub-OTP' => $OTPCode);

return $this->post('authorizations', $params, $headers);
}

public function update($id, array $params)
/**
* Update an authorization.
*
* @param $clientId
* @param array $params
*
* @return array
*/
public function update($clientId, array $params)
{
return $this->patch('authorizations/'.rawurlencode($clientId), $params);
}

/**
* Remove an authorization.
*
* @param $clientId
*
* @return array
*/
public function remove($clientId)
{
return $this->delete('authorizations/'.rawurlencode($clientId));
}

/**
* Check an authorization.
*
* @param $clientId
* @param $token
*
* @return array
*/
public function check($clientId, $token)
{
return $this->get('applications/'.rawurlencode($clientId).'/tokens/'.rawurlencode($token));
}

/**
* Reset an authorization.
*
* @param $clientId
* @param $token
*
* @return array
*/
public function reset($clientId, $token)
{
return $this->patch('authorizations/'.rawurlencode($id), $params);
return $this->post('applications/'.rawurlencode($clientId).'/tokens/'.rawurlencode($token));
}

public function remove($id)
/**
* Remove an authorization.
*
* @param $clientId
* @param $token
*
* @return array
*/
public function revoke($clientId, $token)
{
return $this->delete('authorizations/'.rawurlencode($id));
return $this->delete('applications/'.rawurlencode($clientId).'/tokens/'.rawurlencode($token));
}

public function check($id, $token)
/**
* Revoke all authorizations.
*
* @param $clientId
*/
public function revokeAll($clientId)
{
return $this->get('authorizations/'.rawurlencode($id).'/tokens/'.rawurlencode($token));
$this->delete('applications/'.rawurlencode($clientId).'/tokens');
}
}
81 changes: 64 additions & 17 deletions test/Github/Tests/Api/AuthorizationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,55 @@ public function shouldShowAuthorization()
$this->assertEquals($expectedArray, $api->show($id));
}

/**
* @test
*/
public function shouldAuthorization()
{
$input = array(
'note' => '',
);

$api = $this->getApiMock();
$api->expects($this->once())
->method('post')
->with('authorizations', $input);

$api->create($input);
}

/**
* @test
*/
public function shouldUpdateAuthorization()
{
$id = 123;
$input = array(
'note' => '',
);

$api = $this->getApiMock();
$api->expects($this->once())
->method('patch')
->with('authorizations/'.$id, $input);

$api->update($id, $input);
}

/**
* @test
*/
public function shouldDeleteAuthorization()
{
$id = 123;
$api = $this->getApiMock();
$api->expects($this->once())
->method('delete')
->with('authorizations/'.$id);

$api->remove($id);
}

/**
* @test
*/
Expand All @@ -49,7 +98,7 @@ public function shouldCheckAuthorization()
$api = $this->getApiMock();
$api->expects($this->once())
->method('get')
->with('authorizations/'.$id.'/tokens/'.$token)
->with('applications/'.$id.'/tokens/'.$token)
->will($this->returnValue($expectedArray));

$this->assertEquals($expectedArray, $api->check($id, $token));
Expand All @@ -58,50 +107,48 @@ public function shouldCheckAuthorization()
/**
* @test
*/
public function shouldAuthorization()
public function shouldResetAuthorization()
{
$input = array(
'note' => '',
);
$id = 123;
$token = 'abcde';

$api = $this->getApiMock();
$api->expects($this->once())
->method('post')
->with('authorizations', $input);
->with('applications/'.$id.'/tokens/'.$token);

$api->create($input);
$api->reset($id, $token);
}

/**
* @test
*/
public function shouldUpdateAuthorization()
public function shouldRevokeAuthorization()
{
$id = 123;
$input = array(
'note' => '',
);
$token = 'abcde';

$api = $this->getApiMock();
$api->expects($this->once())
->method('patch')
->with('authorizations/'.$id, $input);
->method('delete')
->with('applications/'.$id.'/tokens/'.$token);

$api->update($id, $input);
$api->revoke($id, $token);
}

/**
* @test
*/
public function shouldDeleteAuthorization()
public function shouldRevokeAllAuthorizations()
{
$id = 123;

$api = $this->getApiMock();
$api->expects($this->once())
->method('delete')
->with('authorizations/'.$id);
->with('applications/'.$id.'/tokens');

$api->remove($id);
$api->revokeAll($id);
}

protected function getApiClass()
Expand Down

0 comments on commit 2ea5d50

Please sign in to comment.