title: How to test authentication via API with Laravel Passport?
body: <p>I'm trying to test the authentication with Laravel's Passport and there's no way... always received a 401 of that client is invalid, I'll leave you what I've tried:</p>
  
  <p>My phpunit configuration is the one that comes from base with laravel </p>
  
  <h3>tests/TestCase.php</h3>
  
  <pre><code>abstract class TestCase extends BaseTestCase
  {
      use CreatesApplication, DatabaseTransactions;
  
      protected $client, $user, $token;
  
      public function setUp()
      {
          parent::setUp();
  
          $clientRepository = new ClientRepository();
          $this-&gt;client = $clientRepository-&gt;createPersonalAccessClient(
              null, 'Test Personal Access Client', '/'
          );
          DB::table('oauth_personal_access_clients')-&gt;insert([
              'client_id' =&gt; $this-&gt;client-&gt;id,
              'created_at' =&gt; date('Y-m-d'),
              'updated_at' =&gt; date('Y-m-d'),
          ]);
          $this-&gt;user = User::create([
              'id' =&gt; 1,
              'name' =&gt; 'test',
              'lastname' =&gt; 'er',
              'email' =&gt; 'test@test.test',
              'password' =&gt; bcrypt('secret')
          ]);
          $this-&gt;token = $this-&gt;user-&gt;createToken('TestToken', [])-&gt;accessToken;
      }
  }
  </code></pre>
  
  <h3>tests/Feature/AuthTest.php</h3>
  
  <pre><code>class AuthTest extends TestCase
  {
      use DatabaseMigrations;
  
      public function testShouldSignIn()
      {
          // Arrange
          $body = [
              'client_id' =&gt; (string) $this-&gt;client-&gt;id,
              'client_secret' =&gt; $this-&gt;client-&gt;secret,
              'email' =&gt; 'test@test.test',
              'password' =&gt; 'secret',
          ];
          // Act
          $this-&gt;json('POST', '/api/signin', $body, ['Accept' =&gt; 'application/json'])
          // Assert
          -&gt;assertStatus(200)
          -&gt;assertJsonStructure([
              'data' =&gt; [
                  'jwt' =&gt; [
                      'access_token',
                      'expires_in',
                      'token_type',
                  ]
              ],
              'errors'
          ]);
      }
  }
  </code></pre>
  
  <p>My handy authentication with passport for testing purposes</p>
  
  <h3>routes/api.php</h3>
  
  <pre><code>Route::post('/signin', function () {
      $args = request()-&gt;only(['email', 'password', 'client_id', 'client_secret']);
      request()-&gt;request-&gt;add([
          'grant_type' =&gt; 'password',
          'client_id' =&gt; $args['client_id'] ?? env('PASSPORT_CLIENT_ID', ''),
          'client_secret' =&gt; $args['client_secret'] ?? env('PASSPORT_CLIENT_SECRET', ''),
          'username' =&gt; $args['email'],
          'password' =&gt; $args['password'],
          'scope' =&gt; '*',
      ]);
      $res = Route::dispatch(Request::create('oauth/token', 'POST'));
      $data = json_decode($res-&gt;getContent());
      $isOk = $res-&gt;getStatusCode() === 200;
      return response()-&gt;json([
          'data' =&gt; $isOk ? [ 'jwt' =&gt; $data ] : null,
          'errors' =&gt; $isOk ? null : [ $data ]
      ], 200);
  });
  </code></pre>