2015-05-05 21 views
11

Ho appena aggiunto tymondesigns/jwt-auth per supportare l'autenticazione del token e, di conseguenza, i miei casi di test non funzionano perché non esiste alcun token nei parametri dell'intestazione. Come posso prendere in giro (usando Mockery) un componente per bypassare questo?Come imitare tymondesigns/jwt-auth in Laravel 5?

Nota:$this->be() non funziona

+0

Avete trovato una soluzione per questo? –

+0

Ho letto l'intero codice sorgente e preso in giro ogni chiamata di metodo da jwtauth. Ma sembra che Laravel 5.1 abbia un tratto per aggirare i middleware, potresti voler dare un'occhiata a –

+0

Sono anche molto interessato a una soluzione per questo. Ho provato a disattivare il middleware ma non riesco a farlo funzionare. Hai risolto questo? @ChristopherFrancisco – Mattias

risposta

8

Un alternativa è di autenticare la richiesta durante l'esecuzione del test di superamento di un particolare utente. Ecco come lo ha fatto:

# tests/TestCase.php 

/** 
* Return request headers needed to interact with the API. 
* 
* @return Array array of headers. 
*/ 
protected function headers($user = null) 
{ 
    $headers = ['Accept' => 'application/json']; 

    if (!is_null($user)) { 
     $token = JWTAuth::fromUser($user); 
     JWTAuth::setToken($token); 
     $headers['Authorization'] = 'Bearer '.$token; 
    } 

    return $headers; 
} 

Poi nel mio test ho usare in questo modo:

# tests/StuffTest.php 

/** 
* Test: GET /api/stuff. 
*/ 
public function testIndex() 
{ 
    $url = '/api/stuff'; 

    // Test unauthenticated access. 
    $this->get($url, $this->headers()) 
     ->assertResponseStatus(400); 

    // Test authenticated access. 
    $this->get($url, $this->headers(User::first())) 
     ->seeJson() 
     ->assertResponseOk(); 
} 

Spero che questo aiuto a tutti voi. Buona programmazione!