-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Merged and improved the articles about testing + authentication #7507
8000 New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,26 +4,24 @@ | |
How to Simulate HTTP Authentication in a Functional Test | ||
======================================================== | ||
|
||
If your application needs HTTP authentication, pass the username and password | ||
as server variables to ``createClient()``:: | ||
Authenticating requests in functional tests can slow down the entire test suite. | ||
This could become an issue especially when the tests reproduce the same steps | ||
that users follow to authenticate, such as submitting a login form or using | ||
OAuth authentication services. | ||
|
||
$client = static::createClient(array(), array( | ||
'PHP_AUTH_USER' => 'username', | ||
'PHP_AUTH_PW' => 'pa$$word', | ||
)); | ||
This article explains the two most popular techniques to avoid these issues and | ||
create fast tests when using authentication. | ||
|
||
You can also override it on a per request basis:: | ||
Using a Faster Authentication Mechanism Only for Tests | ||
------------------------------------------------------ | ||
|
||
$client->request('DELETE', '/post/12', array(), array(), array( | ||
'PHP_AUTH_USER' => 'username', | ||
'PHP_AUTH_PW' => 'pa$$word', | ||
)); | ||
When your application is using a ``form_login`` authentication, you can make | ||
your tests faster by allowing them to use HTTP authentication. This way your | ||
tests authenticate with the simple and fast HTTP Basic method whilst your real | ||
users still log in via the normal login form. | ||
|
||
When your application is using a ``form_login``, you can simplify your tests | ||
by allowing your test configuration to make use of HTTP authentication. This | ||
way you can use the above to authenticate in tests, but still have your users | ||
log in via the normal ``form_login``. The trick is to include the ``http_basic`` | ||
key in your firewall, along with the ``form_login`` key: | ||
The trick is to use the ``http_basic`` authentication in your application | ||
firewall, but only in the configuration file used by tests: | ||
|
||
.. configuration-block:: | ||
|
||
|
@@ -54,3 +52,72 @@ key in your firewall, along with the ``form_login`` key: | |
), | ||
), | ||
)); | ||
|
||
Tests can now authenticate via HTTP passing the username and password as server | ||
variables using the second argument of ``createClient()``:: | ||
|
||
$client = static::createClient(array(), array( | ||
'PHP_AUTH_USER' => 'username', | ||
'PHP_AUTH_PW' => 'pa$$word', | ||
)); | ||
|
||
The username and password can also be passed on a per request basis:: | ||
|
||
$client->request('DELETE', '/post/12', array(), array(), array( | ||
'PHP_AUTH_USER' => 'username', | ||
'PHP_AUTH_PW' => 'pa$$word', | ||
)); | ||
|
||
Creating the Authentication Token | ||
--------------------------------- | ||
|
||
If your application uses a more advanced authentication mechanism, you can't | ||
use the previous trick, but it's still possible to make tests faster. The trick | ||
now is to bypass the authentication process, create the *authentication token* | ||
yourself and store it in the session. | ||
|
||
This technique requires some knowledge of the security component internals, | ||
but the following example shows a complete example that you can adapt to your | ||
needs:: | ||
|
||
// src/AppBundle/Tests/Controller/DefaultControllerTest.php | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be changed to |
||
namespace Appbundle\Tests\Controller; | ||
|
||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; | ||
use Symfony\Component\BrowserKit\Cookie; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; | ||
|
||
class DefaultControllerTest extends WebTestCase | ||
{ | ||
private $client = null; | ||
|
||
public function setUp() | ||
{ | ||
$this->client = static::createClient(); | ||
} | ||
|
||
public function testSecuredHello() | ||
{ | ||
$this->logIn(); | ||
$crawler = $this->client->request('GET', '/admin'); | ||
|
||
$this->assertSame(Response::HTTP_OK, $this->client->getResponse()->getStatusCode()); | ||
$this->assertSame('Admin Dashboard', $crawler->filter('h1')->text()); | ||
} | ||
|
||
private function logIn() | ||
{ | ||
$session = $this->client->getContainer()->get('session'); | ||
|
||
// the firewall context defaults to the firewall name | ||
$firewallContext = 'secured_area'; | ||
|
||
$token = new UsernamePasswordToken('admin', null, $firewallContext, array('ROLE_ADMIN')); | ||
$session->set('_security_'.$firewallContext, serialize($token)); | ||
$session->save(); | ||
|
||
$cookie = new Cookie($session->getName(), $session->getId()); | ||
$this->client->getCookieJar()->set($cookie); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
whilst
=>while
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I treat them as synonyms. I think this is correct 🤓 but let's wait for more opinions.