You are here

public function Auth0Test::testThatRenewTokensSucceeds in Auth0 Single Sign On 8.2

Test that renewTokens succeeds with non-empty access_token and refresh_token stored.

Throws

ApiException Should not be thrown in this test.

CoreException Should not be thrown in this test.

File

vendor/auth0/auth0-php/tests/Auth0Test.php, line 269

Class

Auth0Test
Class Auth0Test

Namespace

Auth0\Tests

Code

public function testThatRenewTokensSucceeds() {
  $id_token = JWT::encode([
    'sub' => uniqid(),
  ], '__test_client_secret__');
  $request_history = [];
  $mock = new MockHandler([
    // Code exchange response.
    new Response(200, self::$headers, '{"access_token":"1.2.3","refresh_token":"2.3.4"}'),
    // Refresh token response.
    new Response(200, self::$headers, '{"access_token":"__test_access_token__","id_token":"' . $id_token . '"}'),
  ]);
  $handler = HandlerStack::create($mock);
  $handler
    ->push(Middleware::history($request_history));
  $add_config = [
    'skip_userinfo' => true,
    'persist_access_token' => true,
    'guzzle_options' => [
      'handler' => $handler,
    ],
  ];
  $auth0 = new Auth0(self::$baseConfig + $add_config);
  $_GET['code'] = uniqid();
  $this
    ->assertTrue($auth0
    ->exchange());
  $auth0
    ->renewTokens([
    'scope' => 'openid',
  ]);
  $this
    ->assertEquals('__test_access_token__', $auth0
    ->getAccessToken());
  $this
    ->assertEquals($id_token, $auth0
    ->getIdToken());
  $renew_request = $request_history[1]['request'];
  $renew_body = json_decode($renew_request
    ->getBody(), true);
  $this
    ->assertEquals('openid', $renew_body['scope']);
  $this
    ->assertEquals('__test_client_secret__', $renew_body['client_secret']);
  $this
    ->assertEquals('__test_client_id__', $renew_body['client_id']);
  $this
    ->assertEquals('2.3.4', $renew_body['refresh_token']);
  $this
    ->assertEquals('https://__test_domain__/oauth/token', (string) $renew_request
    ->getUri());
}