From 699b997d36390972cb9a66debac2514932d8c5bf Mon Sep 17 00:00:00 2001 From: Antonio Pauletich Date: Sat, 4 Jan 2020 23:04:19 +0100 Subject: [PATCH 1/6] Add Orchestra Testbench package --- composer.json | 1 + tests/Feature/ActingAsClientTest.php | 10 ++++++++++ 2 files changed, 11 insertions(+) create mode 100644 tests/Feature/ActingAsClientTest.php diff --git a/composer.json b/composer.json index c5fc88c63..c5822ee45 100644 --- a/composer.json +++ b/composer.json @@ -34,6 +34,7 @@ }, "require-dev": { "mockery/mockery": "^1.0", + "orchestra/testbench": "^4.4|^5.0", "phpunit/phpunit": "^8.0" }, "autoload": { diff --git a/tests/Feature/ActingAsClientTest.php b/tests/Feature/ActingAsClientTest.php new file mode 100644 index 000000000..37fa8f811 --- /dev/null +++ b/tests/Feature/ActingAsClientTest.php @@ -0,0 +1,10 @@ + Date: Sat, 4 Jan 2020 23:50:38 +0100 Subject: [PATCH 2/6] Add ActingAsClientTest and resolve storage_path function conflict --- tests/Feature/ActingAsClientTest.php | 43 ++++++++++++++++++++++++++-- tests/KeysCommandTest.php | 8 ++++++ tests/bootstrap.php | 5 ---- 3 files changed, 49 insertions(+), 7 deletions(-) diff --git a/tests/Feature/ActingAsClientTest.php b/tests/Feature/ActingAsClientTest.php index 37fa8f811..edba5440d 100644 --- a/tests/Feature/ActingAsClientTest.php +++ b/tests/Feature/ActingAsClientTest.php @@ -1,10 +1,49 @@ 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'); + } } diff --git a/tests/KeysCommandTest.php b/tests/KeysCommandTest.php index 8199f6b4a..a738a92e4 100644 --- a/tests/KeysCommandTest.php +++ b/tests/KeysCommandTest.php @@ -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; @@ -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 @@ -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); diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 9914b328d..991ea4397 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,8 +1,3 @@ Date: Sun, 5 Jan 2020 01:17:00 +0100 Subject: [PATCH 3/6] Add ServerRequestInterface typehint --- src/Passport.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Passport.php b/src/Passport.php index 103ff3957..992f4333b 100644 --- a/src/Passport.php +++ b/src/Passport.php @@ -8,6 +8,7 @@ use Illuminate\Support\Facades\Route; use League\OAuth2\Server\ResourceServer; use Mockery; +use Psr\Http\Message\ServerRequestInterface; class Passport { @@ -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); From e4046aa107020241bef0ab6608e9a0c6b6f3dbd0 Mon Sep 17 00:00:00 2001 From: Antonio Pauletich Date: Sun, 5 Jan 2020 01:17:34 +0100 Subject: [PATCH 4/6] Create PassportTestCase --- tests/Feature/PassportTestCase.php | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 tests/Feature/PassportTestCase.php diff --git a/tests/Feature/PassportTestCase.php b/tests/Feature/PassportTestCase.php new file mode 100644 index 000000000..cee7f1685 --- /dev/null +++ b/tests/Feature/PassportTestCase.php @@ -0,0 +1,27 @@ +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]; + } +} From 5212c9ccabbeaefb33f2ff2176b03dfe2ec0e9f4 Mon Sep 17 00:00:00 2001 From: Antonio Pauletich Date: Sun, 5 Jan 2020 01:27:22 +0100 Subject: [PATCH 5/6] Add ActingAsTest --- tests/Feature/ActingAsTest.php | 74 ++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 tests/Feature/ActingAsTest.php diff --git a/tests/Feature/ActingAsTest.php b/tests/Feature/ActingAsTest.php new file mode 100644 index 000000000..e50e5d941 --- /dev/null +++ b/tests/Feature/ActingAsTest.php @@ -0,0 +1,74 @@ +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'; +} From feaa05104249f00a387e5d633554f3cf1b66b1fc Mon Sep 17 00:00:00 2001 From: Antonio Pauletich Date: Sun, 5 Jan 2020 01:52:59 +0100 Subject: [PATCH 6/6] Fix lint issues --- tests/Feature/ActingAsClientTest.php | 2 +- tests/Feature/ActingAsTest.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Feature/ActingAsClientTest.php b/tests/Feature/ActingAsClientTest.php index edba5440d..8fc99b777 100644 --- a/tests/Feature/ActingAsClientTest.php +++ b/tests/Feature/ActingAsClientTest.php @@ -38,7 +38,7 @@ public function testActingAsClientWhenTheRouteIsProtectedByCheckClientCredential $router->get('/foo', function () { return 'bar'; - })->middleware(CheckClientCredentialsForAnyScope::class . ':testFoo'); + })->middleware(CheckClientCredentialsForAnyScope::class.':testFoo'); Passport::actingAsClient(new Client(), ['testFoo']); diff --git a/tests/Feature/ActingAsTest.php b/tests/Feature/ActingAsTest.php index e50e5d941..aa1b3ecea 100644 --- a/tests/Feature/ActingAsTest.php +++ b/tests/Feature/ActingAsTest.php @@ -38,7 +38,7 @@ public function testActingAsWhenTheRouteIsProtectedByCheckScopesMiddleware() $router->get('/foo', function () { return 'bar'; - })->middleware(CheckScopes::class . ':admin,footest'); + })->middleware(CheckScopes::class.':admin,footest'); Passport::actingAs(new PassportUser(), ['admin', 'footest']); @@ -56,7 +56,7 @@ public function testActingAsWhenTheRouteIsProtectedByCheckForAnyScopeMiddleware( $router->get('/foo', function () { return 'bar'; - })->middleware(CheckForAnyScope::class . ':admin,footest'); + })->middleware(CheckForAnyScope::class.':admin,footest'); Passport::actingAs(new PassportUser(), ['footest']);