Skip to content

Instantly share code, notes, and snippets.

@denikus
Created January 18, 2012 10:01
Show Gist options
  • Save denikus/1632262 to your computer and use it in GitHub Desktop.
Save denikus/1632262 to your computer and use it in GitHub Desktop.
acceptance test for signin
class Users::SessionController < Devise::SessionsController
def new
render :nothing => true
end
def create
respond_to do |format|
format.json do
response_data = {:email => [], :password => [], :general => []}
#if one of the field is empty
if params[:user][:email].blank?
response_data[:email] << I18n.t("errors.messages.empty")
end
if params[:user][:password].blank?
response_data[:password] << I18n.t("errors.messages.empty")
end
return render :json => response_data, :status => 400 if !response_data[:email].empty? || !response_data[:password].empty?
user = User.find_for_authentication(:email => params[:user][:email])
if !user || !user.valid_password?(params[:user][:password])
logger.info "Total users: #{User.all.length.to_s}"
response_data[:general] << I18n.t("devise.failure.invalid")
return render :json => response_data, :status => 400
end
unless user.confirmed?
response_data[:general] << I18n.t("devise.failure.unconfirmed")
return render :json => response_data, :status => 400
end
if params[:user][:remember_me]=="true"
user.remember_me!
end
sign_in(:user, user)
render :json => {:user => user, :redirect_url => after_sign_in_path_for(user)}
end
end
end
end
require 'spec_helper'
describe "Sign in" do
it "should sign in with correct data", :js => true do
password = 'test123'
email = '[email protected]'
@user = Factory.create(:user, :password => password, :email => email, :confirmed_at => Time.now)
puts "#{User.all.length.to_s}"
visit root_path
click_link "login_nav_link"
#login window should be visible
find(:role, 'login-window').should have_css('div', :display => 'block')
within(:css, "div#login-window") do
fill_in("user[email]", :with => email)
fill_in("user[password]", :with => password)
check("user[remember_me]")
find(:role, "user_login_submit").click
end
page.find("div.topbar").should have_content("Profile")
end
end
require 'rubygems'
require 'spork'
#add role attribute to capybara selectors
Capybara.add_selector(:role) do
xpath { |role| XPath.descendant[XPath.attr(:role) == role.to_s] }
match { |value| value.is_a?(Symbol) }
end
Spork.prefork do
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
# Add this to load Capybara integration:
require 'capybara/rspec'
require 'capybara/rails'
require 'simplecov'
SimpleCov.start 'rails'
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
config.mock_with :rspec
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = false
config.before :each do
DatabaseCleaner.start
end
config.after :each do
DatabaseCleaner.clean
end
# If true, the base class of anonymous controllers will be inferred
# automatically. This will be the default behavior in future versions of
# rspec-rails.
config.infer_base_class_for_anonymous_controllers = false
config.include Devise::TestHelpers, :type => :controller
#config.treat_symbols_as_metadata_keys_with_true_values = true
#config.filter_run :focus => true
#config.run_all_when_everything_filtered = true
end
end
Spork.each_run do
# This code will be run each time you run your specs.
FactoryGirl.reload
end
(0.1ms) BEGIN
(0.1ms) SAVEPOINT active_record_1
(0.3ms) SELECT 1 FROM `users` WHERE `users`.`email` = BINARY '[email protected]' LIMIT 1
SQL (0.2ms) INSERT INTO `users` (`confirmation_sent_at`, `confirmation_token`, `confirmed_at`, `created_at`, `current_sign_in_at`, `current_sign_in_ip`, `email`, `encrypted_password`, `id`, `last_sign_in_at`, `last_sign_in_ip`, `remember_created_at`, `reset_password_sent_at`, `reset_password_token`, `sign_in_count`, `updated_at`, `username`) VALUES (NULL, NULL, '2012-01-18 10:01:37', '2012-01-18 10:01:37', NULL, NULL, '[email protected]', '$2a$04$fPOQ4m5IoicG2OeqCbAur.LNSXq1uhBnD9Fa6G22TIKag7/X/X0Yq', 1, NULL, NULL, NULL, NULL, NULL, 0, '2012-01-18 10:01:37', NULL)
(0.1ms) RELEASE SAVEPOINT active_record_1
User Load (0.5ms) SELECT `users`.* FROM `users`
Started GET "/" for 127.0.0.1 at 2012-01-18 12:01:41 +0200
Processing by MainController#index as HTML
Rendered matches/_match_modal.html.haml (5.8ms)
Rendered shared/_visitor_navigation.html.haml (3.4ms)
Rendered popups/_register.html.haml (11.9ms)
Rendered popups/_login.html.haml (13.1ms)
Rendered js_templates/_error.html.haml (1.5ms)
Rendered js_templates/_message_boxes.html.haml (4.4ms)
Completed 200 OK in 112ms (Views: 111.9ms | ActiveRecord: 0.0ms)
Started GET "/assets/application.css" for 127.0.0.1 at 2012-01-18 12:01:41 +0200
Served asset /application.css - 200 OK (14ms)
Started GET "/assets/application.js" for 127.0.0.1 at 2012-01-18 12:01:41 +0200
Served asset /application.js - 200 OK (59ms)
Started POST "/users/sign_in" for 127.0.0.1 at 2012-01-18 12:01:42 +0200
Processing by Users::SessionController#create as JSON
Parameters: {"user"=>{"email"=>"[email protected]", "password"=>"[FILTERED]", "remember_me"=>"true"}}
User Load (0.7ms) SELECT `users`.* FROM `users` WHERE `users`.`email` = '[email protected]' LIMIT 1
User Load (0.3ms) SELECT `users`.* FROM `users`
Total users: 0
CACHE (0.0ms) SELECT `users`.* FROM `users` WHERE `users`.`email` = '[email protected]' LIMIT 1
Completed 400 Bad Request in 39ms (Views: 2.8ms | ActiveRecord: 1.0ms)
(0.4ms) ROLLBACK
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment