title: Laravel running tests as different users
body: <p>I am using Laravel 5.1 and I am trying to test my controllers.</p>
  
  <p>I have several roles for my users and policies defined for different actions. Firstly, each of the requests needs to be made by an authenticated user, so running a test with no user returns a 401 Unauthorized, as expected.</p>
  
  <p>But when I want to test the functionality for authorized users, I still get the 401 Unauthorized status code.</p>
  
  <p>It may be worth mentioning that I use basic stateless HTTP authentication on these controllers.</p>
  
  <p>I have tried the following:</p>
  
  <pre><code>public function testViewAllUsersAsAdmin()
  {
      $user = UserRepositoryTest::createTestAdmin();
  
      Auth::login($user);
  
      $response = $this-&gt;call('GET', route('users.index'));
      $this-&gt;assertEquals($response-&gt;getStatusCode(), Response::HTTP_OK);
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>public function testViewAllUsersAsAdmin()
  {
      $user = UserRepositoryTest::createTestAdmin();
  
      $response = $this-&gt;actingAs($user)
          -&gt;call('GET', route('users.index'));
  
      $this-&gt;assertEquals($response-&gt;getStatusCode(), Response::HTTP_OK);
  }
  </code></pre>
  
  <p>and also this (in case there was anything wrong with my new user, which there shouldn't be)</p>
  
  <pre><code>public function testViewAllUsersAsAdmin()
  {
      $user = User::find(1);
  
      $response = $this-&gt;actingAs($user)
          -&gt;call('GET', route('users.index'));
  
      $this-&gt;assertEquals($response-&gt;getStatusCode(), Response::HTTP_OK);
  }
  </code></pre>
  
  <p>but in every case I get a 401 response code so my tests fail.</p>
  
  <p>I can access the routes fine using postman when logging in as a dummy user.</p>
  
  <p>I am running out of ideas, so any help would be appreciated.</p>