2015-11-13 17:50:50 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
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\Downloader\TransportException;
|
|
|
|
use Composer\Util\GitLab;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Jérôme Tamarelle <jerome@tamarelle.net>
|
|
|
|
*/
|
|
|
|
class GitLabTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
|
|
|
private $username = 'username';
|
|
|
|
private $password = 'password';
|
|
|
|
private $authcode = 'authcode';
|
|
|
|
private $message = 'mymessage';
|
|
|
|
private $origin = 'gitlab.com';
|
|
|
|
private $token = 'gitlabtoken';
|
|
|
|
|
|
|
|
public function testUsernamePasswordAuthenticationFlow()
|
|
|
|
{
|
|
|
|
$io = $this->getIOMock();
|
|
|
|
$io
|
|
|
|
->expects($this->at(0))
|
|
|
|
->method('writeError')
|
|
|
|
->with($this->message)
|
|
|
|
;
|
|
|
|
$io
|
|
|
|
->expects($this->once())
|
|
|
|
->method('ask')
|
|
|
|
->with('Username: ')
|
|
|
|
->willReturn($this->username)
|
|
|
|
;
|
|
|
|
$io
|
|
|
|
->expects($this->once())
|
|
|
|
->method('askAndHideAnswer')
|
|
|
|
->with('Password: ')
|
|
|
|
->willReturn($this->password)
|
|
|
|
;
|
|
|
|
|
|
|
|
$rfs = $this->getRemoteFilesystemMock();
|
|
|
|
$rfs
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getContents')
|
|
|
|
->with(
|
|
|
|
$this->equalTo($this->origin),
|
|
|
|
$this->equalTo(sprintf('http://%s/oauth/token', $this->origin)),
|
|
|
|
$this->isFalse(),
|
|
|
|
$this->anything()
|
|
|
|
)
|
|
|
|
->willReturn(sprintf('{"access_token": "%s", "token_type": "bearer", "expires_in": 7200}', $this->token))
|
|
|
|
;
|
|
|
|
|
|
|
|
$config = $this->getConfigMock();
|
|
|
|
$config
|
|
|
|
->expects($this->exactly(2))
|
|
|
|
->method('getAuthConfigSource')
|
|
|
|
->willReturn($this->getAuthJsonMock())
|
|
|
|
;
|
|
|
|
|
|
|
|
$gitLab = new GitLab($io, $config, null, $rfs);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \RuntimeException
|
|
|
|
* @expectedExceptionMessage Invalid GitLab credentials 5 times in a row, aborting.
|
|
|
|
*/
|
|
|
|
public function testUsernamePasswordFailure()
|
|
|
|
{
|
|
|
|
$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)
|
|
|
|
;
|
|
|
|
|
|
|
|
$rfs = $this->getRemoteFilesystemMock();
|
|
|
|
$rfs
|
|
|
|
->expects($this->exactly(5))
|
|
|
|
->method('getContents')
|
|
|
|
->will($this->throwException(new TransportException('', 401)))
|
|
|
|
;
|
|
|
|
|
|
|
|
$config = $this->getConfigMock();
|
|
|
|
$config
|
|
|
|
->expects($this->exactly(1))
|
|
|
|
->method('getAuthConfigSource')
|
|
|
|
->willReturn($this->getAuthJsonMock())
|
|
|
|
;
|
|
|
|
|
|
|
|
$gitLab = new GitLab($io, $config, null, $rfs);
|
|
|
|
|
2015-11-14 15:00:14 +00:00
|
|
|
$gitLab->authorizeOAuthInteractively('https', $this->origin);
|
2015-11-13 17:50:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function getIOMock()
|
|
|
|
{
|
|
|
|
$io = $this
|
|
|
|
->getMockBuilder('Composer\IO\ConsoleIO')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock()
|
|
|
|
;
|
|
|
|
|
|
|
|
return $io;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getConfigMock()
|
|
|
|
{
|
2017-09-11 17:40:43 +00:00
|
|
|
return $this->getMock('Composer\Config');
|
2015-11-13 17:50:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function getRemoteFilesystemMock()
|
|
|
|
{
|
|
|
|
$rfs = $this
|
|
|
|
->getMockBuilder('Composer\Util\RemoteFilesystem')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock()
|
|
|
|
;
|
|
|
|
|
|
|
|
return $rfs;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getAuthJsonMock()
|
|
|
|
{
|
|
|
|
$authjson = $this
|
|
|
|
->getMockBuilder('Composer\Config\JsonConfigSource')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock()
|
|
|
|
;
|
|
|
|
$authjson
|
|
|
|
->expects($this->atLeastOnce())
|
|
|
|
->method('getName')
|
|
|
|
->willReturn('auth.json')
|
|
|
|
;
|
|
|
|
|
|
|
|
return $authjson;
|
|
|
|
}
|
|
|
|
}
|