使用虚拟数据库测试passport oauth2时,laravel测试失败
我正在使用虚拟测试数据库进行测试。我的API正在与邮递员合作。但在编写测试时会产生问题。当我执行测试时,它会显示一长串错误,包含下面的消息- “消息”:“客户端错误:使用虚拟数据库测试passport oauth2时,laravel测试失败,laravel,laravel-passport,laravel-testing,Laravel,Laravel Passport,Laravel Testing,我正在使用虚拟测试数据库进行测试。我的API正在与邮递员合作。但在编写测试时会产生问题。当我执行测试时,它会显示一长串错误,包含下面的消息- “消息”:“客户端错误:POST”http://localhost/oauth/token 在401未经授权的 响应:\n{“错误”:“无效的客户端”,“消息”:“客户端” 身份验证失败\“} 这是我的路线- Route::post('/v1/create', 'API\v1\UserController@register'); 这是我的控制器 pub
POST”http://localhost/oauth/token
在401未经授权的
响应:\n{“错误”:“无效的客户端”,“消息”:“客户端”
身份验证失败\“}
这是我的路线-
Route::post('/v1/create', 'API\v1\UserController@register');
这是我的控制器
public function register(Request $request)
{
$validator = Validator::make($request->all(), [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:6', 'confirmed'],
]);
if ($validator->fails()) {
return response()->json(['error'=>$validator->errors()], 401);
}
$input = $request->all();
$input['password'] = bcrypt($input['password']);
$user = User::create($input);
$http=new Client;
$response=$http->post(url("/oauth/token"),[
'form_params'=>[
'grant_type' =>'password',
'client_id' =>$request->client_id,
'client_secret' =>$request->client_secret,
'password' =>$request->password,
'username' =>$request->email,
'scope' =>''
]
]);
// return response()->json(['success'=>$response], $this->successStatus);
return $response;
}
这是我的测试-
<?php
namespace Tests\Feature\API\v1;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
use App\User;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use PharIo\Manifest\Email;
class UserTest extends TestCase
{
use WithFaker;
use DatabaseMigrations;
public $mockConsoleOutput = false;
/** @test */
public function a_user_can_create_user(){
$user= factory(User::class)->create(); //create user
$login=$this->actingAs($user,'api'); //user login with api
//create password grant client
$this->artisan('passport:client', ['--password' =>true, '--no-interaction' => true, '--redirect_uri'=>'http://localhost', '--name'=>'test client']);
// fetch client for id and secret
$client = \DB::table('oauth_clients')->where('password_client', 1)->first();
// dd($client->getData());
$email=$this->faker->email();
$password=$this->faker->password;
$newUser=$this->json('POST','/api/v1/create',[
'grant_type' =>'password',
'client_id' => $client->id,
'client_secret' => $client->secret,
'name' => $this->faker->name(),
'email' => $email,
'password' => $password,
'password_confirmation' => $password,
'remember_token' => str_random(10),
]);
// ->assertJsonStructure(['access_token', 'refresh_token']);
dd($newUser);
// $this->assertDatabaseHas('users',['email'=>$email]);
// $newUser->assertJsonFragment(['token_type'=>'Bearer']);
}
}
我通过代理请求解决了这个问题。这是因为——当我用guzzle调用“oauth/token”端点时——这个调用被视为真正的调用,而测试在那里不起作用。所以这对我解决这个问题帮助很大
$tokenRequest = Request::create('oauth/token', 'POST',$request->toArray());
$response = Route::dispatch($tokenRequest);
我通过代理请求解决了这个问题。这是因为——当我用guzzle调用“oauth/token”端点时——这个调用被视为真正的调用,测试在那里不起作用。所以这对我解决问题有很大帮助
$tokenRequest = Request::create('oauth/token', 'POST',$request->toArray());
$response = Route::dispatch($tokenRequest);
我认为问题出在数据库上。当控制器调用“oauth/token”时,服务器试图从真实数据库而不是测试数据库中找到客户端。不知道如何克服它。如果有人认为问题出在数据库上,请提供帮助。当控制器调用“oauth/token”时“-服务器正在尝试从真实数据库而不是测试数据库中查找客户端。不知道如何克服它。如果有人能帮忙,请帮忙