wout
wout

Reputation: 2577

Testing login session with Behat

I am testing an XML and JSON REST API with Behat. For authentication, I use Sentinel. Now, if I log in a user through Behat, and I test the status of the login session, the API returns a negative response (e.g. user not logged in). When I do the exact same thing manually in a browser using AJAX requests, everything works fine. So the end result is fine but the scenario fails. Any suggestions on how to overcome this issue?

UPDATE:

I'll elaborate a bit more on the setup of the app, which is based on Slim, Twig, Eloquent and Sentinel.

The API has some routes to create, show (yes or no) and delete user session. So logging in a user is done by POSTing an email address and password to /session.json or /session.xml. Behind the scenes, the controller will basically call Sentinel::authenticate(...).

This all works fine in the browser. If I perform a GET request to /session.json, I will see the user is logged in correctly. If I follow the same steps with Behat, I will get the right return value on the POST request, so Sentinel says the user is logged in. But the GET request will tell me the user is not logged in.

To give you an idea, the Behat scenario looks something like this:

Scenario Outline: Logging in a user
  Given I have a user with "<email>" and "<password>"
  And I use the "<format>" format
  When I log in the user with "<email>" and "<password>"
  Then the "success" message should be "session.active"

  Examples:
    | email | password | format |
    | [email protected] | 32165487 | xml    |
    | [email protected] | 89056734 | json   |

In the context I use the Requests library to actually perform the requests. So Behat acts as a regular client.

Upvotes: 0

Views: 677

Answers (0)

Related Questions