symfony 测试 - 经过身份验证的新请求不起作用
symfony test - authenticated doesnt work on new request
Good morning.
Tldr:我认为,我需要 Symfony 测试客户端类似于 js xhr 设置的东西:withcredentals:true.
Symfony 3.1. I have action in rest api controller:
/**
* @Rest\GET("/get-stuff")
* @Rest\View
* @Security("is_granted('IS_AUTHENTICATED_FULLY')")
*/
public function GetStuffAction($userId)
{
// get userId from session
$userId = $this->getUser()->getId();
$em = $this->getDoctrine()->getManager();
$Stuff = $em->getRepository('MyApiBundle:Stuff')->findBy(['user' => $userId]);
return $this->viewGroup($Stuff, ['stuff']);
}
它确实工作正常。
现在我要测试,所以我有:
class TestCase extends WebTestCase
{
public function testIndex()
{
$this->client = self::createClient();
$this->storage = new MockFileSessionStorage(__dir__.'/../../../../app/cache/test/sessions');
$this->session = new Session($this->storage);
$this->logIn($this->getUser(), new Response());
$this->client->request('GET', '/get-stuff');
$content = $this->client->getResponse()->getContent();
$this->assertEquals({"message":"Full authentication is required to access this resource.","code":0},$content);
}
public function logIn(User $user, Response $response)
{
$this->session->start();
$this->cookie = new Cookie('MOCKSESSID', $this->storage->getId());
$this->cookieJar = new CookieJar();
$this->cookieJar->set($this->cookie);
$this->token = new UsernamePasswordToken($user, 'user', 'main', $user->getRoles());
$this->session->set('_security_main', serialize($this->token));
$this->getSecurityManager()->loginUser(
$this->container->getParameter('fos_user.firewall_name'),
$user,
$response
);
$this->session->save();
}
}
并测试它给我消息:"Full authentication is required to access this resource"。在方法 logIn() 之后,我可以读取与登录用户连接的会话数据。
我怎样才能在这部分登录而不接收关于身份验证的消息?:
$this->client->request('GET', '/get-stuff');
$content = $this->client->getResponse()->getContent();
更多详情:
1. 我的测试 class 扩展了 WebTestCase。
2.我的用户被FosUserBundle覆盖了。
我想这类似于 javascript:
var xhr = new XMLHttpRequest();
xhr.withCredentials = 真;
但我不知道是什么。
预先感谢您帮助解决我的问题!
好的,我是这样做的好方法:
public function logIn(User $user) {
$session = $this->session;
$firewallContext = 'secured_area';
$token = new UsernamePasswordToken($user, 'user', 'main', $user->getRoles());
$session->set('_security_'.$firewallContext, serialize($token));
$session->save();
$cookie = new Cookie($session->getName(), $session->getId());
$this->client->getCookieJar()->set($cookie);
}
Good morning.
Tldr:我认为,我需要 Symfony 测试客户端类似于 js xhr 设置的东西:withcredentals:true.
Symfony 3.1. I have action in rest api controller:
/**
* @Rest\GET("/get-stuff")
* @Rest\View
* @Security("is_granted('IS_AUTHENTICATED_FULLY')")
*/
public function GetStuffAction($userId)
{
// get userId from session
$userId = $this->getUser()->getId();
$em = $this->getDoctrine()->getManager();
$Stuff = $em->getRepository('MyApiBundle:Stuff')->findBy(['user' => $userId]);
return $this->viewGroup($Stuff, ['stuff']);
}
它确实工作正常。
现在我要测试,所以我有:
class TestCase extends WebTestCase
{
public function testIndex()
{
$this->client = self::createClient();
$this->storage = new MockFileSessionStorage(__dir__.'/../../../../app/cache/test/sessions');
$this->session = new Session($this->storage);
$this->logIn($this->getUser(), new Response());
$this->client->request('GET', '/get-stuff');
$content = $this->client->getResponse()->getContent();
$this->assertEquals({"message":"Full authentication is required to access this resource.","code":0},$content);
}
public function logIn(User $user, Response $response)
{
$this->session->start();
$this->cookie = new Cookie('MOCKSESSID', $this->storage->getId());
$this->cookieJar = new CookieJar();
$this->cookieJar->set($this->cookie);
$this->token = new UsernamePasswordToken($user, 'user', 'main', $user->getRoles());
$this->session->set('_security_main', serialize($this->token));
$this->getSecurityManager()->loginUser(
$this->container->getParameter('fos_user.firewall_name'),
$user,
$response
);
$this->session->save();
}
}
并测试它给我消息:"Full authentication is required to access this resource"。在方法 logIn() 之后,我可以读取与登录用户连接的会话数据。 我怎样才能在这部分登录而不接收关于身份验证的消息?:
$this->client->request('GET', '/get-stuff');
$content = $this->client->getResponse()->getContent();
更多详情: 1. 我的测试 class 扩展了 WebTestCase。 2.我的用户被FosUserBundle覆盖了。
我想这类似于 javascript: var xhr = new XMLHttpRequest(); xhr.withCredentials = 真; 但我不知道是什么。
预先感谢您帮助解决我的问题!
好的,我是这样做的好方法:
public function logIn(User $user) {
$session = $this->session;
$firewallContext = 'secured_area';
$token = new UsernamePasswordToken($user, 'user', 'main', $user->getRoles());
$session->set('_security_'.$firewallContext, serialize($token));
$session->save();
$cookie = new Cookie($session->getName(), $session->getId());
$this->client->getCookieJar()->set($cookie);
}