Skip to content

Instantly share code, notes, and snippets.

@cAstraea
Forked from archy-bold/ExamplePassportTest.php
Created January 22, 2022 20:26
Show Gist options
  • Save cAstraea/7a68b28fd30685bd3dea2fad653b5cce to your computer and use it in GitHub Desktop.
Save cAstraea/7a68b28fd30685bd3dea2fad653b5cce to your computer and use it in GitHub Desktop.
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));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment