Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Stub client on guard when calling Passport::actingAsClient() #1519

Merged
merged 3 commits into from
Jan 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/Guards/TokenGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Illuminate\Cookie\Middleware\EncryptCookies;
use Illuminate\Http\Request;
use Illuminate\Support\Traits\Macroable;
use Laravel\Passport\Client;
use Laravel\Passport\ClientRepository;
use Laravel\Passport\Passport;
use Laravel\Passport\PassportUserProvider;
Expand Down Expand Up @@ -353,4 +354,17 @@ public static function serialized()
{
return EncryptCookies::serialized('XSRF-TOKEN');
}

/**
* Set the client for the current request.
*
* @param \Laravel\Passport\Client $client
* @return $this
*/
public function setClient(Client $client)
{
$this->client = $client;

return $this;
}
}
7 changes: 6 additions & 1 deletion src/Passport.php
Original file line number Diff line number Diff line change
Expand Up @@ -381,9 +381,10 @@ public static function actingAs($user, $scopes = [], $guard = 'api')
*
* @param \Laravel\Passport\Client $client
* @param array $scopes
* @param string $guard
* @return \Laravel\Passport\Client
*/
public static function actingAsClient($client, $scopes = [])
public static function actingAsClient($client, $scopes = [], $guard = 'api')
{
$token = app(self::tokenModel());

Expand All @@ -407,6 +408,10 @@ public static function actingAsClient($client, $scopes = [])

app()->instance(TokenRepository::class, $mock);

app('auth')->guard($guard)->setClient($client);

app('auth')->shouldUse($guard);

return $client;
}

Expand Down
10 changes: 8 additions & 2 deletions tests/Feature/ActingAsClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
use Laravel\Passport\Http\Middleware\CheckClientCredentials;
use Laravel\Passport\Http\Middleware\CheckClientCredentialsForAnyScope;
use Laravel\Passport\Passport;
use Orchestra\Testbench\TestCase;

class ActingAsClientTest extends TestCase
class ActingAsClientTest extends PassportTestCase
{
public function testActingAsClientWhenTheRouteIsProtectedByCheckClientCredentialsMiddleware()
{
Expand Down Expand Up @@ -46,4 +45,11 @@ public function testActingAsClientWhenTheRouteIsProtectedByCheckClientCredential
$response->assertSuccessful();
$response->assertSee('bar');
}

public function testActingAsClientSetsTheClientOnTheGuard()
{
Passport::actingAsClient($client = new Client());

$this->assertSame($client, app('auth')->client());
}
}