Last active
December 6, 2018 12:11
-
-
Save renalpha/c762c4233919c7c7decd3d7975860538 to your computer and use it in GitHub Desktop.
Larvel Passport test get access token
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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