GitHubTest.php 4.5 KB

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