GitHubTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 Composer\Util\Http\Response;
  15. use Composer\Test\TestCase;
  16. use RecursiveArrayIterator;
  17. use RecursiveIteratorIterator;
  18. /**
  19. * @author Rob Bast <rob.bast@gmail.com>
  20. */
  21. class GitHubTest extends TestCase
  22. {
  23. private $password = 'password';
  24. private $message = 'mymessage';
  25. private $origin = 'github.com';
  26. public function testUsernamePasswordAuthenticationFlow()
  27. {
  28. $io = $this->getIOMock();
  29. $io
  30. ->expects($this->at(0))
  31. ->method('writeError')
  32. ->with($this->message)
  33. ;
  34. $io
  35. ->expects($this->once())
  36. ->method('askAndHideAnswer')
  37. ->with('Token (hidden): ')
  38. ->willReturn($this->password)
  39. ;
  40. $httpDownloader = $this->getHttpDownloaderMock();
  41. $httpDownloader
  42. ->expects($this->once())
  43. ->method('get')
  44. ->with(
  45. $this->equalTo($url = sprintf('https://api.%s/', $this->origin)),
  46. $this->anything()
  47. )
  48. ->willReturn(new Response(array('url' => $url), 200, array(), '{}'));
  49. ;
  50. $config = $this->getConfigMock();
  51. $config
  52. ->expects($this->exactly(2))
  53. ->method('getAuthConfigSource')
  54. ->willReturn($this->getAuthJsonMock())
  55. ;
  56. $config
  57. ->expects($this->once())
  58. ->method('getConfigSource')
  59. ->willReturn($this->getConfJsonMock())
  60. ;
  61. $github = new GitHub($io, $config, null, $httpDownloader);
  62. $this->assertTrue($github->authorizeOAuthInteractively($this->origin, $this->message));
  63. }
  64. public function testUsernamePasswordFailure()
  65. {
  66. $io = $this->getIOMock();
  67. $io
  68. ->expects($this->exactly(1))
  69. ->method('askAndHideAnswer')
  70. ->with('Token (hidden): ')
  71. ->willReturn($this->password)
  72. ;
  73. $httpDownloader = $this->getHttpDownloaderMock();
  74. $httpDownloader
  75. ->expects($this->exactly(1))
  76. ->method('get')
  77. ->will($this->throwException(new TransportException('', 401)))
  78. ;
  79. $config = $this->getConfigMock();
  80. $config
  81. ->expects($this->exactly(1))
  82. ->method('getAuthConfigSource')
  83. ->willReturn($this->getAuthJsonMock())
  84. ;
  85. $github = new GitHub($io, $config, null, $httpDownloader);
  86. $this->assertFalse($github->authorizeOAuthInteractively($this->origin));
  87. }
  88. private function getIOMock()
  89. {
  90. $io = $this
  91. ->getMockBuilder('Composer\IO\ConsoleIO')
  92. ->disableOriginalConstructor()
  93. ->getMock()
  94. ;
  95. return $io;
  96. }
  97. private function getConfigMock()
  98. {
  99. return $this->getMockBuilder('Composer\Config')->getMock();
  100. }
  101. private function getHttpDownloaderMock()
  102. {
  103. $httpDownloader = $this
  104. ->getMockBuilder('Composer\Util\HttpDownloader')
  105. ->disableOriginalConstructor()
  106. ->getMock()
  107. ;
  108. return $httpDownloader;
  109. }
  110. private function getAuthJsonMock()
  111. {
  112. $authjson = $this
  113. ->getMockBuilder('Composer\Config\JsonConfigSource')
  114. ->disableOriginalConstructor()
  115. ->getMock()
  116. ;
  117. $authjson
  118. ->expects($this->atLeastOnce())
  119. ->method('getName')
  120. ->willReturn('auth.json')
  121. ;
  122. return $authjson;
  123. }
  124. private function getConfJsonMock()
  125. {
  126. $confjson = $this
  127. ->getMockBuilder('Composer\Config\JsonConfigSource')
  128. ->disableOriginalConstructor()
  129. ->getMock()
  130. ;
  131. $confjson
  132. ->expects($this->atLeastOnce())
  133. ->method('removeConfigSetting')
  134. ->with('github-oauth.'.$this->origin)
  135. ;
  136. return $confjson;
  137. }
  138. public static function recursiveFind($array, $needle)
  139. {
  140. $iterator = new RecursiveArrayIterator($array);
  141. $recursive = new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::SELF_FIRST);
  142. foreach ($recursive as $key => $value) {
  143. if ($key === $needle) {
  144. return $value;
  145. }
  146. }
  147. }
  148. }