2016-03-14 01:33:13 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of Composer.
|
|
|
|
*
|
|
|
|
* (c) Nils Adermann <naderman@naderman.de>
|
|
|
|
* Jordi Boggiano <j.boggiano@seld.be>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Composer\Test\Util;
|
|
|
|
|
|
|
|
use Composer\Util\Bitbucket;
|
2020-02-07 03:18:45 +00:00
|
|
|
use Composer\Test\TestCase;
|
2016-03-14 01:33:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Paul Wenke <wenke.paul@gmail.com>
|
|
|
|
*/
|
2017-11-04 14:52:13 +00:00
|
|
|
class BitbucketTest extends TestCase
|
2016-03-14 01:33:13 +00:00
|
|
|
{
|
|
|
|
private $username = 'username';
|
|
|
|
private $password = 'password';
|
2016-06-11 13:05:36 +00:00
|
|
|
private $consumer_key = 'consumer_key';
|
|
|
|
private $consumer_secret = 'consumer_secret';
|
2016-03-14 01:33:13 +00:00
|
|
|
private $message = 'mymessage';
|
|
|
|
private $origin = 'bitbucket.org';
|
|
|
|
private $token = 'bitbuckettoken';
|
|
|
|
|
2016-06-11 13:05:36 +00:00
|
|
|
/** @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;
|
2016-12-26 23:39:02 +00:00
|
|
|
/** @var int */
|
|
|
|
private $time;
|
2016-06-11 13:05:36 +00:00
|
|
|
|
|
|
|
protected function setUp()
|
|
|
|
{
|
|
|
|
$this->io = $this
|
|
|
|
->getMockBuilder('Composer\IO\ConsoleIO')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock()
|
|
|
|
;
|
|
|
|
|
|
|
|
$this->rfs = $this
|
|
|
|
->getMockBuilder('Composer\Util\RemoteFilesystem')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock()
|
|
|
|
;
|
|
|
|
|
2018-04-12 08:24:56 +00:00
|
|
|
$this->config = $this->getMockBuilder('Composer\Config')->getMock();
|
2016-06-11 13:05:36 +00:00
|
|
|
|
2016-12-26 23:39:02 +00:00
|
|
|
$this->time = time();
|
|
|
|
|
|
|
|
$this->bitbucket = new Bitbucket($this->io, $this->config, null, $this->rfs, $this->time);
|
2016-06-11 13:05:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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',
|
2017-03-08 14:07:29 +00:00
|
|
|
),
|
2016-06-11 13:05:36 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
->willReturn(
|
|
|
|
sprintf(
|
|
|
|
'{"access_token": "%s", "scopes": "repository", "expires_in": 3600, "refresh_token": "refreshtoken", "token_type": "bearer"}',
|
|
|
|
$this->token
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2016-12-26 23:39:02 +00:00
|
|
|
$this->config->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with('bitbucket-oauth')
|
|
|
|
->willReturn(null);
|
|
|
|
|
|
|
|
$this->setExpectationsForStoringAccessToken();
|
|
|
|
|
2016-06-11 13:05:36 +00:00
|
|
|
$this->assertEquals(
|
2016-12-26 23:39:02 +00:00
|
|
|
$this->token,
|
2016-06-11 13:05:36 +00:00
|
|
|
$this->bitbucket->requestToken($this->origin, $this->consumer_key, $this->consumer_secret)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-12-27 11:48:54 +00:00
|
|
|
public function testRequestAccessTokenWithValidOAuthConsumerAndValidStoredAccessToken()
|
|
|
|
{
|
|
|
|
$this->config->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with('bitbucket-oauth')
|
|
|
|
->willReturn(
|
|
|
|
array(
|
|
|
|
$this->origin => array(
|
|
|
|
'access-token' => $this->token,
|
|
|
|
'access-token-expiration' => $this->time + 1800,
|
|
|
|
'consumer-key' => $this->consumer_key,
|
2017-03-08 14:07:29 +00:00
|
|
|
'consumer-secret' => $this->consumer_secret,
|
|
|
|
),
|
2016-12-27 11:48:54 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
$this->token,
|
|
|
|
$this->bitbucket->requestToken($this->origin, $this->consumer_key, $this->consumer_secret)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRequestAccessTokenWithValidOAuthConsumerAndExpiredAccessToken()
|
|
|
|
{
|
|
|
|
$this->config->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with('bitbucket-oauth')
|
|
|
|
->willReturn(
|
|
|
|
array(
|
|
|
|
$this->origin => array(
|
|
|
|
'access-token' => 'randomExpiredToken',
|
|
|
|
'access-token-expiration' => $this->time - 400,
|
|
|
|
'consumer-key' => $this->consumer_key,
|
2017-03-08 14:07:29 +00:00
|
|
|
'consumer-secret' => $this->consumer_secret,
|
|
|
|
),
|
2016-12-27 11:48:54 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$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',
|
2017-03-08 14:07:29 +00:00
|
|
|
),
|
2016-12-27 11:48:54 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
->willReturn(
|
|
|
|
sprintf(
|
|
|
|
'{"access_token": "%s", "scopes": "repository", "expires_in": 3600, "refresh_token": "refreshtoken", "token_type": "bearer"}',
|
|
|
|
$this->token
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->setExpectationsForStoringAccessToken();
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
$this->token,
|
|
|
|
$this->bitbucket->requestToken($this->origin, $this->consumer_key, $this->consumer_secret)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-06-11 13:05:36 +00:00
|
|
|
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',
|
2017-03-08 14:07:29 +00:00
|
|
|
),
|
2016-06-11 13:05:36 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
->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
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2016-12-26 23:39:02 +00:00
|
|
|
$this->config->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with('bitbucket-oauth')
|
|
|
|
->willReturn(null);
|
|
|
|
|
|
|
|
$this->assertEquals('', $this->bitbucket->requestToken($this->origin, $this->username, $this->password));
|
2016-06-11 13:05:36 +00:00
|
|
|
}
|
|
|
|
|
2016-03-14 01:33:13 +00:00
|
|
|
public function testUsernamePasswordAuthenticationFlow()
|
|
|
|
{
|
2016-06-11 13:05:36 +00:00
|
|
|
$this->io
|
2016-03-14 01:33:13 +00:00
|
|
|
->expects($this->at(0))
|
|
|
|
->method('writeError')
|
|
|
|
->with($this->message)
|
|
|
|
;
|
|
|
|
|
2016-06-11 13:05:36 +00:00
|
|
|
$this->io->expects($this->exactly(2))
|
2016-03-14 01:33:13 +00:00
|
|
|
->method('askAndHideAnswer')
|
|
|
|
->withConsecutive(
|
|
|
|
array('Consumer Key (hidden): '),
|
|
|
|
array('Consumer Secret (hidden): ')
|
|
|
|
)
|
2016-06-11 13:05:36 +00:00
|
|
|
->willReturnOnConsecutiveCalls($this->consumer_key, $this->consumer_secret);
|
2016-03-14 01:33:13 +00:00
|
|
|
|
2016-06-11 13:05:36 +00:00
|
|
|
$this->rfs
|
2016-03-14 01:33:13 +00:00
|
|
|
->expects($this->once())
|
|
|
|
->method('getContents')
|
|
|
|
->with(
|
|
|
|
$this->equalTo($this->origin),
|
|
|
|
$this->equalTo(sprintf('https://%s/site/oauth2/access_token', $this->origin)),
|
|
|
|
$this->isFalse(),
|
|
|
|
$this->anything()
|
|
|
|
)
|
2016-12-26 23:39:02 +00:00
|
|
|
->willReturn(
|
|
|
|
sprintf(
|
|
|
|
'{"access_token": "%s", "scopes": "repository", "expires_in": 3600, "refresh_token": "refresh_token", "token_type": "bearer"}',
|
|
|
|
$this->token
|
2016-07-02 15:03:01 +00:00
|
|
|
)
|
2016-12-26 23:39:02 +00:00
|
|
|
)
|
|
|
|
;
|
2016-07-02 15:03:01 +00:00
|
|
|
|
2016-12-26 23:39:02 +00:00
|
|
|
$this->setExpectationsForStoringAccessToken(true);
|
2016-07-02 15:03:01 +00:00
|
|
|
|
2016-06-11 13:05:36 +00:00
|
|
|
$this->assertTrue($this->bitbucket->authorizeOAuthInteractively($this->origin, $this->message));
|
2016-03-14 01:33:13 +00:00
|
|
|
}
|
|
|
|
|
2016-12-26 23:39:02 +00:00
|
|
|
private function setExpectationsForStoringAccessToken($removeBasicAuth = false)
|
2016-03-14 01:33:13 +00:00
|
|
|
{
|
2018-04-12 08:24:56 +00:00
|
|
|
$configSourceMock = $this->getMockBuilder('Composer\Config\ConfigSourceInterface')->getMock();
|
2016-12-26 23:39:02 +00:00
|
|
|
$this->config->expects($this->once())
|
|
|
|
->method('getConfigSource')
|
|
|
|
->willReturn($configSourceMock);
|
2016-03-14 01:33:13 +00:00
|
|
|
|
2016-12-26 23:39:02 +00:00
|
|
|
$configSourceMock->expects($this->once())
|
2016-03-14 01:33:13 +00:00
|
|
|
->method('removeConfigSetting')
|
2016-12-26 23:39:02 +00:00
|
|
|
->with('bitbucket-oauth.' . $this->origin);
|
|
|
|
|
2018-04-12 08:24:56 +00:00
|
|
|
$authConfigSourceMock = $this->getMockBuilder('Composer\Config\ConfigSourceInterface')->getMock();
|
2016-12-26 23:39:02 +00:00
|
|
|
$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,
|
2017-03-08 14:07:29 +00:00
|
|
|
"access-token-expiration" => $this->time + 3600,
|
2016-12-26 23:39:02 +00:00
|
|
|
)
|
|
|
|
);
|
2016-03-14 01:33:13 +00:00
|
|
|
|
2016-12-26 23:39:02 +00:00
|
|
|
if ($removeBasicAuth) {
|
|
|
|
$authConfigSourceMock->expects($this->once())
|
|
|
|
->method('removeConfigSetting')
|
|
|
|
->with('http-basic.' . $this->origin);
|
|
|
|
}
|
2016-03-14 01:33:13 +00:00
|
|
|
}
|
|
|
|
}
|