summaryrefslogtreecommitdiff
path: root/test/support
diff options
context:
space:
mode:
Diffstat (limited to 'test/support')
-rw-r--r--test/support/assert_responses.rb46
-rw-r--r--test/support/auth_test_helper.rb65
-rw-r--r--test/support/browser_integration_test.rb81
-rw-r--r--test/support/rack_test.rb38
-rw-r--r--test/support/stub_record_helper.rb53
-rw-r--r--test/support/time_test_helper.rb30
-rw-r--r--test/support/with_config_helper.rb16
7 files changed, 329 insertions, 0 deletions
diff --git a/test/support/assert_responses.rb b/test/support/assert_responses.rb
new file mode 100644
index 0000000..b01166f
--- /dev/null
+++ b/test/support/assert_responses.rb
@@ -0,0 +1,46 @@
+module AssertResponses
+
+ # response that works with different TestCases:
+ # ActionController::TestCase has @response
+ # ActionDispatch::IntegrationTest has @response
+ # Rack::Test::Methods defines last_response
+ def get_response
+ @response || last_response
+ end
+
+ def assert_attachement_filename(name)
+ assert_equal %Q(attachment; filename="#{name}"),
+ get_response.headers["Content-Disposition"]
+ end
+
+ def json_response
+ response = JSON.parse(get_response.body)
+ response.respond_to?(:with_indifferent_access) ?
+ response.with_indifferent_access :
+ response
+ end
+
+ def assert_json_response(object)
+ assert_equal 'application/json',
+ get_response.content_type.to_s.split(';').first
+ if object.is_a? Hash
+ object.stringify_keys! if object.respond_to? :stringify_keys!
+ assert_equal object, json_response
+ else
+ assert_equal object.to_json, get_response.body
+ end
+ end
+
+ def assert_json_error(object)
+ object.stringify_keys! if object.respond_to? :stringify_keys!
+ assert_json_response :errors => object
+ end
+end
+
+class ::ActionController::TestCase
+ include AssertResponses
+end
+
+class ::ActionDispatch::IntegrationTest
+ include AssertResponses
+end
diff --git a/test/support/auth_test_helper.rb b/test/support/auth_test_helper.rb
new file mode 100644
index 0000000..57f9f9b
--- /dev/null
+++ b/test/support/auth_test_helper.rb
@@ -0,0 +1,65 @@
+module AuthTestHelper
+ extend ActiveSupport::Concern
+
+ # Controller will fetch current user from warden.
+ # Make it pick up our current_user
+ included do
+ setup do
+ request.env['warden'] ||= stub :user => nil
+ end
+ end
+
+ def login(user_or_method_hash = {})
+ if user_or_method_hash.respond_to?(:reverse_merge)
+ user_or_method_hash.reverse_merge! :is_admin? => false
+ end
+ @current_user = stub_record(:user, user_or_method_hash)
+ request.env['warden'] = stub :user => @current_user
+ request.env['HTTP_AUTHORIZATION'] = header_for_token_auth
+ return @current_user
+ end
+
+ def assert_access_denied(denied = true, logged_in = true)
+ if denied
+ if @response.content_type == 'application/json'
+ assert_json_response('error' => I18n.t(:not_authorized))
+ assert_response :unprocessable_entity
+ else
+ if logged_in
+ assert_equal({:alert => I18n.t(:not_authorized)}, flash.to_hash)
+ assert_redirected_to home_url
+ else
+ assert_equal({:alert => I18n.t(:not_authorized_login)}, flash.to_hash)
+ assert_redirected_to login_url
+ end
+ end
+ else
+ assert flash[:alert].blank?
+ end
+ end
+
+ def expect_logout
+ expect_warden_logout
+ @token.expects(:destroy) if @token
+ end
+
+ protected
+
+ def header_for_token_auth
+ @token = find_record(:token, :authenticate => @current_user)
+ ActionController::HttpAuthentication::Token.encode_credentials @token.id
+ end
+
+ def expect_warden_logout
+ raw = mock('raw session') do
+ expects(:inspect)
+ end
+ request.env['warden'].expects(:raw_session).returns(raw)
+ request.env['warden'].expects(:logout)
+ end
+
+end
+
+class ActionController::TestCase
+ include AuthTestHelper
+end
diff --git a/test/support/browser_integration_test.rb b/test/support/browser_integration_test.rb
new file mode 100644
index 0000000..2885c3a
--- /dev/null
+++ b/test/support/browser_integration_test.rb
@@ -0,0 +1,81 @@
+#
+# BrowserIntegrationTest
+#
+# Use this class for capybara based integration tests for the ui.
+#
+
+class BrowserIntegrationTest < ActionDispatch::IntegrationTest
+
+ CONFIG_RU = (Rails.root + 'config.ru').to_s
+ OUTER_APP = Rack::Builder.parse_file(CONFIG_RU).first
+
+ require 'capybara/poltergeist'
+
+ Capybara.register_driver :rack_test do |app|
+ Capybara::RackTest::Driver.new(app)
+ end
+
+ Capybara.register_driver :poltergeist do |app|
+ Capybara::Poltergeist::Driver.new(app)
+ end
+
+ # this is integration testing. So let's make the whole
+ # rack stack available...
+ Capybara.app = OUTER_APP
+ Capybara.run_server = true
+ Capybara.app_host = 'http://lvh.me:3003'
+ Capybara.server_port = 3003
+ Capybara.javascript_driver = :poltergeist
+ Capybara.default_wait_time = 5
+
+
+ # Make the Capybara DSL available
+ include Capybara::DSL
+
+ setup do
+ Capybara.current_driver = Capybara.javascript_driver
+ page.driver.add_headers 'ACCEPT-LANGUAGE' => 'en-EN'
+ end
+
+ teardown do
+ Capybara.reset_sessions! # Forget the (simulated) browser state
+ Capybara.use_default_driver # Revert Capybara.current_driver to Capybara.default_driver
+ end
+
+ def submit_signup(username = nil, password = nil)
+ username ||= "test_#{SecureRandom.urlsafe_base64}".downcase
+ password ||= SecureRandom.base64
+ visit '/users/new'
+ fill_in 'Username', with: username
+ fill_in 'Password', with: password
+ fill_in 'Password confirmation', with: password
+ click_on 'Sign Up'
+ return username, password
+ end
+
+ add_teardown_hook do |testcase|
+ unless testcase.passed?
+ testcase.save_state
+ end
+ end
+
+ def save_state
+ page.save_screenshot screenshot_path
+ File.open(logfile_path, 'w') do |test_log|
+ test_log.puts self.class.name
+ test_log.puts "========================="
+ test_log.puts __name__
+ test_log.puts Time.now
+ test_log.puts current_path
+ test_log.puts page.status_code
+ test_log.puts page.response_headers
+ test_log.puts "page.html"
+ test_log.puts "------------------------"
+ test_log.puts page.html
+ test_log.puts "server log"
+ test_log.puts "------------------------"
+ test_log.puts `tail log/test.log -n 200`
+ end
+ end
+
+end
diff --git a/test/support/rack_test.rb b/test/support/rack_test.rb
new file mode 100644
index 0000000..806339a
--- /dev/null
+++ b/test/support/rack_test.rb
@@ -0,0 +1,38 @@
+require_relative 'assert_responses'
+
+class RackTest < ActiveSupport::TestCase
+ include Rack::Test::Methods
+ include Warden::Test::Helpers
+
+ CONFIG_RU = (Rails.root + 'config.ru').to_s
+ OUTER_APP = Rack::Builder.parse_file(CONFIG_RU).first
+
+ def app
+ OUTER_APP
+ end
+
+ def assert_access_denied
+ assert_json_response('error' => I18n.t(:not_authorized))
+ assert_response :unprocessable_entity
+ end
+
+ # inspired by rails 4
+ # -> actionpack/lib/action_dispatch/testing/assertions/response.rb
+ def assert_response(type, message = nil)
+ # RackTest does not know @response
+ response_code = last_response.status
+ message ||= "Expected response to be a <#{type}>, but was <#{response_code}>"
+
+ if Symbol === type
+ if [:success, :missing, :redirect, :error].include?(type)
+ assert last_response.send("#{type}?"), message
+ else
+ code = Rack::Utils::SYMBOL_TO_STATUS_CODE[type]
+ assert_equal code, response_code, message
+ end
+ else
+ assert_equal type, response_code, message
+ end
+ end
+
+end
diff --git a/test/support/stub_record_helper.rb b/test/support/stub_record_helper.rb
new file mode 100644
index 0000000..25138a0
--- /dev/null
+++ b/test/support/stub_record_helper.rb
@@ -0,0 +1,53 @@
+module StubRecordHelper
+
+ #
+ # We will stub find when called on the records class and
+ # return the record given.
+ #
+ # If no record is given but a hash or nil will create a stub based on
+ # that instead and returns the stub.
+ #
+ def find_record(factory, record_or_attribs_hash = {})
+ record = stub_record factory, record_or_attribs_hash, true
+ klass = record.class
+ # find is just an alias for get with CouchRest Model
+ klass.stubs(:get).with(record.to_param.to_s).returns(record)
+ klass.stubs(:find).with(record.to_param.to_s).returns(record)
+ return record
+ end
+
+ # Create a stub that has the usual functions of a database record.
+ # It won't fail on rendering a form for example.
+ #
+ # If the second parameter is a record we return the record itself.
+ # This way you can build functions that either take a record or a
+ # method hash to stub from. See find_record for an example.
+ def stub_record(factory, record_or_method_hash = {}, persisted=false)
+ if record_or_method_hash && !record_or_method_hash.is_a?(Hash)
+ return record_or_method_hash
+ end
+ FactoryGirl.build_stubbed(factory).tap do |record|
+ if persisted or record.persisted?
+ record_or_method_hash.reverse_merge! :created_at => Time.now,
+ :updated_at => Time.now, :id => Random.rand(100000).to_s
+ end
+ record.stubs(record_or_method_hash) if record_or_method_hash.present?
+ end
+ end
+
+ # returns deep stringified attributes so they can be compared to
+ # what the controller receives as params
+ def record_attributes_for(factory, attribs_hash = nil)
+ FactoryGirl.attributes_for(factory, attribs_hash).tap do |attribs|
+ attribs.keys.each do |key|
+ val = attribs.delete(key)
+ attribs[key.to_s] = val.is_a?(Hash) ? val.stringify_keys! : val
+ end
+ end
+ end
+
+end
+
+class ActionController::TestCase
+ include StubRecordHelper
+end
diff --git a/test/support/time_test_helper.rb b/test/support/time_test_helper.rb
new file mode 100644
index 0000000..f673f12
--- /dev/null
+++ b/test/support/time_test_helper.rb
@@ -0,0 +1,30 @@
+# Extend the Time class so that we can offset the time that 'now'
+# returns. This should allow us to effectively time warp for functional
+# tests that require limits per hour, what not.
+class Time #:nodoc:
+ class <<self
+ attr_accessor :testing_offset
+
+ def now_with_testing_offset
+ now_without_testing_offset - testing_offset
+ end
+ alias_method_chain :now, :testing_offset
+ end
+end
+Time.testing_offset = 0
+
+module TimeTestHelper
+ # Time warp to the specified time for the duration of the passed block
+ def pretend_now_is(time)
+ begin
+ Time.testing_offset = Time.now - time
+ yield
+ ensure
+ Time.testing_offset = 0
+ end
+ end
+end
+
+class ActiveSupport::TestCase
+ include TimeTestHelper
+end
diff --git a/test/support/with_config_helper.rb b/test/support/with_config_helper.rb
new file mode 100644
index 0000000..65eb7bc
--- /dev/null
+++ b/test/support/with_config_helper.rb
@@ -0,0 +1,16 @@
+module WithConfigHelper
+ extend ActiveSupport::Concern
+
+ def with_config(options)
+ old_config = APP_CONFIG.dup
+ APP_CONFIG.merge! options
+ yield
+ ensure
+ APP_CONFIG.replace old_config
+ end
+
+end
+
+class ActiveSupport::TestCase
+ include WithConfigHelper
+end