mercredi 29 juillet 2015

Laravel: Unit testing with authentication

I'm trying to put some tests in my Laravel app. First, I'm checking that the login is working fine :

public function testLogin(){

    $this->visit('/auth/login')
     ->type('mylogin', 'login')
     ->type('mypassword', 'password')
     ->press('Login')
     ->seePageIs('/home');
}

Ok, login is working fine! Now, I would like to check that all the information in the page /accountInfo are correct:

public function testAccountInfoDisplay(){
    $this->visit('/accountInfo')
        ->see('criticaldata');
}

But I never see /accountInfo page, since I'm redirected because I'm not logged in.

I've seen a few solutions in the doc like:

$this->actingAs($user)
             ->withSession(['foo' => 'bar'])
             ->visit('/')
             ->see('Hello, '.$user->name);

But I cannot fake a session, since when I log in, I'm in fact asking for an access token to another server for the authentication. Without this token, I'm unable to display any page, since the data server will refuse the connection.

In a nutshell, I've got this token in the testLogin function, but it disappears after.

I can of course do the login for each test, but that's a lot of requests if I have like 150 tests to run.

Is there a better way to keep this token during all the tests?

Aucun commentaire:

Enregistrer un commentaire