使用虚拟数据库测试通行证oauth2时,laravel测试失败

时间:2019-02-05 06:28:14

标签: laravel laravel-passport laravel-testing

我正在使用虚拟测试数据库进行测试。我的API正在与邮递员一起使用。但是在编写测​​试时会产生问题。当我执行测试时,它会显示一长串错误,其中包含以下消息-

  

“邮件”:“客户端错误:POST http://localhost/oauth/token   在401 Unauthorized中   响应:\ n {\“错误\”:\“ invalid_client \”,\“消息\”:\“客户端   身份验证失败\“}

这是我的路线-

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']);
  }


  }

请帮助我,我想念什么

1 个答案:

答案 0 :(得分:0)

我已经通过代理请求解决了它。发生这种情况的原因是-当我用大口大口呼叫“ oauth / token”端点时,该呼叫被视为真实呼叫,并且测试在那里无效。所以这对我解决问题大有帮助。

$tokenRequest = Request::create('oauth/token', 'POST',$request->toArray()); 
$response = Route::dispatch($tokenRequest);