Add unit tests for the Bitbucket class.
parent
1241e3e83c
commit
2d52531365
|
@ -21,30 +21,138 @@ class BitbucketTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
private $username = 'username';
|
private $username = 'username';
|
||||||
private $password = 'password';
|
private $password = 'password';
|
||||||
private $authcode = 'authcode';
|
private $consumer_key = 'consumer_key';
|
||||||
|
private $consumer_secret = 'consumer_secret';
|
||||||
private $message = 'mymessage';
|
private $message = 'mymessage';
|
||||||
private $origin = 'bitbucket.org';
|
private $origin = 'bitbucket.org';
|
||||||
private $token = 'bitbuckettoken';
|
private $token = 'bitbuckettoken';
|
||||||
|
|
||||||
|
/** @type \Composer\IO\ConsoleIO|\PHPUnit_Framework_MockObject_MockObject */
|
||||||
|
private $io;
|
||||||
|
/** @type \Composer\Util\RemoteFilesystem|\PHPUnit_Framework_MockObject_MockObject */
|
||||||
|
private $rfs;
|
||||||
|
/** @type \Composer\Config|\PHPUnit_Framework_MockObject_MockObject */
|
||||||
|
private $config;
|
||||||
|
/** @type Bitbucket */
|
||||||
|
private $bitbucket;
|
||||||
|
|
||||||
|
protected function setUp()
|
||||||
|
{
|
||||||
|
$this->io = $this
|
||||||
|
->getMockBuilder('Composer\IO\ConsoleIO')
|
||||||
|
->disableOriginalConstructor()
|
||||||
|
->getMock()
|
||||||
|
;
|
||||||
|
|
||||||
|
$this->rfs = $this
|
||||||
|
->getMockBuilder('Composer\Util\RemoteFilesystem')
|
||||||
|
->disableOriginalConstructor()
|
||||||
|
->getMock()
|
||||||
|
;
|
||||||
|
|
||||||
|
$this->config = $this->getMock('Composer\Config');
|
||||||
|
|
||||||
|
$this->bitbucket = new Bitbucket($this->io, $this->config, null, $this->rfs);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testRequestAccessTokenWithValidOAuthConsumer()
|
||||||
|
{
|
||||||
|
$this->io->expects($this->once())
|
||||||
|
->method('setAuthentication')
|
||||||
|
->with($this->origin, $this->consumer_key, $this->consumer_secret);
|
||||||
|
|
||||||
|
$this->rfs->expects($this->once())
|
||||||
|
->method('getContents')
|
||||||
|
->with(
|
||||||
|
$this->origin,
|
||||||
|
Bitbucket::OAUTH2_ACCESS_TOKEN_URL,
|
||||||
|
false,
|
||||||
|
array(
|
||||||
|
'retry-auth-failure' => false,
|
||||||
|
'http' => array(
|
||||||
|
'method' => 'POST',
|
||||||
|
'content' => 'grant_type=client_credentials',
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
->willReturn(
|
||||||
|
sprintf(
|
||||||
|
'{"access_token": "%s", "scopes": "repository", "expires_in": 3600, "refresh_token": "refreshtoken", "token_type": "bearer"}',
|
||||||
|
$this->token
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
array(
|
||||||
|
'access_token' => $this->token,
|
||||||
|
'scopes' => 'repository',
|
||||||
|
'expires_in' => 3600,
|
||||||
|
'refresh_token' => 'refreshtoken',
|
||||||
|
'token_type' => 'bearer'
|
||||||
|
),
|
||||||
|
$this->bitbucket->requestToken($this->origin, $this->consumer_key, $this->consumer_secret)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testRequestAccessTokenWithUsernameAndPassword()
|
||||||
|
{
|
||||||
|
$this->io->expects($this->once())
|
||||||
|
->method('setAuthentication')
|
||||||
|
->with($this->origin, $this->username, $this->password);
|
||||||
|
|
||||||
|
$this->io->expects($this->any())
|
||||||
|
->method('writeError')
|
||||||
|
->withConsecutive(
|
||||||
|
array('<error>Invalid OAuth consumer provided.</error>'),
|
||||||
|
array('This can have two reasons:'),
|
||||||
|
array('1. You are authenticating with a bitbucket username/password combination'),
|
||||||
|
array('2. You are using an OAuth consumer, but didn\'t configure a (dummy) callback url')
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->rfs->expects($this->once())
|
||||||
|
->method('getContents')
|
||||||
|
->with(
|
||||||
|
$this->origin,
|
||||||
|
Bitbucket::OAUTH2_ACCESS_TOKEN_URL,
|
||||||
|
false,
|
||||||
|
array(
|
||||||
|
'retry-auth-failure' => false,
|
||||||
|
'http' => array(
|
||||||
|
'method' => 'POST',
|
||||||
|
'content' => 'grant_type=client_credentials',
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
->willThrowException(
|
||||||
|
new \Composer\Downloader\TransportException(
|
||||||
|
sprintf(
|
||||||
|
'The \'%s\' URL could not be accessed: HTTP/1.1 400 BAD REQUEST',
|
||||||
|
Bitbucket::OAUTH2_ACCESS_TOKEN_URL
|
||||||
|
),
|
||||||
|
400
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertEquals(array(), $this->bitbucket->requestToken($this->origin, $this->username, $this->password));
|
||||||
|
}
|
||||||
|
|
||||||
public function testUsernamePasswordAuthenticationFlow()
|
public function testUsernamePasswordAuthenticationFlow()
|
||||||
{
|
{
|
||||||
$io = $this->getIOMock();
|
$this->io
|
||||||
$io
|
|
||||||
->expects($this->at(0))
|
->expects($this->at(0))
|
||||||
->method('writeError')
|
->method('writeError')
|
||||||
->with($this->message)
|
->with($this->message)
|
||||||
;
|
;
|
||||||
|
|
||||||
$io->expects($this->exactly(2))
|
$this->io->expects($this->exactly(2))
|
||||||
->method('askAndHideAnswer')
|
->method('askAndHideAnswer')
|
||||||
->withConsecutive(
|
->withConsecutive(
|
||||||
array('Consumer Key (hidden): '),
|
array('Consumer Key (hidden): '),
|
||||||
array('Consumer Secret (hidden): ')
|
array('Consumer Secret (hidden): ')
|
||||||
)
|
)
|
||||||
->willReturnOnConsecutiveCalls($this->username, $this->password);
|
->willReturnOnConsecutiveCalls($this->consumer_key, $this->consumer_secret);
|
||||||
|
|
||||||
$rfs = $this->getRemoteFilesystemMock();
|
$this->rfs
|
||||||
$rfs
|
|
||||||
->expects($this->once())
|
->expects($this->once())
|
||||||
->method('getContents')
|
->method('getContents')
|
||||||
->with(
|
->with(
|
||||||
|
@ -56,50 +164,18 @@ class BitbucketTest extends \PHPUnit_Framework_TestCase
|
||||||
->willReturn(sprintf('{}', $this->token))
|
->willReturn(sprintf('{}', $this->token))
|
||||||
;
|
;
|
||||||
|
|
||||||
$config = $this->getConfigMock();
|
$this->config
|
||||||
$config
|
|
||||||
->expects($this->exactly(2))
|
->expects($this->exactly(2))
|
||||||
->method('getAuthConfigSource')
|
->method('getAuthConfigSource')
|
||||||
->willReturn($this->getAuthJsonMock())
|
->willReturn($this->getAuthJsonMock())
|
||||||
;
|
;
|
||||||
$config
|
$this->config
|
||||||
->expects($this->once())
|
->expects($this->once())
|
||||||
->method('getConfigSource')
|
->method('getConfigSource')
|
||||||
->willReturn($this->getConfJsonMock())
|
->willReturn($this->getConfJsonMock())
|
||||||
;
|
;
|
||||||
|
|
||||||
$bitbucket = new Bitbucket($io, $config, null, $rfs);
|
$this->assertTrue($this->bitbucket->authorizeOAuthInteractively($this->origin, $this->message));
|
||||||
|
|
||||||
$this->assertTrue($bitbucket->authorizeOAuthInteractively($this->origin, $this->message));
|
|
||||||
}
|
|
||||||
|
|
||||||
private function getIOMock()
|
|
||||||
{
|
|
||||||
$io = $this
|
|
||||||
->getMockBuilder('Composer\IO\ConsoleIO')
|
|
||||||
->disableOriginalConstructor()
|
|
||||||
->getMock()
|
|
||||||
;
|
|
||||||
|
|
||||||
return $io;
|
|
||||||
}
|
|
||||||
|
|
||||||
private function getConfigMock()
|
|
||||||
{
|
|
||||||
$config = $this->getMock('Composer\Config');
|
|
||||||
|
|
||||||
return $config;
|
|
||||||
}
|
|
||||||
|
|
||||||
private function getRemoteFilesystemMock()
|
|
||||||
{
|
|
||||||
$rfs = $this
|
|
||||||
->getMockBuilder('Composer\Util\RemoteFilesystem')
|
|
||||||
->disableOriginalConstructor()
|
|
||||||
->getMock()
|
|
||||||
;
|
|
||||||
|
|
||||||
return $rfs;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getAuthJsonMock()
|
private function getAuthJsonMock()
|
||||||
|
|
Loading…
Reference in New Issue