BitbucketTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. /**
  14. * @author Paul Wenke <wenke.paul@gmail.com>
  15. */
  16. class BitbucketTest extends \PHPUnit_Framework_TestCase
  17. {
  18. private $username = 'username';
  19. private $password = 'password';
  20. private $consumer_key = 'consumer_key';
  21. private $consumer_secret = 'consumer_secret';
  22. private $message = 'mymessage';
  23. private $origin = 'bitbucket.org';
  24. private $token = 'bitbuckettoken';
  25. /** @type \Composer\IO\ConsoleIO|\PHPUnit_Framework_MockObject_MockObject */
  26. private $io;
  27. /** @type \Composer\Util\RemoteFilesystem|\PHPUnit_Framework_MockObject_MockObject */
  28. private $rfs;
  29. /** @type \Composer\Config|\PHPUnit_Framework_MockObject_MockObject */
  30. private $config;
  31. /** @type Bitbucket */
  32. private $bitbucket;
  33. protected function setUp()
  34. {
  35. $this->io = $this
  36. ->getMockBuilder('Composer\IO\ConsoleIO')
  37. ->disableOriginalConstructor()
  38. ->getMock()
  39. ;
  40. $this->rfs = $this
  41. ->getMockBuilder('Composer\Util\RemoteFilesystem')
  42. ->disableOriginalConstructor()
  43. ->getMock()
  44. ;
  45. $this->config = $this->getMock('Composer\Config');
  46. $this->bitbucket = new Bitbucket($this->io, $this->config, null, $this->rfs);
  47. }
  48. public function testRequestAccessTokenWithValidOAuthConsumer()
  49. {
  50. $this->io->expects($this->once())
  51. ->method('setAuthentication')
  52. ->with($this->origin, $this->consumer_key, $this->consumer_secret);
  53. $this->rfs->expects($this->once())
  54. ->method('getContents')
  55. ->with(
  56. $this->origin,
  57. Bitbucket::OAUTH2_ACCESS_TOKEN_URL,
  58. false,
  59. array(
  60. 'retry-auth-failure' => false,
  61. 'http' => array(
  62. 'method' => 'POST',
  63. 'content' => 'grant_type=client_credentials',
  64. )
  65. )
  66. )
  67. ->willReturn(
  68. sprintf(
  69. '{"access_token": "%s", "scopes": "repository", "expires_in": 3600, "refresh_token": "refreshtoken", "token_type": "bearer"}',
  70. $this->token
  71. )
  72. );
  73. $this->assertEquals(
  74. array(
  75. 'access_token' => $this->token,
  76. 'scopes' => 'repository',
  77. 'expires_in' => 3600,
  78. 'refresh_token' => 'refreshtoken',
  79. 'token_type' => 'bearer'
  80. ),
  81. $this->bitbucket->requestToken($this->origin, $this->consumer_key, $this->consumer_secret)
  82. );
  83. }
  84. public function testRequestAccessTokenWithUsernameAndPassword()
  85. {
  86. $this->io->expects($this->once())
  87. ->method('setAuthentication')
  88. ->with($this->origin, $this->username, $this->password);
  89. $this->io->expects($this->any())
  90. ->method('writeError')
  91. ->withConsecutive(
  92. array('<error>Invalid OAuth consumer provided.</error>'),
  93. array('This can have two reasons:'),
  94. array('1. You are authenticating with a bitbucket username/password combination'),
  95. array('2. You are using an OAuth consumer, but didn\'t configure a (dummy) callback url')
  96. );
  97. $this->rfs->expects($this->once())
  98. ->method('getContents')
  99. ->with(
  100. $this->origin,
  101. Bitbucket::OAUTH2_ACCESS_TOKEN_URL,
  102. false,
  103. array(
  104. 'retry-auth-failure' => false,
  105. 'http' => array(
  106. 'method' => 'POST',
  107. 'content' => 'grant_type=client_credentials',
  108. )
  109. )
  110. )
  111. ->willThrowException(
  112. new \Composer\Downloader\TransportException(
  113. sprintf(
  114. 'The \'%s\' URL could not be accessed: HTTP/1.1 400 BAD REQUEST',
  115. Bitbucket::OAUTH2_ACCESS_TOKEN_URL
  116. ),
  117. 400
  118. )
  119. );
  120. $this->assertEquals(array(), $this->bitbucket->requestToken($this->origin, $this->username, $this->password));
  121. }
  122. public function testUsernamePasswordAuthenticationFlow()
  123. {
  124. $this->io
  125. ->expects($this->at(0))
  126. ->method('writeError')
  127. ->with($this->message)
  128. ;
  129. $this->io->expects($this->exactly(2))
  130. ->method('askAndHideAnswer')
  131. ->withConsecutive(
  132. array('Consumer Key (hidden): '),
  133. array('Consumer Secret (hidden): ')
  134. )
  135. ->willReturnOnConsecutiveCalls($this->consumer_key, $this->consumer_secret);
  136. $this->rfs
  137. ->expects($this->once())
  138. ->method('getContents')
  139. ->with(
  140. $this->equalTo($this->origin),
  141. $this->equalTo(sprintf('https://%s/site/oauth2/access_token', $this->origin)),
  142. $this->isFalse(),
  143. $this->anything()
  144. )
  145. ->willReturn(sprintf('{}', $this->token))
  146. ;
  147. $authJson = $this->getAuthJsonMock();
  148. $this->config
  149. ->expects($this->exactly(3))
  150. ->method('getAuthConfigSource')
  151. ->willReturn($authJson)
  152. ;
  153. $this->config
  154. ->expects($this->once())
  155. ->method('getConfigSource')
  156. ->willReturn($this->getConfJsonMock())
  157. ;
  158. $authJson->expects($this->once())
  159. ->method('addConfigSetting')
  160. ->with(
  161. 'bitbucket-oauth.'.$this->origin,
  162. array(
  163. 'consumer-key' => $this->consumer_key,
  164. 'consumer-secret' => $this->consumer_secret
  165. )
  166. );
  167. $authJson->expects($this->once())
  168. ->method('removeConfigSetting')
  169. ->with('http-basic.'.$this->origin);
  170. $this->assertTrue($this->bitbucket->authorizeOAuthInteractively($this->origin, $this->message));
  171. }
  172. private function getAuthJsonMock()
  173. {
  174. $authjson = $this
  175. ->getMockBuilder('Composer\Config\JsonConfigSource')
  176. ->disableOriginalConstructor()
  177. ->getMock()
  178. ;
  179. $authjson
  180. ->expects($this->atLeastOnce())
  181. ->method('getName')
  182. ->willReturn('auth.json')
  183. ;
  184. return $authjson;
  185. }
  186. private function getConfJsonMock()
  187. {
  188. $confjson = $this
  189. ->getMockBuilder('Composer\Config\JsonConfigSource')
  190. ->disableOriginalConstructor()
  191. ->getMock()
  192. ;
  193. $confjson
  194. ->expects($this->atLeastOnce())
  195. ->method('removeConfigSetting')
  196. ->with('bitbucket-oauth.'.$this->origin)
  197. ;
  198. return $confjson;
  199. }
  200. }