I am new to rails and I am writing some tests using Capybara and cucumber.
Scenario: Successful login
Given a user "admin#adm.com" with password "admin123"
And I am on the sign in page
Given /^I am on the sign in page$/ do |page_name|
visit('/users/sign_in')
save_and_open_page
end
I am having trouble figuring out a way to write a named path instead of the '/users/sign_in' url.
How can I figure it out?
The way to do this is,
Go to the project root directory and run
rake routes
This will give a list of all the requests handled by rails as follows:
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
So looking at the first column above, we see the "new_user_session"
This can be written as new_user_session_path and placed in visit() as:
visit(new_user_session_path)
And (as far as I know) it works just fine :)
Related
I have a Rails 3.2 app with Devise for authenticating users.
On local development mode, the Sign-out link is not working. It redirects me to home page instead of showing Sign-in page and the session is not destroyed. But on Heroku, clicking on Sign-out link properly logs me out and shows me the Sign-in page destroying the user session.
In application .haml layout page, the link to Sign-out page is:
= link_to "Sign Out", destroy_user_session_path, :method => :delete
Related routes:
devise_for :users, :controllers => { :sessions => 'sessions' }
new_user_session GET /users/sign_in(.:format) sessions#new
user_session POST /users/sign_in(.:format) sessions#create
destroy_user_session DELETE /users/sign_out(.:format) sessions#destroy
I have inherited the Devise::SessionsController to SessionsController in my app as below:
class SessionsController < Devise::SessionsController
layout 'devise_layout'
end
Now the weird case is that, after the session is expired which is default 30 minutes, I log in again and click on Sign-out link, it redirects me back to Sign-in page.
All works fine on Heroku, it fails on local. I am unable to figure it out what is happening on local.
I don't think I am doing anything wrong here because same is deployed to Heroku and is working fine there. What's wrong with development mode on local?
Oops...answering late.
Anyway, I was able to figure out the issue. Actually the app is subdomain based. So I used lvh.me:3000 for testing on local as localhost:3000 doesnt support subdomain.
The workaround is to set subdomain as "lvh.me" in session_store.rb.
domain: 'lvh.me'
Uploading files won't work for me. The file does get saved but looking at the parameters there is no authenticity token sent by CKeditor. So I get a "Can't verify CSRF authenticity" warning, which resets the session, then I can't update the post.
The token is set in the form but CKEditor doesn't seem to send it for picture or file uploads. I think this may have been OK when I used CKE with Paperclip, but not certain of that. All other CKE operations work.
Answered at Ruby on Rails: problem getting CKeditor to upload images
You need to skip verification
skip_before_filter :verify_authenticity_token, :only => [:new_from_disk]
Please see the ckeditor code:
class Ckeditor::ApplicationController < ::ApplicationController
Please add some alike:
skip_before_filter :verify_authenticity_token
I have been trying to authenticate users using twitter-omniauth gem for last days, yet not successful. (Authentication with facebook works perfectly)
I'm keep getting 401 Unauthorized error.
I search through stackoverflow, but none of the answers could solve my problem.
I reach the twitter login when I try http://127.0.0.1/users/auth/twitter. I login and I'm redirected to http://127.0.0.1/users/auth/twitter/callback and unauthorized error comes.
Below callback url I have entered in twitter
http://127.0.0.1/users/auth/twitter/callback
rake routes output
new_user_session GET /users/sign_in(.:format) {:action=>"new", :controller=>"devise/sessions"}
user_session POST /users/sign_in(.:format) {:action=>"create", :controller=>"devise/sessions"}
destroy_user_session DELETE /users/sign_out(.:format) {:action=>"destroy", :controller=>"devise/sessions"}
user_omniauth_callback /users/auth/:action/callback(.:format) {:action=>/twitter|facebook/, :controller=>"users/omniauth_callbacks"}
user_password POST /users/password(.:format) {:action=>"create", :controller=>"devise/passwords"}
new_user_password GET /users/password/new(.:format) {:action=>"new", :controller=>"devise/passwords"}
edit_user_password GET /users/password/edit(.:format) {:action=>"edit", :controller=>"devise/passwords"}
PUT /users/password(.:format) {:action=>"update", :controller=>"devise/passwords"}
cancel_user_registration GET /users/cancel(.:format) {:action=>"cancel", :controller=>"devise/registrations"}
user_registration POST /users(.:format) {:action=>"create", :controller=>"devise/registrations"}
new_user_registration GET /users/sign_up(.:format) {:action=>"new", :controller=>"devise/registrations"}
edit_user_registration GET /users/edit(.:format) {:action=>"edit", :controller=>"devise/registrations"}
PUT /users(.:format) {:action=>"update", :controller=>"devise/registrations"}
DELETE /users(.:format) {:action=>"destroy", :controller=>"devise/registrations"}
login /login(.:format) {:action=>"login", :controller=>"home"}
root / {:controller=>"home", :action=>"index"}
If you need anymore info, I'll provide. Please help me to solve this.
I had this issue today and found that I had to set a callback URL in the settings at dev.twitter.com under my application. So check that you have set a callback URL settings, I set it to my intended production address and when running in dev mode locally it redirected fine.
It mentions under the callback url input box that if left blank it will restrict the app from using callback urls.
I'm pretty sure I ran into issues with this when it was coming from localhost...
This article says they allow it, but if memory serves me correctly, I had to use an externally accessible URL. When I was testing my oauth stuff, I used DynDns and opened a port in my router.
In my case I used "Access Token" and secret instead of "Consumer Key" caused this error.
In my case, we were using the Timecop gem and it was causing the omniauth-twitter gem to fail because of a conflict with the local server time being set by timecop.
I ran into this issue because I had put my keys in my environment in ~/.bash_profile, but i never sourced it. Once i opened a new terminal it worked fine.
Apparently Twitter added a checkbox on the App Settings tab.
At the bottom of the "Application details" pane, you have to check the box saying "Allow this application to be used to Sign in with Twitter".
After checking it and waiting a few minutes as the alert box is telling me it all began to work.
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!
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