Thursday, November 06, 2008

Creating Functional Tests for Restful Authencation based websites

At eSpace we stress on having automated tests to cover our code.

If you are using Restful Authentication plugin in your rails application, You will have to bypass the authentication filter that is processed before your controller actions.Restful authentication helps you does that through the login_as() method of theAuthenticatedTestHelper module. Let me show you how.

In your functional test, login before you send the request.

   class PublicationsControllerTest < ActionController::TestCase
       fixtures :users

       def test_method
          login_as (:user_one)
          Your test code...
       end
   end

That user is loaded from the users fixtures and will act as the current logged in user. The fixtures must have something like:

user_one:
    id: 1
    login: test
    email: test@espace.com.eg
    salt: 7e3041ebc2fc05a40c60028e2c4901a81035d3cd
    crypted_password: 00742970dc9e6319f8019fd54864d3ea740f04b1 # test

The login_as method is defined in the AuthenticatedTestHelper module, so you need to make sure you are including that module (preferably in the test helper)

Now you can create functional tests for controllers that requires login.

No comments: