123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- <?php
- namespace Composer\Test\Util;
- use Composer\Downloader\TransportException;
- use Composer\Util\GitHub;
- use RecursiveArrayIterator;
- use RecursiveIteratorIterator;
- class GitHubTest extends \PHPUnit_Framework_TestCase
- {
- 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))
- ->method('write')
- ->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('https://api.%s/authorizations', $this->origin)),
- $this->isFalse(),
- $this->anything()
- )
- ->willReturn(sprintf('{"token": "%s"}', $this->token))
- ;
- $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
- ->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())
- ;
- $github = new GitHub($io, $config, null, $rfs);
- $github->authorizeOAuthInteractively($this->origin);
- }
- public function testTwoFactorAuthentication()
- {
- $io = $this->getIOMock();
- $io
- ->expects($this->exactly(2))
- ->method('hasAuthentication')
- ->will($this->onConsecutiveCalls(true, true))
- ;
- $io
- ->expects($this->exactly(2))
- ->method('ask')
- ->withConsecutive(
- array('Username: '),
- array('Authentication Code: ')
- )
- ->will($this->onConsecutiveCalls($this->username, $this->authcode))
- ;
- $io
- ->expects($this->once())
- ->method('askAndHideAnswer')
- ->with('Password: ')
- ->willReturn($this->password)
- ;
- $exception = new TransportException('', 401);
- $exception->setHeaders(array('X-GitHub-OTP: required; app'));
- $rfs = $this->getRemoteFilesystemMock();
- $rfs
- ->expects($this->at(0))
- ->method('getContents')
- ->will($this->throwException($exception))
- ;
- $rfs
- ->expects($this->at(1))
- ->method('getContents')
- ->with(
- $this->equalTo($this->origin),
- $this->equalTo(sprintf('https://api.%s/authorizations', $this->origin)),
- $this->isFalse(),
- $this->callback(function ($array) {
- $headers = GitHubTest::recursiveFind($array, 'header');
- foreach ($headers as $string) {
- if ('X-GitHub-OTP: authcode' === $string) {
- return true;
- }
- }
- return false;
- })
- )
- ->willReturn(sprintf('{"token": "%s"}', $this->token))
- ;
- $config = $this->getConfigMock();
- $config
- ->expects($this->atLeastOnce())
- ->method('getAuthConfigSource')
- ->willReturn($this->getAuthJsonMock())
- ;
- $config
- ->expects($this->atLeastOnce())
- ->method('getConfigSource')
- ->willReturn($this->getConfJsonMock())
- ;
- $github = new GitHub($io, $config, null, $rfs);
- $this->assertTrue($github->authorizeOAuthInteractively($this->origin));
- }
- private function getIOMock()
- {
- $io = $this
- ->getMockBuilder('Composer\IO\ConsoleIO')
- ->disableOriginalConstructor()
- ->getMock()
- ;
- return $io;
- }
- private function getConfigMock()
- {
- $config = $this->getMock('Composer\Config');
- return $config;
- }
- 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;
- }
- }
- }
- }
|