Skip to content

Commit

Permalink
test: update OAuth2 tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanorosanelli committed Mar 20, 2024
1 parent d938763 commit e555107
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions tests/TestCase/Authenticator/OAuth2AuthenticatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Cake\Http\Session;
use Cake\TestSuite\TestCase;
use Cake\Utility\Hash;
use Firebase\JWT\JWT;

/**
* {@see \BEdita\WebTools\Authenticator\OAuth2Authenticator} Test Case
Expand Down Expand Up @@ -50,6 +51,7 @@ public function authenticateProvider(): array
'status' => Result::SUCCESS,
],
[
'environment' => ['REQUEST_METHOD' => 'POST'],
'url' => '/ext/login/gustavo',
],
[
Expand Down Expand Up @@ -186,4 +188,52 @@ public function getErrors(): array
static::assertNotNull($result);
static::assertEquals($expected['status'], $result->getStatus());
}

/**
* Test JWT leeway config in `authenticate` method
*
* @return void
* @covers ::authenticate()
*/
public function testAuthenticateLeeway(): void
{
$identifier = new class () implements IdentifierInterface {
public function identify(array $credentials)
{
return $credentials;
}

public function getErrors(): array
{
return [];
}
};
$reqConfig = [
'url' => '/ext/login/gustavo',
];
$request = new ServerRequest($reqConfig);
$session = new Session();
$session->write(Hash::get($reqConfig, 'data'));
$request = $request->withAttribute('session', $session);

$authenticator = new OAuth2Authenticator($identifier, [
'urlResolver' => fn () => '',
'providers' => [
'gustavo' => [
'class' => TestProvider::class,
'setup' => [
'clientId' => '',
],
'clientOptions' => [
'jwtLeeway' => 10,
],
],
],
]);
$result = $authenticator->authenticate($request);

static::assertNotNull($result);
static::assertEquals(Result::SUCCESS, $result->getStatus());
static::assertEquals(JWT::$leeway, 10);
}
}

0 comments on commit e555107

Please sign in to comment.