Skip to content

Commit

Permalink
trim token just in case and update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldaek committed May 6, 2015
1 parent 41f4451 commit 52d290f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 105 deletions.
2 changes: 1 addition & 1 deletion src/Composer/Util/GitHub.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function authorizeOAuthInteractively($originUrl, $message = null)
$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>');
Expand Down
15 changes: 5 additions & 10 deletions tests/Composer/Test/Repository/Vcs/GitHubDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,24 +64,19 @@ public function testPrivateRepository()
->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')
Expand Down
101 changes: 7 additions & 94 deletions tests/Composer/Test/Util/GitHubTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,10 @@ public function testUsernamePasswordAuthenticationFlow()
->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)
;

Expand All @@ -56,11 +50,11 @@ public function testUsernamePasswordAuthenticationFlow()
->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();
Expand All @@ -80,29 +74,19 @@ public function testUsernamePasswordAuthenticationFlow()
$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)))
;
Expand All @@ -116,78 +100,7 @@ public function testUsernamePasswordFailure()

$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()
Expand Down

0 comments on commit 52d290f

Please sign in to comment.