Search code examples
seleniumruby-on-rails-3.2authlogicrspec-rails

Authlogic / Selenium breaks after rails 3.2.12 upgrade


I'm using Authlogic and having difficulty getting my Selenium tests to work with :js=>true after upgrading to Rails 3.2.12. The same tests use to worked under Rails 3.1.3. I am running my tests using Spork.

I am logging in through Selenium launches Firefox (19.0.2), fills the login form but then I get a permissions error from Authlogic, the standard "You are not allowed to access this action."

I can see that many people are having issues with this but as I mentioned, has only became a problem for me once upgrading from Rails 3.1.3 to Rails 3.2.12. I suspect that the issue may be within my spec_helper file (below) and in particularly within the

module Authlogic

block which I got from here:

Authlogic with Capybara + Cucumber + Selenium Driver not working

**spec_helper.rb**

require 'rubygems'
require 'spork'
require 'authlogic/test_case' 

include Authlogic::TestCase

Spork.prefork do
  ENV["RAILS_ENV"] ||= 'test'
  require File.expand_path("../../config/environment", __FILE__)

  require 'rspec/rails'

  Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

  RSpec.configure do |config|

    config.color_enabled = true

    ApplicationController.skip_before_filter :activate_authlogic
    config.before(:each, :type => :request) do
      activate_authlogic
    end

    config.include FactoryGirl::Syntax::Methods

    config.include Capybara::DSL

    config.use_transactional_fixtures = false

    config.before(:suite) do
      DatabaseCleaner.clean_with(:truncation)
    end

    config.before(:each) do
      DatabaseCleaner.strategy = :transaction
    end

    config.before(:each, :js => true) do
      Capybara.current_driver = :selenium

      DatabaseCleaner.strategy = :truncation

      module Authlogic
          module Session
            module Activation
              module ClassMethods
                def controller
                  if !Thread.current[:authlogic_controller]
                    Thread.current[:authlogic_controller] = Authlogic::TestCase::MockController.new
                  end
                Thread.current[:authlogic_controller]
              end
            end
          end
        end
       end
    end

    config.before(:each) do
      DatabaseCleaner.start
    end

    config.after(:each) do
      DatabaseCleaner.clean
    end

    config.include(MailerMacros)
    config.before(:each) { reset_email }

    config.mock_with :mocha

    config.infer_base_class_for_anonymous_controllers = false

    config.order = "random"

    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
  FactoryGirl.reload
end

My understanding is that Authlogic and Selenium Webdriver work on different threads hence the need for this patch in the spec_helper file.

In the Request Spec I get a permissions error from Authlogic. Here is the Request Spec test in question:

# UNIT REQUEST SPEC
require 'spec_helper'

describe "Units" do

  describe "GET /admin/units/new" do
    before(:each) do
      activate_authlogic
      UserSession.create FactoryGirl.create(:admin_user, :email => "[email protected]", :password => "password", :password_confirmation => "password")      

      visit root_path
      fill_in "user_session[email]", :with => "[email protected]"
      fill_in "user_session[password]", :with => "password" 
      click_button "Sign In"    
    end

    it "displays a pop up dialog after unit is created", :focus, :js => true do
      visit new_admin_unit_path
      fill_in "Title", :with => "Unit Title"
      fill_in "Code", :with => "U-TEST"
      fill_in "Learning Outcome", :with => "Some Learning Outcome"
      fill_in "unit[learning_outcomes_attributes][0][assessment_methods_attributes][0][content]", :with => "Some Assessment Criteria"

      click_button "Save and Publish"      
      page.should have_css('div.ui-dialog')
    end
  end
end

My Capybara tests are working ok with:

activate_authlogic
UserSession.create FactoryGirl.build(:user)

in the before(:each) block, the problem only arises when using :js=>true

So, my questions are:

  • Is it a case that with Rails 3.2.x that the spec_helper fix (module Authlogic ... ) no longer works.
  • If that is in fact the case, what is the "normal" way of getting this to work?

Solution

  • Just revisited this today. When I upgraded Rails I also upgraded capybara to the latest version (2.1.0). It seems that downgrading capybara from this version (2.1.0) to version 2.0.1 seemed to do the trick. I can see that capybara (2.0.1) uses xpath (1.0.0) while capybara (2.1.0) uses xpath (2.0.0) along with a different version of nokogiri gem. I have no idea if there is a link there but here is my gem file if anyone cares to take a look:

    source 'http://rubygems.org'
    
    gem 'rails', '3.2.12'
    
    gem 'rack-mini-profiler'        
    gem 'authlogic'                 
    gem 'rails3-generators'   
    gem 'mysql2'  
    gem 'declarative_authorization' 
    gem 'kaminari'                  
    gem "foreigner"                
    gem 'validates_timeliness'     
    gem "activerecord-import", ">= 0.2.0"
    gem 'ezcrypto'                 
    gem 'thin'                       
    gem 'exception_notification'    
    gem 'mail'
    gem 'libv8', '~> 3.11.8.0'
    gem "therubyracer", :require => 'v8'
    gem 'jasmine', :group => [:development, :test]  
    gem 'sprockets'
    gem 'jquery-rails'
    gem 'tinymce-rails'             
    gem 'acts_as_list'              
    gem 'cocaine'
    gem 'rmagick'                   
    gem 'carrierwave'               
    gem 'remotipart'                
    gem 'deep_cloneable', '~> 1.4.0'
    gem 'delayed_job_active_record' 
    gem 'daemons'
    gem 'rb-readline'
    gem "jquery-migrate-rails", "~> 1.0.0"
    gem 'ransack'
    # Gems used only for assets and not required
    # in production environments by default.
    group :assets do
      gem 'sass-rails', "~> 3.2.3"
      gem 'coffee-rails', "~> 3.2.1"
      gem 'uglifier', '>= 1.0.3'
    end
    
    # Bundle gems for the local environment. Make sure to
    # put test-only gems in this group so their generators
    # and rake tasks are available in development mode:
    group :development, :test do
      gem 'cheat'                   
      gem 'ruby-growl'
      gem 'letter_opener'           
      gem 'rspec-rails', "~> 2.0"
      gem 'database_cleaner'        
      gem 'rb-fsevent', '~> 0.9.1'
      gem 'guard-rspec'
      gem 'spork-rails'
      gem 'guard-spork'
      gem 'factory_girl_rails'
      gem 'capybara', '2.0.1'
      gem 'launchy'
      gem 'mocha', :require => false
      gem 'better_errors'
      gem 'binding_of_caller'
      gem 'meta_request'
    end