I have a problem with my unit test when i wanna test controller with authentication.
My test controller :
class AcademyControllerTest extends WebTestCase {
private $client = null;
public function setUp() {
$this->client = static::createClient();
}
public function testSecuredHello() {
$this->logIn();
$crawler = $this->client->request('GET', '/academies');
$this->assertSame(Response::HTTP_OK, $this->client->getResponse()->getStatusCode());
}
private function logIn() {
$session = $this->client->getContainer()->get('session');
// the firewall context defaults to the firewall name
$firewallContext = 'default';
$token = new UsernamePasswordToken('admin', null, $firewallContext, array('ROLE_SUPER_ADMIN'));
$session->set('_security_' . $firewallContext, serialize($token));
$session->save();
$cookie = new Cookie($session->getName(), $session->getId());
$this->client->getCookieJar()->set($cookie);
}
}
security.yml :
For my app i use "default" section with guard authentication
firewalls:
# disables authentication for assets and the profiler, adapt it according to your needs
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
default:
pattern: ^/
guard:
authenticators:
- app.authenticator
provider: app
logout:
path: /logout
target: /
success_handler: app.authenticator
config_test.yml :
For my test i wanna use http_basic and i redefine "default" section
security:
firewalls:
# replace 'main' by the name of your own firewall
default:
http_basic: ~
When i test with this configuration i have error and this error refere to my guard authentication but it shouldn't use it
If i create new section in security.yml with name "test" like this :
firewalls:
# disables authentication for assets and the profiler, adapt it according to your needs
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
/***************NEW ADD************/
test:
pattern: ^/
http_basic: ~
/***********************************/
default:
pattern: ^/
guard:
authenticators:
- app.authenticator
provider: app
logout:
path: /logout
target: /
success_handler: app.authenticator
And use it in my test controller, it working but my app not using the guard so not working...
please help
==> Symfony 3.4
reference : http://symfony.com/doc/3.4/testing/http_authentication.html
Aucun commentaire:
Enregistrer un commentaire