GitLabTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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\GitLab;
  14. use Composer\Util\Http\Response;
  15. use PHPUnit\Framework\TestCase;
  16. /**
  17. * @author Jérôme Tamarelle <jerome@tamarelle.net>
  18. */
  19. class GitLabTest extends TestCase
  20. {
  21. private $username = 'username';
  22. private $password = 'password';
  23. private $authcode = 'authcode';
  24. private $message = 'mymessage';
  25. private $origin = 'gitlab.com';
  26. private $token = 'gitlabtoken';
  27. public function testUsernamePasswordAuthenticationFlow()
  28. {
  29. $io = $this->getIOMock();
  30. $io
  31. ->expects($this->at(0))
  32. ->method('writeError')
  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. $httpDownloader = $this->getHttpDownloaderMock();
  48. $httpDownloader
  49. ->expects($this->once())
  50. ->method('get')
  51. ->with(
  52. $this->equalTo($url = sprintf('http://%s/oauth/token', $this->origin)),
  53. $this->anything()
  54. )
  55. ->willReturn(new Response(array('url' => $url), 200, array(), sprintf('{"access_token": "%s", "token_type": "bearer", "expires_in": 7200}', $this->token)));
  56. ;
  57. $config = $this->getConfigMock();
  58. $config
  59. ->expects($this->exactly(2))
  60. ->method('getAuthConfigSource')
  61. ->willReturn($this->getAuthJsonMock())
  62. ;
  63. $gitLab = new GitLab($io, $config, null, $httpDownloader);
  64. $this->assertTrue($gitLab->authorizeOAuthInteractively('http', $this->origin, $this->message));
  65. }
  66. /**
  67. * @expectedException \RuntimeException
  68. * @expectedExceptionMessage Invalid GitLab credentials 5 times in a row, aborting.
  69. */
  70. public function testUsernamePasswordFailure()
  71. {
  72. $io = $this->getIOMock();
  73. $io
  74. ->expects($this->exactly(5))
  75. ->method('ask')
  76. ->with('Username: ')
  77. ->willReturn($this->username)
  78. ;
  79. $io
  80. ->expects($this->exactly(5))
  81. ->method('askAndHideAnswer')
  82. ->with('Password: ')
  83. ->willReturn($this->password)
  84. ;
  85. $httpDownloader = $this->getHttpDownloaderMock();
  86. $httpDownloader
  87. ->expects($this->exactly(5))
  88. ->method('get')
  89. ->will($this->throwException(new TransportException('', 401)))
  90. ;
  91. $config = $this->getConfigMock();
  92. $config
  93. ->expects($this->exactly(1))
  94. ->method('getAuthConfigSource')
  95. ->willReturn($this->getAuthJsonMock())
  96. ;
  97. $gitLab = new GitLab($io, $config, null, $httpDownloader);
  98. $gitLab->authorizeOAuthInteractively('https', $this->origin);
  99. }
  100. private function getIOMock()
  101. {
  102. $io = $this
  103. ->getMockBuilder('Composer\IO\ConsoleIO')
  104. ->disableOriginalConstructor()
  105. ->getMock()
  106. ;
  107. return $io;
  108. }
  109. private function getConfigMock()
  110. {
  111. return $this->getMockBuilder('Composer\Config')->getMock();
  112. }
  113. private function getHttpDownloaderMock()
  114. {
  115. $httpDownloader = $this
  116. ->getMockBuilder('Composer\Util\HttpDownloader')
  117. ->disableOriginalConstructor()
  118. ->getMock()
  119. ;
  120. return $httpDownloader;
  121. }
  122. private function getAuthJsonMock()
  123. {
  124. $authjson = $this
  125. ->getMockBuilder('Composer\Config\JsonConfigSource')
  126. ->disableOriginalConstructor()
  127. ->getMock()
  128. ;
  129. $authjson
  130. ->expects($this->atLeastOnce())
  131. ->method('getName')
  132. ->willReturn('auth.json')
  133. ;
  134. return $authjson;
  135. }
  136. }