为了账号安全,请及时绑定邮箱和手机立即绑定

你如何使用中间件('auth:api')测试路由?

你如何使用中间件('auth:api')测试路由?

PHP
慕标5832272 2021-10-22 15:25:45
我正在为我正在处理的项目编写一些单元测试,但我似乎无法以登录用户的身份测试发布。我曾尝试向 env 文件添加不记名令牌,这在我的机器上工作时有效,但在推送后运行测试的 buddyworks 失败。 public function test_orders_route_authenticated_user () {        $data =            '{                "orderID": "001241",                "sku": "123456",                "quantity": 9,                "pricePerUnit": 78,                "priceTotal": 702            }';        $this->user = factory(User::class)->create();;        //dd($user);        $response = $this->withHeaders([            'Authorization'=>'MPBtsJN5qf',            ])->json('POST', 'api/products',[                $data            ]);        $response->assertStatus(200);}所以这段代码给了我错误 500。我设法得到错误 401 和 500,但从来没有达到预期的状态 200。请帮忙
查看完整描述

2 回答

?
catspeake

TA贡献1111条经验 获得超0个赞

如Passport 文档中所述,您可以使用Passport::actingAs(...):


public function test_orders_route_authenticated_user()

{

    Passport::actingAs(

        factory(User::class)->create()

    );


    $data = [

        'orderID'      => '001241',

        'sku'          => '123456',

        'quantity'     => 9,

        'pricePerUnit' => 78,

        'priceTotal'   => 702,

    ];


    $this->json('post', 'api/products', $data)->assertStatus(200);

}


查看完整回答
反对 回复 2021-10-22
?
炎炎设计

TA贡献1808条经验 获得超4个赞

您应该在您的路线上应用 jwt auth,例如:


Route::group(['middleware' => 'jwt.auth'], function () {

  Route::get('/index','IndexController@index');


});


查看完整回答
反对 回复 2021-10-22
  • 2 回答
  • 0 关注
  • 206 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号