Browse files

fixes #2272 setup capybara and spork and modified test_helper for fut…

…ure integration testing
  • Loading branch information...
1 parent 91de7d4 commit 444636008e799264ee72fd9023cd2068ea2e8e54 @isratrade isratrade committed with ohadlevy Mar 4, 2013
Showing with 144 additions and 79 deletions.
  1. +6 −1 bundler.d/test.rb
  2. +3 −1 test/fixtures/users.yml
  3. +135 −77 test/test_helper.rb
View
7 bundler.d/test.rb
@@ -6,4 +6,9 @@
gem 'single_test'
gem 'ci_reporter', '>= 1.6.3', :require => false
gem 'minitest', '~> 3.5', :platforms => :ruby_19
-end
+ gem 'capybara'
+ gem 'database_cleaner'
+ gem 'launchy'
+ gem 'spork'
+ gem 'spork-testunit'
+end
View
4 test/fixtures/users.yml
@@ -25,7 +25,9 @@ admin:
mail: admin@someware.com
admin: true
last_login_on: 2009-10-12 21:50:04
- auth_source: one
+ auth_source: internal
+ password_hash: 3e4991bbc1df7cc5d43c8cf94838993a3045ba2a
+ password_salt: 852cefe7366f5c4e59276d2a753cba4302e44df3
internal:
login: test
View
212 test/test_helper.rb
@@ -1,99 +1,157 @@
-ENV["RAILS_ENV"] = "test"
-require File.expand_path('../../config/environment', __FILE__)
-require 'rails/test_help'
-
-class ActiveSupport::TestCase
- # Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
- #
- # Note: You'll currently still have to declare fixtures explicitly in integration tests
- # -- they do not yet inherit this setting
+require 'rubygems'
+require 'spork'
+#uncomment the following line to use spork with the debugger
+#require 'spork/ext/ruby-debug'
+
+Spork.prefork do
+ # Loading more in this block will cause your tests to run faster. However,
+ # if you change any configuration or code from libraries loaded here, you'll
+ # need to restart spork for it take effect.
+
+ ENV["RAILS_ENV"] = "test"
+ require File.expand_path('../../config/environment', __FILE__)
+ require 'rails/test_help'
+ require 'capybara/rails'
+
+ class ActiveSupport::TestCase
+ # Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
+ #
+ # Note: You'll currently still have to declare fixtures explicitly in integration tests
+ # -- they do not yet inherit this setting
+
+ fixtures :all
+
+ set_fixture_class({ :hosts => Host::Base })
+ # Add more helper methods to be used by all tests here...
+
+ def logger
+ Rails.logger
+ end
- fixtures :all
+ class Test::Unit::TestCase
+ include RR::Adapters::TestUnit
+ end
- set_fixture_class({ :hosts => Host::Base })
- # Add more helper methods to be used by all tests here...
+ def set_session_user
+ SETTINGS[:login] ? {:user => User.admin.id, :expires_at => 5.minutes.from_now} : {}
+ end
- def logger
- Rails.logger
- end
+ def as_user user
+ saved_user = User.current
+ User.current = users(user)
+ result = yield
+ User.current = saved_user
+ result
+ end
- class Test::Unit::TestCase
- include RR::Adapters::TestUnit
- end
+ def as_admin &block
+ as_user :admin, &block
+ end
- def set_session_user
- SETTINGS[:login] ? {:user => User.admin.id, :expires_at => 5.minutes.from_now} : {}
- end
+ def setup_users
+ User.current = users :admin
+ user = User.find_by_login("one")
+ @request.session[:user] = user.id
+ @request.session[:expires_at] = 5.minutes.from_now
+ user.roles = [Role.find_by_name('Anonymous'), Role.find_by_name('Viewer')]
+ user.save!
+ end
- def as_user user
- saved_user = User.current
- User.current = users(user)
- result = yield
- User.current = saved_user
- result
- end
+ def setup_user operation, type=""
+ @one = users(:one)
+ as_admin do
+ role = Role.find_or_create_by_name :name => "#{operation}_#{type}"
+ role.permissions = ["#{operation}_#{type}".to_sym]
+ @one.roles = [role]
+ @one.save!
+ end
+ User.current = @one
+ end
- def as_admin &block
- as_user :admin, &block
- end
+ def unattended?
+ SETTINGS[:unattended].nil? or SETTINGS[:unattended]
+ end
- def setup_users
- User.current = users :admin
- user = User.find_by_login("one")
- @request.session[:user] = user.id
- @request.session[:expires_at] = 5.minutes.from_now
- user.roles = [Role.find_by_name('Anonymous'), Role.find_by_name('Viewer')]
- user.save!
- end
+ def self.disable_orchestration
+ #This disables the DNS/DHCP orchestration
+ Host.any_instance.stubs(:boot_server).returns("boot_server")
+ Resolv::DNS.any_instance.stubs(:getname).returns("foo.fqdn")
+ Resolv::DNS.any_instance.stubs(:getaddress).returns("127.0.0.1")
+ Net::DNS::ARecord.any_instance.stubs(:conflicts).returns([])
+ Net::DNS::ARecord.any_instance.stubs(:conflicting?).returns(false)
+ Net::DNS::PTRRecord.any_instance.stubs(:conflicting?).returns(false)
+ Net::DNS::PTRRecord.any_instance.stubs(:conflicts).returns([])
+ Net::DHCP::Record.any_instance.stubs(:create).returns(true)
+ Net::DHCP::SparcRecord.any_instance.stubs(:create).returns(true)
+ Net::DHCP::Record.any_instance.stubs(:conflicting?).returns(false)
+ ProxyAPI::Puppet.any_instance.stubs(:environments).returns(["production"])
+ ProxyAPI::DHCP.any_instance.stubs(:unused_ip).returns('127.0.0.1')
+ end
- def setup_user operation, type=""
- @one = users(:one)
- as_admin do
- role = Role.find_or_create_by_name :name => "#{operation}_#{type}"
- role.permissions = ["#{operation}_#{type}".to_sym]
- @one.roles = [role]
- @one.save!
+ def disable_orchestration
+ ActiveSupport::TestCase.disable_orchestration
end
- User.current = @one
end
- def unattended?
- SETTINGS[:unattended].nil? or SETTINGS[:unattended]
- end
+ Apipie.configuration.validate = false
- def self.disable_orchestration
- #This disables the DNS/DHCP orchestration
- Host.any_instance.stubs(:boot_server).returns("boot_server")
- Resolv::DNS.any_instance.stubs(:getname).returns("foo.fqdn")
- Resolv::DNS.any_instance.stubs(:getaddress).returns("127.0.0.1")
- Net::DNS::ARecord.any_instance.stubs(:conflicts).returns([])
- Net::DNS::ARecord.any_instance.stubs(:conflicting?).returns(false)
- Net::DNS::PTRRecord.any_instance.stubs(:conflicting?).returns(false)
- Net::DNS::PTRRecord.any_instance.stubs(:conflicts).returns([])
- Net::DHCP::Record.any_instance.stubs(:create).returns(true)
- Net::DHCP::SparcRecord.any_instance.stubs(:create).returns(true)
- Net::DHCP::Record.any_instance.stubs(:conflicting?).returns(false)
- ProxyAPI::Puppet.any_instance.stubs(:environments).returns(["production"])
- ProxyAPI::DHCP.any_instance.stubs(:unused_ip).returns('127.0.0.1')
- end
+ # Transactional fixtures do not work with Selenium tests, because Capybara
+ # uses a separate server thread, which the transactions would be hidden
+ # from. We hence use DatabaseCleaner to truncate our test database.
+ DatabaseCleaner.strategy = :truncation
+
+ class ActionDispatch::IntegrationTest
+ # Make the Capybara DSL available in all integration tests
+ include Capybara::DSL
- def disable_orchestration
- ActiveSupport::TestCase.disable_orchestration
+ # Stop ActiveRecord from wrapping tests in transactions
+ self.use_transactional_fixtures = false
end
+
end
-class ActionController::TestCase
- setup :setup_set_script_name, :set_api_user
- def setup_set_script_name
- @request.env["SCRIPT_NAME"] = @controller.config.relative_url_root
- end
- def set_api_user
- return unless self.class.to_s[/api/i]
- @request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials(users(:apiadmin).login, "secret")
+Spork.each_run do
+ # This code will be run each time you run your specs.
+ class ActionController::TestCase
+ setup :setup_set_script_name, :set_api_user
+
+ def setup_set_script_name
+ @request.env["SCRIPT_NAME"] = @controller.config.relative_url_root
+ end
+
+ def set_api_user
+ return unless self.class.to_s[/api/i]
+ @request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials(users(:apiadmin).login, "secret")
+ end
end
-end
+ class ActionDispatch::IntegrationTest
+
+ def setup
+ login_admin
+ end
+
+ teardown do
+ DatabaseCleaner.clean # Truncate the database
+ Capybara.reset_sessions! # Forget the (simulated) browser state
+ Capybara.use_default_driver # Revert Capybara.current_driver to Capybara.default_driver
+ end
+
+ private
+
+ def login_admin
+ visit "/"
+ fill_in "login_login", :with => "admin"
+ fill_in "login_password", :with => "secret"
+ click_button "Login"
+ end
+
+ def logout_admin
+ click_link "Sign Out"
+ end
+
+ end
-Apipie.configuration.validate = false
+end

0 comments on commit 4446360

Please sign in to comment.