BitbucketTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. }
  111. public function testRequestAccessTokenWithValidOAuthConsumerAndExpiredAccessToken()
  112. {
  113. $this->config->expects($this->once())
  114. ->method('get')
  115. ->with('bitbucket-oauth')
  116. ->willReturn(
  117. array(
  118. $this->origin => array(
  119. 'access-token' => 'randomExpiredToken',
  120. 'access-token-expiration' => $this->time - 400,
  121. 'consumer-key' => $this->consumer_key,
  122. 'consumer-secret' => $this->consumer_secret,
  123. ),
  124. )
  125. );
  126. $this->io->expects($this->once())
  127. ->method('setAuthentication')
  128. ->with($this->origin, $this->consumer_key, $this->consumer_secret);
  129. $this->httpDownloader->expects($this->once())
  130. ->method('get')
  131. ->with(
  132. Bitbucket::OAUTH2_ACCESS_TOKEN_URL,
  133. array(
  134. 'retry-auth-failure' => false,
  135. 'http' => array(
  136. 'method' => 'POST',
  137. 'content' => 'grant_type=client_credentials',
  138. ),
  139. )
  140. )
  141. ->willReturn(
  142. new Response(
  143. array('url' => Bitbucket::OAUTH2_ACCESS_TOKEN_URL),
  144. 200,
  145. array(),
  146. sprintf(
  147. '{"access_token": "%s", "scopes": "repository", "expires_in": 3600, "refresh_token": "refreshtoken", "token_type": "bearer"}',
  148. $this->token
  149. )
  150. )
  151. );
  152. $this->setExpectationsForStoringAccessToken();
  153. $this->assertEquals(
  154. $this->token,
  155. $this->bitbucket->requestToken($this->origin, $this->consumer_key, $this->consumer_secret)
  156. );
  157. }
  158. public function testRequestAccessTokenWithUsernameAndPassword()
  159. {
  160. $this->io->expects($this->once())
  161. ->method('setAuthentication')
  162. ->with($this->origin, $this->username, $this->password);
  163. $this->io->expects($this->any())
  164. ->method('writeError')
  165. ->withConsecutive(
  166. array('<error>Invalid OAuth consumer provided.</error>'),
  167. array('This can have two reasons:'),
  168. array('1. You are authenticating with a bitbucket username/password combination'),
  169. array('2. You are using an OAuth consumer, but didn\'t configure a (dummy) callback url')
  170. );
  171. $this->httpDownloader->expects($this->once())
  172. ->method('get')
  173. ->with(
  174. Bitbucket::OAUTH2_ACCESS_TOKEN_URL,
  175. array(
  176. 'retry-auth-failure' => false,
  177. 'http' => array(
  178. 'method' => 'POST',
  179. 'content' => 'grant_type=client_credentials',
  180. ),
  181. )
  182. )
  183. ->willThrowException(
  184. new \Composer\Downloader\TransportException(
  185. sprintf(
  186. 'The \'%s\' URL could not be accessed: HTTP/1.1 400 BAD REQUEST',
  187. Bitbucket::OAUTH2_ACCESS_TOKEN_URL
  188. ),
  189. 400
  190. )
  191. );
  192. $this->config->expects($this->once())
  193. ->method('get')
  194. ->with('bitbucket-oauth')
  195. ->willReturn(null);
  196. $this->assertEquals('', $this->bitbucket->requestToken($this->origin, $this->username, $this->password));
  197. }
  198. public function testUsernamePasswordAuthenticationFlow()
  199. {
  200. $this->io
  201. ->expects($this->at(0))
  202. ->method('writeError')
  203. ->with($this->message)
  204. ;
  205. $this->io->expects($this->exactly(2))
  206. ->method('askAndHideAnswer')
  207. ->withConsecutive(
  208. array('Consumer Key (hidden): '),
  209. array('Consumer Secret (hidden): ')
  210. )
  211. ->willReturnOnConsecutiveCalls($this->consumer_key, $this->consumer_secret);
  212. $this->httpDownloader
  213. ->expects($this->once())
  214. ->method('get')
  215. ->with(
  216. $this->equalTo($url = sprintf('https://%s/site/oauth2/access_token', $this->origin)),
  217. $this->anything()
  218. )
  219. ->willReturn(
  220. new Response(
  221. array('url' => $url),
  222. 200,
  223. array(),
  224. sprintf(
  225. '{"access_token": "%s", "scopes": "repository", "expires_in": 3600, "refresh_token": "refresh_token", "token_type": "bearer"}',
  226. $this->token
  227. )
  228. )
  229. );
  230. ;
  231. $this->setExpectationsForStoringAccessToken(true);
  232. $this->assertTrue($this->bitbucket->authorizeOAuthInteractively($this->origin, $this->message));
  233. }
  234. private function setExpectationsForStoringAccessToken($removeBasicAuth = false)
  235. {
  236. $configSourceMock = $this->getMockBuilder('Composer\Config\ConfigSourceInterface')->getMock();
  237. $this->config->expects($this->once())
  238. ->method('getConfigSource')
  239. ->willReturn($configSourceMock);
  240. $configSourceMock->expects($this->once())
  241. ->method('removeConfigSetting')
  242. ->with('bitbucket-oauth.' . $this->origin);
  243. $authConfigSourceMock = $this->getMockBuilder('Composer\Config\ConfigSourceInterface')->getMock();
  244. $this->config->expects($this->atLeastOnce())
  245. ->method('getAuthConfigSource')
  246. ->willReturn($authConfigSourceMock);
  247. $authConfigSourceMock->expects($this->once())
  248. ->method('addConfigSetting')
  249. ->with(
  250. 'bitbucket-oauth.' . $this->origin,
  251. array(
  252. "consumer-key" => $this->consumer_key,
  253. "consumer-secret" => $this->consumer_secret,
  254. "access-token" => $this->token,
  255. "access-token-expiration" => $this->time + 3600,
  256. )
  257. );
  258. if ($removeBasicAuth) {
  259. $authConfigSourceMock->expects($this->once())
  260. ->method('removeConfigSetting')
  261. ->with('http-basic.' . $this->origin);
  262. }
  263. }
  264. }