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

[8.x] Add tests for Passport::actingAs and Passport::actingAsClient #1155

Merged
merged 6 commits into from
Jan 6, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
},
"require-dev": {
"mockery/mockery": "^1.0",
"orchestra/testbench": "^4.4|^5.0",
"phpunit/phpunit": "^8.0"
},
"autoload": {
Expand Down
3 changes: 2 additions & 1 deletion src/Passport.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Support\Facades\Route;
use League\OAuth2\Server\ResourceServer;
use Mockery;
use Psr\Http\Message\ServerRequestInterface;

class Passport
{
Expand Down Expand Up @@ -429,7 +430,7 @@ public static function actingAsClient($client, $scopes = [])

$mock = Mockery::mock(ResourceServer::class);
$mock->shouldReceive('validateAuthenticatedRequest')
->andReturnUsing(function ($request) use ($token) {
->andReturnUsing(function (ServerRequestInterface $request) use ($token) {
return $request->withAttribute('oauth_client_id', $token->client->id)
->withAttribute('oauth_access_token_id', $token->id)
->withAttribute('oauth_scopes', $token->scopes);
Expand Down
49 changes: 49 additions & 0 deletions tests/Feature/ActingAsClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Laravel\Passport\Tests\Feature;

use Illuminate\Contracts\Routing\Registrar;
use Laravel\Passport\Client;
use Laravel\Passport\Http\Middleware\CheckClientCredentials;
use Laravel\Passport\Http\Middleware\CheckClientCredentialsForAnyScope;
use Laravel\Passport\Passport;
use Orchestra\Testbench\TestCase;

class ActingAsClientTest extends TestCase
{
public function testActingAsClientWhenTheRouteIsProtectedByCheckClientCredentialsMiddleware()
{
$this->withoutExceptionHandling();

/** @var Registrar $router */
$router = $this->app->make(Registrar::class);

$router->get('/foo', function () {
return 'bar';
})->middleware(CheckClientCredentials::class);

Passport::actingAsClient(new Client());

$response = $this->get('/foo');
$response->assertSuccessful();
$response->assertSee('bar');
}

public function testActingAsClientWhenTheRouteIsProtectedByCheckClientCredentialsForAnyScope()
{
$this->withoutExceptionHandling();

/** @var Registrar $router */
$router = $this->app->make(Registrar::class);

$router->get('/foo', function () {
return 'bar';
})->middleware(CheckClientCredentialsForAnyScope::class.':testFoo');

Passport::actingAsClient(new Client(), ['testFoo']);

$response = $this->get('/foo');
$response->assertSuccessful();
$response->assertSee('bar');
}
}
74 changes: 74 additions & 0 deletions tests/Feature/ActingAsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace Laravel\Passport\Tests\Feature;

use Illuminate\Contracts\Routing\Registrar;
use Illuminate\Foundation\Auth\User;
use Laravel\Passport\HasApiTokens;
use Laravel\Passport\Http\Middleware\CheckForAnyScope;
use Laravel\Passport\Http\Middleware\CheckScopes;
use Laravel\Passport\Passport;

class ActingAsTest extends PassportTestCase
{
public function testActingAsWhenTheRouteIsProtectedByAuthMiddleware()
{
$this->withoutExceptionHandling();

/** @var Registrar $router */
$router = $this->app->make(Registrar::class);

$router->get('/foo', function () {
return 'bar';
})->middleware('auth:api');

Passport::actingAs(new PassportUser());

$response = $this->get('/foo');
$response->assertSuccessful();
$response->assertSee('bar');
}

public function testActingAsWhenTheRouteIsProtectedByCheckScopesMiddleware()
{
$this->withoutExceptionHandling();

/** @var Registrar $router */
$router = $this->app->make(Registrar::class);

$router->get('/foo', function () {
return 'bar';
})->middleware(CheckScopes::class.':admin,footest');

Passport::actingAs(new PassportUser(), ['admin', 'footest']);

$response = $this->get('/foo');
$response->assertSuccessful();
$response->assertSee('bar');
}

public function testActingAsWhenTheRouteIsProtectedByCheckForAnyScopeMiddleware()
{
$this->withoutExceptionHandling();

/** @var Registrar $router */
$router = $this->app->make(Registrar::class);

$router->get('/foo', function () {
return 'bar';
})->middleware(CheckForAnyScope::class.':admin,footest');

Passport::actingAs(new PassportUser(), ['footest']);

$response = $this->get('/foo');
$response->assertSuccessful();
$response->assertSee('bar');
}
}

class PassportUser extends User
{
use HasApiTokens;

protected $table = 'users';
}
27 changes: 27 additions & 0 deletions tests/Feature/PassportTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Laravel\Passport\Tests\Feature;

use Illuminate\Contracts\Config\Repository;
use Laravel\Passport\PassportServiceProvider;
use Orchestra\Testbench\TestCase;

abstract class PassportTestCase extends TestCase
{
protected function setUp(): void
{
parent::setUp();

$this->artisan('passport:keys');
}

protected function getEnvironmentSetUp($app)
{
$app->make(Repository::class)->set('auth.guards.api', ['driver' => 'passport', 'provider' => 'users']);
}

protected function getPackageProviders($app)
{
return [PassportServiceProvider::class];
}
}
8 changes: 8 additions & 0 deletions tests/KeysCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Laravel\Passport\Tests;

use Illuminate\Container\Container;
use Laravel\Passport\Console\KeysCommand;
use Laravel\Passport\Passport;
use Mockery as m;
Expand All @@ -13,6 +14,11 @@ function custom_path($file = null)
return __DIR__.DIRECTORY_SEPARATOR.'files'.DIRECTORY_SEPARATOR.$file;
}

function storage_path($file = null)
{
return __DIR__.DIRECTORY_SEPARATOR.$file;
}

class KeysCommandTest extends TestCase
{
protected function tearDown(): void
Expand All @@ -33,6 +39,8 @@ public function testPrivateAndPublicKeysAreGenerated()
->with('Encryption keys generated successfully.')
->getMock();

Container::getInstance()->instance('path.storage', storage_path());

$rsa = new RSA();

$command->handle($rsa);
Expand Down
5 changes: 0 additions & 5 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
<?php

require __DIR__.'/../vendor/autoload.php';

function storage_path($file = null)
{
return __DIR__.DIRECTORY_SEPARATOR.$file;
}