GitLabTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 Composer\Test\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 $message = 'mymessage';
  24. private $origin = 'gitlab.com';
  25. private $token = 'gitlabtoken';
  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('ask')
  37. ->with('Username: ')
  38. ->willReturn($this->username)
  39. ;
  40. $io
  41. ->expects($this->once())
  42. ->method('askAndHideAnswer')
  43. ->with('Password: ')
  44. ->willReturn($this->password)
  45. ;
  46. $httpDownloader = $this->getHttpDownloaderMock();
  47. $httpDownloader
  48. ->expects($this->once())
  49. ->method('get')
  50. ->with(
  51. $this->equalTo($url = sprintf('http://%s/oauth/token', $this->origin)),
  52. $this->anything()
  53. )
  54. ->willReturn(new Response(array('url' => $url), 200, array(), sprintf('{"access_token": "%s", "token_type": "bearer", "expires_in": 7200}', $this->token)));
  55. ;
  56. $config = $this->getConfigMock();
  57. $config
  58. ->expects($this->exactly(2))
  59. ->method('getAuthConfigSource')
  60. ->willReturn($this->getAuthJsonMock())
  61. ;
  62. $gitLab = new GitLab($io, $config, null, $httpDownloader);
  63. $this->assertTrue($gitLab->authorizeOAuthInteractively('http', $this->origin, $this->message));
  64. }
  65. /**
  66. * @expectedException \RuntimeException
  67. * @expectedExceptionMessage Invalid GitLab credentials 5 times in a row, aborting.
  68. */
  69. public function testUsernamePasswordFailure()
  70. {
  71. $io = $this->getIOMock();
  72. $io
  73. ->expects($this->exactly(5))
  74. ->method('ask')
  75. ->with('Username: ')
  76. ->willReturn($this->username)
  77. ;
  78. $io
  79. ->expects($this->exactly(5))
  80. ->method('askAndHideAnswer')
  81. ->with('Password: ')
  82. ->willReturn($this->password)
  83. ;
  84. $httpDownloader = $this->getHttpDownloaderMock();
  85. $httpDownloader
  86. ->expects($this->exactly(5))
  87. ->method('get')
  88. ->will($this->throwException(new TransportException('', 401)))
  89. ;
  90. $config = $this->getConfigMock();
  91. $config
  92. ->expects($this->exactly(1))
  93. ->method('getAuthConfigSource')
  94. ->willReturn($this->getAuthJsonMock())
  95. ;
  96. $gitLab = new GitLab($io, $config, null, $httpDownloader);
  97. $gitLab->authorizeOAuthInteractively('https', $this->origin);
  98. }
  99. private function getIOMock()
  100. {
  101. $io = $this
  102. ->getMockBuilder('Composer\IO\ConsoleIO')
  103. ->disableOriginalConstructor()
  104. ->getMock()
  105. ;
  106. return $io;
  107. }
  108. private function getConfigMock()
  109. {
  110. return $this->getMockBuilder('Composer\Config')->getMock();
  111. }
  112. private function getHttpDownloaderMock()
  113. {
  114. $httpDownloader = $this
  115. ->getMockBuilder('Composer\Util\HttpDownloader')
  116. ->disableOriginalConstructor()
  117. ->getMock()
  118. ;
  119. return $httpDownloader;
  120. }
  121. private function getAuthJsonMock()
  122. {
  123. $authjson = $this
  124. ->getMockBuilder('Composer\Config\JsonConfigSource')
  125. ->disableOriginalConstructor()
  126. ->getMock()
  127. ;
  128. $authjson
  129. ->expects($this->atLeastOnce())
  130. ->method('getName')
  131. ->willReturn('auth.json')
  132. ;
  133. return $authjson;
  134. }
  135. }