Search code examples
laravelphpunitlaravel-passport

Simple Laravel Passeport Route Testing


I encounter a small problem when performing unit tests for the default Passport 5.8 routes.

In fact I tested the route / oauth / clients in get mode:

/** @test */
   public function getOauthClients()
   {
       $user = factory(User::class)->make();
       $response = $this->actingAs($user)->getJson('/oauth/clients');
       $response->assertSuccessful();
   }

But when I want to test the route provided by default in get mode: /oauth/token , I do not know what are the steps I need to follow.

Thank you in advance.


Solution

  • You should try with:

    Passport::actingAs(
        factory(User::class)->create()
    );
    $response = $this->getJson('/oauth/clients');
    // ...
    

    Passport ship with some testing helpers for that purpose, like the actingAs method above.

    Quoting from documentation:

    Passport's actingAs method may be used to specify the currently authenticated user as well as its scopes. The first argument given to the actingAs method is the user instance and the second is an array of scopes that should be granted to the user's token: