Symfony2 authenticate user through an API on integration testing

438 views Asked by At

I would like to authenticate the user through an API (external user provider) running my integration tests.

my config_test.yml

imports:
    - { resource: config.yml }
    - { resource: parameters_test.yml }

framework:
    test: ~
    session:
        storage_id: session.storage.mock_file

monolog:
    handlers:
        main:
            type:  stream
            path:  %kernel.logs_dir%/%kernel.environment%.log
            level: info

After the login (SUCCEED) the next step is ask for user information and I got:

"Full authentication is required to access this resource."

I guess something is happening with the stored session. I am using Redis on dev and prod to store sessions. Mocking sessions on the test environment because of

"Failed to start the session because headers have already been sent by "/path/to/bin/phpunit" at line 2."

Doing it manually is working like a charm.

1

There are 1 answers

7
origaminal On

session.storage.mock_file uses by default %kernel.cache_dir%/sessions for a storing of session's file. Check if this folder is writable for both server user and cli user.

'Setting up Permissions' http://symfony.com/doc/current/book/installation.html#configuration-and-setup

Another 2 possible reasons of error:

  1. stateless: true on your firewall in firewalls section. The ContextListener is not created and Token is not saved to the session. AnonymousToken is assigned to next request.

  2. stateless: true you also can have RememberMeToken instead of AnonymousToken if remember_me feature enabled. This token is also not full fledged.

UPDATE

Ensure that intercept_redirects is false in config_test.yml as it may break all redirects.

web_profiler:
    toolbar: false
    intercept_redirects: false