Skip to content

Instantly share code, notes, and snippets.

@renalpha
Last active December 6, 2018 12:11
Show Gist options
  • Save renalpha/c762c4233919c7c7decd3d7975860538 to your computer and use it in GitHub Desktop.
Save renalpha/c762c4233919c7c7decd3d7975860538 to your computer and use it in GitHub Desktop.
Larvel Passport test get access token
class PassportTest extends TestCase
{
/** @test */
public function unauthenticated_cannot_access_api_endpoint()
{
$response = $this->get('api/queue/list');
$response->assertRedirect('/login');
}
/** @test */
public function authenticated_can_access_endpoint()
{
$response = $this->requestAccessToken();
$this->assertArrayHasKey('access_token', $response);
$response = $this->get('api/queue/list', [
'Authorization' => 'Bearer '. $response['access_token'],
'Accept' => 'application/json',
'Content-Type' => 'application/json',
]);
}
/**
* @return array
*/
private function requestAccessToken()
{
$client = new \Laravel\Passport\ClientRepository();
$client = $client->createPersonalAccessClient(null, 'Foobar', config('app.url'));
// Arrange
$body = [
'grant_type' => 'client_credentials',
'client_id' => (string) $client->id,
'client_secret' => $client->secret,
];
return $this->post('/oauth/token', $body, ['Accept' => 'application/json'])->json();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment