Skip to content

Commit 790ea2d

Browse files
pierre-Hdunglas
authored andcommitted
Add support of jwt with behat (#288)
1 parent 95591e4 commit 790ea2d

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

core/jwt.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,74 @@ security:
6060
- { path: ^/, roles: [ ROLE_READER ] }
6161
```
6262
63+
## Testing with Behat
64+
65+
Let's configure Behat to automatically send a `Authorization` HTTP header containing a valid JWT token when a scenario is marked with a `@login` annotation. Edit `features/bootstrap/FeatureContext.php` and add the following methods:
66+
67+
```php
68+
<?php
69+
70+
// features/bootstrap/FeatureContext.php
71+
72+
use AppBundle\Entity\User;
73+
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
74+
use Behatch\Context\RestContext;
75+
76+
class FeatureContext implements Context, SnippetAcceptingContext
77+
{
78+
// ...
79+
// Must be aster createDatabase() and dropDatabase() functions (the order matters)
80+
81+
/**
82+
* @BeforeScenario
83+
* @login
84+
*
85+
* @see https://symfony.com/doc/current/security/entity_provider.html#creating-your-first-user
86+
*/
87+
public function login(BeforeScenarioScope $scope)
88+
{
89+
$user = new User();
90+
$user->setUsername('admin');
91+
$user->setPassword('ATestPassword');
92+
$user->setEmail('[email protected]');
93+
94+
$this->manager->persist($user);
95+
$this->manager->flush();
96+
97+
$token = $this->jwtManager->create($user);
98+
99+
$this->restContext = $scope->getEnvironment()->getContext(RestContext::class);
100+
$this->restContext->iAddHeaderEqualTo('Authorization', "Bearer $token");
101+
}
102+
103+
/**
104+
* @AfterScenario
105+
* @logout
106+
*/
107+
public function logout() {
108+
$this->restContext->iAddHeaderEqualTo('Authorization', '');
109+
}
110+
}
111+
```
112+
113+
Then, update `behat.yml` to inject the `lexik_jwt_authentication.jwt_manager`:
114+
115+
```yaml
116+
# behat.yml
117+
default:
118+
# ...
119+
suites:
120+
default:
121+
contexts:
122+
- FeatureContext: { doctrine: '@doctrine', 'jwtManager': '@lexik_jwt_authentication.jwt_manager' }
123+
- Behat\MinkExtension\Context\MinkContext
124+
- Behatch\Context\RestContext
125+
- Behatch\Context\JsonContext
126+
# ...
127+
```
128+
129+
Finally, mark your scenarios with the `@login` annotation to automatically add a valid `Authorization` header and with `@logout` to be sure to detroy the token after this scerario.
130+
63131
Previous chapter: [FOSUserBundle Integration](fosuser-bundle.md)
64132

65133
Next chapter: [NelmioApiDocBundle integration](nelmio-api-doc.md)

index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
20. [FOSUserBundle Integration](core/fosuser-bundle.md)
8383
1. [Creating a `User` Entity with Serialization Groups](core/fosuser-bundle.md#creating-a-user-entity-with-serialization-groups)
8484
21. [Adding a JWT authentication using LexikJWTAuthenticationBundle](core/jwt.md)
85+
1. [Testing with Behat](core/jwt.md#testing-with-behat)
8586
22. [NelmioApiDocBundle integration](core/nelmio-api-doc.md)
8687
23. [AngularJS Integration](core/angularjs-integration.md)
8788
1. [Restangular](core/angularjs-integration.md#restangular)

0 commit comments

Comments
 (0)