GitLabTest.php 4.2 KB

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