OAuth1Test.php 4.08 KB
Newer Older
1 2 3 4
<?php

namespace yiiunit\extensions\authclient\oauth;

5 6 7
use yii\authclient\OAuth1;
use yii\authclient\signature\PlainText;
use yii\authclient\OAuthToken;
8 9
use yiiunit\extensions\authclient\TestCase;

10
class OAuth1Test extends TestCase
11
{
12 13 14 15 16 17 18 19 20 21 22 23
    protected function setUp()
    {
        $config = [
            'components' => [
                'request' => [
                    'hostInfo' => 'http://testdomain.com',
                    'scriptUrl' => '/index.php',
                ],
            ]
        ];
        $this->mockApplication($config, '\yii\web\Application');
    }
24

25 26 27 28 29 30 31 32 33 34 35 36 37 38
    /**
     * Invokes the OAuth client method even if it is protected.
     * @param  OAuth1 $oauthClient OAuth client instance.
     * @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);
39

40 41
        return $result;
    }
42

43
    // Tests :
44

45 46 47
    public function testSignRequest()
    {
        $oauthClient = new OAuth1();
48

49 50
        $oauthSignatureMethod = new PlainText();
        $oauthClient->setSignatureMethod($oauthSignatureMethod);
51

52 53 54
        $signedParams = $this->invokeOAuthClientMethod($oauthClient, 'signRequest', ['GET', 'http://test.url', []]);
        $this->assertNotEmpty($signedParams['oauth_signature'], 'Unable to sign request!');
    }
55

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    /**
     * 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"'
            ],
        ];
    }
89

90 91 92 93 94 95 96 97 98 99 100 101 102
    /**
     * @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)
    {
        $oauthClient = new OAuth1();
        $authorizationHeader = $this->invokeOAuthClientMethod($oauthClient, 'composeAuthorizationHeader', [$params, $realm]);
        $this->assertEquals($expectedAuthorizationHeader, $authorizationHeader);
    }
103

104 105 106 107 108
    public function testBuildAuthUrl()
    {
        $oauthClient = new OAuth1();
        $authUrl = 'http://test.auth.url';
        $oauthClient->authUrl = $authUrl;
109

110 111 112
        $requestTokenToken = 'test_request_token';
        $requestToken = new OAuthToken();
        $requestToken->setToken($requestTokenToken);
113

114 115 116 117 118
        $builtAuthUrl = $oauthClient->buildAuthUrl($requestToken);

        $this->assertContains($authUrl, $builtAuthUrl, 'No auth URL present!');
        $this->assertContains($requestTokenToken, $builtAuthUrl, 'No token present!');
    }
Qiang Xue committed
119
}