You are here

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

Test that renewTokens fails if the API response is invalid.

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 218

Class

Auth0Test
Class Auth0Test

Namespace

Auth0\Tests

Code

public function testThatRenewTokensFailsIfNoAccessOrIdTokenReturned() {
  $mock = new MockHandler([
    // Code exchange response.
    new Response(200, self::$headers, '{"access_token":"1.2.3","refresh_token":"2.3.4"}'),
    // Refresh token response without ID token.
    new Response(200, self::$headers, '{"access_token":"1.2.3"}'),
    // Refresh token response without access token.
    new Response(200, self::$headers, '{"id_token":"1.2.3"}'),
  ]);
  $add_config = [
    'skip_userinfo' => true,
    'persist_access_token' => true,
    'guzzle_options' => [
      'handler' => HandlerStack::create($mock),
    ],
  ];
  $auth0 = new Auth0(self::$baseConfig + $add_config);
  $_GET['code'] = uniqid();
  $this
    ->assertTrue($auth0
    ->exchange());
  try {
    $caught_exception = false;
    $auth0
      ->renewTokens();
  } catch (ApiException $e) {
    $caught_exception = $this
      ->errorHasString($e, 'Token did not refresh correctly. Access or ID token not provided');
  }
  $this
    ->assertTrue($caught_exception);

  // Run the same method again to get next queued response without an access token.
  try {
    $caught_exception = false;
    $auth0
      ->renewTokens();
  } catch (ApiException $e) {
    $caught_exception = $this
      ->errorHasString($e, 'Token did not refresh correctly. Access or ID token not provided');
  }
  $this
    ->assertTrue($caught_exception);
}