4

I'm trying to use the following technique described on symfony.com : http://symfony.com/doc/current/cookbook/testing/http_authentication.html in an attempt to functionally test a controller that requires a user to be logged in.

So far, my login form is working, I can login, and the Symfony2 debug web toolbar is showing my user as authenticated. Also, I have already written a functional test for the login process itself, this passes. So I now by two scenarios, that my login is working.

The only problem I'm having, is when trying to simulate HTTP authentication like so for other controllers:

$client = static::createClient(array(), array(
    'PHP_AUTH_USER' => 'tester',
    'PHP_AUTH_PW'   => 'testpass',
));

I can see by inspecting the $client, that I am being redirected to my login page, the moment I try something like this:

$crawler = $client->request('GET', '/');

I know that the user tester with password testpass exists in the DB, as I can login with that account via browser as well.

I can use the following code from the security controller test:

    $client = $this->createClient(array(), array('HTTP_HOST' => 'myapp.test'));

    // Visit user login page and login
    $crawler = $client->request('GET', '/login');
    $form = $crawler->selectButton('Login')->form();
    $crawler = $client->submit($form, array('_username' => 'tester', '_password' => 'testpass'));

    // ... carry on with remainder of tests

but I'm not too sure if this is the most efficient way to do this.

I'm a bit stunned as to what is wrong. Any ideas? Was there a change applied to Symfony2 that means that this process has changed and the HTTP authentication simulation now doesn't work or works differently?

hakre
  • 193,403
  • 52
  • 435
  • 836
josef.van.niekerk
  • 11,941
  • 20
  • 97
  • 157

2 Answers2

5

Thinking about it, I might just do the login using the following setUp method:

public function setUp()
{
    // Perform user login.
    $this->client = $this->createClient(array(), array('HTTP_HOST' => 'scire.test'));
    $crawler = $this->client->request('GET', '/login');
    $form = $crawler->selectButton('Login')->form();
    $this->client->submit($form, array('_username' => 'tester', '_password' => 'tester'));
}

The HTTP authentication won't work here, unless I alter my config_test.yml with some security setup to allow HTTP authentication.

Note to self: HTTP authentication is different from using a Doctrine user provider!!!

josef.van.niekerk
  • 11,941
  • 20
  • 97
  • 157
  • I am having trouble with the exact same thing. I've managed to get logged in using your method but every time i try to access another page i have to go via the login again, which ends up putting me in a loop. – greg Jul 18 '12 at 18:07
0

Send your request like this:

<?php
$client->request(
    'GET',
    '/',
    array(),
    array(),
    array('PHP_AUTH_USER' => 'username', 'PHP_AUTH_PW' => 'pa$$word')
);

This actually sends the information as real authentication header information.

pfeigl
  • 457
  • 5
  • 12