dimanche 17 novembre 2019

Laravel test api controller fail, method on production work fine and logs not show problem

I'm working with controller for my API, and then create a test for method update I cured a very strange thing.

If I test the code in postman, it works correctly, and the PUT / PATCH method works perfectly.

But going to do one of the tests fails me.

Model () // I indicate the relevant since the primary key is not incremental if not the domain column. There is redundant and unnecessary code to try to analyze the problem (logs)

public function update(Request $request, Domain $domain)
{
    if (!Auth::user()) {
        return  $this->sendError('Unauthenticated User', [], Response::HTTP_FORBIDDEN);
    }

    if (empty($request->all())) {
        return  $this->sendError(
            'Not data for update ' . $domain->domain,
            [],
            Response::HTTP_UNPROCESSABLE_ENTITY
        );
    }

    Log::debug('Domain user_id: '.$domain->user_id.' '.Auth::user()->id);
    Log::debug('Is super admin '.(Auth::user()->is_super_admin ? 'true' : 'false'));

    if (($domain->user_id !== Auth::user()->id) && (!Auth::user()->is_super_admin)) {
        Log::debug('After conditional Domain user_id: '.$domain->user_id.' '.Auth::user()->id);
        Log::debug('After conditional Is super admin '.(Auth::user()->is_super_admin ? 'true' : 'false'));
        return $this->sendError(
            'The user can\'t update the domain '.$domain->domain,
            [],
            Response::HTTP_FORBIDDEN
        );
    }

    $domain->update($request->all());

    if (empty($domain->getChanges())) {
        return $this->sendError(
            'Any change on domain '.$domain->domain,
            [],
            Response::HTTP_PRECONDITION_FAILED
        );
    }

    return $this->sendResponse($domain, 'Domain data updated');
}

Well, if in postman run a PUT with an authenticated user, and owner of the domain, https://mydomain.test/api/v1/domains/baumbach.com?maxquota=2048199 get the correct response

{
    "success": true,
    "data": {
        "domain": "baumbach.com",
        "user_id": 2,
        ...
        "maxquota": "2048199",
        ...
        "updated_at": "2019-11-17 11:55:26"
    },
    "message": "Domain data updated"
}

Code of my test

function an_owner_user_can_edit_a_domain()
{
    $user = Passport::actingAs(
        factory(User::class)->create(['is_super_admin' => false])
    );

    $domain = factory(Domain::class)->create(['user_id' => $user->id]);

    $params = ['maxquota' => 204800];

    $response = $this->json('PUT', '/api/v1/domains/'.$domain->domain, $params);

    $response
        ->assertStatus(200)
        ->assertJsonPath('message', 'Domain data updated');;
}

Call to test

1) Tests\Feature\Domains\DomainsEditionFeatureTest::an_owner_user_can_edit_a_domain
Expected status code 200 but received 403.
Failed asserting that false is true.

/home/abkrim/Sites/albarid/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestResponse.php:183
/home/abkrim/Sites/albarid/tests/Feature/Domains/DomainsEditionFeatureTest.php:55

Well logs in all cases

[2019-11-17 12:03:52] testing.DEBUG: Domain user_id: 18 18  
[2019-11-17 12:03:52] testing.DEBUG: Is super admin false  
[2019-11-17 12:03:52] testing.DEBUG: After conditional Domain user_id: 18 18  
[2019-11-17 12:03:52] testing.DEBUG: After conditional Is super admin false  
[2019-11-17 12:06:31] local.DEBUG: Domain user_id: 2 2  
[2019-11-17 12:06:31] local.DEBUG: Is super admin false 

I certainly feel stunned, obtuse and unable to do anything to solve it. I do not understand what the nature of the failure is, and surely it is there, with the naked eye.

Aucun commentaire:

Enregistrer un commentaire