Symfony2 + Behat/Mink + User Token

1,352 views
Skip to first unread message

kwattro

unread,
Apr 10, 2012, 3:12:56 PM4/10/12
to be...@googlegroups.com
Hello guys,

I'm building a little registration and authentication system for sf2, development driven by Behat/Mink.

When arriving to the point that I have to check the User roles after the login, I come up with a problem, The User Token seems to be empty so the call to the ->getToken()->getUser() is invalid.

Here is my code :

Feature: Login
In order to be authenticated
As a visitor
I need to login
Background:
Given There is a "user" User with a "password" Password
Scenario: A user login
Given I am on "/login"
Then I should see "Username"
And I should see "Password"
And I fill in "username" with "user"
And I fill in "password" with "password"
And I press "submit_login"
Then I should be on "/"
When I go to "/demo/secured/hello/world"
Then I should have a "ROLE_USER" role

/**
    * @Then /I should have a "([^"]*)" role/
    */
    public function iShouldHaveARole($role)
    {
        $container = $this->getContainer();
        $context = $container->get('security.context');
        $user = $context->getToken()->getUser();

        assertTrue($user->hasRole($role));
    }

I think I miss something to pass the session between the steps, but don't know what ?

Any suggestions ?

Thanks

Regards,

Christophe

kwattro

unread,
Apr 11, 2012, 4:17:03 AM4/11/12
to be...@googlegroups.com
Well it is solved, I needed to set the Token manually in the context. I made abstraction of it as it use a browser driver so I was thinking like I was browsing.

Cheers,

@clemherreman

unread,
Apr 11, 2012, 4:40:16 AM4/11/12
to be...@googlegroups.com
If I remember good, behat (with the BehatBundle) doesn't **share** the symfony2 kernel, but **clone** it. This is why you can't log in via some method and has to simulate inputs in the login form.
That's also why you doesn't seem to have any role in your User after you logged in.


Le mardi 10 avril 2012 21:12:56 UTC+2, kwattro a écrit :

Christophe Willemsen

unread,
Apr 11, 2012, 5:03:54 AM4/11/12
to be...@googlegroups.com
Hi @clemherreman,

Oh that's good to know. Btw If I create a Given statement "When I am logged in as" and I manually create the token and set it in the Security Context, I can retrieve the user roles in further steps.

Thanks for your answer.

Grtz

Christophe

kwattro

unread,
Apr 11, 2012, 12:29:40 PM4/11/12
to be...@googlegroups.com
Momently I'm doing this :

Scenario: A user has a default role when he's logged in
    Given I am logged in as User
    Then I should have a "ROLE_USER" role

/**
    * @Given /^(?:|I )am logged in as User$/
    */
    public function IAmLoggedInAsUser()
    {
        return array(
            new Step\When('I am on "/login"'),
            new Step\When('I fill in "username" with "user"'),
            new Step\When('I fill in "password" with "password"'),
            new Step\When('I press "submit_login"'),
            new Step\Then('I have a token for "user" User')
            );
    }

    /**
    * @Then /^(?:|I) have a token for "([^"]*)" User$/
    */
    public function IHaveATokenForUser($user)
    {
        $user = $this->getRepository('HoverflySecurityBundle:User')->findOneByUsername($user);
            $context = $this->getContainer()->get('security.context');
            $provider = 'hoverflyers';
            $token = new UsernamePasswordToken($user, null, $provider, $user->getRoles());
            $context->setToken($token);

    }
    /**
    * @Then /I should have a "([^"]*)" role/
    */
    public function iShouldHaveARole($role)
    {

        $user = $this->getContainer()->get('security.context')->getToken()->getUser();

        assertTrue(in_array($role, $user->getRoles()));
    }
Is it the right way ?

Luis Cordova

unread,
Apr 11, 2012, 12:30:51 PM4/11/12
to be...@googlegroups.com
how can you copy paste such nice code, what tool are you using?

Christophe Willemsen

unread,
Apr 11, 2012, 12:44:59 PM4/11/12
to be...@googlegroups.com

how well i've copy pasted from my blog in draft mode. the css is from the twitter bootstrap. i use only the <code> tags in the html.

ptit cub

unread,
Jul 18, 2012, 7:43:00 AM7/18/12
to be...@googlegroups.com
Hi Kwattro,

Did you have one answer about User Token? 
If yes, could you show us how do you send token...?


Thanks

ptitcub



Le mercredi 11 avril 2012 18:44:59 UTC+2, kwattro a écrit :

how well i've copy pasted from my blog in draft mode. the css is from the twitter bootstrap. i use only the <code> tags in the html.

Le 11 avr. 2012 18:30, "Luis Cordova" <cord...@gmail.com> a écrit :
how can you copy paste such nice code, what tool are you using?

Christophe Willemsen

unread,
Jul 18, 2012, 10:00:42 AM7/18/12
to be...@googlegroups.com

Hi.

Yes I found a solution. In fact you can log in your user manually by generating the token.

I do not have the code here right now, ill send you when im at home this evening

ptit cub

unread,
Jul 18, 2012, 11:15:52 AM7/18/12
to be...@googlegroups.com
Hi, 

I've just found the solution too, thanks to @ubermuda on http://knplabs.fr/blog/behat-like-a-boss-meta-steps : 

    /**
     * @Given /^(?:|I )am logged in as Administrator$/
     */
    public function iAmLoggedInAsAdministrator()
    {
        $this->visit('/connexion');
        $this->fillField('_username', 'e...@mail.com' );
        $this->fillField('_password', 'pass');
        $this->pressButton('_submit_login');
        $this->visit('/');
    }    

The important things to know are :
- $this->visit 
- no need to disable csrf_protection
- no need to override security section into config_test.yml but just insert :
  framework:
      test: ~
- in behat.yml, here's my config:

user:
    context:
      class:  Path\ToMyBundle\Features\Context\UserContext
    extensions:
      Behat\Symfony2Extension\Extension: 
          mink_driver: true
          kernel:
            env: test
            debug: true          
      Behat\MinkExtension\Extension:
          base_url: 'http://my_url.dev/app_test.php/'
          goutte:    ~
          default_session: symfony2

- In my user.feature : 
  Scenario: View users connected
    Given I am logged in as Administrator
    Then I should see "some_element_of_page"
       
Hope this help other users...

Ptitcub



Le mardi 10 avril 2012 21:12:56 UTC+2, kwattro a écrit :
Reply all
Reply to author
Forward
0 new messages