1
votes

I have Postman API requests (POST request) containing grant_type = 'password', username = 'abc', password='xyz'. By passing this I get an token = 'xxx' and token_type = 'Bearer'. How do I write an phpunit test in laravel for such api requests?

1

1 Answers

1
votes

you can make a test by php artisan make test:PassportTest.

Then you can use

$response = $this->json('POST','/api/token',['grant_type'=>'password','username'='abc','password'=>'xyz']);
$response-> assertJsonStructure([
    'token'=>'xxx',
    'token_type'=>'Bearer'
]);

You can also use $response->assertDatabaseHas($tokenTable,$data) to check if there is a record in you databse;

Cant see your database structures,so you need a little change to suit your own project.