处理认证在Symfony2的单元测试 [英] Handling Authentication in Symfony2 Unit-Tests
问题描述
我想写对于那些通过Symfony2的认证机制保护控制器的功能测试。我看了很多教程描述它但不幸的是他们都没有为我目前Symfony2的版本(2.0.4测试2.0.7到)
工作我迄今所做的:
在添加安全设置config_test.yml
安全性:
EN codeRS:
Symfony的\\分量\\安全\\核心\\用户\\用户名:明文 供应商:
主要:
用户:
管理员:{密码:adminsmurf,角色:['ROLE_USER']}
盘点:{密码:inventorysmurf,角色:['ROLE_ADMIN','ROLE_USER','ROLE_INVENTORY']}
安东:{密码:andonsmurf,角色:['ROLE_ADMIN','ROLE_ANDON']} 防火墙:
主要:
模式:/.*
http_basic:
境界:安全区
供应商:主
退出:真
安全性:真实
无状态:真
匿名:真
单元测试
类DefaultControllerTest扩展WebTestCase
{ 公共职能testCorrectAuthentificationCredentials()
{
$客户端=静态:: createClient(); $履带= $客户 - >请求(GET,/库存/索引,阵列(),阵列()
阵列(
'PHP_AUTH_USER'=> '管理员',
'PHP_AUTH_PW'=> adminsmurf
)); $响应= $客户 - > GETRESPONSE(); $这个 - >的assertEquals(200,$响应 - >的getStatus code());
}
}
此测试失败(预期200,获得302)。任何人可以帮助我在做什么错了?
更新16/12
我得到了一步。由于我使用的登录表单在我的正常安全文件。 Symfony的刚添加的http_basic登录到防火墙。这是为重新导向(302)的原因。我只是增加了一个
form_login:假的
要测试安全设置。现在有没有重定向,但我得到一个401状态code作为一个结果。
我展示了HTTP基本身份验证应该作为第四个参数要求,而不是第五名。来源是文档 - 如果他们不工作,我们需要更新它们。但让我知道:
http://symfony.com/doc/2.0/cookbook/testing/ http_authentication.html
此外,一旦你这个工作正常,你可以把窗体身份验证回来,因为symfony不会有任何理由使用窗体的入口点(小于 - 花式字:))将您重定向。 p>
希望帮助!
I want to write functional tests for Controllers that are secured via the Symfony2 authentication mechanisms. I read a lot of tutorials describing it but unfortunately all of them doesn't work for me with current Symfony2 version (tested 2.0.4 to 2.0.7)
What I have done so far:
Adding security settings in config_test.yml
security:
encoders:
Symfony\Component\Security\Core\User\User: plaintext
providers:
main:
users:
admin: { password: adminsmurf, roles: [ 'ROLE_USER' ] }
inventory: { password: inventorysmurf, roles: [ 'ROLE_ADMIN', 'ROLE_USER', 'ROLE_INVENTORY' ] }
andon: { password: andonsmurf, roles: [ 'ROLE_ADMIN', 'ROLE_ANDON' ] }
firewalls:
main:
pattern: /.*
http_basic:
realm: "Secured Area"
provider: main
logout: true
security: true
stateless: true
anonymous: true
Unit-Test
class DefaultControllerTest extends WebTestCase
{
public function testCorrectAuthentificationCredentials()
{
$client = static::createClient();
$crawler = $client->request('GET', '/inventory/index', array(), array(),
array(
'PHP_AUTH_USER' => 'admin',
'PHP_AUTH_PW' => 'adminsmurf'
));
$response = $client->getResponse();
$this->assertEquals(200, $response->getStatusCode());
}
}
This test fails (Expected 200, get 302). Can anybody help what I'm doing wrong?
Update 16/12
I got a step further. As I'm using a form login in my regular security file. Symfony just added the http_basic login to the firewall. This was the reason for the redirect (302). I just added a
form_login: false
to the test security settings. Now there is no redirect but I get an 401 status code as a result.
I'm showing that the HTTP basic auth should go as the 4th argument to request, not the 5th. Source is the docs - if they don't work, we need to update them. But let me know:
http://symfony.com/doc/2.0/cookbook/testing/http_authentication.html
Also, once you have this working correctly, you can turn form auth back on, as Symfony won't have any reason to use the form's entry point (<-- fancy word :)) to redirect you.
Hope that helps!
这篇关于处理认证在Symfony2的单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!