jeudi 2 avril 2020

API PLATFORM ApiTestCase authentication send unexpected response

  • Api platform version : 2.5.4
  • PHPUnit version : 9.0.2
  • Symfony version : 5.0.7

I made an API with API Platform. I handle security with a custom guard authenticator based on the symfony doc and a bearer JWT token. When I'm testing it manually with Postman, all is ok and works fine. When I'm testing it with the ApiTestCase class provided with API Platform, I got two kinds of error. They both happen in the same case :

  • test 1 -> not authenticated -> response is ok
  • test 2 -> authenticated -> response is ok
  • test 3 -> authenticated or not -> response is wrong

When I am requesting a collection, the API only returns the first 2 or 3 elements and not all of them as it supposed to do.

When I am requestion an item, I simply got a 401 auth error.

Here is the code of my test class to reproduce the error :

<?php

namespace App\Tests;

use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\ApiTestCase;

class MyCollectionTest extends ApiTestCase
{
    public function test1() #test is passing
    {
        $client = static::createClient();
        $response = $this->client->request(
            'GET',
            '/foos',
            [
                'headers'=> [],
                'json' => []
            ]
        );

        $this->assertResponseStatusCodeSame(200);
        $this->assertJsonEquals(['item1', 'item2', 'item3','item4', 'item5', 'item6']);
    }


    public function test2() #test is passing
    {
        $client = static::createClient();
        $response = $this->client->request(
            'GET',
            '/foos',
            [
                'headers'=> ['Authorization'=>'Bearer:valid_token'],
                'json' => []
            ]
        );

        $this->assertResponseStatusCodeSame(200);
        $this->assertJsonEquals(['item1', 'item2', 'item3','item4', 'item5', 'item6']);
    }

    public function test3() #test is invalid, only 'item1', 'item2', 'item3' are returned
    {
        $client = static::createClient();
        $response = $this->client->request(
            'GET',
            '/foos',
            [
                'headers'=> [],
                'json' => []
            ]
        );

        $this->assertResponseStatusCodeSame(200);
        $this->assertJsonEquals(['item1', 'item2', 'item3','item4', 'item5', 'item6']);
    }
}
?>
<?php

namespace App\Tests;

use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\ApiTestCase;

class MyItemTest extends ApiTestCase
{
    public function test1() #test is passing
    {
        $client = static::createClient();
        $response = $this->client->request(
            'GET',
            '/foos/1',
            [
                'headers'=> [],
                'json' => []
            ]
        );

        $this->assertResponseStatusCodeSame(200);
        $this->assertJsonEquals(['item1']);
    }


    public function test2() #test is passing
    {
        $client = static::createClient();
        $response = $this->client->request(
            'GET',
            '/foos/1',
            [
                'headers'=> ['Authorization'=>'Bearer:valid_token'],
                'json' => []
            ]
        );

        $this->assertResponseStatusCodeSame(200);
        $this->assertJsonEquals(['item1']);
    }

    public function test3() #test is invalid, 401 is returned
    {
        $client = static::createClient();
        $response = $this->client->request(
            'GET',
            '/foos/1',
            [
                'headers'=> [],
                'json' => []
            ]
        );

        $this->assertResponseStatusCodeSame(200);
        $this->assertJsonEquals(['item1']);
    }
}
?>

Any idea ? I already posted an issue on the Api Platform github last week but got no responses...

Aucun commentaire:

Enregistrer un commentaire