1
0
Fork 0

trim token just in case and update tests

pull/4010/head
Jordi Boggiano 2015-05-07 00:37:08 +01:00
parent 41f4451c20
commit 52d290f5f2
3 changed files with 13 additions and 105 deletions

View File

@ -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('<warning>No token given, aborting.</warning>');

View File

@ -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')

View File

@ -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()