- я могу получить доступ к маршруту, используя грант клиента из токена, полученного в
/oauth/token - Я также могу получить доступ к маршруту, используя токен от вошедшего в систему пользователя (клиент личного доступа)
из oauth/токена, затем вхожу в систему, чтобы получить токен доступа для пользователя, и использую этот токен пользователя на этом конкретном маршруте, у меня нет проблем
(test_get_products_using_authenticated_user)
НО когда я использую Passport::actingAs, я получаю сообщение об ошибке
(test_get_products_using_authenticated_user2)
вот фрагмент кода
Код: Выделить всё
public function test_get_products_using_authenticated_user(): void
{
$data = [
'email' => CustomerSeeder::CUSTOMER_EXAMPLE_EMAIL,
'password' => CustomerSeeder::CUSTOMER_EXAMPLE_PASSWORD,
];
$loginResponse = $this->withHeaders([
'Authorization' => 'Bearer ' . $this->generateAccessToken(),
])->postJson('/api/login', $data);
$loginResponse->assertStatus(200);
$loginToken = $loginResponse->json('data.token');
$response = $this->withHeaders([
'Authorization' => 'Bearer ' . $loginToken,
])->getJson('/api/products');
$response = $this->get('/api/products');
$response->assertStatus(200);
}
public function test_products_using_authenticated_user2(): void
{
$user = User::where('email', CustomerSeeder::CUSTOMER_EXAMPLE_EMAIL)->first();
Passport::actingAs(
$user,
[],
'api'
);
$response = $this->get('/api/products');
$response->assertStatus(200);
}
Код: Выделить всё
Route [login] not defined.
at tests\Feature\api\ProductTest.php:58
54▕ 'api'
55▕ );
56▕
57▕ $response = $this->get('/api/products');
➜ 58▕ $response->assertStatus(200);
59▕ }
60▕ }
61▕
Код: Выделить всё
Route::group(['prefix' => 'products', 'middleware' => 'client'], function () {
Route::get('/', [ProductController::class, 'index'])- >name('product.list');
});
Код: Выделить всё
protected $middlewareAliases = [
'auth' => \App\Http\Middleware\Authenticate::class,
'client' => CheckClientCredentials::class,
Код: Выделить всё
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'passport',
'provider' => 'users',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
],
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],
что мне здесь не хватает?
я использую laravel 10.10, паспорт 12 и php 8.1
Подробнее здесь: https://stackoverflow.com/questions/783 ... s-on-tests
Мобильная версия