jeudi 16 février 2017

Laravel 5.3 PHPUnit Tests Using JWTAuth Requests Don't Require Token and Service Container Persists Between Requests

I'm using JWTAuth tokens for authentication. In the tests I've been passing the token up on each request after receiving it upon authentication, but when a few specific tests needed to be run on a set of different users the tests that worked for single users began to fail, which didn't make sense.

Through logging I can see that regardless that I'm invoking logout the facade I use on each request to get the authenticated user is only constructed on the first request and then persists. So even though the user was logged out, and a new user was logged in, and is now attempting to create an application the first user is still being used.

Is there a way to have the tests work similar to the application where the user is authenticated based on the token? and not stored or persisted in the service container? I noticed through debugging that I didn't even need to pass a token on my requests for the request to make it through to the service, which doesn't mimic the actual application and is troubling since I can't seem to figure out how to turn that off.

The example below works for a single user, but when iterated over using multiple users the rule that a single user can't have two open applications throws and indicates the first user on the second iteration is being used.

public function applicant_can_prequalify_if_age_of_majority_for_province_or_older()
{
    // Arrange
    $this->runPrequalifySeeders();

    $provinces = Province::select('id', 'name', 'majority_age')->get();
    $provinces->each(function ($province) {

        // Act
        $provinceName = str_replace(' ', '', strtolower($province->name));
        $username = "{$provinceName}@example.com";

        $applicant = $this->createApplicant($username);
        $this->login($applicant->username);

        Log::info($applicant->username); // correct username: check
        Log::info($this->token); // different token each time: check

        // Applicants can only have one application at a time hence the
        // new user for each iteration per province: fails on second
        // iteration as service container persisted... I think
        $application = $this->createNewApplication()->decodeResponseJson();

        // Removed for brevity...

        $this->logout();
    });
}



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2lcY1bU
via IFTTT

Aucun commentaire:

Enregistrer un commentaire