2015-02-05 13:46:14 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2015-10-13 09:34:02 +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-02-05 13:46:14 +00:00
|
|
|
|
|
|
|
namespace Composer\Test\Util;
|
|
|
|
|
|
|
|
use Composer\Downloader\TransportException;
|
|
|
|
use Composer\Util\GitHub;
|
2017-11-04 14:52:13 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2015-02-05 13:46:14 +00:00
|
|
|
use RecursiveArrayIterator;
|
|
|
|
use RecursiveIteratorIterator;
|
|
|
|
|
|
|
|
/**
|
2015-09-28 09:51:14 +00:00
|
|
|
* @author Rob Bast <rob.bast@gmail.com>
|
|
|
|
*/
|
2017-11-04 14:52:13 +00:00
|
|
|
class GitHubTest extends TestCase
|
2015-02-05 13:46:14 +00:00
|
|
|
{
|
|
|
|
private $username = 'username';
|
|
|
|
private $password = 'password';
|
|
|
|
private $authcode = 'authcode';
|
|
|
|
private $message = 'mymessage';
|
|
|
|
private $origin = 'github.com';
|
|
|
|
private $token = 'githubtoken';
|
|
|
|
|
|
|
|
public function testUsernamePasswordAuthenticationFlow()
|
|
|
|
{
|
|
|
|
$io = $this->getIOMock();
|
|
|
|
$io
|
|
|
|
->expects($this->at(0))
|
2015-02-06 12:52:44 +00:00
|
|
|
->method('writeError')
|
2015-02-05 13:46:14 +00:00
|
|
|
->with($this->message)
|
|
|
|
;
|
|
|
|
$io
|
|
|
|
->expects($this->once())
|
|
|
|
->method('askAndHideAnswer')
|
2015-05-06 23:37:08 +00:00
|
|
|
->with('Token (hidden): ')
|
2015-02-05 13:46:14 +00:00
|
|
|
->willReturn($this->password)
|
|
|
|
;
|
|
|
|
|
|
|
|
$rfs = $this->getRemoteFilesystemMock();
|
|
|
|
$rfs
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getContents')
|
|
|
|
->with(
|
|
|
|
$this->equalTo($this->origin),
|
2016-02-24 19:54:01 +00:00
|
|
|
$this->equalTo(sprintf('https://api.%s/', $this->origin)),
|
2015-02-05 13:46:14 +00:00
|
|
|
$this->isFalse(),
|
|
|
|
$this->anything()
|
|
|
|
)
|
2017-09-11 17:40:43 +00:00
|
|
|
->willReturn('{}')
|
2015-02-05 13:46:14 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
$config = $this->getConfigMock();
|
|
|
|
$config
|
|
|
|
->expects($this->exactly(2))
|
|
|
|
->method('getAuthConfigSource')
|
|
|
|
->willReturn($this->getAuthJsonMock())
|
|
|
|
;
|
|
|
|
$config
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getConfigSource')
|
|
|
|
->willReturn($this->getConfJsonMock())
|
|
|
|
;
|
|
|
|
|
|
|
|
$github = new GitHub($io, $config, null, $rfs);
|
|
|
|
|
|
|
|
$this->assertTrue($github->authorizeOAuthInteractively($this->origin, $this->message));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testUsernamePasswordFailure()
|
|
|
|
{
|
|
|
|
$io = $this->getIOMock();
|
|
|
|
$io
|
2015-05-06 23:37:08 +00:00
|
|
|
->expects($this->exactly(1))
|
2015-02-05 13:46:14 +00:00
|
|
|
->method('askAndHideAnswer')
|
2015-05-06 23:37:08 +00:00
|
|
|
->with('Token (hidden): ')
|
2015-02-05 13:46:14 +00:00
|
|
|
->willReturn($this->password)
|
|
|
|
;
|
|
|
|
|
|
|
|
$rfs = $this->getRemoteFilesystemMock();
|
|
|
|
$rfs
|
2015-05-06 23:37:08 +00:00
|
|
|
->expects($this->exactly(1))
|
2015-02-05 13:46:14 +00:00
|
|
|
->method('getContents')
|
|
|
|
->will($this->throwException(new TransportException('', 401)))
|
|
|
|
;
|
|
|
|
|
|
|
|
$config = $this->getConfigMock();
|
|
|
|
$config
|
|
|
|
->expects($this->exactly(1))
|
|
|
|
->method('getAuthConfigSource')
|
|
|
|
->willReturn($this->getAuthJsonMock())
|
|
|
|
;
|
|
|
|
|
|
|
|
$github = new GitHub($io, $config, null, $rfs);
|
|
|
|
|
2015-05-06 23:37:08 +00:00
|
|
|
$this->assertFalse($github->authorizeOAuthInteractively($this->origin));
|
2015-02-05 13:46:14 +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-02-05 13:46:14 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getConfJsonMock()
|
|
|
|
{
|
|
|
|
$confjson = $this
|
|
|
|
->getMockBuilder('Composer\Config\JsonConfigSource')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock()
|
|
|
|
;
|
|
|
|
$confjson
|
|
|
|
->expects($this->atLeastOnce())
|
|
|
|
->method('removeConfigSetting')
|
|
|
|
->with('github-oauth.'.$this->origin)
|
|
|
|
;
|
|
|
|
|
|
|
|
return $confjson;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function recursiveFind($array, $needle)
|
|
|
|
{
|
|
|
|
$iterator = new RecursiveArrayIterator($array);
|
|
|
|
$recursive = new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::SELF_FIRST);
|
|
|
|
|
|
|
|
foreach ($recursive as $key => $value) {
|
|
|
|
if ($key === $needle) {
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|