Skip to content

Commit

Permalink
Correct issue with GoogleProvider getUserByToken() method (#689)
Browse files Browse the repository at this point in the history
* fix(google provider): check if response body of GoogleProvider's getUserByToken method is a GuzzleHttp\Psr7\Stream instance

* feat(google provider): add test for GoogleProvider's getUserByToken method last change

---------

Co-authored-by: Axel Libori Roch <alibori@wwwarcelona.com>
  • Loading branch information
alibori and aliboriwwwarcelona authored Feb 11, 2024
1 parent d2a8113 commit ffeeb2c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Two/GoogleProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Laravel\Socialite\Two;

use GuzzleHttp\Psr7\Stream;
use GuzzleHttp\RequestOptions;
use Illuminate\Support\Arr;

Expand Down Expand Up @@ -56,6 +57,10 @@ protected function getUserByToken($token)
],
]);

if ($response->getBody() instanceof Stream) {
return json_decode($response->getBody()->getContents(), true);
}

return json_decode($response->getBody(), true);
}

Expand Down
48 changes: 48 additions & 0 deletions tests/GoogleProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Laravel\Socialite\Tests;

use GuzzleHttp\Client;
use GuzzleHttp\RequestOptions;
use Illuminate\Http\Request;
use Laravel\Socialite\Tests\Fixtures\GoogleTestProviderStub;
use Laravel\Socialite\Two\User;
use Mockery as m;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;

class GoogleProviderTest extends TestCase
{
protected function tearDown(): void
{
parent::tearDown();

m::close();
}

public function test_it_can_map_a_user_from_an_access_token()
{
$request = Request::create('/');

$provider = new GoogleTestProviderStub($request, 'client_id', 'client_secret', 'redirect_uri');

$provider->http = m::mock(Client::class);

$provider->http->allows('get')->with('https://www.googleapis.com/oauth2/v3/userinfo', [
RequestOptions::QUERY => [
'prettyPrint' => 'false',
],
RequestOptions::HEADERS => [
'Accept' => 'application/json',
'Authorization' => 'Bearer fake-token',
],
])->andReturns($response = m::mock(ResponseInterface::class));

$response->allows('getBody')->andReturns(m::mock(StreamInterface::class));

$user = $provider->userFromToken('fake-token');

$this->assertInstanceOf(User::class, $user);
}
}

0 comments on commit ffeeb2c

Please sign in to comment.