Parham Doustdar
Parham Doustdar

Reputation: 2039

How can I test a page behind a security firewall in Symfony?

In Symfony's cookbook, there is a page entitled How to Simulate Authentication with a Token in a Functional Test. In it, it is said that:

The technique described in How to Simulate HTTP Authentication in a Functional Test is cleaner and therefore the preferred way.

Also, on the page that the quotation above links to, the documentation says:

The trick is to include the http_basic key in your firewall, along with the form_login key

This tells me that it is all right to have the form_login key, along with the http_basic key, and somehow http_basic should take precedence.

Here is my config_test.yml configuration file:

imports:
    - { resource: config_dev.yml }

framework:
    test: ~
    session:
        storage_id: session.storage.mock_file
    profiler:
        collect: false

web_profiler:
    toolbar: false
    intercept_redirects: false

swiftmailer:
    disable_delivery: true

liip_functional_test:
    cache_sqlite_db: true

doctrine:
    dbal:
        default_connection: default
        connections:
            default:
                driver:   pdo_sqlite
                path:     %kernel.cache_dir%/test.db

security:
    firewalls:
        default:
            http_basic: ~

However, when I open my application in the test environment, I still get redirected to the login_form URL.

Why isn't setting the http_basic acting like the documentation says it should, namely it getting activated rather than form_login?

Upvotes: 2

Views: 1238

Answers (2)

Parham Doustdar
Parham Doustdar

Reputation: 2039

As commented here, having the code I pasted in my original question works just fine. The reason it is loading the login form is because I am not logging in via http_basic. In other words, when I have both form_login and http_basic enabled, I can login both by providing the PHP_AUTH_USER/PHP_AUTH_PASSWORD, and by logging in through the form. In effect, I don't need different security_*.yml files; I just need to add http_basic: ~ to the already-defined firewall.

Upvotes: 2

Valentas
Valentas

Reputation: 2175

Just split security.yml to security_test.yml and security_prod.yml. In security_test.yml put default security configuration (as delivered with Symfony) or other one, which doesn't have firewall restrictions.

Create a specific config file for test environment, like config_test.yml with

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

note here config.yml itself doesn't have any security imports, because You will receive some Exception about overriding security directive or smth.

Create a separate config_prod.yml with

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

Now You have separate security for test and prod environments.

If Your environment naming is good, then Kernel will pick config_test.yml only when tests are executed. For development environment Your should use config_dev.yml instead of config_test.yml

Upvotes: 1

Related Questions