2

So I want to unit test my API endpoints. I'm using Laravel 5.8 and api authentication is done with Passport I have the following test:

public function guest_can_login()
{
    $user = factory(User::class)->create();

    $response = $this->json('POST', 'api/login', [
        'email' => $user->email,
        'password' => 'secret',
    ]);

    $response
        ->assertStatus(200)
        ->assertJson([
            'success' => true,
        ]);
}

when i execute the request with Postman it works well, authenticates user and return a token but when i launch the test it fails with ("Expected status code 200 but received 500.")

So i searched a little deeper in the response and i found this:

"message": "Personal access client not found. Please create one."

no clue why this is happening, if anyone has insight on this

EDIT: fixed see my last answer below

5 Answers 5

6

just to make it clear for someone who has the same issue, the problem was that the fact i'm using an inmemory database for testing, it resets everytime you run the tests so of course the passport keys are wiped aswell, that's why adding \Artisan::call('passport:install'); at the start of your test fixes this issue.

3

If you are using the refresh database trait

public function setUp(){
    parent::setUp();
     \Artisan::call('passport:install');  
  }
1
  • 1
    I'm using Laravel 7 with php 7.2.29 and I had to put 'void' as return type public function setUp(): void May 12, 2020 at 15:28
1

You are seeing this error because you haven't created any passport client. Firstly, make sure you have installed passport correctly. Then run the following command in the root directory of your project to create access client;

php artisan passport:install

0
1

The passport setup when running the test was not found that is why it is giving the error 500 when running the test. the command artisan passport install setup the passport for the testing environment to run the test that is why it is working well with the command line below;

  \Artisan::call('passport:install');
1
  • 1
    Hi and welcome to Stack Overflow! Please take the tour. The exact same command was already suggested in the accepted answer and another answer and with more code and an explanation. Can you edit your answer so it adds something new to the question? Dec 6, 2020 at 7:32
0

this is how I had my setup written... it used to work without the passport hash now that is hashed it does not work... anyone know why? it would be cool thnx...

public function setUp(): void
{
    parent::setUp();

    Route::get('/test-route', function (Request $request) {
        return $request->user();
    })->middleware('auth:api');

    $clientRepository = new ClientRepository();

    $personal_access_client = $clientRepository->createPersonalAccessClient(
        null,
        'Personal Access Client Test',
        '/'
    );

    DB::table('oauth_personal_access_clients')->insert([
        'client_id' => $personal_access_client->id,
        'created_at' => date('Y-m-d'),
        'updated_at' => date('Y-m-d'),
    ]);
}
1
  • to anyone having the problem I had with the hashed passwords... it is as simple as rewriting the config('passport')... Apr 24, 2021 at 2:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.