GitLabTest.php 4.1 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\Test\TestCase;
  15. /**
  16. * @author Jérôme Tamarelle <jerome@tamarelle.net>
  17. */
  18. class GitLabTest extends TestCase
  19. {
  20. private $username = 'username';
  21. private $password = 'password';
  22. private $message = 'mymessage';
  23. private $origin = 'gitlab.com';
  24. private $token = 'gitlabtoken';
  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('ask')
  36. ->with('Username: ')
  37. ->willReturn($this->username)
  38. ;
  39. $io
  40. ->expects($this->once())
  41. ->method('askAndHideAnswer')
  42. ->with('Password: ')
  43. ->willReturn($this->password)
  44. ;
  45. $rfs = $this->getRemoteFilesystemMock();
  46. $rfs
  47. ->expects($this->once())
  48. ->method('getContents')
  49. ->with(
  50. $this->equalTo($this->origin),
  51. $this->equalTo(sprintf('http://%s/oauth/token', $this->origin)),
  52. $this->isFalse(),
  53. $this->anything()
  54. )
  55. ->willReturn(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, $rfs);
  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. $rfs = $this->getRemoteFilesystemMock();
  86. $rfs
  87. ->expects($this->exactly(5))
  88. ->method('getContents')
  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, $rfs);
  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 getRemoteFilesystemMock()
  114. {
  115. $rfs = $this
  116. ->getMockBuilder('Composer\Util\RemoteFilesystem')
  117. ->disableOriginalConstructor()
  118. ->getMock()
  119. ;
  120. return $rfs;
  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. }