Complete OpenID provider tests

This commit is contained in:
Bubka 2024-09-25 17:58:23 +02:00
parent 922902c934
commit 306ff57616
2 changed files with 58 additions and 8 deletions

View File

@ -14,15 +14,15 @@ class OpenIdProviderStub extends OpenId
*/
public $http;
protected function getAuthUrl($state)
{
return $this->buildAuthUrlFromBase('http://auth.url', $state);
}
// protected function getAuthUrl($state)
// {
// return $this->buildAuthUrlFromBase('http://auth.url', $state);
// }
protected function getTokenUrl()
{
return 'http://token.url';
}
// protected function getTokenUrl()
// {
// return 'http://token.url';
// }
/**
* Get the access token response for the given code.

View File

@ -5,10 +5,12 @@
use App\Providers\Socialite\OpenId;
use GuzzleHttp\RequestOptions;
use Illuminate\Http\Request;
use Laravel\Socialite\Two\Token;
use Laravel\Socialite\Two\User;
use Mockery;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use SocialiteProviders\Manager\Helpers\ConfigRetriever;
use stdClass;
use Tests\TestCase;
@ -77,4 +79,52 @@ public function test_it_can_map_a_user_from_an_access_token_with_missing_fields(
$this->assertSame(null, $user->email_verified);
$this->assertSame(null, $user->groups);
}
#[Test]
public function test_it_fetches_token_url_from_config()
{
$tokenUrl = 'http://token.url';
config(['services.openid.token_url' => $tokenUrl]);
$request = Request::create('/');
$provider = new OpenIdProviderStub($request, 'client_id', 'client_secret', 'redirect_uri');
$provider->http = Mockery::mock(stdClass::class);
$config = (new ConfigRetriever)->fromServices('openid', $provider->additionalConfigKeys());
$provider->setConfig($config);
$provider->http->expects('post')->with($tokenUrl, [
RequestOptions::HEADERS => ['Accept' => 'application/json'],
RequestOptions::FORM_PARAMS => [
'grant_type' => 'refresh_token',
'refresh_token' => 'refresh_token',
'client_id' => null,
'client_secret' => null,
],
])->andReturns($response = Mockery::mock(stdClass::class));
$response->expects('getBody')->andReturns('{ "access_token" : "access_token", "refresh_token" : "refresh_token", "expires_in" : 3600, "scope" : "scope1,scope2" }');
$token = $provider->refreshToken('refresh_token');
$this->assertInstanceOf(Token::class, $token);
}
#[Test]
public function test_it_redirects_to_url_from_config()
{
$authUrl = 'http://auth.url';
config(['services.openid.authorize_url' => $authUrl]);
$request = Request::create('/');
$provider = new OpenIdProviderStub($request, 'client_id', 'client_secret', 'redirect_uri');
$provider->http = Mockery::mock(stdClass::class);
$provider->stateless();
$config = (new ConfigRetriever)->fromServices('openid', $provider->additionalConfigKeys());
$provider->setConfig($config);
$response = $provider->redirect();
$this->assertStringStartsWith($authUrl, $response->getTargetUrl());
}
}