1
0
Fork 0
mirror of https://github.com/composer/composer synced 2025-05-09 00:22:53 +00:00

Store access-token for re-use

Store the Bitbucket access-token (and the expiration time) so it can be re-used within the time it is valid.
The Bitbucket::requestToken and Bitbucket::getToken now only return the access-token and not all other parameters it receives from the Bitbucket API.
This commit is contained in:
Stefan Grootscholten 2016-12-27 00:39:02 +01:00
parent 810267e2a7
commit a4af559ca8
5 changed files with 134 additions and 84 deletions

View file

@ -35,6 +35,8 @@ class BitbucketTest extends \PHPUnit_Framework_TestCase
private $config;
/** @type Bitbucket */
private $bitbucket;
/** @var int */
private $time;
protected function setUp()
{
@ -52,7 +54,9 @@ class BitbucketTest extends \PHPUnit_Framework_TestCase
$this->config = $this->getMock('Composer\Config');
$this->bitbucket = new Bitbucket($this->io, $this->config, null, $this->rfs);
$this->time = time();
$this->bitbucket = new Bitbucket($this->io, $this->config, null, $this->rfs, $this->time);
}
public function testRequestAccessTokenWithValidOAuthConsumer()
@ -82,14 +86,15 @@ class BitbucketTest extends \PHPUnit_Framework_TestCase
)
);
$this->config->expects($this->once())
->method('get')
->with('bitbucket-oauth')
->willReturn(null);
$this->setExpectationsForStoringAccessToken();
$this->assertEquals(
array(
'access_token' => $this->token,
'scopes' => 'repository',
'expires_in' => 3600,
'refresh_token' => 'refreshtoken',
'token_type' => 'bearer'
),
$this->token,
$this->bitbucket->requestToken($this->origin, $this->consumer_key, $this->consumer_secret)
);
}
@ -133,7 +138,12 @@ class BitbucketTest extends \PHPUnit_Framework_TestCase
)
);
$this->assertEquals(array(), $this->bitbucket->requestToken($this->origin, $this->username, $this->password));
$this->config->expects($this->once())
->method('get')
->with('bitbucket-oauth')
->willReturn(null);
$this->assertEquals('', $this->bitbucket->requestToken($this->origin, $this->username, $this->password));
}
public function testUsernamePasswordAuthenticationFlow()
@ -161,67 +171,51 @@ class BitbucketTest extends \PHPUnit_Framework_TestCase
$this->isFalse(),
$this->anything()
)
->willReturn(sprintf('{}', $this->token))
;
$authJson = $this->getAuthJsonMock();
$this->config
->expects($this->exactly(3))
->method('getAuthConfigSource')
->willReturn($authJson)
;
$this->config
->expects($this->once())
->method('getConfigSource')
->willReturn($this->getConfJsonMock())
;
$authJson->expects($this->once())
->method('addConfigSetting')
->with(
'bitbucket-oauth.'.$this->origin,
array(
'consumer-key' => $this->consumer_key,
'consumer-secret' => $this->consumer_secret
->willReturn(
sprintf(
'{"access_token": "%s", "scopes": "repository", "expires_in": 3600, "refresh_token": "refresh_token", "token_type": "bearer"}',
$this->token
)
);
)
;
$authJson->expects($this->once())
->method('removeConfigSetting')
->with('http-basic.'.$this->origin);
$this->setExpectationsForStoringAccessToken(true);
$this->assertTrue($this->bitbucket->authorizeOAuthInteractively($this->origin, $this->message));
}
private function getAuthJsonMock()
private function setExpectationsForStoringAccessToken($removeBasicAuth = false)
{
$authjson = $this
->getMockBuilder('Composer\Config\JsonConfigSource')
->disableOriginalConstructor()
->getMock()
;
$authjson
->expects($this->atLeastOnce())
->method('getName')
->willReturn('auth.json')
;
$configSourceMock = $this->getMock('Composer\Config\ConfigSourceInterface');
$this->config->expects($this->once())
->method('getConfigSource')
->willReturn($configSourceMock);
return $authjson;
}
private function getConfJsonMock()
{
$confjson = $this
->getMockBuilder('Composer\Config\JsonConfigSource')
->disableOriginalConstructor()
->getMock()
;
$confjson
->expects($this->atLeastOnce())
$configSourceMock->expects($this->once())
->method('removeConfigSetting')
->with('bitbucket-oauth.'.$this->origin)
;
->with('bitbucket-oauth.' . $this->origin);
return $confjson;
$authConfigSourceMock = $this->getMock('Composer\Config\ConfigSourceInterface');
$this->config->expects($this->atLeastOnce())
->method('getAuthConfigSource')
->willReturn($authConfigSourceMock);
$authConfigSourceMock->expects($this->once())
->method('addConfigSetting')
->with(
'bitbucket-oauth.' . $this->origin,
array(
"consumer-key" => $this->consumer_key,
"consumer-secret" => $this->consumer_secret,
"access-token" => $this->token,
"access-token-expiration" => $this->time + 3600
)
);
if ($removeBasicAuth) {
$authConfigSourceMock->expects($this->once())
->method('removeConfigSetting')
->with('http-basic.' . $this->origin);
}
}
}