Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Testing Passport Authenticated Controllers and Routes in Laravel
{
...
"autoload-dev": {
"classmap": [
"tests/TestCase.php",
"tests/PassportTestCase.php"
]
},
...
}
<?php
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ExamplePassportTest extends \PassportTestCase
{
use DatabaseTransactions;
protected $scopes = ['restricted-scope'];
public function testRestrictedRoute()
{
$this->get('/api/user')
->assertResponseStatus(401);
}
public function testUnrestrictedRoute()
{
$this->get('/api/restricted')
->assertResponseOk();
}
}
<?php
use App\User;
use Laravel\Passport\ClientRepository;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class PassportTestCase extends TestCase
{
use DatabaseTransactions;
protected $headers = [];
protected $scopes = [];
protected $user;
public function setUp()
{
parent::setUp();
$clientRepository = new ClientRepository();
$client = $clientRepository->createPersonalAccessClient(
null, 'Test Personal Access Client', $this->baseUrl
);
DB::table('oauth_personal_access_clients')->insert([
'client_id' => $client->id,
'created_at' => new DateTime,
'updated_at' => new DateTime,
]);
$this->user = factory(User::class)->create();
$token = $this->user->createToken('TestToken', $this->scopes)->accessToken;
$this->headers['Accept'] = 'application/json';
$this->headers['Authorization'] = 'Bearer '.$token;
}
public function get($uri, array $headers = [])
{
return parent::get($uri, array_merge($this->headers, $headers));
}
public function getJson($uri, array $headers = [])
{
return parent::getJson($uri, array_merge($this->headers, $headers));
}
public function post($uri, array $data = [], array $headers = [])
{
return parent::post($uri, $data, array_merge($this->headers, $headers));
}
public function postJson($uri, array $data = [], array $headers = [])
{
return parent::postJson($uri, $data, array_merge($this->headers, $headers));
}
public function put($uri, array $data = [], array $headers = [])
{
return parent::put($uri, $data, array_merge($this->headers, $headers));
}
public function putJson($uri, array $data = [], array $headers = [])
{
return parent::putJson($uri, $data, array_merge($this->headers, $headers));
}
public function patch($uri, array $data = [], array $headers = [])
{
return parent::patch($uri, $data, array_merge($this->headers, $headers));
}
public function patchJson($uri, array $data = [], array $headers = [])
{
return parent::patchJson($uri, $data, array_merge($this->headers, $headers));
}
public function delete($uri, array $data = [], array $headers = [])
{
return parent::delete($uri, $data, array_merge($this->headers, $headers));
}
public function deleteJson($uri, array $data = [], array $headers = [])
{
return parent::deleteJson($uri, $data, array_merge($this->headers, $headers));
}
}
@ronnievisser

This comment has been minimized.

Copy link

ronnievisser commented Nov 3, 2016

Hi,

I'm having an issue that migration are not applied when migrating in tests. Do you have a solution for this also?

thanks

@ghost

This comment has been minimized.

Copy link

ghost commented Dec 30, 2016

Same issues for me : " no such table : oauth_clients"

@archy-bold

This comment has been minimized.

Copy link
Owner Author

archy-bold commented Jun 16, 2017

Sorry, I didn't get any notifications for this gist for some reason. You need to ensure Passport is installed and running on your Laravel app. You can do that as follows:

Install with composer:

composer require laravel/passport

Then add the following to the providers array of your app/config.php file:

Laravel\Passport\PassportServiceProvider::class,

Finally, run the setup for Passport:

php artisan migrate
php artisan passport:install

Source: https://laravel.com/docs/5.4/passport#installation

@jesseschutt

This comment has been minimized.

Copy link

jesseschutt commented Aug 16, 2017

Thanks for writing this up! It helped me :D

@kingsleyudenewu

This comment has been minimized.

Copy link

kingsleyudenewu commented Aug 2, 2018

Hi please can i do a test without using ClientRepository

@AhmedHelalAhmed

This comment has been minimized.

Copy link

AhmedHelalAhmed commented Aug 5, 2018

How about

  1. RestrictionPassportTest::testRestrictedRoute
    ErrorException: Undefined property: RestrictionPassportTest::$baseUrl

??

@hindermyertim

This comment has been minimized.

Copy link

hindermyertim commented Feb 5, 2019

How about

  1. RestrictionPassportTest::testRestrictedRoute
    ErrorException: Undefined property: RestrictionPassportTest::$baseUrl

??

Ya same issue here.

@datashaman

This comment has been minimized.

Copy link

datashaman commented Feb 23, 2019

Instead of using $this->baseUrl, just use an empty string ''.

@datashaman

This comment has been minimized.

Copy link

datashaman commented Feb 23, 2019

With Laravel 5.4+ you can use this very simple method to test passport routes: https://laravel.com/docs/5.7/passport#testing

@hfatahi

This comment has been minimized.

Copy link

hfatahi commented Feb 27, 2019

@archy-bold Is this any different than simply calling Passport::actingAs()?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.