GitHubTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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\Downloader\TransportException;
  13. use Composer\Util\GitHub;
  14. use RecursiveArrayIterator;
  15. use RecursiveIteratorIterator;
  16. /**
  17. * @author Rob Bast <rob.bast@gmail.com>
  18. */
  19. class GitHubTest extends \PHPUnit_Framework_TestCase
  20. {
  21. private $username = 'username';
  22. private $password = 'password';
  23. private $authcode = 'authcode';
  24. private $message = 'mymessage';
  25. private $origin = 'github.com';
  26. private $token = 'githubtoken';
  27. public function testUsernamePasswordAuthenticationFlow()
  28. {
  29. $io = $this->getIOMock();
  30. $io
  31. ->expects($this->at(0))
  32. ->method('write')
  33. ->with($this->message)
  34. ;
  35. $io
  36. ->expects($this->once())
  37. ->method('ask')
  38. ->with('Username: ')
  39. ->willReturn($this->username)
  40. ;
  41. $io
  42. ->expects($this->once())
  43. ->method('askAndHideAnswer')
  44. ->with('Password: ')
  45. ->willReturn($this->password)
  46. ;
  47. $rfs = $this->getRemoteFilesystemMock();
  48. $rfs
  49. ->expects($this->once())
  50. ->method('getContents')
  51. ->with(
  52. $this->equalTo($this->origin),
  53. $this->equalTo(sprintf('https://api.%s/authorizations', $this->origin)),
  54. $this->isFalse(),
  55. $this->anything()
  56. )
  57. ->willReturn(sprintf('{"token": "%s"}', $this->token))
  58. ;
  59. $config = $this->getConfigMock();
  60. $config
  61. ->expects($this->exactly(2))
  62. ->method('getAuthConfigSource')
  63. ->willReturn($this->getAuthJsonMock())
  64. ;
  65. $config
  66. ->expects($this->once())
  67. ->method('getConfigSource')
  68. ->willReturn($this->getConfJsonMock())
  69. ;
  70. $github = new GitHub($io, $config, null, $rfs);
  71. $this->assertTrue($github->authorizeOAuthInteractively($this->origin, $this->message));
  72. }
  73. /**
  74. * @expectedException \RuntimeException
  75. * @expectedExceptionMessage Invalid GitHub credentials 5 times in a row, aborting.
  76. */
  77. public function testUsernamePasswordFailure()
  78. {
  79. $io = $this->getIOMock();
  80. $io
  81. ->expects($this->exactly(5))
  82. ->method('ask')
  83. ->with('Username: ')
  84. ->willReturn($this->username)
  85. ;
  86. $io
  87. ->expects($this->exactly(5))
  88. ->method('askAndHideAnswer')
  89. ->with('Password: ')
  90. ->willReturn($this->password)
  91. ;
  92. $rfs = $this->getRemoteFilesystemMock();
  93. $rfs
  94. ->expects($this->exactly(5))
  95. ->method('getContents')
  96. ->will($this->throwException(new TransportException('', 401)))
  97. ;
  98. $config = $this->getConfigMock();
  99. $config
  100. ->expects($this->exactly(1))
  101. ->method('getAuthConfigSource')
  102. ->willReturn($this->getAuthJsonMock())
  103. ;
  104. $github = new GitHub($io, $config, null, $rfs);
  105. $github->authorizeOAuthInteractively($this->origin);
  106. }
  107. public function testTwoFactorAuthentication()
  108. {
  109. $io = $this->getIOMock();
  110. $io
  111. ->expects($this->exactly(2))
  112. ->method('hasAuthentication')
  113. ->will($this->onConsecutiveCalls(true, true))
  114. ;
  115. $io
  116. ->expects($this->exactly(2))
  117. ->method('ask')
  118. ->withConsecutive(
  119. array('Username: '),
  120. array('Authentication Code: ')
  121. )
  122. ->will($this->onConsecutiveCalls($this->username, $this->authcode))
  123. ;
  124. $io
  125. ->expects($this->once())
  126. ->method('askAndHideAnswer')
  127. ->with('Password: ')
  128. ->willReturn($this->password)
  129. ;
  130. $exception = new TransportException('', 401);
  131. $exception->setHeaders(array('X-GitHub-OTP: required; app'));
  132. $rfs = $this->getRemoteFilesystemMock();
  133. $rfs
  134. ->expects($this->at(0))
  135. ->method('getContents')
  136. ->will($this->throwException($exception))
  137. ;
  138. $rfs
  139. ->expects($this->at(1))
  140. ->method('getContents')
  141. ->with(
  142. $this->equalTo($this->origin),
  143. $this->equalTo(sprintf('https://api.%s/authorizations', $this->origin)),
  144. $this->isFalse(),
  145. $this->callback(function ($array) {
  146. $headers = GitHubTest::recursiveFind($array, 'header');
  147. foreach ($headers as $string) {
  148. if ('X-GitHub-OTP: authcode' === $string) {
  149. return true;
  150. }
  151. }
  152. return false;
  153. })
  154. )
  155. ->willReturn(sprintf('{"token": "%s"}', $this->token))
  156. ;
  157. $config = $this->getConfigMock();
  158. $config
  159. ->expects($this->atLeastOnce())
  160. ->method('getAuthConfigSource')
  161. ->willReturn($this->getAuthJsonMock())
  162. ;
  163. $config
  164. ->expects($this->atLeastOnce())
  165. ->method('getConfigSource')
  166. ->willReturn($this->getConfJsonMock())
  167. ;
  168. $github = new GitHub($io, $config, null, $rfs);
  169. $this->assertTrue($github->authorizeOAuthInteractively($this->origin));
  170. }
  171. private function getIOMock()
  172. {
  173. $io = $this
  174. ->getMockBuilder('Composer\IO\ConsoleIO')
  175. ->disableOriginalConstructor()
  176. ->getMock()
  177. ;
  178. return $io;
  179. }
  180. private function getConfigMock()
  181. {
  182. $config = $this->getMock('Composer\Config');
  183. return $config;
  184. }
  185. private function getRemoteFilesystemMock()
  186. {
  187. $rfs = $this
  188. ->getMockBuilder('Composer\Util\RemoteFilesystem')
  189. ->disableOriginalConstructor()
  190. ->getMock()
  191. ;
  192. return $rfs;
  193. }
  194. private function getAuthJsonMock()
  195. {
  196. $authjson = $this
  197. ->getMockBuilder('Composer\Config\JsonConfigSource')
  198. ->disableOriginalConstructor()
  199. ->getMock()
  200. ;
  201. $authjson
  202. ->expects($this->atLeastOnce())
  203. ->method('getName')
  204. ->willReturn('auth.json')
  205. ;
  206. return $authjson;
  207. }
  208. private function getConfJsonMock()
  209. {
  210. $confjson = $this
  211. ->getMockBuilder('Composer\Config\JsonConfigSource')
  212. ->disableOriginalConstructor()
  213. ->getMock()
  214. ;
  215. $confjson
  216. ->expects($this->atLeastOnce())
  217. ->method('removeConfigSetting')
  218. ->with('github-oauth.'.$this->origin)
  219. ;
  220. return $confjson;
  221. }
  222. public static function recursiveFind($array, $needle)
  223. {
  224. $iterator = new RecursiveArrayIterator($array);
  225. $recursive = new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::SELF_FIRST);
  226. foreach ($recursive as $key => $value) {
  227. if ($key === $needle) {
  228. return $value;
  229. }
  230. }
  231. }
  232. }