trim token just in case and update tests

main
Jordi Boggiano 9 years ago
parent 41f4451c20
commit 52d290f5f2

@ -90,7 +90,7 @@ class GitHub
$this->io->writeError(sprintf('Head to %s', $url)); $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())); $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) { if (!$token) {
$this->io->writeError('<warning>No token given, aborting.</warning>'); $this->io->writeError('<warning>No token given, aborting.</warning>');

@ -64,24 +64,19 @@ class GitHubDriverTest extends \PHPUnit_Framework_TestCase
->with($this->equalTo('github.com'), $this->equalTo($repoApiUrl), $this->equalTo(false)) ->with($this->equalTo('github.com'), $this->equalTo($repoApiUrl), $this->equalTo(false))
->will($this->throwException(new TransportException('HTTP/1.1 404 Not Found', 404))); ->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()) $io->expects($this->once())
->method('askAndHideAnswer') ->method('askAndHideAnswer')
->with($this->equalTo('Password: ')) ->with($this->equalTo('Token (hidden): '))
->will($this->returnValue('somepassword')); ->will($this->returnValue('sometoken'));
$io->expects($this->any()) $io->expects($this->any())
->method('setAuthentication') ->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)) $remoteFilesystem->expects($this->at(1))
->method('getContents') ->method('getContents')
->with($this->equalTo('github.com'), $this->equalTo('https://api.github.com/authorizations'), $this->equalTo(false)) ->with($this->equalTo('github.com'), $this->equalTo('https://api.github.com/rate_limit'), $this->equalTo(false))
->will($this->returnValue('{"token": "abcdef"}')); ->will($this->returnValue('{}'));
$remoteFilesystem->expects($this->at(2)) $remoteFilesystem->expects($this->at(2))
->method('getContents') ->method('getContents')

@ -37,16 +37,10 @@ class GitHubTest extends \PHPUnit_Framework_TestCase
->method('writeError') ->method('writeError')
->with($this->message) ->with($this->message)
; ;
$io
->expects($this->once())
->method('ask')
->with('Username: ')
->willReturn($this->username)
;
$io $io
->expects($this->once()) ->expects($this->once())
->method('askAndHideAnswer') ->method('askAndHideAnswer')
->with('Password: ') ->with('Token (hidden): ')
->willReturn($this->password) ->willReturn($this->password)
; ;
@ -56,11 +50,11 @@ class GitHubTest extends \PHPUnit_Framework_TestCase
->method('getContents') ->method('getContents')
->with( ->with(
$this->equalTo($this->origin), $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->isFalse(),
$this->anything() $this->anything()
) )
->willReturn(sprintf('{"token": "%s"}', $this->token)) ->willReturn(sprintf('{}', $this->token))
; ;
$config = $this->getConfigMock(); $config = $this->getConfigMock();
@ -80,29 +74,19 @@ class GitHubTest extends \PHPUnit_Framework_TestCase
$this->assertTrue($github->authorizeOAuthInteractively($this->origin, $this->message)); $this->assertTrue($github->authorizeOAuthInteractively($this->origin, $this->message));
} }
/**
* @expectedException \RuntimeException
* @expectedExceptionMessage Invalid GitHub credentials 5 times in a row, aborting.
*/
public function testUsernamePasswordFailure() public function testUsernamePasswordFailure()
{ {
$io = $this->getIOMock(); $io = $this->getIOMock();
$io $io
->expects($this->exactly(5)) ->expects($this->exactly(1))
->method('ask')
->with('Username: ')
->willReturn($this->username)
;
$io
->expects($this->exactly(5))
->method('askAndHideAnswer') ->method('askAndHideAnswer')
->with('Password: ') ->with('Token (hidden): ')
->willReturn($this->password) ->willReturn($this->password)
; ;
$rfs = $this->getRemoteFilesystemMock(); $rfs = $this->getRemoteFilesystemMock();
$rfs $rfs
->expects($this->exactly(5)) ->expects($this->exactly(1))
->method('getContents') ->method('getContents')
->will($this->throwException(new TransportException('', 401))) ->will($this->throwException(new TransportException('', 401)))
; ;
@ -116,78 +100,7 @@ class GitHubTest extends \PHPUnit_Framework_TestCase
$github = new GitHub($io, $config, null, $rfs); $github = new GitHub($io, $config, null, $rfs);
$github->authorizeOAuthInteractively($this->origin); $this->assertFalse($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() private function getIOMock()

Loading…
Cancel
Save