GitHubTest.php 4.6 KB

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