LiipFunctionalTestBundle icon indicating copy to clipboard operation
LiipFunctionalTestBundle copied to clipboard

WebTestCase::loginAs() not working

Open lukepass opened this issue 7 years ago • 4 comments

Hello, I followed the guide to authenticate using WebTestCase::loginAs() but it's not working and it's redirecting me to the login page.

This is my config_test.yml:

imports:
    - { resource: config_dev.yml }

framework:
    test: ~
    session:
        # handler_id set to null will use default session handler from php.ini
        handler_id:  ~
        storage_id: session.storage.mock_file
        name: MOCKSESSID
    profiler:
        collect: false

web_profiler:
    toolbar: false
    intercept_redirects: false

swiftmailer:
    disable_delivery: true

liip_functional_test:
    cache_db:
        sqlite: liip_functional_test.services_database_backup.sqlite

doctrine:
    dbal:
        default_connection: default
        connections:
            default:
                driver:   pdo_sqlite
                path:     "%kernel.cache_dir%/test.db"
security:
    firewalls:
        main:
            http_basic: ~

And this is the test:

class DivisionControllerTest extends FixtureWebTestCase
{
    public function testDivisionList()
    {
        $this->loginAs($this->fixtures->getReference('admin-user'), 'main');
        $client = $this->makeClient();

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

        // successful
        $this->isSuccessful($client->getResponse());

        // more than 1 division
        $this->assertGreaterThan(
            1,
            $crawler->filter('tr')->count()
        );
    }
}

And this is the log:

[2019-01-16 12:49:32] request.INFO: Matched route "teacher_division_list". {"route":"teacher_division_list","route_parameters":{"orderBy":"name","_controller":"AppBundle\\Controller\\DivisionController::listAction","_route":"teacher_division_list"},"request_uri":"http://localhost/teachers/divisions/list","method":"GET"} []
[2019-01-16 12:49:32] security.INFO: Populated the TokenStorage with an anonymous Token. [] []
[2019-01-16 12:49:32] security.DEBUG: Access denied, the user is not fully authenticated; redirecting to authentication entry point. {"exception":"[object] (Symfony\\Component\\Security\\Core\\Exception\\AccessDeniedException(code: 403): Access Denied. at /xyz/vendor/symfony/symfony/src/Symfony/Component/Security/Http/Firewall/AccessListener.php:68)"} []
[2019-01-16 12:49:32] security.DEBUG: Calling Authentication entry point. [] []

Preconditions

  1. Symfony 3.4
  2. PHP v7.2
  3. Liip v2.0.0-alpha12

Steps to reproduce

See above.

Expected result

See above.

Actual result

See above.

lukepass avatar Jan 16 '19 11:01 lukepass

Hello, any news on this subject? Thanks!

lukepass avatar Oct 02 '19 08:10 lukepass

No, sorry, is the problem still here with the 3.x branch?

alexislefebvre avatar Oct 12 '19 00:10 alexislefebvre

Got same problem here .. We got 302 redirection (due to Access Denied)

ElNelyo avatar Jan 28 '20 15:01 ElNelyo

Is it working for you if you follow the redirect?

$client->followRedirects();

I just got into the same issue. With auto-follow the client goes to the login page, gets authenticated and redirected back. So the tests succeeds, but this feels weird.

althaus avatar Apr 29 '21 14:04 althaus