123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483 |
- <?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;
- use Composer\Util\Http\Response;
- use Composer\Test\TestCase;
- /**
- * @author Paul Wenke <wenke.paul@gmail.com>
- */
- class BitbucketTest extends TestCase
- {
- private $username = 'username';
- private $password = 'password';
- private $consumer_key = 'consumer_key';
- private $consumer_secret = 'consumer_secret';
- private $message = 'mymessage';
- private $origin = 'bitbucket.org';
- private $token = 'bitbuckettoken';
- /** @type \Composer\IO\ConsoleIO|\PHPUnit_Framework_MockObject_MockObject */
- private $io;
- /** @type \Composer\Util\HttpDownloader|\PHPUnit_Framework_MockObject_MockObject */
- private $httpDownloader;
- /** @type \Composer\Config|\PHPUnit_Framework_MockObject_MockObject */
- private $config;
- /** @type Bitbucket */
- private $bitbucket;
- /** @var int */
- private $time;
- protected function setUp()
- {
- $this->io = $this
- ->getMockBuilder('Composer\IO\ConsoleIO')
- ->disableOriginalConstructor()
- ->getMock()
- ;
- $this->httpDownloader = $this
- ->getMockBuilder('Composer\Util\HttpDownloader')
- ->disableOriginalConstructor()
- ->getMock()
- ;
- $this->config = $this->getMockBuilder('Composer\Config')->getMock();
- $this->time = time();
- $this->bitbucket = new Bitbucket($this->io, $this->config, null, $this->httpDownloader, $this->time);
- }
- public function testRequestAccessTokenWithValidOAuthConsumer()
- {
- $this->io->expects($this->once())
- ->method('setAuthentication')
- ->with($this->origin, $this->consumer_key, $this->consumer_secret);
- $this->httpDownloader->expects($this->once())
- ->method('get')
- ->with(
- Bitbucket::OAUTH2_ACCESS_TOKEN_URL,
- array(
- 'retry-auth-failure' => false,
- 'http' => array(
- 'method' => 'POST',
- 'content' => 'grant_type=client_credentials',
- ),
- )
- )
- ->willReturn(
- new Response(
- array('url' => Bitbucket::OAUTH2_ACCESS_TOKEN_URL),
- 200,
- array(),
- sprintf(
- '{"access_token": "%s", "scopes": "repository", "expires_in": 3600, "refresh_token": "refreshtoken", "token_type": "bearer"}',
- $this->token
- )
- )
- );
- $this->config->expects($this->once())
- ->method('get')
- ->with('bitbucket-oauth')
- ->willReturn(null);
- $this->setExpectationsForStoringAccessToken();
- $this->assertEquals(
- $this->token,
- $this->bitbucket->requestToken($this->origin, $this->consumer_key, $this->consumer_secret)
- );
- }
- 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,
- 'consumer-secret' => $this->consumer_secret,
- ),
- )
- );
- $this->assertEquals(
- $this->token,
- $this->bitbucket->requestToken($this->origin, $this->consumer_key, $this->consumer_secret)
- );
- return $this->bitbucket;
- }
- 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,
- 'consumer-secret' => $this->consumer_secret,
- ),
- )
- );
- $this->io->expects($this->once())
- ->method('setAuthentication')
- ->with($this->origin, $this->consumer_key, $this->consumer_secret);
- $this->httpDownloader->expects($this->once())
- ->method('get')
- ->with(
- Bitbucket::OAUTH2_ACCESS_TOKEN_URL,
- array(
- 'retry-auth-failure' => false,
- 'http' => array(
- 'method' => 'POST',
- 'content' => 'grant_type=client_credentials',
- ),
- )
- )
- ->willReturn(
- new Response(
- array('url' => Bitbucket::OAUTH2_ACCESS_TOKEN_URL),
- 200,
- array(),
- 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)
- );
- }
- 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->httpDownloader->expects($this->once())
- ->method('get')
- ->with(
- Bitbucket::OAUTH2_ACCESS_TOKEN_URL,
- 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->config->expects($this->once())
- ->method('get')
- ->with('bitbucket-oauth')
- ->willReturn(null);
- $this->assertEquals('', $this->bitbucket->requestToken($this->origin, $this->username, $this->password));
- }
- public function testRequestAccessTokenWithUsernameAndPasswordWithUnauthorizedResponse()
- {
- $this->config->expects($this->once())
- ->method('get')
- ->with('bitbucket-oauth')
- ->willReturn(null);
- $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(
- 'You can also add it manually later by using "composer config --global --auth bitbucket-oauth.bitbucket.org <consumer-key> <consumer-secret>"')
- );
- $this->httpDownloader->expects($this->once())
- ->method('get')
- ->with(
- Bitbucket::OAUTH2_ACCESS_TOKEN_URL,
- array(
- 'retry-auth-failure' => false,
- 'http' => array(
- 'method' => 'POST',
- 'content' => 'grant_type=client_credentials',
- ),
- )
- )
- ->willThrowException(new \Composer\Downloader\TransportException('HTTP/1.1 401 UNAUTHORIZED',401));
- $this->assertEquals('', $this->bitbucket->requestToken($this->origin, $this->username, $this->password));
- }
- /**
- * @expectedException \Composer\Downloader\TransportException
- */
- public function testRequestAccessTokenWithUsernameAndPasswordWithNotFoundResponse()
- {
- $this->config->expects($this->once())
- ->method('get')
- ->with('bitbucket-oauth')
- ->willReturn(null);
- $this->io->expects($this->once())
- ->method('setAuthentication')
- ->with($this->origin, $this->username, $this->password);
- $exception = new \Composer\Downloader\TransportException('HTTP/1.1 404 NOT FOUND',404);
- $this->httpDownloader->expects($this->once())
- ->method('get')
- ->with(
- Bitbucket::OAUTH2_ACCESS_TOKEN_URL,
- array(
- 'retry-auth-failure' => false,
- 'http' => array(
- 'method' => 'POST',
- 'content' => 'grant_type=client_credentials',
- ),
- )
- )
- ->willThrowException($exception);
- $this->bitbucket->requestToken($this->origin, $this->username, $this->password);
- }
- public function testUsernamePasswordAuthenticationFlow()
- {
- $this->io
- ->expects($this->at(0))
- ->method('writeError')
- ->with($this->message)
- ;
- $this->io->expects($this->exactly(2))
- ->method('askAndHideAnswer')
- ->withConsecutive(
- array('Consumer Key (hidden): '),
- array('Consumer Secret (hidden): ')
- )
- ->willReturnOnConsecutiveCalls($this->consumer_key, $this->consumer_secret);
- $this->httpDownloader
- ->expects($this->once())
- ->method('get')
- ->with(
- $this->equalTo($url = sprintf('https://%s/site/oauth2/access_token', $this->origin)),
- $this->anything()
- )
- ->willReturn(
- new Response(
- array('url' => $url),
- 200,
- array(),
- sprintf(
- '{"access_token": "%s", "scopes": "repository", "expires_in": 3600, "refresh_token": "refresh_token", "token_type": "bearer"}',
- $this->token
- )
- )
- );
- ;
- $this->setExpectationsForStoringAccessToken(true);
- $this->assertTrue($this->bitbucket->authorizeOAuthInteractively($this->origin, $this->message));
- }
- public function testAuthorizeOAuthInteractivelyWithEmptyUsername()
- {
- $authConfigSourceMock = $this->getMockBuilder('Composer\Config\ConfigSourceInterface')->getMock();
- $this->config->expects($this->atLeastOnce())
- ->method('getAuthConfigSource')
- ->willReturn($authConfigSourceMock);
- $this->io->expects($this->once())
- ->method('askAndHideAnswer')
- ->with('Consumer Key (hidden): ')
- ->willReturnOnConsecutiveCalls(null);
- $this->assertFalse($this->bitbucket->authorizeOAuthInteractively($this->origin, $this->message));
- }
- public function testAuthorizeOAuthInteractivelyWithEmptyPassword()
- {
- $authConfigSourceMock = $this->getMockBuilder('Composer\Config\ConfigSourceInterface')->getMock();
- $this->config->expects($this->atLeastOnce())
- ->method('getAuthConfigSource')
- ->willReturn($authConfigSourceMock);
- $this->io->expects($this->exactly(2))
- ->method('askAndHideAnswer')
- ->withConsecutive(
- array('Consumer Key (hidden): '),
- array('Consumer Secret (hidden): ')
- )
- ->willReturnOnConsecutiveCalls($this->consumer_key, null);
- $this->assertFalse($this->bitbucket->authorizeOAuthInteractively($this->origin, $this->message));
- }
- public function testAuthorizeOAuthInteractivelyWithRequestAccessTokenFailure()
- {
- $authConfigSourceMock = $this->getMockBuilder('Composer\Config\ConfigSourceInterface')->getMock();
- $this->config->expects($this->atLeastOnce())
- ->method('getAuthConfigSource')
- ->willReturn($authConfigSourceMock);
- $this->io->expects($this->exactly(2))
- ->method('askAndHideAnswer')
- ->withConsecutive(
- array('Consumer Key (hidden): '),
- array('Consumer Secret (hidden): ')
- )
- ->willReturnOnConsecutiveCalls($this->consumer_key, $this->consumer_secret);
- $this->httpDownloader
- ->expects($this->once())
- ->method('get')
- ->with(
- $this->equalTo($url = sprintf('https://%s/site/oauth2/access_token', $this->origin)),
- $this->anything()
- )
- ->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->assertFalse($this->bitbucket->authorizeOAuthInteractively($this->origin, $this->message));
- }
- private function setExpectationsForStoringAccessToken($removeBasicAuth = false)
- {
- $configSourceMock = $this->getMockBuilder('Composer\Config\ConfigSourceInterface')->getMock();
- $this->config->expects($this->once())
- ->method('getConfigSource')
- ->willReturn($configSourceMock);
- $configSourceMock->expects($this->once())
- ->method('removeConfigSetting')
- ->with('bitbucket-oauth.' . $this->origin);
- $authConfigSourceMock = $this->getMockBuilder('Composer\Config\ConfigSourceInterface')->getMock();
- $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);
- }
- }
- public function testGetTokenWithoutAccessToken()
- {
- $this->assertSame('', $this->bitbucket->getToken());
- }
- /**
- * @depends testRequestAccessTokenWithValidOAuthConsumerAndValidStoredAccessToken
- *
- * @param Bitbucket $bitbucket
- */
- public function testGetTokenWithAccessToken(Bitbucket $bitbucket)
- {
- $this->assertSame($this->token, $bitbucket->getToken());
- }
- public function testAuthorizeOAuthWithWrongOriginUrl()
- {
- $this->assertFalse($this->bitbucket->authorizeOAuth('non-' . $this->origin));
- }
- public function testAuthorizeOAuthWithoutAvailableGitConfigToken()
- {
- $process = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
- $process->expects($this->once())
- ->method('execute')
- ->willReturn(-1);
- $bitbucket = new Bitbucket($this->io, $this->config, $process, $this->httpDownloader, $this->time);
- $this->assertFalse($bitbucket->authorizeOAuth($this->origin));
- }
- public function testAuthorizeOAuthWithAvailableGitConfigToken()
- {
- $process = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
- $process->expects($this->once())
- ->method('execute')
- ->willReturn(0);
- $bitbucket = new Bitbucket($this->io, $this->config, $process, $this->httpDownloader, $this->time);
- $this->assertTrue($bitbucket->authorizeOAuth($this->origin));
- }
- }
|