2022-02-23 15:58:18 +00:00
|
|
|
<?php declare(strict_types=1);
|
2015-11-13 17:50:50 +00:00
|
|
|
|
|
|
|
/*
|
2015-11-21 21:51:43 +00:00
|
|
|
* This file is part of Composer.
|
|
|
|
*
|
|
|
|
* (c) Nils Adermann <naderman@naderman.de>
|
|
|
|
* Jordi Boggiano <j.boggiano@seld.be>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
2015-11-13 17:50:50 +00:00
|
|
|
|
|
|
|
namespace Composer\Test\Util;
|
|
|
|
|
|
|
|
use Composer\Util\GitLab;
|
2020-02-07 03:18:45 +00:00
|
|
|
use Composer\Test\TestCase;
|
2015-11-13 17:50:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Jérôme Tamarelle <jerome@tamarelle.net>
|
|
|
|
*/
|
2017-11-04 14:52:13 +00:00
|
|
|
class GitLabTest extends TestCase
|
2015-11-13 17:50:50 +00:00
|
|
|
{
|
2021-10-27 14:18:46 +00:00
|
|
|
/** @var string */
|
2015-11-13 17:50:50 +00:00
|
|
|
private $username = 'username';
|
2021-10-27 14:18:46 +00:00
|
|
|
/** @var string */
|
2015-11-13 17:50:50 +00:00
|
|
|
private $password = 'password';
|
2021-10-27 14:18:46 +00:00
|
|
|
/** @var string */
|
2015-11-13 17:50:50 +00:00
|
|
|
private $message = 'mymessage';
|
2021-10-27 14:18:46 +00:00
|
|
|
/** @var string */
|
2015-11-13 17:50:50 +00:00
|
|
|
private $origin = 'gitlab.com';
|
2021-10-27 14:18:46 +00:00
|
|
|
/** @var string */
|
2015-11-13 17:50:50 +00:00
|
|
|
private $token = 'gitlabtoken';
|
2022-08-16 11:34:18 +00:00
|
|
|
/** @var string */
|
|
|
|
private $refreshtoken = 'gitlabrefreshtoken';
|
2015-11-13 17:50:50 +00:00
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testUsernamePasswordAuthenticationFlow(): void
|
2015-11-13 17:50:50 +00:00
|
|
|
{
|
|
|
|
$io = $this->getIOMock();
|
|
|
|
$io
|
2021-12-09 16:09:07 +00:00
|
|
|
->expects($this->atLeastOnce())
|
2015-11-13 17:50:50 +00:00
|
|
|
->method('writeError')
|
2021-12-09 16:09:07 +00:00
|
|
|
->withConsecutive([$this->message])
|
2015-11-13 17:50:50 +00:00
|
|
|
;
|
|
|
|
$io
|
|
|
|
->expects($this->once())
|
|
|
|
->method('ask')
|
|
|
|
->with('Username: ')
|
|
|
|
->willReturn($this->username)
|
|
|
|
;
|
|
|
|
$io
|
|
|
|
->expects($this->once())
|
|
|
|
->method('askAndHideAnswer')
|
|
|
|
->with('Password: ')
|
|
|
|
->willReturn($this->password)
|
|
|
|
;
|
|
|
|
|
2018-10-31 11:44:54 +00:00
|
|
|
$httpDownloader = $this->getHttpDownloaderMock();
|
2021-12-09 16:09:07 +00:00
|
|
|
$httpDownloader->expects(
|
2022-08-16 11:34:18 +00:00
|
|
|
[['url' => sprintf('http://%s/oauth/token', $this->origin), 'body' => sprintf('{"access_token": "%s", "refresh_token": "%s", "token_type": "bearer", "expires_in": 7200, "created_at": 0}', $this->token, $this->refreshtoken)]],
|
2021-12-09 16:09:07 +00:00
|
|
|
true
|
|
|
|
);
|
2015-11-13 17:50:50 +00:00
|
|
|
|
|
|
|
$config = $this->getConfigMock();
|
|
|
|
$config
|
|
|
|
->expects($this->exactly(2))
|
|
|
|
->method('getAuthConfigSource')
|
|
|
|
->willReturn($this->getAuthJsonMock())
|
|
|
|
;
|
|
|
|
|
2018-10-31 11:44:54 +00:00
|
|
|
$gitLab = new GitLab($io, $config, null, $httpDownloader);
|
2015-11-13 17:50:50 +00:00
|
|
|
|
2015-11-14 15:00:14 +00:00
|
|
|
$this->assertTrue($gitLab->authorizeOAuthInteractively('http', $this->origin, $this->message));
|
2015-11-13 17:50:50 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testUsernamePasswordFailure(): void
|
2015-11-13 17:50:50 +00:00
|
|
|
{
|
2021-12-09 19:55:26 +00:00
|
|
|
self::expectException('RuntimeException');
|
|
|
|
self::expectExceptionMessage('Invalid GitLab credentials 5 times in a row, aborting.');
|
2015-11-13 17:50:50 +00:00
|
|
|
$io = $this->getIOMock();
|
|
|
|
$io
|
|
|
|
->expects($this->exactly(5))
|
|
|
|
->method('ask')
|
|
|
|
->with('Username: ')
|
|
|
|
->willReturn($this->username)
|
|
|
|
;
|
|
|
|
$io
|
|
|
|
->expects($this->exactly(5))
|
|
|
|
->method('askAndHideAnswer')
|
|
|
|
->with('Password: ')
|
|
|
|
->willReturn($this->password)
|
|
|
|
;
|
|
|
|
|
2018-10-31 11:44:54 +00:00
|
|
|
$httpDownloader = $this->getHttpDownloaderMock();
|
2021-12-09 16:09:07 +00:00
|
|
|
$httpDownloader->expects(
|
|
|
|
[
|
|
|
|
['url' => 'https://gitlab.com/oauth/token', 'status' => 401, 'body' => '{}'],
|
|
|
|
['url' => 'https://gitlab.com/oauth/token', 'status' => 401, 'body' => '{}'],
|
|
|
|
['url' => 'https://gitlab.com/oauth/token', 'status' => 401, 'body' => '{}'],
|
|
|
|
['url' => 'https://gitlab.com/oauth/token', 'status' => 401, 'body' => '{}'],
|
|
|
|
['url' => 'https://gitlab.com/oauth/token', 'status' => 401, 'body' => '{}'],
|
|
|
|
],
|
|
|
|
true
|
|
|
|
);
|
2015-11-13 17:50:50 +00:00
|
|
|
|
|
|
|
$config = $this->getConfigMock();
|
|
|
|
$config
|
|
|
|
->expects($this->exactly(1))
|
|
|
|
->method('getAuthConfigSource')
|
|
|
|
->willReturn($this->getAuthJsonMock())
|
|
|
|
;
|
|
|
|
|
2018-10-31 11:44:54 +00:00
|
|
|
$gitLab = new GitLab($io, $config, null, $httpDownloader);
|
2015-11-13 17:50:50 +00:00
|
|
|
|
2015-11-14 15:00:14 +00:00
|
|
|
$gitLab->authorizeOAuthInteractively('https', $this->origin);
|
2015-11-13 17:50:50 +00:00
|
|
|
}
|
|
|
|
|
2021-10-27 14:18:46 +00:00
|
|
|
/**
|
|
|
|
* @return \PHPUnit\Framework\MockObject\MockObject&\Composer\IO\ConsoleIO
|
|
|
|
*/
|
2015-11-13 17:50:50 +00:00
|
|
|
private function getIOMock()
|
|
|
|
{
|
|
|
|
$io = $this
|
|
|
|
->getMockBuilder('Composer\IO\ConsoleIO')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock()
|
|
|
|
;
|
|
|
|
|
|
|
|
return $io;
|
|
|
|
}
|
|
|
|
|
2021-10-27 14:18:46 +00:00
|
|
|
/**
|
|
|
|
* @return \PHPUnit\Framework\MockObject\MockObject&\Composer\Config
|
|
|
|
*/
|
2015-11-13 17:50:50 +00:00
|
|
|
private function getConfigMock()
|
|
|
|
{
|
2018-04-12 08:24:56 +00:00
|
|
|
return $this->getMockBuilder('Composer\Config')->getMock();
|
2015-11-13 17:50:50 +00:00
|
|
|
}
|
|
|
|
|
2021-10-27 14:18:46 +00:00
|
|
|
/**
|
|
|
|
* @return \PHPUnit\Framework\MockObject\MockObject&\Composer\Config\JsonConfigSource
|
|
|
|
*/
|
2015-11-13 17:50:50 +00:00
|
|
|
private function getAuthJsonMock()
|
|
|
|
{
|
|
|
|
$authjson = $this
|
|
|
|
->getMockBuilder('Composer\Config\JsonConfigSource')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock()
|
|
|
|
;
|
|
|
|
$authjson
|
|
|
|
->expects($this->atLeastOnce())
|
|
|
|
->method('getName')
|
|
|
|
->willReturn('auth.json')
|
|
|
|
;
|
|
|
|
|
|
|
|
return $authjson;
|
|
|
|
}
|
|
|
|
}
|