Devise integration test always fails - ruby-on-rails

I am trying to test my sign in for Devise in rails, but no matter what I do I get a 401 unauthorized error. I think I have set up my testing wrong or something. I don't think it's a syntax error.
Here is what my fixture looks like.
one:
firstname: John
lastname: Smith
email: example#email.com
encrypted_password: <%= Devise::Encryptor.digest(Traveler, 'password') %>
confirmed_at: 2016-01-02 08:31:23
confirmation_sent_at: 2016-01-02 08:30:59
This is what my controller (integration) test looks like.
require 'test_helper'
class TourControllerTest < ActionDispatch::IntegrationTest
include Devise::Test::IntegrationHelpers
def setup
#traveler = travelers(:one)
end
test "successful login of confirmed user" do
get traveler_session_path
assert_equal 200, status
post traveler_session_path 'traveler[email]' => #traveler.email, 'traveler[password]' => #traveler.encrypted_password
follow_redirect!
assert_equal 200, status
end
end
My test.log looks like this
-----------------------------------------------------------
TourControllerTest: test_successful_login_of_confirmed_user
-----------------------------------------------------------
[1m[36mTraveler Load (0.1ms)[0m [1m[34mSELECT "travelers".* FROM "travelers" WHERE "travelers"."id" = ? LIMIT ?[0m [["id", 980190962], ["LIMIT", 1]]
Started GET "/login" for 127.0.0.1 at 2018-03-31 23:43:28 -0400
Processing by Devise::SessionsController#new as HTML
Rendering devise/sessions/new.html.erb within layouts/application
Rendered devise/sessions/new.html.erb within layouts/application (7.9ms)
Rendered shared/_navbar.html.erb (3.6ms)
Rendered shared/_message.html.erb (0.9ms)
Completed 200 OK in 559ms (Views: 557.2ms | ActiveRecord: 0.0ms)
Started POST "/login?traveler%5Bemail%5D=example%40email.com&traveler%5Bpassword%5D=[FILTERED]" for 127.0.0.1 at 2018-03-31 23:43:29 -0400
Processing by Devise::SessionsController#create as HTML
Parameters: {"traveler"=>{"email"=>"example#email.com", "password"=>"[FILTERED]"}}
[1m[36mTraveler Load (0.4ms)[0m [1m[34mSELECT "travelers".* FROM "travelers" WHERE "travelers"."email" = ? ORDER BY "travelers"."id" ASC LIMIT ?[0m [["email", "example#email.com"], ["LIMIT", 1]]
Completed 401 Unauthorized in 6ms (ActiveRecord: 0.4ms)
Processing by Devise::SessionsController#new as HTML
Parameters: {"traveler"=>{"email"=>"example#email.com", "password"=>"[FILTERED]"}}
Rendering devise/sessions/new.html.erb within layouts/application
Rendered devise/sessions/new.html.erb within layouts/application (1.1ms)
Rendered shared/_navbar.html.erb (1.0ms)
Rendered shared/_message.html.erb (0.1ms)
Completed 200 OK in 7ms (Views: 5.0ms | ActiveRecord: 0.0ms)
[1m[35m (0.1ms)[0m [1m[31mrollback transaction[0m

try replace this line
post traveler_session_path 'traveler[email]' => #traveler.email, 'traveler[password]' => #traveler.encrypted_password
to
post traveler_session_path 'traveler[email]' => #traveler.email, 'traveler[password]' => 'password'
according to encrypted_password: <%= Devise::Encryptor.digest(Traveler, 'password') %>
I think the test need to login by email: 'example#email.com' / password: 'password'

Related

Rails devise sign up, sign in not working

I am new to rails and just tried to setup user sign up, sign through devise but none of the sign up and sign in are working, when I click on sign up the page just keeps unchanged, it does not even show error messages.
I just added this in my application.html.erb file for errors
<%- flash.each do |name, msg| -%>
<%= content_tag :div, msg, :id => "flash_#{name}" if msg.is_a?(String) %>
<%- end -%>
Console messages
Started POST "/users" for ::1 at 2022-02-18 21:23:31 +0530
Processing by Devise::RegistrationsController#create as TURBO_STREAM
Parameters: {"authenticity_token"=>"[FILTERED]", "user"=>{"email"=>"ry201#gmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
(0.1ms) SELECT sqlite_version(*)
TRANSACTION (0.2ms) begin transaction
User Exists? (0.2ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = ? LIMIT ? [["email", "ry201#gmail.com"], ["LIMIT", 1]]
TRANSACTION (0.1ms) rollback transaction
Rendering layout layouts/application.html.erb
Rendering devise/registrations/new.html.erb within layouts/application
Rendered devise/shared/_error_messages.html.erb (Duration: 0.9ms | Allocations: 462)
Rendered devise/shared/_links.html.erb (Duration: 0.2ms | Allocations: 93)
Rendered devise/registrations/new.html.erb within layouts/application (Duration: 4.2ms | Allocations: 2047)
Rendered home/_header.html.erb (Duration: 0.2ms | Allocations: 128)
Rendered layout layouts/application.html.erb (Duration: 13.5ms | Allocations: 4452)
Completed 200 OK in 216ms (Views: 15.0ms | ActiveRecord: 1.3ms | Allocations: 10722)

Why does my Capybara feature test intermittently fail when waiting for Ajax?

I'm using Capybara 2.13.0 and I've got a test that relies on an Ajax request adding dynamic content into the DOM.
test 'contact data becomes visible when providing the correct password' do
login_as_researcher
visit '/participants/3'
assert page.has_css?("ul#redacted-contact-data")
click_button('Unlock Contact Data')
# Use the correct password
fill_in('enter your password', with: 'password1')
# Ajax happens here. It renders a partial and places it in the dom.
click_button('Submit')
# Test sometimes fails here..
assert page.has_css?("ul#contact-data"), -> { page.body }
end
I've read a lot of articles that say Capybara automatically waits for elements to appear in the HTML and there's no need to manually tell Capybara to wait anymore, but I'm not experiencing this in my case.
75% of the time this test passes and when it does fail and the page is dumped, it contains the content of the partial rendered in the JS response instead of the full HTML page.
Any idea what's going on here? I can only assume it's due to how long it takes the server to respond the Ajax request each time and Capybara not waiting long enough in certain situations.
I've set Capybara.default_max_wait_time = 5 in my test_helper.
I've tried with both the selenium and poltergeist drivers and it makes no difference.
Here's the output from test.log
Started GET "/" for 127.0.0.1 at 2017-05-01 18:09:23 +0100
Processing by HomeController#index as HTML
Researcher Load (0.3ms) SELECT "researchers".* FROM "researchers" WHERE "researchers"."id" IS NULL LIMIT 1
Redirected to http://www.example.com/session/new
Filter chain halted as :researcher_only rendered or redirected
Completed 302 Found in 30ms (ActiveRecord: 0.3ms)
Started GET "/session/new" for 127.0.0.1 at 2017-05-01 18:09:23 +0100
Processing by SessionsController#new as HTML
Rendered sessions/new.html.erb within layouts/application (9.5ms)
Researcher Load (0.1ms) SELECT "researchers".* FROM "researchers" WHERE "researchers"."id" IS NULL LIMIT 1
Completed 200 OK in 248ms (Views: 247.6ms | ActiveRecord: 0.1ms)
Started POST "/session" for 127.0.0.1 at 2017-05-01 18:09:23 +0100
Processing by SessionsController#create as HTML
Parameters: {"utf8"=>"✓", "username"=>"nick1", "password"=>"[FILTERED]", "commit"=>"Enter"}
Researcher Load (0.1ms) SELECT "researchers".* FROM "researchers" WHERE "researchers"."username" = ? LIMIT 1 [["username", "nick1"]]
Redirected to http://www.example.com/
Completed 302 Found in 78ms (ActiveRecord: 0.1ms)
Started GET "/" for 127.0.0.1 at 2017-05-01 18:09:23 +0100
Processing by HomeController#index as HTML
Researcher Load (0.1ms) SELECT "researchers".* FROM "researchers" WHERE "researchers"."id" = ? LIMIT 1 [["id", 2]]
Rendered home/index.html.erb within layouts/application (0.6ms)
Completed 200 OK in 10ms (Views: 8.6ms | ActiveRecord: 0.1ms)
Started GET "/participants/3" for 127.0.0.1 at 2017-05-01 18:09:23 +0100
Processing by ParticipantsController#show as HTML
Parameters: {"id"=>"3"}
Participant Load (1.9ms) SELECT "participants".* FROM "participants" WHERE "participants"."id" = ? LIMIT 1 [["id", 3]]
Researcher Load (0.1ms) SELECT "researchers".* FROM "researchers" WHERE "researchers"."id" = ? LIMIT 1 [["id", 2]]
ContactData Load (0.2ms) SELECT "contact_data".* FROM "contact_data" WHERE "contact_data"."participant_id" = ? LIMIT 1 [["participant_id", 3]]
AlcoholService Load (0.1ms) SELECT "alcohol_services".* FROM "alcohol_services" WHERE "alcohol_services"."id" = ? LIMIT 1 [["id", 16]]
Baseline Load (0.3ms) SELECT "baselines".* FROM "baselines" WHERE "baselines"."participant_id" = ? LIMIT 1 [["participant_id", 3]]
Rendered participants/_unlock_contact_data.html.erb (1.1ms)
Rendered participants/_details.html.erb (52.8ms)
Rendered participants/show.html.erb within layouts/application (58.8ms)
Completed 200 OK in 94ms (Views: 69.9ms | ActiveRecord: 3.0ms)
Started POST "/participants/unlock_contact_data?participant_id=3" for 127.0.0.1 at 2017-05-01 18:09:24 +0100
Processing by ParticipantsController#unlock_contact_data as JS
Parameters: {"utf8"=>"✓", "from"=>"show", "password"=>"[FILTERED]", "commit"=>"Submit", "participant_id"=>"3"}
Researcher Load (0.1ms) SELECT "researchers".* FROM "researchers" WHERE "researchers"."id" = ? LIMIT 1 [["id", 2]]
Participant Load (0.0ms) SELECT "participants".* FROM "participants" WHERE "participants"."id" = ? LIMIT 1 [["id", 3]]
ContactData Load (0.1ms) SELECT "contact_data".* FROM "contact_data" WHERE "contact_data"."participant_id" = ? LIMIT 1 [["participant_id", 3]]
AlcoholService Load (0.1ms) SELECT "alcohol_services".* FROM "alcohol_services" WHERE "alcohol_services"."id" = ? LIMIT 1 [["id", 16]]
Baseline Load (0.0ms) SELECT "baselines".* FROM "baselines" WHERE "baselines"."participant_id" = ? LIMIT 1 [["participant_id", 3]]
Rendered participants/_details.html.erb (4.6ms)
Rendered participants/unlock_contact_data.js.erb (6.7ms)
Completed 200 OK in 81ms (Views: 10.8ms | ActiveRecord: 0.3ms)
(0.1ms) rollback transaction
(0.1ms) begin transaction
Is there anything I might be missing?

devise throw me out right after sign in on production but working great on development environment

I'm using rails 3.2 and Devise 3.4.1.
On development environment all is working great but on production I'm getting this:
Started POST "/users/sign_in" for 111.111.111.11 at 2015-01-18 15:04:58 +0000
Processing by SessionsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"PGRrSKfVz8fZiv1a1qBJN2TzmFRAvUgEfYRpmdWiPWk=", "user"=>{"email"=>"email#email.com", "password"=>"[FILTERED]"}, "commit"=>"Log in"}
User Load (0.5ms) SELECT `users`.* FROM `users` WHERE `users`.`email` = 'email#email.com' LIMIT 1
(0.2ms) BEGIN
(0.5ms) UPDATE `users` SET `last_sign_in_at` = '2015-01-18 15:04:53', `current_sign_in_at` = '2015-01-18 15:04:59', `sign_in_count` = 25, `updated_at` = '2015-01-18 15:04:59' WHERE `users`.`id` = 2
(1.2ms) COMMIT
Redirected to http://subdomain.domain.com/
Completed 302 Found in 100.0ms (ActiveRecord: 0.0ms)
Started GET "/" for 111.111.111.11 at 2015-01-18 15:04:59 +0000
Started GET "/authorized" for 111.111.111.11 at 2015-01-18 15:04:59 +0000
Processing by AuthorizedController#index as HTML
Completed 401 Unauthorized in 0.3ms
Started GET "/users/sign_in" for 111.111.111.11 at 2015-01-18 15:05:00 +0000
Processing by SessionsController#new as HTML
Rendered devise/shared/_links.html.erb (0.1ms)
Rendered devise/sessions/new.html.erb (4.0ms)
Completed 200 OK in 5.3ms (Views: 4.5ms | ActiveRecord: 0.0ms)
as you can see the user is being authorised but signing out immediately after that.
help?
well, the CDN just blocked Devise's cookie so it's solved now
thank you all!

Debuging Devise in simple Rails app

I configure Devise gem for Rails and in code it seems look OK but when I'm trying to register new user I have the problem.
In register form Devise show notice:
Please review the problems below:
but that's all. Nothing specific message. Where I should looking for first? How to debug something like this?
Any suggestions?
Log from rails server:
Started POST "/auth/register" for 127.0.0.1 at 2014-09-14 17:09:13
+0200 Processing by Devise::RegistrationsController#create as HTML Parameters: {"utf8"=>"✓",
"authenticity_token"=>"iRsLU68tBckSzK1tupMoXZQlGw7xOuzMlfuiIVVs+rc=",
"user"=>{"email"=>"test#test.com", "password"=>"[FILTERED]",
"password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
(0.2ms) BEGIN User Exists (0.4ms) SELECT 1 AS one FROM "users"
WHERE "users"."email" = 'test#test.com' LIMIT 1 (0.2ms) ROLLBACK
Rendered devise/shared/_links.haml (0.5ms) Rendered
devise/registrations/new.html.haml within layouts/application (15.9ms)
Completed 200 OK in 121ms (Views: 24.2ms | ActiveRecord: 0.8ms)
And when I try log in:
Started POST "/auth/login" for 127.0.0.1 at 2014-09-14 17:10:23 +0200
Processing by Devise::SessionsController#create as HTML Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"iRsLU68tBckSzK1tupMoXZQlGw7xOuzMlfuiIVVs+rc=",
"user"=>{"email"=>"test#test.com", "password"=>"[FILTERED]",
"remember_me"=>"0"}, "commit"=>"Sign in"} User Load (0.7ms) SELECT
"users".* FROM "users" WHERE "users"."email" = 'test#test.com' ORDER
BY "users"."id" ASC LIMIT 1 Completed 401 Unauthorized in 2ms
Processing by Devise::SessionsController#new as HTML Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"iRsLU68tBckSzK1tupMoXZQlGw7xOuzMlfuiIVVs+rc=",
"user"=>{"email"=>"test#test.com", "password"=>"[FILTERED]",
"remember_me"=>"0"}, "commit"=>"Sign in"} Rendered
devise/shared/_links.haml (0.4ms) Rendered
devise/sessions/new.html.haml within layouts/application (9.8ms)
Completed 200 OK in 106ms (Views: 15.4ms | ActiveRecord: 0.0ms)
I debug these things by inspecting the object.errors
In this case, try to
raise #user.errors.inspect
Somewhere after the save in the create method

Capybara with selenium fails. Works in browser

I have a simple login system that works in the browser. I recently switched my cucumber tests to use selenium because I need ajax calls and now the login/auth steps do not pass anymore. The app still functions normally in firefox and chromium.
Here are the Steps and the definitions
features/authentication.features
Feature: Require Authentication
In order to restrict access to the app
A User
Must be logged in
Scenario: Accessing Tracks
Given I am not logged in
And I visit the "Tracks" page
Then I should see the "Welcome" page
And I should see "You need to be logged in to access this page"
Scenario: Accessing Tracks when Logged in
Given I am logged in
And I visit the "Tracks" page
Then I should see the "Tracks" page
features/step_definitions/register_and_login_steps.rb
When(/^I log in as "(.*?)"$/) do |name|
create_and_login(name)
end
When(/^I log in$/) do
create_and_login('tester')
end
When(/^I log out$/) do
visit(logout_path)
end
When(/^I should be logged out$/) do
page.should have_title "Welcome"
page.should have_text "Please log in"
end
When(/^I am( not)? logged in$/) do |negative|
if negative
visit(logout_path)
else
create_and_login('anyone')
page.should have_title 'Welcome'
end
end
features/step_definitions/should_see_steps.rb
When(/I should( not)? see the "(.*?)" page$/) do |negative, page_title|
if negative
page.should_not have_title page_title
else
page.should have_title page_title
end
end
When(/^I should( not)? see "(.*?)"$/) do |negative, text|
if negative
page.should_not have_text text
else
page.should have_text text
end
end
When(/^I visit the "(.*?)" page$/) do |page|
path = page.downcase + "_path"
visit_path(path)
end
features/support/helpers/user.rb
def password_for(user)
user + '_password_'
end
def create_user(name)
return if User.exists?(name: name)
user = User.create!(name: name,
password: password_for(name),
password_confirmation: password_for(name))
end
def create_and_login(name)
create_user(name)
visit(logout_path)
visit(login_path)
fill_in 'Name', with: name
fill_in 'Password', with: password_for(name)
click_button 'Login'
end
Gemfile (test group only)
group :test do
gem 'guard-spork', '~> 1.5.0'
gem 'rb-inotify', '~> 0.9.0'
gem 'spork', '~> 0.9.2'
gem 'guard-rspec','~> 2.5.0'
gem 'cucumber-rails' , '~> 1.3.0'
gem 'database_cleaner','~> 0.9.1'
gem 'guard-cucumber', '~> 1.3.2'
gem 'capybara', '~> 2.1.0'
gem 'selenium-webdriver', '~>2.31.0'
end
log/test.log
Connecting to database specified by database.yml
(0.3ms) begin transaction
Started GET "/logout" for 127.0.0.1 at 2013-04-22 01:57:49 +0200
Processing by SessionsController#destroy as HTML
Redirected to http://127.0.0.1:42286/login
Completed 302 Found in 1ms (ActiveRecord: 0.0ms)
Started GET "/login" for 127.0.0.1 at 2013-04-22 01:57:49 +0200
Processing by SessionsController#new as HTML
Rendered sessions/new.html.haml within layouts/application (26.4ms)
Rendered layouts/_player.html.haml (0.9ms)
Completed 200 OK in 64ms (Views: 63.6ms | ActiveRecord: 0.0ms)
Started GET "/assets/application.js" for 127.0.0.1 at 2013-04-22 01:57:49 +0200
Served asset /application.js - 200 OK (8ms)
Started GET "/assets/application.css" for 127.0.0.1 at 2013-04-22 01:57:49 +0200
Served asset /application.css - 200 OK (3ms)
Started GET "/tracks" for 127.0.0.1 at 2013-04-22 01:57:49 +0200
Processing by TracksController#index as HTML
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."session" = '' LIMIT 1
Redirected to http://127.0.0.1:42286/login
Filter chain halted as :require_login rendered or redirected
Completed 302 Found in 62ms (ActiveRecord: 1.6ms)
Started GET "/login" for 127.0.0.1 at 2013-04-22 01:57:49 +0200
Processing by SessionsController#new as HTML
Rendered sessions/new.html.haml within layouts/application (1.8ms)
Rendered layouts/_player.html.haml (0.1ms)
Completed 200 OK in 4ms (Views: 3.8ms | ActiveRecord: 0.0ms)
(0.1ms) rollback transaction
(0.0ms) begin transaction
User Exists (1.1ms) SELECT 1 AS one FROM "users" WHERE "users"."name" = 'anyone' LIMIT 1
(0.0ms) SAVEPOINT active_record_1
User Exists (0.1ms) SELECT 1 AS one FROM "users" WHERE "users"."name" = 'anyone' LIMIT 1
User Exists (0.0ms) SELECT 1 AS one FROM "users" WHERE "users"."session" IS NULL LIMIT 1
Binary data inserted for `string` type on column `password_digest`
Binary data inserted for `string` type on column `session`
SQL (0.3ms) INSERT INTO "users" ("admin", "created_at", "name", "password_digest", "session", "updated_at") VALUES (?, ?, ?, ?, ?, ?) [["admin", nil], ["created_at", Sun, 21 Apr 2013 23:57:49 UTC +00:00], ["name", "anyone"], ["password_digest", "$2a$10$2kPN1wqnXI/G9b/1KMR2x.7yCHCaKwftE7PXm/q4u9Q9bcWCTenMG"], ["session", "91e2394dcdc86ada3836b258ad6bd2c850f99e03"], ["updated_at", Sun, 21 Apr 2013 23:57:49 UTC +00:00]]
(0.0ms) RELEASE SAVEPOINT active_record_1
Started GET "/logout" for 127.0.0.1 at 2013-04-22 01:57:49 +0200
Processing by SessionsController#destroy as HTML
Redirected to http://127.0.0.1:42286/login
Completed 302 Found in 0ms (ActiveRecord: 0.0ms)
Started GET "/login" for 127.0.0.1 at 2013-04-22 01:57:49 +0200
Processing by SessionsController#new as HTML
Rendered sessions/new.html.haml within layouts/application (0.9ms)
Rendered layouts/_player.html.haml (0.0ms)
Completed 200 OK in 2ms (Views: 1.9ms | ActiveRecord: 0.0ms)
Started GET "/login" for 127.0.0.1 at 2013-04-22 01:57:50 +0200
Processing by SessionsController#new as HTML
Rendered sessions/new.html.haml within layouts/application (0.9ms)
Rendered layouts/_player.html.haml (0.0ms)
Completed 200 OK in 2ms (Views: 2.0ms | ActiveRecord: 0.0ms)
Started POST "/sessions" for 127.0.0.1 at 2013-04-22 01:57:50 +0200
Processing by SessionsController#create as HTML
Parameters: {"utf8"=>"✓", "session"=>{"name"=>"anyone", "password"=>"[FILTERED]"}, "commit"=>"Login"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."name" = 'anyone' LIMIT 1
Redirected to http://127.0.0.1:42286/login
Completed 302 Found in 2ms (ActiveRecord: 0.2ms)
Started GET "/login" for 127.0.0.1 at 2013-04-22 01:57:50 +0200
Processing by SessionsController#new as HTML
Rendered sessions/new.html.haml within layouts/application (0.8ms)
Rendered layouts/_player.html.haml (0.1ms)
Completed 200 OK in 2ms (Views: 1.8ms | ActiveRecord: 0.0ms)
Started GET "/tracks" for 127.0.0.1 at 2013-04-22 01:57:50 +0200
Processing by TracksController#index as HTML
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."session" = '' LIMIT 1
Redirected to http://127.0.0.1:42286/login
Filter chain halted as :require_login rendered or redirected
Completed 302 Found in 2ms (ActiveRecord: 0.2ms)
Started GET "/login" for 127.0.0.1 at 2013-04-22 01:57:50 +0200
Processing by SessionsController#new as HTML
Rendered sessions/new.html.haml within layouts/application (27.5ms)
Rendered layouts/_player.html.haml (0.0ms)
Completed 200 OK in 29ms (Views: 28.9ms | ActiveRecord: 0.0ms)
(0.1ms) rollback transaction
The fix was to not set selenium as the default capybara driver and tag the tests that require ajax with #javascript.

Resources