You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					109 lines
				
				3.2 KiB
			
		
		
			
		
	
	
					109 lines
				
				3.2 KiB
			| 
								 
											12 years ago
										 
									 | 
							
								<?php
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								namespace yiiunit\extensions\authclient\oauth;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
								use yii\authclient\OAuth1;
							 | 
						||
| 
								 | 
							
								use yii\authclient\signature\PlainText;
							 | 
						||
| 
								 | 
							
								use yii\authclient\OAuthToken;
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
								use yiiunit\extensions\authclient\TestCase;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
								class OAuth1Test extends TestCase
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
								{
							 | 
						||
| 
								 | 
							
									protected function setUp()
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 | 
							
										$this->mockApplication([], '\yii\web\Application');
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 | 
							
									 * Invokes the OAuth client method even if it is protected.
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									 * @param OAuth1 $oauthClient OAuth client instance.
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									 * @param string $methodName name of the method to be invoked.
							 | 
						||
| 
								 | 
							
									 * @param array $arguments method arguments.
							 | 
						||
| 
								 | 
							
									 * @return mixed method invoke result.
							 | 
						||
| 
								 | 
							
									 */
							 | 
						||
| 
								 | 
							
									protected function invokeOAuthClientMethod($oauthClient, $methodName, array $arguments = [])
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 | 
							
										$classReflection = new \ReflectionClass(get_class($oauthClient));
							 | 
						||
| 
								 | 
							
										$methodReflection = $classReflection->getMethod($methodName);
							 | 
						||
| 
								 | 
							
										$methodReflection->setAccessible(true);
							 | 
						||
| 
								 | 
							
										$result = $methodReflection->invokeArgs($oauthClient, $arguments);
							 | 
						||
| 
								 | 
							
										$methodReflection->setAccessible(false);
							 | 
						||
| 
								 | 
							
										return $result;
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									// Tests :
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									public function testSignRequest()
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										$oauthClient = new OAuth1();
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
										$oauthSignatureMethod = new PlainText();
							 | 
						||
| 
								 | 
							
										$oauthClient->setSignatureMethod($oauthSignatureMethod);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
										$signedParams = $this->invokeOAuthClientMethod($oauthClient, 'signRequest', ['GET', 'http://test.url', []]);
							 | 
						||
| 
								 | 
							
										$this->assertNotEmpty($signedParams['oauth_signature'], 'Unable to sign request!');
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 | 
							
									 * Data provider for [[testComposeAuthorizationHeader()]].
							 | 
						||
| 
								 | 
							
									 * @return array test data.
							 | 
						||
| 
								 | 
							
									 */
							 | 
						||
| 
								 | 
							
									public function composeAuthorizationHeaderDataProvider()
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 | 
							
										return [
							 | 
						||
| 
								 | 
							
											[
							 | 
						||
| 
								 | 
							
												'',
							 | 
						||
| 
								 | 
							
												[
							 | 
						||
| 
								 | 
							
													'oauth_test_name_1' => 'oauth_test_value_1',
							 | 
						||
| 
								 | 
							
													'oauth_test_name_2' => 'oauth_test_value_2',
							 | 
						||
| 
								 | 
							
												],
							 | 
						||
| 
								 | 
							
												'Authorization: OAuth oauth_test_name_1="oauth_test_value_1", oauth_test_name_2="oauth_test_value_2"'
							 | 
						||
| 
								 | 
							
											],
							 | 
						||
| 
								 | 
							
											[
							 | 
						||
| 
								 | 
							
												'test_realm',
							 | 
						||
| 
								 | 
							
												[
							 | 
						||
| 
								 | 
							
													'oauth_test_name_1' => 'oauth_test_value_1',
							 | 
						||
| 
								 | 
							
													'oauth_test_name_2' => 'oauth_test_value_2',
							 | 
						||
| 
								 | 
							
												],
							 | 
						||
| 
								 | 
							
												'Authorization: OAuth realm="test_realm", oauth_test_name_1="oauth_test_value_1", oauth_test_name_2="oauth_test_value_2"'
							 | 
						||
| 
								 | 
							
											],
							 | 
						||
| 
								 | 
							
											[
							 | 
						||
| 
								 | 
							
												'',
							 | 
						||
| 
								 | 
							
												[
							 | 
						||
| 
								 | 
							
													'oauth_test_name_1' => 'oauth_test_value_1',
							 | 
						||
| 
								 | 
							
													'test_name_2' => 'test_value_2',
							 | 
						||
| 
								 | 
							
												],
							 | 
						||
| 
								 | 
							
												'Authorization: OAuth oauth_test_name_1="oauth_test_value_1"'
							 | 
						||
| 
								 | 
							
											],
							 | 
						||
| 
								 | 
							
										];
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 | 
							
									 * @dataProvider composeAuthorizationHeaderDataProvider
							 | 
						||
| 
								 | 
							
									 *
							 | 
						||
| 
								 | 
							
									 * @param string $realm authorization realm.
							 | 
						||
| 
								 | 
							
									 * @param array $params request params.
							 | 
						||
| 
								 | 
							
									 * @param string $expectedAuthorizationHeader expected authorization header.
							 | 
						||
| 
								 | 
							
									 */
							 | 
						||
| 
								 | 
							
									public function testComposeAuthorizationHeader($realm, array $params, $expectedAuthorizationHeader)
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										$oauthClient = new OAuth1();
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										$authorizationHeader = $this->invokeOAuthClientMethod($oauthClient, 'composeAuthorizationHeader', [$params, $realm]);
							 | 
						||
| 
								 | 
							
										$this->assertEquals($expectedAuthorizationHeader, $authorizationHeader);
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									public function testBuildAuthUrl() {
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										$oauthClient = new OAuth1();
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										$authUrl = 'http://test.auth.url';
							 | 
						||
| 
								 | 
							
										$oauthClient->authUrl = $authUrl;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
										$requestTokenToken = 'test_request_token';
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										$requestToken = new OAuthToken();
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										$requestToken->setToken($requestTokenToken);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
										$builtAuthUrl = $oauthClient->buildAuthUrl($requestToken);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
										$this->assertContains($authUrl, $builtAuthUrl, 'No auth URL present!');
							 | 
						||
| 
								 | 
							
										$this->assertContains($requestTokenToken, $builtAuthUrl, 'No token present!');
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								}
							 |