Cucumber BDD with capybara and devise integration - devise

As a followup to my previous question on SO, I have followed the tutorial at https://github.com/RailsApps/rails3-devise-rspec-cucumber/wiki/Tutorial religiously to try pinpoint the origin of my test failures.
My basic scenario fails:
Feature: Sign in
Scenario: User signs in successfully with email
Given I am a new, authenticated user
When I go to the tour page
Then I should be signed in
These are my steps:
Given /^I have one\s+user "([^\"]*)" with password "([^\"]*)"$/ do |email, password|
u = User.new(:email => email,
:password => password,
:password_confirmation => password)
u.skip_confirmation!
u.save!
end
Given /^I am a new, authenticated user$/ do
email = 'user#test.com'
password = 'please'
Given %{I have one user "#{email}" with password "#{password}"}
And %{I go to the sign in page}
And %{I fill in "user_email" with "#{email}"}
And %{I fill in "user_password" with "#{password}"}
And %{I press "Log Me In"}
end
Then /^I should be signed in$/ do
And %{I should see "Sign out"}
end
The login fails even though my test user is correctly created. Using save_and_open_page shows that capybara fills the form as expected so it seems like this is a devise issue.
I am wondering whether there is an integration issue with the components since I am using a rails 3.0 setup (the tutorial is on 3.1).
My environment is using the following:
ruby 1.8.7
rails 3.0.3
capybara 1.0.0
cucumber 1.0.0
devise 1.2.rc
rake 0.9.2

Related

devise redirects to login after login

When a user logins into my page it redirects them to the sign-in page without error. It actually logs them in but for some reason it won't redirect to the user's show page.
I have this, which as worked in other cases, in my application controller:
def after_sign_in_path_for(user)
user_url(user)
end
here is the link to my repo https://github.com/samwat2/Ships-Project
I'm new to rails and I'm not quiet sure this isn't working! When it has before...
From looking at your repo, you added the two custom fields :first_name and :last_name to the registration form, and you validate the presence of those attributes. If you have added these validations after creating some users, and try to login with a user that misses some of these fields, devise will encounter a validation error when saving the user and redirect back to the login form, even though the authentication itself was successful.
Try making the users valid by filling the missing attributes, and the login should behave again as intended.

Log user in via frontend in Selenium Capybara Rspec Tests

I want to be logged in as a user during my tests that use javascript, so I figured I should have a helper method which logs a user in via frontend.
However with the helper
def log_user_in_via_frontend(user)
visit root_path
fill_in 'user_session_login', :with => user.login
fill_in 'user_session_password', :with => user.password
click_on('Login')
end
I am able to be logged in and am redirected to the right page as long as I don't put :js => true at the end. I call this helper via
user = FactoryGirl.create(:user)
log_user_in_via_frontend(user)
Visiting root_path afterwards yields No route matches {:controller=>\"users\", :action=>\"show\", :id=>#<User id: nil, login: nil...
What's the best way to have an logged in user in my selenium tests?
Actually by now this works for me. I am not sure what for exactly, but I think the key is deactivate transitional fixture, and to have database cleaner do the cleaning.

basic http authentication - duration of the session

I have configured basic authentication (in controller)
above everything in controller file:
before_filter :check_logged_in, :only => [:edit, :update, :destroy]
On the bottom of the file:
private
def check_logged_in
authenticate_or_request_with_http_basic("Ads") do |username, password|
username == "admin" && password == "apple"
end
end
It works like a charm, but there is one thing I don;t understand - when I provide username and password it stays logged in for a long period of time and when I click on 'delete' or 'update' for specific entries I'm not getting prompted again. I thought something went wrong, but when I opened another browser - it prompted me again, but only once, I didn;t have to authenticate for the rest.
Then I thought it was a cookie issue, but nothing changed even though I deleted all the cookies in Chrome. So I got a couple of questions:
Is there any way to say for how long I'm going to be authenticated?
Is there any way to be prompted for authentication everytime I click on the resource mentioned here - :check_logged_in, :only => [:edit, :update, :destroy] ?
HTTP basic authentication doesn´t use cookies. The login information is sent with every HTTP request to the specified web server. You are logged in until you close your web browser or delete all active logins.

test sign up with devise

Devise 1.2 ruby on rails
I'm having difficulty testing sign up. When the user clicks sign up, they're logged in and i should see a flash message. This works but my test fails. Not sure why. How does sign up work? is there some sort of internal redirect that happens? This step fails:
Then I should see "You have registered successfully. If enabled, a confirmation was sent your e-mail."
Confirmation is not enabled in my user model.
Tehcnically, you shouldn't feel the need to unit test the devise mechanism--the gem itself is well-tested. I can understand wanting to make sure it is behaving the way you configured it though, so:
Devise definitely redirects after a successful authentication. It will set the flash message and then redirect either to what you set as the root in your routes file, or if you attempted to access a page within the site and got redirected to the login page, it will redirect you back to the page you were trying to access.
For your test, try testing that you get redirected to what you set as root in your routes.rb fil. I.e. in the devise instructions, it says to set it like
root :to => "home#index"
So, in your test try something like this:
require 'spec_helper'
describe YourController do
include Devise::TestHelpers
before (:each) do
#user = Factory.create(:user)
sign_in #user
end
describe "GET 'index'" do
it "should be successful" do
get 'index'
response.should be_success
end
it "should redirect to root" do
get 'index'
response.should redirect_to(root_url)
end
end
You can add your flash message test to this as well. Hope this helps!

RSpec with Capybara - test signing in with OpenID

I am creating request specs following the Railscast at http://railscasts.com/episodes/257-request-specs-and-capybara
In my application, users sign in using their Google OpenID accounts with OmniAuth (http://railscasts.com/episodes/241-simple-omniauth). How can I test this with RSpec and Capybara? When my application redirects to the Google sign in page, I get the following error:
ActionController::RoutingError:
No route matches "/accounts/o8/ud"
It seems that it doesn't allow redirecting away from the application, so how should I test this?
I have no experience with Capybara and can therefore not comment on your question, however, I have saved a bookmark for later usage that may be useful to you: http://blog.zerosum.org/2011/03/19/easy-rails-outh-integration-testing.html