我如何在 Symfony 中测试安全防火墙后面的页面?
How can I test a page behind a security firewall in Symfony?
在 Symfony 的食谱中,有一个标题为 How to Simulate Authentication with a Token in a Functional Test 的页面。其中,据说:
The technique described in How to Simulate HTTP Authentication in a Functional Test is cleaner and therefore the preferred way.
此外,在上面引文链接到的页面上,文档说:
The trick is to include the http_basic key in your firewall, along with the form_login key
这告诉我可以同时使用 form_login
键和 http_basic
键,而且 http_basic
应该优先。
这是我的 config_test.yml
配置文件:
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: ~
然而,当我在 test
环境中打开我的应用程序时,我仍然被重定向到 login_form URL.
为什么 http_basic
没有像文档中所说的那样设置,即它被激活而不是 form_login
?
只需将 security.yml
拆分为 security_test.yml
和 security_prod.yml
。
在 security_test.yml
中放置默认安全配置(与 Symfony 一起提供)或其他没有防火墙限制的配置。
为测试环境创建一个特定的配置文件,如config_test.yml
和
imports:
- { resource: config.yml }
- { resource: security_test.yml }
注意这里 config.yml
本身没有任何安全导入,因为你会收到一些关于覆盖 security
指令或 smth 的异常。
用
创建一个单独的config_prod.yml
imports:
- { resource: config.yml }
- { resource: security_prod.yml }
现在您有单独的 security
用于 test
和 prod
环境。
如果您的环境命名良好,那么内核将仅在执行测试时选择 config_test.yml
。对于开发环境你应该使用 config_dev.yml
而不是 config_test.yml
正如 here 评论的那样,我在原始问题中粘贴的代码工作得很好。它加载登录表单的原因是因为我没有通过 http_basic 登录。换句话说,当我同时启用 form_login
和 http_basic
时,我可以通过提供 PHP_AUTH_USER
/PHP_AUTH_PASSWORD
和通过表单登录来同时登录。实际上,我不需要不同的 security_*.yml
个文件;我只需要在已经定义的防火墙中添加http_basic: ~
。
在 Symfony 的食谱中,有一个标题为 How to Simulate Authentication with a Token in a Functional Test 的页面。其中,据说:
The technique described in How to Simulate HTTP Authentication in a Functional Test is cleaner and therefore the preferred way.
此外,在上面引文链接到的页面上,文档说:
The trick is to include the http_basic key in your firewall, along with the form_login key
这告诉我可以同时使用 form_login
键和 http_basic
键,而且 http_basic
应该优先。
这是我的 config_test.yml
配置文件:
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: ~
然而,当我在 test
环境中打开我的应用程序时,我仍然被重定向到 login_form URL.
为什么 http_basic
没有像文档中所说的那样设置,即它被激活而不是 form_login
?
只需将 security.yml
拆分为 security_test.yml
和 security_prod.yml
。
在 security_test.yml
中放置默认安全配置(与 Symfony 一起提供)或其他没有防火墙限制的配置。
为测试环境创建一个特定的配置文件,如config_test.yml
和
imports:
- { resource: config.yml }
- { resource: security_test.yml }
注意这里 config.yml
本身没有任何安全导入,因为你会收到一些关于覆盖 security
指令或 smth 的异常。
用
创建一个单独的config_prod.yml
imports:
- { resource: config.yml }
- { resource: security_prod.yml }
现在您有单独的 security
用于 test
和 prod
环境。
如果您的环境命名良好,那么内核将仅在执行测试时选择 config_test.yml
。对于开发环境你应该使用 config_dev.yml
而不是 config_test.yml
正如 here 评论的那样,我在原始问题中粘贴的代码工作得很好。它加载登录表单的原因是因为我没有通过 http_basic 登录。换句话说,当我同时启用 form_login
和 http_basic
时,我可以通过提供 PHP_AUTH_USER
/PHP_AUTH_PASSWORD
和通过表单登录来同时登录。实际上,我不需要不同的 security_*.yml
个文件;我只需要在已经定义的防火墙中添加http_basic: ~
。