2022-02-23 15:58:18 +00:00
|
|
|
<?php declare(strict_types=1);
|
2020-05-04 07:48:23 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Composer\Test\Util;
|
|
|
|
|
2022-04-21 08:37:55 +00:00
|
|
|
use Composer\Downloader\TransportException;
|
2020-05-04 07:48:23 +00:00
|
|
|
use Composer\IO\IOInterface;
|
|
|
|
use Composer\Test\TestCase;
|
|
|
|
use Composer\Util\AuthHelper;
|
|
|
|
use Composer\Util\Bitbucket;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Michael Chekin <mchekin@gmail.com>
|
|
|
|
*/
|
|
|
|
class AuthHelperTest extends TestCase
|
|
|
|
{
|
2021-10-27 14:18:46 +00:00
|
|
|
/** @var \Composer\IO\IOInterface&\PHPUnit\Framework\MockObject\MockObject */
|
2020-05-04 07:48:23 +00:00
|
|
|
private $io;
|
|
|
|
|
2021-10-27 14:18:46 +00:00
|
|
|
/** @var \Composer\Config&\PHPUnit\Framework\MockObject\MockObject */
|
2020-05-04 07:48:23 +00:00
|
|
|
private $config;
|
|
|
|
|
2021-10-27 14:18:46 +00:00
|
|
|
/** @var AuthHelper */
|
2020-05-04 07:48:23 +00:00
|
|
|
private $authHelper;
|
|
|
|
|
2021-12-08 16:03:05 +00:00
|
|
|
protected function setUp(): void
|
2020-05-04 07:48:23 +00:00
|
|
|
{
|
|
|
|
$this->io = $this
|
|
|
|
->getMockBuilder('Composer\IO\IOInterface')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$this->config = $this->getMockBuilder('Composer\Config')->getMock();
|
|
|
|
|
|
|
|
$this->authHelper = new AuthHelper($this->io, $this->config);
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testAddAuthenticationHeaderWithoutAuthCredentials(): void
|
2020-05-04 07:48:23 +00:00
|
|
|
{
|
|
|
|
$headers = array(
|
|
|
|
'Accept-Encoding: gzip',
|
2020-11-22 13:48:56 +00:00
|
|
|
'Connection: close',
|
2020-05-04 07:48:23 +00:00
|
|
|
);
|
|
|
|
$origin = 'http://example.org';
|
|
|
|
$url = 'file://' . __FILE__;
|
|
|
|
|
|
|
|
$this->io->expects($this->once())
|
|
|
|
->method('hasAuthentication')
|
|
|
|
->with($origin)
|
|
|
|
->willReturn(false);
|
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
$headers,
|
|
|
|
$this->authHelper->addAuthenticationHeader($headers, $origin, $url)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testAddAuthenticationHeaderWithBearerPassword(): void
|
2020-05-04 07:48:23 +00:00
|
|
|
{
|
|
|
|
$headers = array(
|
|
|
|
'Accept-Encoding: gzip',
|
2020-11-22 13:48:56 +00:00
|
|
|
'Connection: close',
|
2020-05-04 07:48:23 +00:00
|
|
|
);
|
|
|
|
$origin = 'http://example.org';
|
|
|
|
$url = 'file://' . __FILE__;
|
|
|
|
$auth = array(
|
|
|
|
'username' => 'my_username',
|
2020-11-22 13:48:56 +00:00
|
|
|
'password' => 'bearer',
|
2020-05-04 07:48:23 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->expectsAuthentication($origin, $auth);
|
|
|
|
|
|
|
|
$expectedHeaders = array_merge($headers, array('Authorization: Bearer ' . $auth['username']));
|
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
$expectedHeaders,
|
|
|
|
$this->authHelper->addAuthenticationHeader($headers, $origin, $url)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testAddAuthenticationHeaderWithGithubToken(): void
|
2020-05-04 07:48:23 +00:00
|
|
|
{
|
|
|
|
$headers = array(
|
|
|
|
'Accept-Encoding: gzip',
|
2020-11-22 13:48:56 +00:00
|
|
|
'Connection: close',
|
2020-05-04 07:48:23 +00:00
|
|
|
);
|
|
|
|
$origin = 'github.com';
|
|
|
|
$url = 'https://api.github.com/';
|
|
|
|
$auth = array(
|
|
|
|
'username' => 'my_username',
|
2020-11-22 13:48:56 +00:00
|
|
|
'password' => 'x-oauth-basic',
|
2020-05-04 07:48:23 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->expectsAuthentication($origin, $auth);
|
|
|
|
|
|
|
|
$this->io->expects($this->once())
|
|
|
|
->method('writeError')
|
|
|
|
->with('Using GitHub token authentication', true, IOInterface::DEBUG);
|
|
|
|
|
|
|
|
$expectedHeaders = array_merge($headers, array('Authorization: token ' . $auth['username']));
|
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
$expectedHeaders,
|
|
|
|
$this->authHelper->addAuthenticationHeader($headers, $origin, $url)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testAddAuthenticationHeaderWithGitlabOathToken(): void
|
2020-05-04 07:48:23 +00:00
|
|
|
{
|
|
|
|
$headers = array(
|
|
|
|
'Accept-Encoding: gzip',
|
2020-11-22 13:48:56 +00:00
|
|
|
'Connection: close',
|
2020-05-04 07:48:23 +00:00
|
|
|
);
|
|
|
|
$origin = 'gitlab.com';
|
|
|
|
$url = 'https://api.gitlab.com/';
|
|
|
|
$auth = array(
|
|
|
|
'username' => 'my_username',
|
2020-11-22 13:48:56 +00:00
|
|
|
'password' => 'oauth2',
|
2020-05-04 07:48:23 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->expectsAuthentication($origin, $auth);
|
|
|
|
|
|
|
|
$this->config->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with('gitlab-domains')
|
|
|
|
->willReturn(array($origin));
|
|
|
|
|
|
|
|
$this->io->expects($this->once())
|
|
|
|
->method('writeError')
|
|
|
|
->with('Using GitLab OAuth token authentication', true, IOInterface::DEBUG);
|
|
|
|
|
|
|
|
$expectedHeaders = array_merge($headers, array('Authorization: Bearer ' . $auth['username']));
|
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
$expectedHeaders,
|
|
|
|
$this->authHelper->addAuthenticationHeader($headers, $origin, $url)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-02-21 12:42:28 +00:00
|
|
|
public function gitlabPrivateTokenProvider(): array
|
2020-05-04 07:48:23 +00:00
|
|
|
{
|
|
|
|
return array(
|
|
|
|
array('private-token'),
|
|
|
|
array('gitlab-ci-token'),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider gitlabPrivateTokenProvider
|
|
|
|
*
|
|
|
|
* @param string $password
|
|
|
|
*/
|
2022-02-22 15:47:09 +00:00
|
|
|
public function testAddAuthenticationHeaderWithGitlabPrivateToken(string $password): void
|
2020-05-04 07:48:23 +00:00
|
|
|
{
|
|
|
|
$headers = array(
|
|
|
|
'Accept-Encoding: gzip',
|
2020-11-22 13:48:56 +00:00
|
|
|
'Connection: close',
|
2020-05-04 07:48:23 +00:00
|
|
|
);
|
|
|
|
$origin = 'gitlab.com';
|
|
|
|
$url = 'https://api.gitlab.com/';
|
|
|
|
$auth = array(
|
|
|
|
'username' => 'my_username',
|
2020-11-22 13:48:56 +00:00
|
|
|
'password' => $password,
|
2020-05-04 07:48:23 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->expectsAuthentication($origin, $auth);
|
|
|
|
|
|
|
|
$this->config->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with('gitlab-domains')
|
|
|
|
->willReturn(array($origin));
|
|
|
|
|
|
|
|
$this->io->expects($this->once())
|
|
|
|
->method('writeError')
|
|
|
|
->with('Using GitLab private token authentication', true, IOInterface::DEBUG);
|
|
|
|
|
|
|
|
$expectedHeaders = array_merge($headers, array('PRIVATE-TOKEN: ' . $auth['username']));
|
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
$expectedHeaders,
|
|
|
|
$this->authHelper->addAuthenticationHeader($headers, $origin, $url)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testAddAuthenticationHeaderWithBitbucketOathToken(): void
|
2020-05-04 07:48:23 +00:00
|
|
|
{
|
|
|
|
$headers = array(
|
|
|
|
'Accept-Encoding: gzip',
|
2020-11-22 13:48:56 +00:00
|
|
|
'Connection: close',
|
2020-05-04 07:48:23 +00:00
|
|
|
);
|
|
|
|
$origin = 'bitbucket.org';
|
|
|
|
$url = 'https://bitbucket.org/site/oauth2/authorize';
|
|
|
|
$auth = array(
|
|
|
|
'username' => 'x-token-auth',
|
2020-11-22 13:48:56 +00:00
|
|
|
'password' => 'my_password',
|
2020-05-04 07:48:23 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->expectsAuthentication($origin, $auth);
|
|
|
|
|
|
|
|
$this->config->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with('gitlab-domains')
|
|
|
|
->willReturn(array());
|
|
|
|
|
|
|
|
$this->io->expects($this->once())
|
|
|
|
->method('writeError')
|
|
|
|
->with('Using Bitbucket OAuth token authentication', true, IOInterface::DEBUG);
|
|
|
|
|
|
|
|
$expectedHeaders = array_merge($headers, array('Authorization: Bearer ' . $auth['password']));
|
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
$expectedHeaders,
|
|
|
|
$this->authHelper->addAuthenticationHeader($headers, $origin, $url)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-02-21 12:42:28 +00:00
|
|
|
public function bitbucketPublicUrlProvider(): array
|
2020-05-04 07:48:23 +00:00
|
|
|
{
|
|
|
|
return array(
|
|
|
|
array('https://bitbucket.org/user/repo/downloads/whatever'),
|
|
|
|
array('https://bbuseruploads.s3.amazonaws.com/9421ee72-638e-43a9-82ea-39cfaae2bfaa/downloads/b87c59d9-54f3-4922-b711-d89059ec3bcf'),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider bitbucketPublicUrlProvider
|
|
|
|
*
|
|
|
|
* @param string $url
|
|
|
|
*/
|
2022-02-22 15:47:09 +00:00
|
|
|
public function testAddAuthenticationHeaderWithBitbucketPublicUrl(string $url): void
|
2020-05-04 07:48:23 +00:00
|
|
|
{
|
|
|
|
$headers = array(
|
|
|
|
'Accept-Encoding: gzip',
|
2020-11-22 13:48:56 +00:00
|
|
|
'Connection: close',
|
2020-05-04 07:48:23 +00:00
|
|
|
);
|
|
|
|
$origin = 'bitbucket.org';
|
|
|
|
$auth = array(
|
|
|
|
'username' => 'x-token-auth',
|
2020-11-22 13:48:56 +00:00
|
|
|
'password' => 'my_password',
|
2020-05-04 07:48:23 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->expectsAuthentication($origin, $auth);
|
|
|
|
|
|
|
|
$this->config->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with('gitlab-domains')
|
|
|
|
->willReturn(array());
|
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
$headers,
|
|
|
|
$this->authHelper->addAuthenticationHeader($headers, $origin, $url)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-02-21 12:42:28 +00:00
|
|
|
public function basicHttpAuthenticationProvider(): array
|
2020-05-04 07:48:23 +00:00
|
|
|
{
|
|
|
|
return array(
|
|
|
|
array(
|
|
|
|
Bitbucket::OAUTH2_ACCESS_TOKEN_URL,
|
|
|
|
'bitbucket.org',
|
|
|
|
array(
|
|
|
|
'username' => 'x-token-auth',
|
2020-11-22 13:48:56 +00:00
|
|
|
'password' => 'my_password',
|
|
|
|
),
|
2020-05-04 07:48:23 +00:00
|
|
|
),
|
|
|
|
array(
|
|
|
|
'https://some-api.url.com',
|
|
|
|
'some-api.url.com',
|
|
|
|
array(
|
|
|
|
'username' => 'my_username',
|
2020-11-22 13:48:56 +00:00
|
|
|
'password' => 'my_password',
|
|
|
|
),
|
2020-05-04 07:48:23 +00:00
|
|
|
),
|
2020-08-27 04:45:49 +00:00
|
|
|
array(
|
|
|
|
'https://gitlab.com',
|
|
|
|
'gitlab.com',
|
|
|
|
array(
|
|
|
|
'username' => 'my_username',
|
2020-11-22 13:48:56 +00:00
|
|
|
'password' => 'my_password',
|
|
|
|
),
|
2020-08-27 04:45:49 +00:00
|
|
|
),
|
2020-05-04 07:48:23 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider basicHttpAuthenticationProvider
|
|
|
|
*
|
2021-10-27 14:18:46 +00:00
|
|
|
* @param string $url
|
|
|
|
* @param string $origin
|
|
|
|
* @param array<string, string|null> $auth
|
|
|
|
*
|
|
|
|
* @phpstan-param array{username: string|null, password: string|null} $auth
|
2020-05-04 07:48:23 +00:00
|
|
|
*/
|
2022-02-22 15:47:09 +00:00
|
|
|
public function testAddAuthenticationHeaderWithBasicHttpAuthentication(string $url, string $origin, array $auth): void
|
2020-05-04 07:48:23 +00:00
|
|
|
{
|
|
|
|
$headers = array(
|
|
|
|
'Accept-Encoding: gzip',
|
2020-11-22 13:48:56 +00:00
|
|
|
'Connection: close',
|
2020-05-04 07:48:23 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->expectsAuthentication($origin, $auth);
|
|
|
|
|
|
|
|
$this->config->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with('gitlab-domains')
|
2020-08-27 04:45:49 +00:00
|
|
|
->willReturn(array($origin));
|
2020-05-04 07:48:23 +00:00
|
|
|
|
|
|
|
$this->io->expects($this->once())
|
|
|
|
->method('writeError')
|
|
|
|
->with(
|
|
|
|
'Using HTTP basic authentication with username "' . $auth['username'] . '"',
|
|
|
|
true,
|
|
|
|
IOInterface::DEBUG
|
|
|
|
);
|
|
|
|
|
|
|
|
$expectedHeaders = array_merge(
|
|
|
|
$headers,
|
|
|
|
array('Authorization: Basic ' . base64_encode($auth['username'] . ':' . $auth['password']))
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
$expectedHeaders,
|
|
|
|
$this->authHelper->addAuthenticationHeader($headers, $origin, $url)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider bitbucketPublicUrlProvider
|
|
|
|
*
|
|
|
|
* @param string $url
|
|
|
|
*/
|
2022-02-22 15:47:09 +00:00
|
|
|
public function testIsPublicBitBucketDownloadWithBitbucketPublicUrl(string $url): void
|
2020-05-04 07:48:23 +00:00
|
|
|
{
|
|
|
|
$this->assertTrue($this->authHelper->isPublicBitBucketDownload($url));
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testIsPublicBitBucketDownloadWithNonBitbucketPublicUrl(): void
|
2020-05-04 07:48:23 +00:00
|
|
|
{
|
2020-11-22 13:48:56 +00:00
|
|
|
$this->assertFalse(
|
|
|
|
$this->authHelper->isPublicBitBucketDownload(
|
|
|
|
'https://bitbucket.org/site/oauth2/authorize'
|
|
|
|
)
|
2020-05-04 07:48:23 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testStoreAuthAutomatically(): void
|
2020-05-04 07:48:23 +00:00
|
|
|
{
|
|
|
|
$origin = 'github.com';
|
|
|
|
$storeAuth = true;
|
|
|
|
$auth = array(
|
|
|
|
'username' => 'my_username',
|
2020-11-22 13:48:56 +00:00
|
|
|
'password' => 'my_password',
|
2020-05-04 07:48:23 +00:00
|
|
|
);
|
|
|
|
|
2021-10-27 14:18:46 +00:00
|
|
|
/** @var \Composer\Config\ConfigSourceInterface&\PHPUnit\Framework\MockObject\MockObject $configSource */
|
2020-05-04 07:48:23 +00:00
|
|
|
$configSource = $this
|
|
|
|
->getMockBuilder('Composer\Config\ConfigSourceInterface')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$this->config->expects($this->once())
|
|
|
|
->method('getAuthConfigSource')
|
|
|
|
->willReturn($configSource);
|
|
|
|
|
|
|
|
$this->io->expects($this->once())
|
|
|
|
->method('getAuthentication')
|
|
|
|
->with($origin)
|
|
|
|
->willReturn($auth);
|
|
|
|
|
|
|
|
$configSource->expects($this->once())
|
|
|
|
->method('addConfigSetting')
|
2022-02-22 21:10:52 +00:00
|
|
|
->with('http-basic.'.$origin, $auth);
|
2020-05-04 07:48:23 +00:00
|
|
|
|
|
|
|
$this->authHelper->storeAuth($origin, $storeAuth);
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testStoreAuthWithPromptYesAnswer(): void
|
2020-05-04 07:48:23 +00:00
|
|
|
{
|
|
|
|
$origin = 'github.com';
|
|
|
|
$storeAuth = 'prompt';
|
|
|
|
$auth = array(
|
|
|
|
'username' => 'my_username',
|
2020-11-22 13:48:56 +00:00
|
|
|
'password' => 'my_password',
|
2020-05-04 07:48:23 +00:00
|
|
|
);
|
|
|
|
$answer = 'y';
|
|
|
|
$configSourceName = 'https://api.gitlab.com/source';
|
|
|
|
|
2021-10-27 14:18:46 +00:00
|
|
|
/** @var \Composer\Config\ConfigSourceInterface&\PHPUnit\Framework\MockObject\MockObject $configSource */
|
2020-05-04 07:48:23 +00:00
|
|
|
$configSource = $this
|
|
|
|
->getMockBuilder('Composer\Config\ConfigSourceInterface')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$this->config->expects($this->once())
|
|
|
|
->method('getAuthConfigSource')
|
|
|
|
->willReturn($configSource);
|
|
|
|
|
|
|
|
$configSource->expects($this->once())
|
|
|
|
->method('getName')
|
|
|
|
->willReturn($configSourceName);
|
|
|
|
|
|
|
|
$this->io->expects($this->once())
|
|
|
|
->method('askAndValidate')
|
|
|
|
->with(
|
|
|
|
'Do you want to store credentials for '.$origin.' in '.$configSourceName.' ? [Yn] ',
|
|
|
|
$this->anything(),
|
|
|
|
null,
|
|
|
|
'y'
|
|
|
|
)
|
2022-02-21 12:42:28 +00:00
|
|
|
->willReturnCallback(function ($question, $validator, $attempts, $default) use ($answer): string {
|
2020-05-04 07:48:23 +00:00
|
|
|
$validator($answer);
|
|
|
|
|
|
|
|
return $answer;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->io->expects($this->once())
|
|
|
|
->method('getAuthentication')
|
|
|
|
->with($origin)
|
|
|
|
->willReturn($auth);
|
|
|
|
|
|
|
|
$configSource->expects($this->once())
|
|
|
|
->method('addConfigSetting')
|
2022-02-22 21:10:52 +00:00
|
|
|
->with('http-basic.'.$origin, $auth);
|
2020-05-04 07:48:23 +00:00
|
|
|
|
|
|
|
$this->authHelper->storeAuth($origin, $storeAuth);
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testStoreAuthWithPromptNoAnswer(): void
|
2020-05-04 07:48:23 +00:00
|
|
|
{
|
|
|
|
$origin = 'github.com';
|
|
|
|
$storeAuth = 'prompt';
|
|
|
|
$answer = 'n';
|
|
|
|
$configSourceName = 'https://api.gitlab.com/source';
|
|
|
|
|
2021-10-27 14:18:46 +00:00
|
|
|
/** @var \Composer\Config\ConfigSourceInterface&\PHPUnit\Framework\MockObject\MockObject $configSource */
|
2020-05-04 07:48:23 +00:00
|
|
|
$configSource = $this
|
|
|
|
->getMockBuilder('Composer\Config\ConfigSourceInterface')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$this->config->expects($this->once())
|
|
|
|
->method('getAuthConfigSource')
|
|
|
|
->willReturn($configSource);
|
|
|
|
|
|
|
|
$configSource->expects($this->once())
|
|
|
|
->method('getName')
|
|
|
|
->willReturn($configSourceName);
|
|
|
|
|
|
|
|
$this->io->expects($this->once())
|
|
|
|
->method('askAndValidate')
|
|
|
|
->with(
|
|
|
|
'Do you want to store credentials for '.$origin.' in '.$configSourceName.' ? [Yn] ',
|
|
|
|
$this->anything(),
|
|
|
|
null,
|
|
|
|
'y'
|
|
|
|
)
|
2022-02-21 12:42:28 +00:00
|
|
|
->willReturnCallback(function ($question, $validator, $attempts, $default) use ($answer): string {
|
2020-05-04 07:48:23 +00:00
|
|
|
$validator($answer);
|
|
|
|
|
|
|
|
return $answer;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->authHelper->storeAuth($origin, $storeAuth);
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testStoreAuthWithPromptInvalidAnswer(): void
|
2020-05-04 07:48:23 +00:00
|
|
|
{
|
2021-12-09 19:55:26 +00:00
|
|
|
self::expectException('RuntimeException');
|
2020-09-10 15:21:11 +00:00
|
|
|
|
2020-05-04 07:48:23 +00:00
|
|
|
$origin = 'github.com';
|
|
|
|
$storeAuth = 'prompt';
|
|
|
|
$answer = 'invalid';
|
|
|
|
$configSourceName = 'https://api.gitlab.com/source';
|
|
|
|
|
2021-10-27 14:18:46 +00:00
|
|
|
/** @var \Composer\Config\ConfigSourceInterface&\PHPUnit\Framework\MockObject\MockObject $configSource */
|
2020-05-04 07:48:23 +00:00
|
|
|
$configSource = $this
|
|
|
|
->getMockBuilder('Composer\Config\ConfigSourceInterface')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$this->config->expects($this->once())
|
|
|
|
->method('getAuthConfigSource')
|
|
|
|
->willReturn($configSource);
|
|
|
|
|
|
|
|
$configSource->expects($this->once())
|
|
|
|
->method('getName')
|
|
|
|
->willReturn($configSourceName);
|
|
|
|
|
|
|
|
$this->io->expects($this->once())
|
|
|
|
->method('askAndValidate')
|
|
|
|
->with(
|
|
|
|
'Do you want to store credentials for '.$origin.' in '.$configSourceName.' ? [Yn] ',
|
|
|
|
$this->anything(),
|
|
|
|
null,
|
|
|
|
'y'
|
|
|
|
)
|
2022-02-21 12:42:28 +00:00
|
|
|
->willReturnCallback(function ($question, $validator, $attempts, $default) use ($answer): string {
|
2020-05-04 07:48:23 +00:00
|
|
|
$validator($answer);
|
|
|
|
|
|
|
|
return $answer;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->authHelper->storeAuth($origin, $storeAuth);
|
|
|
|
}
|
|
|
|
|
2022-04-29 10:15:20 +00:00
|
|
|
public function testPromptAuthIfNeededGitLabNoAuthChange(): void
|
2022-04-21 08:37:55 +00:00
|
|
|
{
|
2022-04-29 10:15:20 +00:00
|
|
|
self::expectException('Composer\Downloader\TransportException');
|
2022-04-21 08:37:55 +00:00
|
|
|
|
|
|
|
$origin = 'gitlab.com';
|
|
|
|
|
|
|
|
$this->io
|
|
|
|
->method('hasAuthentication')
|
|
|
|
->with($origin)
|
|
|
|
->willReturn(true);
|
|
|
|
|
|
|
|
$this->io
|
|
|
|
->method('getAuthentication')
|
|
|
|
->with($origin)
|
|
|
|
->willReturn(array(
|
|
|
|
'username' => 'gitlab-user',
|
|
|
|
'password' => 'gitlab-password',
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->io
|
|
|
|
->expects($this->once())
|
|
|
|
->method('setAuthentication')
|
|
|
|
->with('gitlab.com', 'gitlab-user', 'gitlab-password');
|
|
|
|
|
|
|
|
$this->config
|
|
|
|
->method('get')
|
|
|
|
->willReturnMap(array(
|
|
|
|
array('github-domains', 0, array()),
|
|
|
|
array('gitlab-domains', 0, array('gitlab.com')),
|
|
|
|
array('gitlab-token', 0, array('gitlab.com' => array('username' => 'gitlab-user', 'token' => 'gitlab-password'))),
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->authHelper->promptAuthIfNeeded('https://gitlab.com/acme/archive.zip', $origin, 404, 'GitLab requires authentication and it was not provided');
|
|
|
|
}
|
|
|
|
|
2020-05-04 07:48:23 +00:00
|
|
|
/**
|
2021-10-27 14:18:46 +00:00
|
|
|
* @param string $origin
|
|
|
|
* @param array<string, string|null> $auth
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*
|
|
|
|
* @phpstan-param array{username: string|null, password: string|null} $auth
|
2020-05-04 07:48:23 +00:00
|
|
|
*/
|
2022-02-22 15:47:09 +00:00
|
|
|
private function expectsAuthentication(string $origin, array $auth): void
|
2020-05-04 07:48:23 +00:00
|
|
|
{
|
|
|
|
$this->io->expects($this->once())
|
|
|
|
->method('hasAuthentication')
|
|
|
|
->with($origin)
|
|
|
|
->willReturn(true);
|
|
|
|
|
|
|
|
$this->io->expects($this->once())
|
|
|
|
->method('getAuthentication')
|
|
|
|
->with($origin)
|
|
|
|
->willReturn($auth);
|
|
|
|
}
|
|
|
|
}
|