BitbucketTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Test\Util;
  12. use Composer\Util\Bitbucket;
  13. use Composer\Util\Http\Response;
  14. use Composer\Test\TestCase;
  15. /**
  16. * @author Paul Wenke <wenke.paul@gmail.com>
  17. */
  18. class BitbucketTest extends TestCase
  19. {
  20. private $username = 'username';
  21. private $password = 'password';
  22. private $consumer_key = 'consumer_key';
  23. private $consumer_secret = 'consumer_secret';
  24. private $message = 'mymessage';
  25. private $origin = 'bitbucket.org';
  26. private $token = 'bitbuckettoken';
  27. /** @type \Composer\IO\ConsoleIO|\PHPUnit_Framework_MockObject_MockObject */
  28. private $io;
  29. /** @type \Composer\Util\HttpDownloader|\PHPUnit_Framework_MockObject_MockObject */
  30. private $httpDownloader;
  31. /** @type \Composer\Config|\PHPUnit_Framework_MockObject_MockObject */
  32. private $config;
  33. /** @type Bitbucket */
  34. private $bitbucket;
  35. /** @var int */
  36. private $time;
  37. protected function setUp()
  38. {
  39. $this->io = $this
  40. ->getMockBuilder('Composer\IO\ConsoleIO')
  41. ->disableOriginalConstructor()
  42. ->getMock()
  43. ;
  44. $this->httpDownloader = $this
  45. ->getMockBuilder('Composer\Util\HttpDownloader')
  46. ->disableOriginalConstructor()
  47. ->getMock()
  48. ;
  49. $this->config = $this->getMockBuilder('Composer\Config')->getMock();
  50. $this->time = time();
  51. $this->bitbucket = new Bitbucket($this->io, $this->config, null, $this->httpDownloader, $this->time);
  52. }
  53. public function testRequestAccessTokenWithValidOAuthConsumer()
  54. {
  55. $this->io->expects($this->once())
  56. ->method('setAuthentication')
  57. ->with($this->origin, $this->consumer_key, $this->consumer_secret);
  58. $this->httpDownloader->expects($this->once())
  59. ->method('get')
  60. ->with(
  61. Bitbucket::OAUTH2_ACCESS_TOKEN_URL,
  62. array(
  63. 'retry-auth-failure' => false,
  64. 'http' => array(
  65. 'method' => 'POST',
  66. 'content' => 'grant_type=client_credentials',
  67. ),
  68. )
  69. )
  70. ->willReturn(
  71. new Response(
  72. array('url' => Bitbucket::OAUTH2_ACCESS_TOKEN_URL),
  73. 200,
  74. array(),
  75. sprintf(
  76. '{"access_token": "%s", "scopes": "repository", "expires_in": 3600, "refresh_token": "refreshtoken", "token_type": "bearer"}',
  77. $this->token
  78. )
  79. )
  80. );
  81. $this->config->expects($this->once())
  82. ->method('get')
  83. ->with('bitbucket-oauth')
  84. ->willReturn(null);
  85. $this->setExpectationsForStoringAccessToken();
  86. $this->assertEquals(
  87. $this->token,
  88. $this->bitbucket->requestToken($this->origin, $this->consumer_key, $this->consumer_secret)
  89. );
  90. }
  91. public function testRequestAccessTokenWithValidOAuthConsumerAndValidStoredAccessToken()
  92. {
  93. $this->config->expects($this->once())
  94. ->method('get')
  95. ->with('bitbucket-oauth')
  96. ->willReturn(
  97. array(
  98. $this->origin => array(
  99. 'access-token' => $this->token,
  100. 'access-token-expiration' => $this->time + 1800,
  101. 'consumer-key' => $this->consumer_key,
  102. 'consumer-secret' => $this->consumer_secret,
  103. ),
  104. )
  105. );
  106. $this->assertEquals(
  107. $this->token,
  108. $this->bitbucket->requestToken($this->origin, $this->consumer_key, $this->consumer_secret)
  109. );
  110. return $this->bitbucket;
  111. }
  112. public function testRequestAccessTokenWithValidOAuthConsumerAndExpiredAccessToken()
  113. {
  114. $this->config->expects($this->once())
  115. ->method('get')
  116. ->with('bitbucket-oauth')
  117. ->willReturn(
  118. array(
  119. $this->origin => array(
  120. 'access-token' => 'randomExpiredToken',
  121. 'access-token-expiration' => $this->time - 400,
  122. 'consumer-key' => $this->consumer_key,
  123. 'consumer-secret' => $this->consumer_secret,
  124. ),
  125. )
  126. );
  127. $this->io->expects($this->once())
  128. ->method('setAuthentication')
  129. ->with($this->origin, $this->consumer_key, $this->consumer_secret);
  130. $this->httpDownloader->expects($this->once())
  131. ->method('get')
  132. ->with(
  133. Bitbucket::OAUTH2_ACCESS_TOKEN_URL,
  134. array(
  135. 'retry-auth-failure' => false,
  136. 'http' => array(
  137. 'method' => 'POST',
  138. 'content' => 'grant_type=client_credentials',
  139. ),
  140. )
  141. )
  142. ->willReturn(
  143. new Response(
  144. array('url' => Bitbucket::OAUTH2_ACCESS_TOKEN_URL),
  145. 200,
  146. array(),
  147. sprintf(
  148. '{"access_token": "%s", "scopes": "repository", "expires_in": 3600, "refresh_token": "refreshtoken", "token_type": "bearer"}',
  149. $this->token
  150. )
  151. )
  152. );
  153. $this->setExpectationsForStoringAccessToken();
  154. $this->assertEquals(
  155. $this->token,
  156. $this->bitbucket->requestToken($this->origin, $this->consumer_key, $this->consumer_secret)
  157. );
  158. }
  159. public function testRequestAccessTokenWithUsernameAndPassword()
  160. {
  161. $this->io->expects($this->once())
  162. ->method('setAuthentication')
  163. ->with($this->origin, $this->username, $this->password);
  164. $this->io->expects($this->any())
  165. ->method('writeError')
  166. ->withConsecutive(
  167. array('<error>Invalid OAuth consumer provided.</error>'),
  168. array('This can have two reasons:'),
  169. array('1. You are authenticating with a bitbucket username/password combination'),
  170. array('2. You are using an OAuth consumer, but didn\'t configure a (dummy) callback url')
  171. );
  172. $this->httpDownloader->expects($this->once())
  173. ->method('get')
  174. ->with(
  175. Bitbucket::OAUTH2_ACCESS_TOKEN_URL,
  176. array(
  177. 'retry-auth-failure' => false,
  178. 'http' => array(
  179. 'method' => 'POST',
  180. 'content' => 'grant_type=client_credentials',
  181. ),
  182. )
  183. )
  184. ->willThrowException(
  185. new \Composer\Downloader\TransportException(
  186. sprintf(
  187. 'The \'%s\' URL could not be accessed: HTTP/1.1 400 BAD REQUEST',
  188. Bitbucket::OAUTH2_ACCESS_TOKEN_URL
  189. ),
  190. 400
  191. )
  192. );
  193. $this->config->expects($this->once())
  194. ->method('get')
  195. ->with('bitbucket-oauth')
  196. ->willReturn(null);
  197. $this->assertEquals('', $this->bitbucket->requestToken($this->origin, $this->username, $this->password));
  198. }
  199. public function testRequestAccessTokenWithUsernameAndPasswordWithUnauthorizedResponse()
  200. {
  201. $this->config->expects($this->once())
  202. ->method('get')
  203. ->with('bitbucket-oauth')
  204. ->willReturn(null);
  205. $this->io->expects($this->once())
  206. ->method('setAuthentication')
  207. ->with($this->origin, $this->username, $this->password);
  208. $this->io->expects($this->any())
  209. ->method('writeError')
  210. ->withConsecutive(
  211. array('<error>Invalid OAuth consumer provided.</error>'),
  212. array(
  213. 'You can also add it manually later by using "composer config --global --auth bitbucket-oauth.bitbucket.org <consumer-key> <consumer-secret>"')
  214. );
  215. $this->httpDownloader->expects($this->once())
  216. ->method('get')
  217. ->with(
  218. Bitbucket::OAUTH2_ACCESS_TOKEN_URL,
  219. array(
  220. 'retry-auth-failure' => false,
  221. 'http' => array(
  222. 'method' => 'POST',
  223. 'content' => 'grant_type=client_credentials',
  224. ),
  225. )
  226. )
  227. ->willThrowException(new \Composer\Downloader\TransportException('HTTP/1.1 401 UNAUTHORIZED',401));
  228. $this->assertEquals('', $this->bitbucket->requestToken($this->origin, $this->username, $this->password));
  229. }
  230. /**
  231. * @expectedException \Composer\Downloader\TransportException
  232. */
  233. public function testRequestAccessTokenWithUsernameAndPasswordWithNotFoundResponse()
  234. {
  235. $this->config->expects($this->once())
  236. ->method('get')
  237. ->with('bitbucket-oauth')
  238. ->willReturn(null);
  239. $this->io->expects($this->once())
  240. ->method('setAuthentication')
  241. ->with($this->origin, $this->username, $this->password);
  242. $exception = new \Composer\Downloader\TransportException('HTTP/1.1 404 NOT FOUND',404);
  243. $this->httpDownloader->expects($this->once())
  244. ->method('get')
  245. ->with(
  246. Bitbucket::OAUTH2_ACCESS_TOKEN_URL,
  247. array(
  248. 'retry-auth-failure' => false,
  249. 'http' => array(
  250. 'method' => 'POST',
  251. 'content' => 'grant_type=client_credentials',
  252. ),
  253. )
  254. )
  255. ->willThrowException($exception);
  256. $this->bitbucket->requestToken($this->origin, $this->username, $this->password);
  257. }
  258. public function testUsernamePasswordAuthenticationFlow()
  259. {
  260. $this->io
  261. ->expects($this->at(0))
  262. ->method('writeError')
  263. ->with($this->message)
  264. ;
  265. $this->io->expects($this->exactly(2))
  266. ->method('askAndHideAnswer')
  267. ->withConsecutive(
  268. array('Consumer Key (hidden): '),
  269. array('Consumer Secret (hidden): ')
  270. )
  271. ->willReturnOnConsecutiveCalls($this->consumer_key, $this->consumer_secret);
  272. $this->httpDownloader
  273. ->expects($this->once())
  274. ->method('get')
  275. ->with(
  276. $this->equalTo($url = sprintf('https://%s/site/oauth2/access_token', $this->origin)),
  277. $this->anything()
  278. )
  279. ->willReturn(
  280. new Response(
  281. array('url' => $url),
  282. 200,
  283. array(),
  284. sprintf(
  285. '{"access_token": "%s", "scopes": "repository", "expires_in": 3600, "refresh_token": "refresh_token", "token_type": "bearer"}',
  286. $this->token
  287. )
  288. )
  289. );
  290. ;
  291. $this->setExpectationsForStoringAccessToken(true);
  292. $this->assertTrue($this->bitbucket->authorizeOAuthInteractively($this->origin, $this->message));
  293. }
  294. public function testAuthorizeOAuthInteractivelyWithEmptyUsername()
  295. {
  296. $authConfigSourceMock = $this->getMockBuilder('Composer\Config\ConfigSourceInterface')->getMock();
  297. $this->config->expects($this->atLeastOnce())
  298. ->method('getAuthConfigSource')
  299. ->willReturn($authConfigSourceMock);
  300. $this->io->expects($this->once())
  301. ->method('askAndHideAnswer')
  302. ->with('Consumer Key (hidden): ')
  303. ->willReturnOnConsecutiveCalls(null);
  304. $this->assertFalse($this->bitbucket->authorizeOAuthInteractively($this->origin, $this->message));
  305. }
  306. public function testAuthorizeOAuthInteractivelyWithEmptyPassword()
  307. {
  308. $authConfigSourceMock = $this->getMockBuilder('Composer\Config\ConfigSourceInterface')->getMock();
  309. $this->config->expects($this->atLeastOnce())
  310. ->method('getAuthConfigSource')
  311. ->willReturn($authConfigSourceMock);
  312. $this->io->expects($this->exactly(2))
  313. ->method('askAndHideAnswer')
  314. ->withConsecutive(
  315. array('Consumer Key (hidden): '),
  316. array('Consumer Secret (hidden): ')
  317. )
  318. ->willReturnOnConsecutiveCalls($this->consumer_key, null);
  319. $this->assertFalse($this->bitbucket->authorizeOAuthInteractively($this->origin, $this->message));
  320. }
  321. public function testAuthorizeOAuthInteractivelyWithRequestAccessTokenFailure()
  322. {
  323. $authConfigSourceMock = $this->getMockBuilder('Composer\Config\ConfigSourceInterface')->getMock();
  324. $this->config->expects($this->atLeastOnce())
  325. ->method('getAuthConfigSource')
  326. ->willReturn($authConfigSourceMock);
  327. $this->io->expects($this->exactly(2))
  328. ->method('askAndHideAnswer')
  329. ->withConsecutive(
  330. array('Consumer Key (hidden): '),
  331. array('Consumer Secret (hidden): ')
  332. )
  333. ->willReturnOnConsecutiveCalls($this->consumer_key, $this->consumer_secret);
  334. $this->httpDownloader
  335. ->expects($this->once())
  336. ->method('get')
  337. ->with(
  338. $this->equalTo($url = sprintf('https://%s/site/oauth2/access_token', $this->origin)),
  339. $this->anything()
  340. )
  341. ->willThrowException(
  342. new \Composer\Downloader\TransportException(
  343. sprintf(
  344. 'The \'%s\' URL could not be accessed: HTTP/1.1 400 BAD REQUEST',
  345. Bitbucket::OAUTH2_ACCESS_TOKEN_URL
  346. ),
  347. 400
  348. )
  349. );
  350. $this->assertFalse($this->bitbucket->authorizeOAuthInteractively($this->origin, $this->message));
  351. }
  352. private function setExpectationsForStoringAccessToken($removeBasicAuth = false)
  353. {
  354. $configSourceMock = $this->getMockBuilder('Composer\Config\ConfigSourceInterface')->getMock();
  355. $this->config->expects($this->once())
  356. ->method('getConfigSource')
  357. ->willReturn($configSourceMock);
  358. $configSourceMock->expects($this->once())
  359. ->method('removeConfigSetting')
  360. ->with('bitbucket-oauth.' . $this->origin);
  361. $authConfigSourceMock = $this->getMockBuilder('Composer\Config\ConfigSourceInterface')->getMock();
  362. $this->config->expects($this->atLeastOnce())
  363. ->method('getAuthConfigSource')
  364. ->willReturn($authConfigSourceMock);
  365. $authConfigSourceMock->expects($this->once())
  366. ->method('addConfigSetting')
  367. ->with(
  368. 'bitbucket-oauth.' . $this->origin,
  369. array(
  370. "consumer-key" => $this->consumer_key,
  371. "consumer-secret" => $this->consumer_secret,
  372. "access-token" => $this->token,
  373. "access-token-expiration" => $this->time + 3600,
  374. )
  375. );
  376. if ($removeBasicAuth) {
  377. $authConfigSourceMock->expects($this->once())
  378. ->method('removeConfigSetting')
  379. ->with('http-basic.' . $this->origin);
  380. }
  381. }
  382. public function testGetTokenWithoutAccessToken()
  383. {
  384. $this->assertSame('', $this->bitbucket->getToken());
  385. }
  386. /**
  387. * @depends testRequestAccessTokenWithValidOAuthConsumerAndValidStoredAccessToken
  388. *
  389. * @param Bitbucket $bitbucket
  390. */
  391. public function testGetTokenWithAccessToken(Bitbucket $bitbucket)
  392. {
  393. $this->assertSame($this->token, $bitbucket->getToken());
  394. }
  395. public function testAuthorizeOAuthWithWrongOriginUrl()
  396. {
  397. $this->assertFalse($this->bitbucket->authorizeOAuth('non-' . $this->origin));
  398. }
  399. public function testAuthorizeOAuthWithoutAvailableGitConfigToken()
  400. {
  401. $process = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
  402. $process->expects($this->once())
  403. ->method('execute')
  404. ->willReturn(-1);
  405. $bitbucket = new Bitbucket($this->io, $this->config, $process, $this->httpDownloader, $this->time);
  406. $this->assertFalse($bitbucket->authorizeOAuth($this->origin));
  407. }
  408. public function testAuthorizeOAuthWithAvailableGitConfigToken()
  409. {
  410. $process = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
  411. $process->expects($this->once())
  412. ->method('execute')
  413. ->willReturn(0);
  414. $bitbucket = new Bitbucket($this->io, $this->config, $process, $this->httpDownloader, $this->time);
  415. $this->assertTrue($bitbucket->authorizeOAuth($this->origin));
  416. }
  417. }