jeudi 5 septembre 2019

How to mock API token to test my controllers

I want to write some feature tests to test my api Controllers in Laravel. I'm using DingoAPI to build my API and tymondesigns/jwt-auth library to authenticate. While testing I'm using sqlite to perform actions on a database.

In previous project when I kept users in LDAP (now I keep users in my DB) I'm just created few test users and use them to authorize and get token before any request. It was worked pretty good but I'm not sure it is good practice.

My actual code looks like that:

public function testGetBuildingsList()
    {
        $this->assertEquals(200, $this->get('/api/objects')->getStatusCode());
    }

And of course I'm getting error, because I don't send auth token and my status code is 401.

Is possible to mock API token to provide keeping unnecessary test users in database? Or maybe my approach is wrong and there is a completely different way to test my API responses.

Aucun commentaire:

Enregistrer un commentaire