Laravel Passport API Auth Test
Laravel Test HTTP Tests: Laravel Passport API Auth Test
Categories:
Acting as User
Create New User From Factory
Passport::actingAs(
User::factory()->create(),
$scopes = ['create-servers'],
$guard = 'api'
);
$response = $this->post('/api/create-server');
Existing User Model
$email = 'kj@example.com';
$UserModel = UserModel::where('email', $email)->first();
Passport::actingAs(
$UserModel,
$scopes = ['create-servers'],
$guard = 'api'
);
$response = $this->post('/api/create-server');
Existing User Token
$accessToken = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.XXXXX';
$header = [
'Accept' => 'application/json',
'Authorization' => 'Bearer '.$accessToken,
];
$input = [];
$response = $this->post('/api/create-server', $input, $header);
Acting as Client
Create New Client From Factory
Passport::actingAsClient(
Client::factory()->create(),
$scopes = ['check-status'],
$guard = 'api'
);
$response = $this->get('/api/orders');