update 1.0.8.0

Commits for version update
This commit is contained in:
Manish Verma
2016-10-17 12:02:27 +05:30
parent dec927987b
commit 76e85db070
9674 changed files with 495757 additions and 58922 deletions

View File

@@ -0,0 +1,47 @@
<?php namespace League\OAuth1\Client\Tests;
/**
* Part of the Sentry package.
*
* NOTICE OF LICENSE
*
* Licensed under the 3-clause BSD License.
*
* This source file is subject to the 3-clause BSD License that is
* bundled with this package in the LICENSE file. It is also available at
* the following URL: http://www.opensource.org/licenses/BSD-3-Clause
*
* @package Sentry
* @version 2.0.0
* @author Cartalyst LLC
* @license BSD License (3-clause)
* @copyright (c) 2011 - 2013, Cartalyst LLC
* @link http://cartalyst.com
*/
use League\OAuth1\Client\Credentials\ClientCredentials;
use Mockery as m;
use PHPUnit_Framework_TestCase;
class ClientCredentialsTest extends PHPUnit_Framework_TestCase
{
/**
* Close mockery.
*
* @return void
*/
public function tearDown()
{
m::close();
}
public function testManipulating()
{
$credentials = new ClientCredentials;
$this->assertNull($credentials->getIdentifier());
$credentials->setIdentifier('foo');
$this->assertEquals('foo', $credentials->getIdentifier());
$this->assertNull($credentials->getSecret());
$credentials->setSecret('foo');
$this->assertEquals('foo', $credentials->getSecret());
}
}

View File

@@ -0,0 +1,164 @@
<?php namespace League\OAuth1\Client\Tests;
/**
* Part of the Sentry package.
*
* NOTICE OF LICENSE
*
* Licensed under the 3-clause BSD License.
*
* This source file is subject to the 3-clause BSD License that is
* bundled with this package in the LICENSE file. It is also available at
* the following URL: http://www.opensource.org/licenses/BSD-3-Clause
*
* @package Sentry
* @version 2.0.0
* @author Cartalyst LLC
* @license BSD License (3-clause)
* @copyright (c) 2011 - 2013, Cartalyst LLC
* @link http://cartalyst.com
*/
use League\OAuth1\Client\Signature\HmacSha1Signature;
use Mockery as m;
use PHPUnit_Framework_TestCase;
class HmacSha1SignatureTest extends PHPUnit_Framework_TestCase
{
/**
* Close mockery.
*
* @return void
*/
public function tearDown()
{
m::close();
}
public function testSigningRequest()
{
$signature = new HmacSha1Signature($this->getMockClientCredentials());
$uri = 'http://www.example.com/?qux=corge';
$parameters = array('foo' => 'bar', 'baz' => null);
$this->assertEquals('A3Y7C1SUHXR1EBYIUlT3d6QT1cQ=', $signature->sign($uri, $parameters));
}
public function testQueryStringFromArray()
{
$array = array('a' => 'b');
$res = $this->invokeQueryStringFromData($array);
$this->assertSame(
'a%3Db',
$res
);
}
public function testQueryStringFromIndexedArray()
{
$array = array('a', 'b');
$res = $this->invokeQueryStringFromData($array);
$this->assertSame(
'0%3Da%261%3Db',
$res
);
}
public function testQueryStringFromMultiDimensionalArray()
{
$array = array(
'a' => array(
'b' => array(
'c' => 'd',
),
'e' => array(
'f' => 'g',
),
),
'h' => 'i',
'empty' => '',
'null' => null,
'false' => false,
);
// Convert to query string.
$res = $this->invokeQueryStringFromData($array);
$this->assertSame(
'a%5Bb%5D%5Bc%5D%3Dd%26a%5Be%5D%5Bf%5D%3Dg%26h%3Di%26empty%3D%26null%3D%26false%3D',
$res
);
// Reverse engineer the string.
$res = urldecode($res);
$this->assertSame(
'a[b][c]=d&a[e][f]=g&h=i&empty=&null=&false=',
$res
);
// Finally, parse the string back to an array.
parse_str($res, $original_array);
// And ensure it matches the orignal array (approximately).
$this->assertSame(
array(
'a' => array(
'b' => array(
'c' => 'd',
),
'e' => array(
'f' => 'g',
),
),
'h' => 'i',
'empty' => '',
'null' => '', // null value gets lost in string translation
'false' => '', // false value gets lost in string translation
),
$original_array
);
}
public function testSigningRequestWithMultiDimensionalParams()
{
$signature = new HmacSha1Signature($this->getMockClientCredentials());
$uri = 'http://www.example.com/';
$parameters = array(
'a' => array(
'b' => array(
'c' => 'd',
),
'e' => array(
'f' => 'g',
),
),
'h' => 'i',
'empty' => '',
'null' => null,
'false' => false,
);
$this->assertEquals('ZUxiJKugeEplaZm9e4hshN0I70U=', $signature->sign($uri, $parameters));
}
protected function invokeQueryStringFromData(array $args)
{
$signature = new HmacSha1Signature(m::mock('League\OAuth1\Client\Credentials\ClientCredentialsInterface'));
$refl = new \ReflectionObject($signature);
$method = $refl->getMethod('queryStringFromData');
$method->setAccessible(true);
return $method->invokeArgs($signature, array($args));
}
protected function getMockClientCredentials()
{
$clientCredentials = m::mock('League\OAuth1\Client\Credentials\ClientCredentialsInterface');
$clientCredentials->shouldReceive('getSecret')->andReturn('clientsecret');
return $clientCredentials;
}
}

View File

@@ -0,0 +1,60 @@
<?php namespace League\OAuth1\Client\Tests;
/**
* Part of the Sentry package.
*
* NOTICE OF LICENSE
*
* Licensed under the 3-clause BSD License.
*
* This source file is subject to the 3-clause BSD License that is
* bundled with this package in the LICENSE file. It is also available at
* the following URL: http://www.opensource.org/licenses/BSD-3-Clause
*
* @package Sentry
* @version 2.0.0
* @author Cartalyst LLC
* @license BSD License (3-clause)
* @copyright (c) 2011 - 2013, Cartalyst LLC
* @link http://cartalyst.com
*/
use League\OAuth1\Client\Signature\PlainTextSignature;
use Mockery as m;
use PHPUnit_Framework_TestCase;
class PlainTextSignatureTest extends PHPUnit_Framework_TestCase
{
/**
* Close mockery.
*
* @return void
*/
public function tearDown()
{
m::close();
}
public function testSigningRequest()
{
$signature = new PlainTextSignature($this->getMockClientCredentials());
$this->assertEquals('clientsecret&', $signature->sign($uri = 'http://www.example.com/'));
$signature->setCredentials($this->getMockCredentials());
$this->assertEquals('clientsecret&tokensecret', $signature->sign($uri));
$this->assertEquals('PLAINTEXT', $signature->method());
}
protected function getMockClientCredentials()
{
$clientCredentials = m::mock('League\OAuth1\Client\Credentials\ClientCredentialsInterface');
$clientCredentials->shouldReceive('getSecret')->andReturn('clientsecret');
return $clientCredentials;
}
protected function getMockCredentials()
{
$credentials = m::mock('League\OAuth1\Client\Credentials\CredentialsInterface');
$credentials->shouldReceive('getSecret')->andReturn('tokensecret');
return $credentials;
}
}

View File

@@ -0,0 +1,285 @@
<?php namespace League\OAuth1\Client\Tests;
/**
* Part of the Sentry package.
*
* NOTICE OF LICENSE
*
* Licensed under the 3-clause BSD License.
*
* This source file is subject to the 3-clause BSD License that is
* bundled with this package in the LICENSE file. It is also available at
* the following URL: http://www.opensource.org/licenses/BSD-3-Clause
*
* @package Sentry
* @version 2.0.0
* @author Cartalyst LLC
* @license BSD License (3-clause)
* @copyright (c) 2011 - 2013, Cartalyst LLC
* @link http://cartalyst.com
*/
use League\OAuth1\Client\Credentials\ClientCredentials;
use Mockery as m;
use PHPUnit_Framework_TestCase;
class ServerTest extends PHPUnit_Framework_TestCase
{
/**
* Setup resources and dependencies.
*
* @return void
*/
public static function setUpBeforeClass()
{
require_once __DIR__.'/stubs/ServerStub.php';
}
/**
* Close mockery.
*
* @return void
*/
public function tearDown()
{
m::close();
}
public function testCreatingWithArray()
{
$server = new ServerStub($this->getMockClientCredentials());
$credentials = $server->getClientCredentials();
$this->assertInstanceOf('League\OAuth1\Client\Credentials\ClientCredentialsInterface', $credentials);
$this->assertEquals('myidentifier', $credentials->getIdentifier());
$this->assertEquals('mysecret', $credentials->getSecret());
$this->assertEquals('http://app.dev/', $credentials->getCallbackUri());
}
public function testCreatingWithObject()
{
$credentials = new ClientCredentials;
$credentials->setIdentifier('myidentifier');
$credentials->setSecret('mysecret');
$credentials->setCallbackUri('http://app.dev/');
$server = new ServerStub($credentials);
$this->assertEquals($credentials, $server->getClientCredentials());
}
/**
* @expectedException InvalidArgumentException
**/
public function testCreatingWithInvalidInput()
{
$server = new ServerStub(uniqid());
}
public function testGettingTemporaryCredentials()
{
$server = m::mock('League\OAuth1\Client\Tests\ServerStub[createHttpClient]', array($this->getMockClientCredentials()));
$server->shouldReceive('createHttpClient')->andReturn($client = m::mock('stdClass'));
$me = $this;
$client->shouldReceive('post')->with('http://www.example.com/temporary', m::on(function($options) use ($me) {
$headers = $options['headers'];
$me->assertTrue(isset($headers['Authorization']));
// OAuth protocol specifies a strict number of
// headers should be sent, in the correct order.
// We'll validate that here.
$pattern = '/OAuth oauth_consumer_key=".*?", oauth_nonce="[a-zA-Z0-9]+", oauth_signature_method="HMAC-SHA1", oauth_timestamp="\d{10}", oauth_version="1.0", oauth_callback="'.preg_quote('http%3A%2F%2Fapp.dev%2F', '/').'", oauth_signature=".*?"/';
$matches = preg_match($pattern, $headers['Authorization']);
$me->assertEquals(1, $matches, 'Asserting that the authorization header contains the correct expression.');
return true;
}))->once()->andReturn($response = m::mock('stdClass'));
$response->shouldReceive('getBody')->andReturn('oauth_token=temporarycredentialsidentifier&oauth_token_secret=temporarycredentialssecret&oauth_callback_confirmed=true');
$credentials = $server->getTemporaryCredentials();
$this->assertInstanceOf('League\OAuth1\Client\Credentials\TemporaryCredentials', $credentials);
$this->assertEquals('temporarycredentialsidentifier', $credentials->getIdentifier());
$this->assertEquals('temporarycredentialssecret', $credentials->getSecret());
}
public function testGettingAuthorizationUrl()
{
$server = new ServerStub($this->getMockClientCredentials());
$expected = 'http://www.example.com/authorize?oauth_token=foo';
$this->assertEquals($expected, $server->getAuthorizationUrl('foo'));
$credentials = m::mock('League\OAuth1\Client\Credentials\TemporaryCredentials');
$credentials->shouldReceive('getIdentifier')->andReturn('foo');
$this->assertEquals($expected, $server->getAuthorizationUrl($credentials));
}
/**
* @expectedException InvalidArgumentException
*/
public function testGettingTokenCredentialsFailsWithManInTheMiddle()
{
$server = new ServerStub($this->getMockClientCredentials());
$credentials = m::mock('League\OAuth1\Client\Credentials\TemporaryCredentials');
$credentials->shouldReceive('getIdentifier')->andReturn('foo');
$server->getTokenCredentials($credentials, 'bar', 'verifier');
}
public function testGettingTokenCredentials()
{
$server = m::mock('League\OAuth1\Client\Tests\ServerStub[createHttpClient]', array($this->getMockClientCredentials()));
$temporaryCredentials = m::mock('League\OAuth1\Client\Credentials\TemporaryCredentials');
$temporaryCredentials->shouldReceive('getIdentifier')->andReturn('temporarycredentialsidentifier');
$temporaryCredentials->shouldReceive('getSecret')->andReturn('temporarycredentialssecret');
$server->shouldReceive('createHttpClient')->andReturn($client = m::mock('stdClass'));
$me = $this;
$client->shouldReceive('post')->with('http://www.example.com/token', m::on(function($options) use ($me) {
$headers = $options['headers'];
$body = $options['form_params'];
$me->assertTrue(isset($headers['Authorization']));
$me->assertFalse(isset($headers['User-Agent']));
// OAuth protocol specifies a strict number of
// headers should be sent, in the correct order.
// We'll validate that here.
$pattern = '/OAuth oauth_consumer_key=".*?", oauth_nonce="[a-zA-Z0-9]+", oauth_signature_method="HMAC-SHA1", oauth_timestamp="\d{10}", oauth_version="1.0", oauth_token="temporarycredentialsidentifier", oauth_signature=".*?"/';
$matches = preg_match($pattern, $headers['Authorization']);
$me->assertEquals(1, $matches, 'Asserting that the authorization header contains the correct expression.');
$me->assertSame($body, array('oauth_verifier' => 'myverifiercode'));
return true;
}))->once()->andReturn($response = m::mock('stdClass'));
$response->shouldReceive('getBody')->andReturn('oauth_token=tokencredentialsidentifier&oauth_token_secret=tokencredentialssecret');
$credentials = $server->getTokenCredentials($temporaryCredentials, 'temporarycredentialsidentifier', 'myverifiercode');
$this->assertInstanceOf('League\OAuth1\Client\Credentials\TokenCredentials', $credentials);
$this->assertEquals('tokencredentialsidentifier', $credentials->getIdentifier());
$this->assertEquals('tokencredentialssecret', $credentials->getSecret());
}
public function testGettingTokenCredentialsWithUserAgent()
{
$userAgent = 'FooBar';
$server = m::mock('League\OAuth1\Client\Tests\ServerStub[createHttpClient]', array($this->getMockClientCredentials()));
$temporaryCredentials = m::mock('League\OAuth1\Client\Credentials\TemporaryCredentials');
$temporaryCredentials->shouldReceive('getIdentifier')->andReturn('temporarycredentialsidentifier');
$temporaryCredentials->shouldReceive('getSecret')->andReturn('temporarycredentialssecret');
$server->shouldReceive('createHttpClient')->andReturn($client = m::mock('stdClass'));
$me = $this;
$client->shouldReceive('post')->with('http://www.example.com/token', m::on(function($options) use ($me, $userAgent) {
$headers = $options['headers'];
$body = $options['form_params'];
$me->assertTrue(isset($headers['Authorization']));
$me->assertTrue(isset($headers['User-Agent']));
$me->assertEquals($userAgent, $headers['User-Agent']);
// OAuth protocol specifies a strict number of
// headers should be sent, in the correct order.
// We'll validate that here.
$pattern = '/OAuth oauth_consumer_key=".*?", oauth_nonce="[a-zA-Z0-9]+", oauth_signature_method="HMAC-SHA1", oauth_timestamp="\d{10}", oauth_version="1.0", oauth_token="temporarycredentialsidentifier", oauth_signature=".*?"/';
$matches = preg_match($pattern, $headers['Authorization']);
$me->assertEquals(1, $matches, 'Asserting that the authorization header contains the correct expression.');
$me->assertSame($body, array('oauth_verifier' => 'myverifiercode'));
return true;
}))->once()->andReturn($response = m::mock('stdClass'));
$response->shouldReceive('getBody')->andReturn('oauth_token=tokencredentialsidentifier&oauth_token_secret=tokencredentialssecret');
$credentials = $server->setUserAgent($userAgent)->getTokenCredentials($temporaryCredentials, 'temporarycredentialsidentifier', 'myverifiercode');
$this->assertInstanceOf('League\OAuth1\Client\Credentials\TokenCredentials', $credentials);
$this->assertEquals('tokencredentialsidentifier', $credentials->getIdentifier());
$this->assertEquals('tokencredentialssecret', $credentials->getSecret());
}
public function testGettingUserDetails()
{
$server = m::mock('League\OAuth1\Client\Tests\ServerStub[createHttpClient,protocolHeader]', array($this->getMockClientCredentials()));
$temporaryCredentials = m::mock('League\OAuth1\Client\Credentials\TokenCredentials');
$temporaryCredentials->shouldReceive('getIdentifier')->andReturn('tokencredentialsidentifier');
$temporaryCredentials->shouldReceive('getSecret')->andReturn('tokencredentialssecret');
$server->shouldReceive('createHttpClient')->andReturn($client = m::mock('stdClass'));
$me = $this;
$client->shouldReceive('get')->with('http://www.example.com/user', m::on(function($options) use ($me) {
$headers = $options['headers'];
$me->assertTrue(isset($headers['Authorization']));
// OAuth protocol specifies a strict number of
// headers should be sent, in the correct order.
// We'll validate that here.
$pattern = '/OAuth oauth_consumer_key=".*?", oauth_nonce="[a-zA-Z0-9]+", oauth_signature_method="HMAC-SHA1", oauth_timestamp="\d{10}", oauth_version="1.0", oauth_token="tokencredentialsidentifier", oauth_signature=".*?"/';
$matches = preg_match($pattern, $headers['Authorization']);
$me->assertEquals(1, $matches, 'Asserting that the authorization header contains the correct expression.');
return true;
}))->once()->andReturn($response = m::mock('stdClass'));
$response->shouldReceive('getBody')->once()->andReturn(json_encode(array('foo' => 'bar', 'id' => 123, 'contact_email' => 'baz@qux.com', 'username' => 'fred')));
$user = $server->getUserDetails($temporaryCredentials);
$this->assertInstanceOf('League\OAuth1\Client\Server\User', $user);
$this->assertEquals('bar', $user->firstName);
$this->assertEquals(123, $server->getUserUid($temporaryCredentials));
$this->assertEquals('baz@qux.com', $server->getUserEmail($temporaryCredentials));
$this->assertEquals('fred', $server->getUserScreenName($temporaryCredentials));
}
public function testGettingHeaders()
{
$server = new ServerStub($this->getMockClientCredentials());
$tokenCredentials = m::mock('League\OAuth1\Client\Credentials\TokenCredentials');
$tokenCredentials->shouldReceive('getIdentifier')->andReturn('mock_identifier');
$tokenCredentials->shouldReceive('getSecret')->andReturn('mock_secret');
// OAuth protocol specifies a strict number of
// headers should be sent, in the correct order.
// We'll validate that here.
$pattern = '/OAuth oauth_consumer_key=".*?", oauth_nonce="[a-zA-Z0-9]+", oauth_signature_method="HMAC-SHA1", oauth_timestamp="\d{10}", oauth_version="1.0", oauth_token="mock_identifier", oauth_signature=".*?"/';
// With a GET request
$headers = $server->getHeaders($tokenCredentials, 'GET', 'http://example.com/');
$this->assertTrue(isset($headers['Authorization']));
$matches = preg_match($pattern, $headers['Authorization']);
$this->assertEquals(1, $matches, 'Asserting that the authorization header contains the correct expression.');
// With a POST request
$headers = $server->getHeaders($tokenCredentials, 'POST', 'http://example.com/', array('body' => 'params'));
$this->assertTrue(isset($headers['Authorization']));
$matches = preg_match($pattern, $headers['Authorization']);
$this->assertEquals(1, $matches, 'Asserting that the authorization header contains the correct expression.');
}
protected function getMockClientCredentials()
{
return array(
'identifier' => 'myidentifier',
'secret' => 'mysecret',
'callback_uri' => 'http://app.dev/',
);
}
}

View File

@@ -0,0 +1,349 @@
<?php namespace League\OAuth1\Client\Tests;
use League\OAuth1\Client\Server\Trello;
use League\OAuth1\Client\Credentials\ClientCredentials;
use Mockery as m;
use PHPUnit_Framework_TestCase;
class TrelloTest extends PHPUnit_Framework_TestCase
{
/**
* Close mockery.
*
* @return void
*/
public function tearDown()
{
m::close();
}
public function testCreatingWithArray()
{
$server = new Trello($this->getMockClientCredentials());
$credentials = $server->getClientCredentials();
$this->assertInstanceOf('League\OAuth1\Client\Credentials\ClientCredentialsInterface', $credentials);
$this->assertEquals($this->getApplicationKey(), $credentials->getIdentifier());
$this->assertEquals('mysecret', $credentials->getSecret());
$this->assertEquals('http://app.dev/', $credentials->getCallbackUri());
}
public function testCreatingWithObject()
{
$credentials = new ClientCredentials;
$credentials->setIdentifier('myidentifier');
$credentials->setSecret('mysecret');
$credentials->setCallbackUri('http://app.dev/');
$server = new Trello($credentials);
$this->assertEquals($credentials, $server->getClientCredentials());
}
public function testGettingTemporaryCredentials()
{
$server = m::mock('League\OAuth1\Client\Server\Trello[createHttpClient]', array($this->getMockClientCredentials()));
$server->shouldReceive('createHttpClient')->andReturn($client = m::mock('stdClass'));
$me = $this;
$client->shouldReceive('post')->with('https://trello.com/1/OAuthGetRequestToken', m::on(function($options) use ($me) {
$headers = $options['headers'];
$me->assertTrue(isset($headers['Authorization']));
// OAuth protocol specifies a strict number of
// headers should be sent, in the correct order.
// We'll validate that here.
$pattern = '/OAuth oauth_consumer_key=".*?", oauth_nonce="[a-zA-Z0-9]+", oauth_signature_method="HMAC-SHA1", oauth_timestamp="\d{10}", oauth_version="1.0", oauth_callback="'.preg_quote('http%3A%2F%2Fapp.dev%2F', '/').'", oauth_signature=".*?"/';
$matches = preg_match($pattern, $headers['Authorization']);
$me->assertEquals(1, $matches, 'Asserting that the authorization header contains the correct expression.');
return true;
}))->once()->andReturn($response = m::mock('stdClass'));
$response->shouldReceive('getBody')->andReturn('oauth_token=temporarycredentialsidentifier&oauth_token_secret=temporarycredentialssecret&oauth_callback_confirmed=true');
$credentials = $server->getTemporaryCredentials();
$this->assertInstanceOf('League\OAuth1\Client\Credentials\TemporaryCredentials', $credentials);
$this->assertEquals('temporarycredentialsidentifier', $credentials->getIdentifier());
$this->assertEquals('temporarycredentialssecret', $credentials->getSecret());
}
public function testGettingDefaultAuthorizationUrl()
{
$server = new Trello($this->getMockClientCredentials());
$expected = 'https://trello.com/1/OAuthAuthorizeToken?response_type=fragment&scope=read&expiration=1day&oauth_token=foo';
$this->assertEquals($expected, $server->getAuthorizationUrl('foo'));
$credentials = m::mock('League\OAuth1\Client\Credentials\TemporaryCredentials');
$credentials->shouldReceive('getIdentifier')->andReturn('foo');
$this->assertEquals($expected, $server->getAuthorizationUrl($credentials));
}
public function testGettingAuthorizationUrlWithExpirationAfterConstructingWithExpiration()
{
$credentials = $this->getMockClientCredentials();
$expiration = $this->getApplicationExpiration(2);
$credentials['expiration'] = $expiration;
$server = new Trello($credentials);
$expected = 'https://trello.com/1/OAuthAuthorizeToken?response_type=fragment&scope=read&expiration='.urlencode($expiration).'&oauth_token=foo';
$this->assertEquals($expected, $server->getAuthorizationUrl('foo'));
$credentials = m::mock('League\OAuth1\Client\Credentials\TemporaryCredentials');
$credentials->shouldReceive('getIdentifier')->andReturn('foo');
$this->assertEquals($expected, $server->getAuthorizationUrl($credentials));
}
public function testGettingAuthorizationUrlWithExpirationAfterSettingExpiration()
{
$expiration = $this->getApplicationExpiration(2);
$server = new Trello($this->getMockClientCredentials());
$server->setApplicationExpiration($expiration);
$expected = 'https://trello.com/1/OAuthAuthorizeToken?response_type=fragment&scope=read&expiration='.urlencode($expiration).'&oauth_token=foo';
$this->assertEquals($expected, $server->getAuthorizationUrl('foo'));
$credentials = m::mock('League\OAuth1\Client\Credentials\TemporaryCredentials');
$credentials->shouldReceive('getIdentifier')->andReturn('foo');
$this->assertEquals($expected, $server->getAuthorizationUrl($credentials));
}
public function testGettingAuthorizationUrlWithNameAfterConstructingWithName()
{
$credentials = $this->getMockClientCredentials();
$name = $this->getApplicationName();
$credentials['name'] = $name;
$server = new Trello($credentials);
$expected = 'https://trello.com/1/OAuthAuthorizeToken?response_type=fragment&scope=read&expiration=1day&name='.urlencode($name).'&oauth_token=foo';
$this->assertEquals($expected, $server->getAuthorizationUrl('foo'));
$credentials = m::mock('League\OAuth1\Client\Credentials\TemporaryCredentials');
$credentials->shouldReceive('getIdentifier')->andReturn('foo');
$this->assertEquals($expected, $server->getAuthorizationUrl($credentials));
}
public function testGettingAuthorizationUrlWithNameAfterSettingName()
{
$name = $this->getApplicationName();
$server = new Trello($this->getMockClientCredentials());
$server->setApplicationName($name);
$expected = 'https://trello.com/1/OAuthAuthorizeToken?response_type=fragment&scope=read&expiration=1day&name='.urlencode($name).'&oauth_token=foo';
$this->assertEquals($expected, $server->getAuthorizationUrl('foo'));
$credentials = m::mock('League\OAuth1\Client\Credentials\TemporaryCredentials');
$credentials->shouldReceive('getIdentifier')->andReturn('foo');
$this->assertEquals($expected, $server->getAuthorizationUrl($credentials));
}
public function testGettingAuthorizationUrlWithScopeAfterConstructingWithScope()
{
$credentials = $this->getMockClientCredentials();
$scope = $this->getApplicationScope(false);
$credentials['scope'] = $scope;
$server = new Trello($credentials);
$expected = 'https://trello.com/1/OAuthAuthorizeToken?response_type=fragment&scope='.urlencode($scope).'&expiration=1day&oauth_token=foo';
$this->assertEquals($expected, $server->getAuthorizationUrl('foo'));
$credentials = m::mock('League\OAuth1\Client\Credentials\TemporaryCredentials');
$credentials->shouldReceive('getIdentifier')->andReturn('foo');
$this->assertEquals($expected, $server->getAuthorizationUrl($credentials));
}
public function testGettingAuthorizationUrlWithScopeAfterSettingScope()
{
$scope = $this->getApplicationScope(false);
$server = new Trello($this->getMockClientCredentials());
$server->setApplicationScope($scope);
$expected = 'https://trello.com/1/OAuthAuthorizeToken?response_type=fragment&scope='.urlencode($scope).'&expiration=1day&oauth_token=foo';
$this->assertEquals($expected, $server->getAuthorizationUrl('foo'));
$credentials = m::mock('League\OAuth1\Client\Credentials\TemporaryCredentials');
$credentials->shouldReceive('getIdentifier')->andReturn('foo');
$this->assertEquals($expected, $server->getAuthorizationUrl($credentials));
}
/**
* @expectedException InvalidArgumentException
*/
public function testGettingTokenCredentialsFailsWithManInTheMiddle()
{
$server = new Trello($this->getMockClientCredentials());
$credentials = m::mock('League\OAuth1\Client\Credentials\TemporaryCredentials');
$credentials->shouldReceive('getIdentifier')->andReturn('foo');
$server->getTokenCredentials($credentials, 'bar', 'verifier');
}
public function testGettingTokenCredentials()
{
$server = m::mock('League\OAuth1\Client\Server\Trello[createHttpClient]', array($this->getMockClientCredentials()));
$temporaryCredentials = m::mock('League\OAuth1\Client\Credentials\TemporaryCredentials');
$temporaryCredentials->shouldReceive('getIdentifier')->andReturn('temporarycredentialsidentifier');
$temporaryCredentials->shouldReceive('getSecret')->andReturn('temporarycredentialssecret');
$server->shouldReceive('createHttpClient')->andReturn($client = m::mock('stdClass'));
$me = $this;
$client->shouldReceive('post')->with('https://trello.com/1/OAuthGetAccessToken', m::on(function($options) use ($me) {
$headers = $options['headers'];
$body = $options['form_params'];
$me->assertTrue(isset($headers['Authorization']));
// OAuth protocol specifies a strict number of
// headers should be sent, in the correct order.
// We'll validate that here.
$pattern = '/OAuth oauth_consumer_key=".*?", oauth_nonce="[a-zA-Z0-9]+", oauth_signature_method="HMAC-SHA1", oauth_timestamp="\d{10}", oauth_version="1.0", oauth_token="temporarycredentialsidentifier", oauth_signature=".*?"/';
$matches = preg_match($pattern, $headers['Authorization']);
$me->assertEquals(1, $matches, 'Asserting that the authorization header contains the correct expression.');
$me->assertSame($body, array('oauth_verifier' => 'myverifiercode'));
return true;
}))->once()->andReturn($response = m::mock('stdClass'));
$response->shouldReceive('getBody')->andReturn('oauth_token=tokencredentialsidentifier&oauth_token_secret=tokencredentialssecret');
$credentials = $server->getTokenCredentials($temporaryCredentials, 'temporarycredentialsidentifier', 'myverifiercode');
$this->assertInstanceOf('League\OAuth1\Client\Credentials\TokenCredentials', $credentials);
$this->assertEquals('tokencredentialsidentifier', $credentials->getIdentifier());
$this->assertEquals('tokencredentialssecret', $credentials->getSecret());
}
public function testGettingUserDetails()
{
$server = m::mock('League\OAuth1\Client\Server\Trello[createHttpClient,protocolHeader]', array($this->getMockClientCredentials()));
$temporaryCredentials = m::mock('League\OAuth1\Client\Credentials\TokenCredentials');
$temporaryCredentials->shouldReceive('getIdentifier')->andReturn('tokencredentialsidentifier');
$temporaryCredentials->shouldReceive('getSecret')->andReturn('tokencredentialssecret');
$server->shouldReceive('createHttpClient')->andReturn($client = m::mock('stdClass'));
$me = $this;
$client->shouldReceive('get')->with('https://trello.com/1/members/me?key='.$this->getApplicationKey().'&token='.$this->getAccessToken(), m::on(function($options) use ($me) {
$headers = $options['headers'];
$me->assertTrue(isset($headers['Authorization']));
// OAuth protocol specifies a strict number of
// headers should be sent, in the correct order.
// We'll validate that here.
$pattern = '/OAuth oauth_consumer_key=".*?", oauth_nonce="[a-zA-Z0-9]+", oauth_signature_method="HMAC-SHA1", oauth_timestamp="\d{10}", oauth_version="1.0", oauth_token="tokencredentialsidentifier", oauth_signature=".*?"/';
$matches = preg_match($pattern, $headers['Authorization']);
$me->assertEquals(1, $matches, 'Asserting that the authorization header contains the correct expression.');
return true;
}))->once()->andReturn($response = m::mock('stdClass'));
$response->shouldReceive('getBody')->once()->andReturn($this->getUserPayload());
$user = $server
->setAccessToken($this->getAccessToken())
->getUserDetails($temporaryCredentials);
$this->assertInstanceOf('League\OAuth1\Client\Server\User', $user);
$this->assertEquals('Matilda Wormwood', $user->name);
$this->assertEquals('545df696e29c0dddaed31967', $server->getUserUid($temporaryCredentials));
$this->assertEquals(null, $server->getUserEmail($temporaryCredentials));
$this->assertEquals('matildawormwood12', $server->getUserScreenName($temporaryCredentials));
}
protected function getMockClientCredentials()
{
return array(
'identifier' => $this->getApplicationKey(),
'secret' => 'mysecret',
'callback_uri' => 'http://app.dev/',
);
}
protected function getAccessToken()
{
return 'lmnopqrstuvwxyz';
}
protected function getApplicationKey()
{
return 'abcdefghijk';
}
protected function getApplicationExpiration($days = 0)
{
return is_numeric($days) && $days > 0 ? $days.'day'.($days == 1 ? '' : 's') : 'never';
}
protected function getApplicationName()
{
return 'fizz buzz';
}
protected function getApplicationScope($readonly = true)
{
return $readonly ? 'read' : 'read,write';
}
private function getUserPayload()
{
return '{
"id": "545df696e29c0dddaed31967",
"avatarHash": null,
"bio": "I have magical powers",
"bioData": null,
"confirmed": true,
"fullName": "Matilda Wormwood",
"idPremOrgsAdmin": [],
"initials": "MW",
"memberType": "normal",
"products": [],
"status": "idle",
"url": "https://trello.com/matildawormwood12",
"username": "matildawormwood12",
"avatarSource": "none",
"email": null,
"gravatarHash": "39aaaada0224f26f0bb8f1965326dcb7",
"idBoards": [
"545df696e29c0dddaed31968",
"545e01d6c7b2dd962b5b46cb"
],
"idOrganizations": [
"54adfd79f9aea14f84009a85",
"54adfde13b0e706947bc4789"
],
"loginTypes": null,
"oneTimeMessagesDismissed": [],
"prefs": {
"sendSummaries": true,
"minutesBetweenSummaries": 1,
"minutesBeforeDeadlineToNotify": 1440,
"colorBlind": false,
"timezoneInfo": {
"timezoneNext": "CDT",
"dateNext": "2015-03-08T08:00:00.000Z",
"offsetNext": 300,
"timezoneCurrent": "CST",
"offsetCurrent": 360
}
},
"trophies": [],
"uploadedAvatarHash": null,
"premiumFeatures": [],
"idBoardsPinned": null
}';
}
}

View File

@@ -0,0 +1,255 @@
<?php namespace League\OAuth1\Client\Tests;
use League\OAuth1\Client\Server\Xing;
use League\OAuth1\Client\Credentials\ClientCredentials;
use Mockery as m;
use PHPUnit_Framework_TestCase;
class XingTest extends PHPUnit_Framework_TestCase
{
/**
* Close mockery.
*
* @return void
*/
public function tearDown()
{
m::close();
}
public function testCreatingWithArray()
{
$server = new Xing($this->getMockClientCredentials());
$credentials = $server->getClientCredentials();
$this->assertInstanceOf('League\OAuth1\Client\Credentials\ClientCredentialsInterface', $credentials);
$this->assertEquals($this->getApplicationKey(), $credentials->getIdentifier());
$this->assertEquals('mysecret', $credentials->getSecret());
$this->assertEquals('http://app.dev/', $credentials->getCallbackUri());
}
public function testCreatingWithObject()
{
$credentials = new ClientCredentials;
$credentials->setIdentifier('myidentifier');
$credentials->setSecret('mysecret');
$credentials->setCallbackUri('http://app.dev/');
$server = new Xing($credentials);
$this->assertEquals($credentials, $server->getClientCredentials());
}
public function testGettingTemporaryCredentials()
{
$server = m::mock('League\OAuth1\Client\Server\Xing[createHttpClient]', array($this->getMockClientCredentials()));
$server->shouldReceive('createHttpClient')->andReturn($client = m::mock('stdClass'));
$me = $this;
$client->shouldReceive('post')->with('https://api.xing.com/v1/request_token', m::on(function ($options) use ($me) {
$headers = $options['headers'];
$me->assertTrue(isset($headers['Authorization']));
// OAuth protocol specifies a strict number of
// headers should be sent, in the correct order.
// We'll validate that here.
$pattern = '/OAuth oauth_consumer_key=".*?", oauth_nonce="[a-zA-Z0-9]+", oauth_signature_method="HMAC-SHA1", oauth_timestamp="\d{10}", oauth_version="1.0", oauth_callback="'.preg_quote('http%3A%2F%2Fapp.dev%2F', '/').'", oauth_signature=".*?"/';
$matches = preg_match($pattern, $headers['Authorization']);
$me->assertEquals(1, $matches, 'Asserting that the authorization header contains the correct expression.');
return true;
}))->once()->andReturn($response = m::mock('stdClass'));
$response->shouldReceive('getBody')->andReturn('oauth_token=temporarycredentialsidentifier&oauth_token_secret=temporarycredentialssecret&oauth_callback_confirmed=true');
$credentials = $server->getTemporaryCredentials();
$this->assertInstanceOf('League\OAuth1\Client\Credentials\TemporaryCredentials', $credentials);
$this->assertEquals('temporarycredentialsidentifier', $credentials->getIdentifier());
$this->assertEquals('temporarycredentialssecret', $credentials->getSecret());
}
public function testGettingDefaultAuthorizationUrl()
{
$server = new Xing($this->getMockClientCredentials());
$expected = 'https://api.xing.com/v1/authorize?oauth_token=foo';
$this->assertEquals($expected, $server->getAuthorizationUrl('foo'));
$credentials = m::mock('League\OAuth1\Client\Credentials\TemporaryCredentials');
$credentials->shouldReceive('getIdentifier')->andReturn('foo');
$this->assertEquals($expected, $server->getAuthorizationUrl($credentials));
}
/**
* @expectedException InvalidArgumentException
*/
public function testGettingTokenCredentialsFailsWithManInTheMiddle()
{
$server = new Xing($this->getMockClientCredentials());
$credentials = m::mock('League\OAuth1\Client\Credentials\TemporaryCredentials');
$credentials->shouldReceive('getIdentifier')->andReturn('foo');
$server->getTokenCredentials($credentials, 'bar', 'verifier');
}
public function testGettingTokenCredentials()
{
$server = m::mock('League\OAuth1\Client\Server\Xing[createHttpClient]', array($this->getMockClientCredentials()));
$temporaryCredentials = m::mock('League\OAuth1\Client\Credentials\TemporaryCredentials');
$temporaryCredentials->shouldReceive('getIdentifier')->andReturn('temporarycredentialsidentifier');
$temporaryCredentials->shouldReceive('getSecret')->andReturn('temporarycredentialssecret');
$server->shouldReceive('createHttpClient')->andReturn($client = m::mock('stdClass'));
$me = $this;
$client->shouldReceive('post')->with('https://api.xing.com/v1/access_token', m::on(function ($options) use ($me) {
$headers = $options['headers'];
$body = $options['form_params'];
$me->assertTrue(isset($headers['Authorization']));
// OAuth protocol specifies a strict number of
// headers should be sent, in the correct order.
// We'll validate that here.
$pattern = '/OAuth oauth_consumer_key=".*?", oauth_nonce="[a-zA-Z0-9]+", oauth_signature_method="HMAC-SHA1", oauth_timestamp="\d{10}", oauth_version="1.0", oauth_token="temporarycredentialsidentifier", oauth_signature=".*?"/';
$matches = preg_match($pattern, $headers['Authorization']);
$me->assertEquals(1, $matches, 'Asserting that the authorization header contains the correct expression.');
$me->assertSame($body, array('oauth_verifier' => 'myverifiercode'));
return true;
}))->once()->andReturn($response = m::mock('stdClass'));
$response->shouldReceive('getBody')->andReturn('oauth_token=tokencredentialsidentifier&oauth_token_secret=tokencredentialssecret');
$credentials = $server->getTokenCredentials($temporaryCredentials, 'temporarycredentialsidentifier', 'myverifiercode');
$this->assertInstanceOf('League\OAuth1\Client\Credentials\TokenCredentials', $credentials);
$this->assertEquals('tokencredentialsidentifier', $credentials->getIdentifier());
$this->assertEquals('tokencredentialssecret', $credentials->getSecret());
}
public function testGettingUserDetails()
{
$server = m::mock('League\OAuth1\Client\Server\Xing[createHttpClient,protocolHeader]', array($this->getMockClientCredentials()));
$temporaryCredentials = m::mock('League\OAuth1\Client\Credentials\TokenCredentials');
$temporaryCredentials->shouldReceive('getIdentifier')->andReturn('tokencredentialsidentifier');
$temporaryCredentials->shouldReceive('getSecret')->andReturn('tokencredentialssecret');
$server->shouldReceive('createHttpClient')->andReturn($client = m::mock('stdClass'));
$me = $this;
$client->shouldReceive('get')->with('https://api.xing.com/v1/users/me', m::on(function ($options) use ($me) {
$headers = $options['headers'];
$me->assertTrue(isset($headers['Authorization']));
// OAuth protocol specifies a strict number of
// headers should be sent, in the correct order.
// We'll validate that here.
$pattern = '/OAuth oauth_consumer_key=".*?", oauth_nonce="[a-zA-Z0-9]+", oauth_signature_method="HMAC-SHA1", oauth_timestamp="\d{10}", oauth_version="1.0", oauth_token="tokencredentialsidentifier", oauth_signature=".*?"/';
$matches = preg_match($pattern, $headers['Authorization']);
$me->assertEquals(1, $matches, 'Asserting that the authorization header contains the correct expression.');
return true;
}))->once()->andReturn($response = m::mock('stdClass'));
$response->shouldReceive('getBody')->once()->andReturn($this->getUserPayload());
$user = $server->getUserDetails($temporaryCredentials);
$this->assertInstanceOf('League\OAuth1\Client\Server\User', $user);
$this->assertEquals('Roman Gelembjuk', $user->name);
$this->assertEquals('17144430_0f9409', $server->getUserUid($temporaryCredentials));
$this->assertEquals('XXXXXXXXXX@gmail.com', $server->getUserEmail($temporaryCredentials));
$this->assertEquals('Roman Gelembjuk', $server->getUserScreenName($temporaryCredentials));
}
protected function getMockClientCredentials()
{
return array(
'identifier' => $this->getApplicationKey(),
'secret' => 'mysecret',
'callback_uri' => 'http://app.dev/',
);
}
protected function getApplicationKey()
{
return 'abcdefghijk';
}
protected function getApplicationExpiration($days = 0)
{
return is_numeric($days) && $days > 0 ? $days.'day'.($days == 1 ? '' : 's') : 'never';
}
protected function getApplicationName()
{
return 'fizz buzz';
}
private function getUserPayload()
{
return '{
"users":[
{
"id":"17144430_0f9409",
"active_email":"XXXXXXXXXX@gmail.com",
"time_zone":
{
"utc_offset":3.0,
"name":"Europe/Kiev"
},
"display_name":"Roman Gelembjuk",
"first_name":"Roman",
"last_name":"Gelembjuk",
"gender":"m",
"page_name":"Roman_Gelembjuk",
"birth_date":
{"year":null,"month":null,"day":null},
"wants":null,
"haves":null,
"interests":null,
"web_profiles":{},
"badges":[],
"photo_urls":
{
"large":"https://x1.xingassets.com/assets/frontend_minified/img/users/nobody_m.140x185.jpg",
"maxi_thumb":"https://x1.xingassets.com/assets/frontend_minified/img/users/nobody_m.70x93.jpg",
"medium_thumb":"https://x1.xingassets.com/assets/frontend_minified/img/users/nobody_m.57x75.jpg"
},
"permalink":"https://www.xing.com/profile/Roman_Gelembjuk",
"languages":{"en":null},
"employment_status":"EMPLOYEE",
"organisation_member":null,
"instant_messaging_accounts":{},
"educational_background":
{"degree":null,"primary_school":null,"schools":[],"qualifications":[]},
"private_address":{
"street":null,
"zip_code":null,
"city":null,
"province":null,
"country":null,
"email":"XXXXXXXX@gmail.com",
"fax":null,
"phone":null,
"mobile_phone":null}
,"business_address":
{
"street":null,
"zip_code":null,
"city":"Ivano-Frankivsk",
"province":null,
"country":"UA",
"email":null,
"fax":null,"phone":null,"mobile_phone":null
},
"premium_services":[]
}]}';
}
}

View File

@@ -0,0 +1,76 @@
<?php
namespace League\OAuth1\Client\Tests;
use League\OAuth1\Client\Credentials\TokenCredentials;
use League\OAuth1\Client\Server\Server;
use League\OAuth1\Client\Server\User;
class ServerStub extends Server
{
/**
* {@inheritDoc}
*/
public function urlTemporaryCredentials()
{
return 'http://www.example.com/temporary';
}
/**
* {@inheritDoc}
*/
public function urlAuthorization()
{
return 'http://www.example.com/authorize';
}
/**
* {@inheritDoc}
*/
public function urlTokenCredentials()
{
return 'http://www.example.com/token';
}
/**
* {@inheritDoc}
*/
public function urlUserDetails()
{
return 'http://www.example.com/user';
}
/**
* {@inheritDoc}
*/
public function userDetails($data, TokenCredentials $tokenCredentials)
{
$user = new User;
$user->firstName = $data['foo'];
return $user;
}
/**
* {@inheritDoc}
*/
public function userUid($data, TokenCredentials $tokenCredentials)
{
return isset($data['id']) ? $data['id'] : null;
}
/**
* {@inheritDoc}
*/
public function userEmail($data, TokenCredentials $tokenCredentials)
{
return isset($data['contact_email']) ? $data['contact_email'] : null;
}
/**
* {@inheritDoc}
*/
public function userScreenName($data, TokenCredentials $tokenCredentials)
{
return isset($data['username']) ? $data['username'] : null;
}
}