Viewing File: /usr/local/cpanel/whostmgr/docroot/cgi/ncssl/source/vendor/riskio/oauth2-auth0/tests/Auth0Test.php

<?php
namespace Riskio\OAuth2\Client\Test\Provider;

use League\OAuth2\Client\Token\AccessToken;
use PHPUnit\Framework\TestCase;
use Riskio\OAuth2\Client\Provider\Auth0 as OauthProvider;
use Riskio\OAuth2\Client\Provider\Exception\AccountNotProvidedException;
use Riskio\OAuth2\Client\Provider\Exception\InvalidRegionException;
use RuntimeException;

class Auth0Test extends TestCase
{
    const DEFAULT_ACCOUNT = 'mock_account';

    protected $config = [
        'account'      => self::DEFAULT_ACCOUNT,
        'clientId'     => 'mock_client_id',
        'clientSecret' => 'mock_secret',
        'redirectUri'  => 'none',
    ];

    /**
     * @dataProvider regionDataProvider
     */
    public function testGetAuthorizationUrl($region, $expectedHost)
    {
        $provider = new OauthProvider(array_merge($this->config, ['region' => $region]));
        $url = $provider->getAuthorizationUrl();
        $parsedUrl = parse_url($url);

        $this->assertEquals($expectedHost, $parsedUrl['host']);
        $this->assertEquals('/authorize', $parsedUrl['path']);
    }

    public function testGetAuthorizationUrlWhenAccountIsNotSpecifiedShouldThrowException()
    {
        unset($this->config['account']);

        $provider = new OauthProvider($this->config);

        $this->expectException(RuntimeException::class);
        $provider->getAuthorizationUrl();
    }

    /**
     * @dataProvider regionDataProvider
     */
    public function testGetUrlAccessToken($region, $expectedHost)
    {
        $provider = new OauthProvider(array_merge($this->config, ['region' => $region]));
        $url = $provider->getBaseAccessTokenUrl();
        $parsedUrl = parse_url($url);

        $this->assertEquals($expectedHost, $parsedUrl['host']);
        $this->assertEquals('/oauth/token', $parsedUrl['path']);
    }

    public function testGetAccessTokenUrlWhenAccountIsNotSpecifiedShouldThrowException()
    {
        unset($this->config['account']);

        $provider = new OauthProvider($this->config);

        $this->expectException(RuntimeException::class);
        $provider->getBaseAccessTokenUrl();
    }

    /**
     * @dataProvider regionDataProvider
     */
    public function testGetUrlUserDetails($region, $expectedHost)
    {
        $provider = new OauthProvider(array_merge($this->config, ['region' => $region]));

        $accessTokenDummy = $this->getAccessToken();

        $url = $provider->getResourceOwnerDetailsUrl($accessTokenDummy);
        $parsedUrl = parse_url($url);

        $this->assertEquals($expectedHost, $parsedUrl['host']);
        $this->assertEquals('/userinfo', $parsedUrl['path']);
    }

    public function testGetUserDetailsUrlWhenAccountIsNotSpecifiedShouldThrowException()
    {
        unset($this->config['account']);

        $provider = new OauthProvider($this->config);

        $accessTokenDummy = $this->getAccessToken();

        $this->expectException(AccountNotProvidedException::class);

        $provider->getResourceOwner($accessTokenDummy);
    }

    public function testGetUserDetailsUrlWhenInvalidRegionIsProvidedShouldThrowException()
    {
        $this->config['region'] = 'invalid_region';

        $provider = new OauthProvider($this->config);

        $accessTokenDummy = $this->getAccessToken();

        $this->expectException(InvalidRegionException::class);

        $provider->getResourceOwner($accessTokenDummy);
    }

    public function regionDataProvider()
    {
        return [
            [
                OauthProvider::REGION_US,
                sprintf('%s.auth0.com', self::DEFAULT_ACCOUNT),
            ],
            [
                OauthProvider::REGION_EU,
                sprintf('%s.%s.auth0.com', self::DEFAULT_ACCOUNT, OauthProvider::REGION_EU),
            ],
            [
                OauthProvider::REGION_AU,
                sprintf('%s.%s.auth0.com', self::DEFAULT_ACCOUNT, OauthProvider::REGION_AU),
            ],
        ];
    }

    /**
     * @return \PHPUnit\Framework\MockObject\MockObject|AccessToken
     */
    private function getAccessToken()
    {
        return $this->getMockBuilder(AccessToken::class)
            ->disableOriginalConstructor()
            ->getMock();
    }

    /**
     * @dataProvider scopeDataProvider
     */
    public function testGetAuthorizationUrlWithScopes($scopes, $expectedScopeParameter)
    {
        $provider = new OauthProvider($this->config);

        $url = $provider->getAuthorizationUrl(['scope' => $scopes]);
        $queryString = parse_url($url, PHP_URL_QUERY);

        parse_str($queryString, $queryParameters);

        $this->assertArrayHasKey('scope', $queryParameters);
        $this->assertSame($expectedScopeParameter, $queryParameters['scope']);
    }

    public function scopeDataProvider()
    {
        return [
            [['openid'], 'openid'],
            [['openid', 'email'], 'openid email'],
        ];
    }

    public function testGetAuthorizationUrlWithCustomDomain()
    {
        $customDomain = 'login.custom-domain.tld';
        $provider = new OauthProvider(array_merge($this->config, ['customDomain' => $customDomain]));
        $url = $provider->getAuthorizationUrl();
        $expectedBaseUrl = 'https://' . $customDomain;

        $this->assertStringStartsWith($expectedBaseUrl, $url);
    }

    /**
     * Test that URL getters work as expected with custom domain set, and account not set.
     * They should not throw AccountNotProvidedException (or any exception),
     * and have to return an url starting with the custom domain.
     */
    public function testCustomDomain()
    {
        $customDomain = 'login.custom-domain.tld';
        $this->config['customDomain'] = $customDomain;
        unset($this->config['account']);
        $expectedBaseUrl = 'https://' . $customDomain;

        $provider = new OauthProvider($this->config);
        $accessTokenDummy = $this->getAccessToken();

        $url = $provider->getBaseAuthorizationUrl();
        $this->assertStringStartsWith($expectedBaseUrl, $url);

        $url = $provider->getBaseAccessTokenUrl();
        $this->assertStringStartsWith($expectedBaseUrl, $url);

        $url = $provider->getResourceOwnerDetailsUrl($accessTokenDummy);
        $this->assertStringStartsWith($expectedBaseUrl, $url);
    }
}
Back to Directory File Manager