2014-07-07 21:25:24 -04:00
|
|
|
ENV['RAILS_ENV'] ||= 'test'
|
2012-04-05 10:43:56 +02:00
|
|
|
require File.expand_path('../../config/environment', __FILE__)
|
|
|
|
require 'rails/test_help'
|
2018-10-19 09:25:15 -05:00
|
|
|
require 'securerandom'
|
2007-05-25 19:01:08 +00:00
|
|
|
|
2012-04-24 20:47:07 +02:00
|
|
|
# set config for tests. Overwrite those read from config/site.yml. Use inject to avoid warning about changing CONSTANT
|
2015-08-04 23:08:13 +02:00
|
|
|
{
|
|
|
|
"authentication_schemes" => ["database"],
|
|
|
|
"prefered_auth" => "database",
|
|
|
|
"email_dispatch" => nil,
|
|
|
|
"time_zone" => "Amsterdam" # force UTC+1 so Travis triggers time zone failures
|
|
|
|
}.inject( SITE_CONFIG ) { |h, elem| h[elem[0]] = elem[1]; h }
|
2012-04-20 14:38:00 +02:00
|
|
|
|
2018-10-19 09:25:15 -05:00
|
|
|
|
2009-06-02 21:22:50 +02:00
|
|
|
class ActiveSupport::TestCase
|
2014-06-19 12:33:38 -04:00
|
|
|
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
|
2007-03-30 04:36:52 +00:00
|
|
|
#
|
2012-04-05 10:43:56 +02:00
|
|
|
# Note: You'll currently still have to declare fixtures explicitly in integration tests
|
|
|
|
# -- they do not yet inherit this setting
|
|
|
|
fixtures :all
|
2014-02-27 16:35:47 +01:00
|
|
|
|
|
|
|
def setup
|
2014-12-30 22:56:19 +00:00
|
|
|
Time.zone = SITE_CONFIG['time_zone']
|
|
|
|
@today = Time.zone.now
|
2014-02-27 16:35:47 +01:00
|
|
|
@tomorrow = @today + 1.day
|
|
|
|
@in_three_days = @today + 3.days
|
|
|
|
@in_four_days = @in_three_days + 1.day # need a day after start_from
|
|
|
|
|
|
|
|
@friday = Time.zone.local(2008,6,6)
|
|
|
|
@saturday = Time.zone.local(2008,6,7)
|
|
|
|
@sunday = Time.zone.local(2008,6,8) # june 8, 2008 was a sunday
|
|
|
|
@monday = Time.zone.local(2008,6,9)
|
|
|
|
@tuesday = Time.zone.local(2008,6,10)
|
|
|
|
@wednesday = Time.zone.local(2008,6,11)
|
|
|
|
@thursday = Time.zone.local(2008,6,12)
|
|
|
|
end
|
|
|
|
|
2012-04-24 20:47:07 +02:00
|
|
|
# Generates a random string of ascii characters (a-z, "1 0")
|
|
|
|
# of a given length for testing assignment to fields
|
|
|
|
# for validation purposes
|
|
|
|
#
|
|
|
|
def generate_random_string(length)
|
2018-10-19 09:25:15 -05:00
|
|
|
o = [('a'..'z'), ('A'..'Z'), (0..9)].flat_map(&:to_a)
|
|
|
|
(0...length).map { o[rand(o.length)] }.join
|
2012-04-24 20:47:07 +02:00
|
|
|
end
|
2015-08-04 23:08:13 +02:00
|
|
|
|
2012-04-24 20:47:07 +02:00
|
|
|
def assert_equal_dmy(date1, date2)
|
|
|
|
assert_equal date1.strftime("%d-%m-%y"), date2.strftime("%d-%m-%y")
|
|
|
|
end
|
2012-04-17 16:47:37 +02:00
|
|
|
end
|
|
|
|
|
2012-04-19 00:02:42 +02:00
|
|
|
class ActionController::TestCase
|
2015-08-04 23:08:13 +02:00
|
|
|
|
2012-04-19 00:02:42 +02:00
|
|
|
def login_as(user)
|
|
|
|
@request.session['user_id'] = user ? users(user).id : nil
|
|
|
|
end
|
2015-08-04 23:08:13 +02:00
|
|
|
|
2012-04-19 00:02:42 +02:00
|
|
|
def assert_ajax_create_increments_count(name)
|
|
|
|
assert_count_after_ajax_create(name, get_class_count + 1)
|
|
|
|
end
|
2015-08-04 23:08:13 +02:00
|
|
|
|
2012-04-19 00:02:42 +02:00
|
|
|
def assert_ajax_create_does_not_increment_count(name)
|
|
|
|
assert_count_after_ajax_create(name, get_class_count)
|
|
|
|
end
|
2015-08-04 23:08:13 +02:00
|
|
|
|
2012-04-19 00:02:42 +02:00
|
|
|
def assert_count_after_ajax_create(name, expected_count)
|
|
|
|
ajax_create(name)
|
|
|
|
assert_equal(expected_count, get_class_count)
|
|
|
|
end
|
2015-08-04 23:08:13 +02:00
|
|
|
|
2012-04-19 00:02:42 +02:00
|
|
|
def ajax_create(name)
|
2018-11-03 11:00:27 -05:00
|
|
|
post :create, xhr: true, params: { get_model_class.downcase => {:name => name} }
|
2012-04-19 15:31:18 +02:00
|
|
|
end
|
2015-08-04 23:08:13 +02:00
|
|
|
|
2016-06-03 23:38:17 +02:00
|
|
|
def assert_number_of_items_in_rss_feed(expected)
|
2016-10-15 13:13:02 -04:00
|
|
|
assert_select 'rss[version="2.0"]' do
|
2016-06-03 23:38:17 +02:00
|
|
|
assert_select 'channel' do
|
|
|
|
assert_select 'item', expected
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_number_of_items_in_atom_feed(expected)
|
2016-10-15 13:13:02 -04:00
|
|
|
assert_equal 'http://www.w3.org/2005/Atom', html_document.children[0].namespace.href
|
|
|
|
assert_select 'feed' do
|
|
|
|
assert_select 'entry', expected
|
2016-06-03 23:38:17 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_number_of_items_in_text_feed(expected)
|
|
|
|
assert_equal expected, @response.body.scan(/^ \- /).size
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_number_of_items_in_ical_feed(expected)
|
|
|
|
assert_equal expected, @response.body.scan(/^BEGIN:VTODO/).size
|
|
|
|
end
|
|
|
|
|
2012-04-19 00:02:42 +02:00
|
|
|
private
|
2015-08-04 23:08:13 +02:00
|
|
|
|
2012-04-19 15:31:18 +02:00
|
|
|
def get_model_class
|
|
|
|
@controller.class.to_s.tableize.split("_")[0].camelcase.singularize #don't ask... converts ContextsController to Context
|
|
|
|
end
|
2015-08-04 23:08:13 +02:00
|
|
|
|
2012-04-19 00:02:42 +02:00
|
|
|
def get_class_count
|
2012-04-19 15:31:18 +02:00
|
|
|
eval("#{get_model_class}.count")
|
2012-04-19 00:02:42 +02:00
|
|
|
end
|
2015-08-04 23:08:13 +02:00
|
|
|
|
2012-04-27 14:22:16 +02:00
|
|
|
end
|
|
|
|
|
2013-05-11 23:13:16 +02:00
|
|
|
class ActionDispatch::IntegrationTest
|
2012-04-27 14:22:16 +02:00
|
|
|
|
|
|
|
def authenticated_post_xml(url, username, password, parameters, headers = {})
|
2018-11-03 11:00:27 -05:00
|
|
|
post url, params: parameters, headers:
|
2012-04-27 14:22:16 +02:00
|
|
|
{ 'HTTP_AUTHORIZATION' => "Basic " + Base64.encode64("#{username}:#{password}"),
|
|
|
|
'ACCEPT' => 'application/xml',
|
|
|
|
'CONTENT_TYPE' => 'application/xml'
|
|
|
|
}.merge(headers)
|
|
|
|
end
|
|
|
|
|
|
|
|
def authenticated_get_xml(url, username, password, parameters, headers = {})
|
2018-11-03 11:00:27 -05:00
|
|
|
get url, params: parameters, headers:
|
2012-04-27 14:22:16 +02:00
|
|
|
{ 'HTTP_AUTHORIZATION' => "Basic " + Base64.encode64("#{username}:#{password}"),
|
|
|
|
'ACCEPT' => 'application/xml',
|
|
|
|
'CONTENT_TYPE' => 'application/xml'
|
|
|
|
}.merge(headers)
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_response_and_body(type, body, message = nil)
|
|
|
|
assert_equal body, @response.body, message
|
|
|
|
assert_response type, message
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_response_and_body_matches(type, body_regex, message = nil)
|
|
|
|
assert_response type, message
|
|
|
|
assert_match body_regex, @response.body, message
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_401_unauthorized
|
|
|
|
assert_response_and_body 401, "401 Unauthorized: You are not authorized to interact with Tracks."
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_401_unauthorized_admin
|
2020-08-11 11:50:35 +03:00
|
|
|
assert_response_and_body 401, "401 Unauthorized: Only administrative users are allowed access to this function."
|
2012-04-27 14:22:16 +02:00
|
|
|
end
|
2015-08-04 23:08:13 +02:00
|
|
|
|
2012-04-27 14:22:16 +02:00
|
|
|
def assert_responses_with_error(error_msg)
|
|
|
|
assert_response 409
|
2015-08-09 17:43:40 -04:00
|
|
|
assert_select 'errors' do
|
2012-04-27 14:22:16 +02:00
|
|
|
assert_select 'error', 1, error_msg
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-22 22:54:40 -06:00
|
|
|
end
|