Logging in through authlogic without having to fill out a form each time

I have a number of Cucumber scripts that work with capybara in a project I'm working on.

Most of these scenarios begin with the step "Given that I am logged in." Currently my implementation is:

visit path_to('the login page') fill_in('Username', :with => 'user') fill_in('Password', :with => 'password') click_button('Login') 

This works great, but it takes a little time to download and submit the login form before each script.

Is there a way to simply set up a session at this point without having to go through the form each time?

+7
authlogic cucumber capybara
source share
3 answers

A bit late for the show, as usual, but this works for me on Rails 3.0.10.

In features/support/authlogic.rb :

 require "authlogic/test_case" World(Authlogic::TestCase) ApplicationController.skip_before_filter :activate_authlogic Before do activate_authlogic end 

Then at features/step_definitions/user_sessions_steps.rb

 Given /^I am already logged in$/ do UserSession.create!(User.find_by_name!('user')) end 

Obviously, you can pass the username to the step definition if you want to log in for a specific user.

Details are in this blog post: http://laserlemon.com/blog/2011/05/20/make-authlogic-and-cucumber-play-nice/

+2
source share

you can use Background in the cucumber.

 Background: Given I am a logged-in admin user 

which will dry your scripts.

http://github.com/aslakhellesoy/cucumber/wiki/background

0
source share

Borrowing a trick from another gem Sorcery wiki: Integration Testing with Rspec, Capybara and Fabricator , they use:

 page.driver.post(user_sessions_url, { username: user, password: password }) 

Which basically calls the user_sessions_controller#create method, so make sure the arguments match. So for me I did:

 activate_authlogic page.driver.post(user_sessions_url, { user_session: { email: user.email, password: user.password } }) 

I have been looking for this for several days. and UserSession.create! couldn't work for me. Although this is an old question, I'm on Rails 4, hopefully this can help others get stuck.

0
source share

All Articles