Testing authlogic-openid with cucumber and webrat

20 views
Skip to first unread message

Mike Barton

unread,
Aug 25, 2009, 12:01:28 PM8/25/09
to Authlogic
Hi,

I'm finding it difficult to test authlogic-openid in rails using
cucumber and webrat. My problems centre around webrat logging into the
application and creating user sessions to test the application.

First I can't simulate an openid login using cucumber and webrat. I
trying using the rots ruby gem which provides a local open id server,
but webrat won't follow the authentication process as the redirects
are viewed as external. Second I'm not sure how to test user sessions
with cucumber since I can't log into the application using webrat.
I've tried the following code in my steps:

UserSession.create(users(:ben))

This doesn't throw any errors but it doesn't look like the session is
persisted in the cucumber tests.

I was hoping for some guidance on where I'm going wrong and how best
to do integrative testing of authlogic-openid.

Any help is really appreciated as I've run out of ideas.

Mike

cee-dub (Cameron Walters)

unread,
Aug 26, 2009, 2:13:23 PM8/26/09
to Authlogic
Hey Mike,

We've been using the Mechanize mode of webrat to do our HTTP-based
integration tests with Cucumber.

We use it because it removes the dependence on Rails'
IntegrationSession and allows us to test our full stack (even through
Nginx) including some redirects that are not present in our
application's logic.

Here's some code we've been using that Patches WWW::Mechanize with
some additional methods that make the Mechanize session duck type more
closely to the IntegrationSession.

If you find it useful or have problems integrating it, we'd like to
hear what they are. We intend to contribute this back to Webrat/
Mechanize as it's proved quite useful for us.

http://gist.github.com/171570

-Cameron

Mike Barton

unread,
Aug 26, 2009, 8:41:17 PM8/26/09
to Authlogic
Hi Cameron,

Thanks for your advice and code, this solved the problem. I'm now able
to integration test open id authentication using webrat and cucumber.
I'll try and give you feedback on how this process was for me.

1) I'm not familiar with mechanize so it was a change in thought
process to start a rails application (e.g. ./script/server) and then
run my cucumber tests against it. Do you have any rake tasks to
automatically start/stop the server before and after testing or do you
just leave it running?

2) I initially kept getting errors requiring an absolute url for the
webrat 'visit' method. It was a bit confusing as to fix this but I
just prefixed all the relative urls with localhost:3000 etc..
Coincidently I was reading a blog post (http://bit.ly/ZUX3B) about how
case statements always return an object, therefore my case statement
for determining url paths can just be prefixed with "localhost:3000"
to solve this relative url problem. I've posted an example here
(http://gist.github.com/175970).

3) Since I'm testing against the development database I need to switch
the cucumber database parameters in database.yml. This was important
because I wanted to do things like User.delete_all to clear all users
before testing. If the database parameters aren't changed then any
cucumber code using models interacts with the testing database while
I'm integration testing the development database. I guess you can also
start ./script/server to use the test database which would do the
same?

4) One final note - webrat seems to be producing some behaviour that I
can't replicate in the browser. However this may because I don't
understand rails or authlogic fully. After I register a new user I set
the controller to redirect back to the application root url. This
works in the browser but webrat instead ends up on '/users' the
controller managing authentication. The returned HTML however matches
what I see when I test authentication in the browser. Could you
suggest where I'm going wrong? I've posted the relevant lines from
webrat.log at the end of this email.

Thanks again for your help and code you sent. I would have been stuck
otherwise. I asked the same question on Stack Overflow (http://bit.ly/
3h3AXb) as well as asking my local ruby users group but hadn't got
anywhere.

Cheers

Mike


Webrat log
----------
REQUESTING PAGE: GET http://0.0.0.0:3000/register with {} and HTTP
headers {}
REQUESTING PAGE: POST /users with {"commit"=>"Submit", "user
[username]"=>"john", "user[openid_identifier]"=>"http://localhost:1123/
john.doe?openid.success=true",
"authenticity_token"=>"I7siuYgiWPa5I2oak07lFh/A6hhr06V7uxU3a5QpK+0=",
"user[email]"=>"jo...@doe.com"} and HTTP headers
{"HTTP_REFERER"=>"http://0.0.0.0:3000/register"}


On Aug 26, 7:13 pm, "cee-dub (Cameron Walters)"
Reply all
Reply to author
Forward
0 new messages