From 52d290f5f2e4f6905fdf171900286ddd1dc70ab0 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Thu, 7 May 2015 00:37:08 +0100 Subject: [PATCH] trim token just in case and update tests --- src/Composer/Util/GitHub.php | 2 +- .../Test/Repository/Vcs/GitHubDriverTest.php | 15 +-- tests/Composer/Test/Util/GitHubTest.php | 101 ++---------------- 3 files changed, 13 insertions(+), 105 deletions(-) diff --git a/src/Composer/Util/GitHub.php b/src/Composer/Util/GitHub.php index 94c41d4b7..22c67c61b 100644 --- a/src/Composer/Util/GitHub.php +++ b/src/Composer/Util/GitHub.php @@ -90,7 +90,7 @@ class GitHub $this->io->writeError(sprintf('Head to %s', $url)); $this->io->writeError(sprintf('to retrieve a token. It will be stored in "%s" for future use by Composer.', $this->config->getAuthConfigSource()->getName())); - $token = $this->io->askAndHideAnswer('Token (hidden): '); + $token = trim($this->io->askAndHideAnswer('Token (hidden): ')); if (!$token) { $this->io->writeError('No token given, aborting.'); diff --git a/tests/Composer/Test/Repository/Vcs/GitHubDriverTest.php b/tests/Composer/Test/Repository/Vcs/GitHubDriverTest.php index e180c5eec..cd40a71f2 100644 --- a/tests/Composer/Test/Repository/Vcs/GitHubDriverTest.php +++ b/tests/Composer/Test/Repository/Vcs/GitHubDriverTest.php @@ -64,24 +64,19 @@ class GitHubDriverTest extends \PHPUnit_Framework_TestCase ->with($this->equalTo('github.com'), $this->equalTo($repoApiUrl), $this->equalTo(false)) ->will($this->throwException(new TransportException('HTTP/1.1 404 Not Found', 404))); - $io->expects($this->once()) - ->method('ask') - ->with($this->equalTo('Username: ')) - ->will($this->returnValue('someuser')); - $io->expects($this->once()) ->method('askAndHideAnswer') - ->with($this->equalTo('Password: ')) - ->will($this->returnValue('somepassword')); + ->with($this->equalTo('Token (hidden): ')) + ->will($this->returnValue('sometoken')); $io->expects($this->any()) ->method('setAuthentication') - ->with($this->equalTo('github.com'), $this->matchesRegularExpression('{someuser|abcdef}'), $this->matchesRegularExpression('{somepassword|x-oauth-basic}')); + ->with($this->equalTo('github.com'), $this->matchesRegularExpression('{sometoken}'), $this->matchesRegularExpression('{x-oauth-basic}')); $remoteFilesystem->expects($this->at(1)) ->method('getContents') - ->with($this->equalTo('github.com'), $this->equalTo('https://api.github.com/authorizations'), $this->equalTo(false)) - ->will($this->returnValue('{"token": "abcdef"}')); + ->with($this->equalTo('github.com'), $this->equalTo('https://api.github.com/rate_limit'), $this->equalTo(false)) + ->will($this->returnValue('{}')); $remoteFilesystem->expects($this->at(2)) ->method('getContents') diff --git a/tests/Composer/Test/Util/GitHubTest.php b/tests/Composer/Test/Util/GitHubTest.php index 488507ec3..1363afd15 100644 --- a/tests/Composer/Test/Util/GitHubTest.php +++ b/tests/Composer/Test/Util/GitHubTest.php @@ -37,16 +37,10 @@ class GitHubTest extends \PHPUnit_Framework_TestCase ->method('writeError') ->with($this->message) ; - $io - ->expects($this->once()) - ->method('ask') - ->with('Username: ') - ->willReturn($this->username) - ; $io ->expects($this->once()) ->method('askAndHideAnswer') - ->with('Password: ') + ->with('Token (hidden): ') ->willReturn($this->password) ; @@ -56,11 +50,11 @@ class GitHubTest extends \PHPUnit_Framework_TestCase ->method('getContents') ->with( $this->equalTo($this->origin), - $this->equalTo(sprintf('https://api.%s/authorizations', $this->origin)), + $this->equalTo(sprintf('https://api.%s/rate_limit', $this->origin)), $this->isFalse(), $this->anything() ) - ->willReturn(sprintf('{"token": "%s"}', $this->token)) + ->willReturn(sprintf('{}', $this->token)) ; $config = $this->getConfigMock(); @@ -80,29 +74,19 @@ class GitHubTest extends \PHPUnit_Framework_TestCase $this->assertTrue($github->authorizeOAuthInteractively($this->origin, $this->message)); } - /** - * @expectedException \RuntimeException - * @expectedExceptionMessage Invalid GitHub 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)) + ->expects($this->exactly(1)) ->method('askAndHideAnswer') - ->with('Password: ') + ->with('Token (hidden): ') ->willReturn($this->password) ; $rfs = $this->getRemoteFilesystemMock(); $rfs - ->expects($this->exactly(5)) + ->expects($this->exactly(1)) ->method('getContents') ->will($this->throwException(new TransportException('', 401))) ; @@ -116,78 +100,7 @@ class GitHubTest extends \PHPUnit_Framework_TestCase $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)); + $this->assertFalse($github->authorizeOAuthInteractively($this->origin)); } private function getIOMock()