I'm writing some unit tests for a project I am working on and I can't seem to test posting as a logged in user.
I have tried adding a bearer token to the env file, this worked when on my machine, but fails with buddyworks running the tests after pushing.
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);
}
So this code gives me error 500. I have managed to get error 401 and 500, but never the intended status 200. Please help
As mentioned in the Passport documentation you can use 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);
}