2010-03-23 15:28:17 +00:00
|
|
|
require 'active_support/test_case'
|
|
|
|
|
2010-01-24 02:38:52 +00:00
|
|
|
class ActiveSupport::TestCase
|
|
|
|
VALID_AUTHENTICATION_TOKEN = 'AbCdEfGhIjKlMnOpQrSt'.freeze
|
|
|
|
|
2010-02-06 00:33:32 +00:00
|
|
|
def setup_mailer
|
|
|
|
ActionMailer::Base.deliveries = []
|
|
|
|
end
|
|
|
|
|
|
|
|
def store_translations(locale, translations, &block)
|
2014-09-11 18:19:41 +00:00
|
|
|
# Calling 'available_locales' before storing the translations to ensure
|
|
|
|
# that the I18n backend will be initialized before we store our custom
|
|
|
|
# translations, so they will always override the translations for the
|
|
|
|
# YML file.
|
|
|
|
I18n.available_locales
|
|
|
|
I18n.backend.store_translations(locale, translations)
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
I18n.reload!
|
2010-02-06 00:33:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def generate_unique_email
|
|
|
|
@@email_count ||= 0
|
|
|
|
@@email_count += 1
|
2011-04-18 01:14:56 +00:00
|
|
|
"test#{@@email_count}@example.com"
|
2010-02-06 00:33:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def valid_attributes(attributes={})
|
2014-02-25 16:42:55 +00:00
|
|
|
{ username: "usertest",
|
|
|
|
email: generate_unique_email,
|
|
|
|
password: '12345678',
|
|
|
|
password_confirmation: '12345678' }.update(attributes)
|
2010-02-06 00:33:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def new_user(attributes={})
|
|
|
|
User.new(valid_attributes(attributes))
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_user(attributes={})
|
|
|
|
User.create!(valid_attributes(attributes))
|
|
|
|
end
|
2010-02-16 13:31:49 +00:00
|
|
|
|
2010-09-21 08:52:57 +00:00
|
|
|
def create_admin(attributes={})
|
|
|
|
valid_attributes = valid_attributes(attributes)
|
|
|
|
valid_attributes.delete(:username)
|
|
|
|
Admin.create!(valid_attributes)
|
|
|
|
end
|
|
|
|
|
2010-02-16 13:31:49 +00:00
|
|
|
# Execute the block setting the given values and restoring old values after
|
|
|
|
# the block is executed.
|
|
|
|
def swap(object, new_values)
|
|
|
|
old_values = {}
|
|
|
|
new_values.each do |key, value|
|
|
|
|
old_values[key] = object.send key
|
|
|
|
object.send :"#{key}=", value
|
|
|
|
end
|
2011-11-10 12:14:02 +00:00
|
|
|
clear_cached_variables(new_values)
|
2010-02-16 13:31:49 +00:00
|
|
|
yield
|
|
|
|
ensure
|
2011-11-10 12:14:02 +00:00
|
|
|
clear_cached_variables(new_values)
|
2010-02-16 13:31:49 +00:00
|
|
|
old_values.each do |key, value|
|
|
|
|
object.send :"#{key}=", value
|
|
|
|
end
|
|
|
|
end
|
2011-11-06 12:47:34 +00:00
|
|
|
|
2011-11-10 12:14:02 +00:00
|
|
|
def clear_cached_variables(options)
|
|
|
|
if options.key?(:case_insensitive_keys) || options.key?(:strip_whitespace_keys)
|
|
|
|
Devise.mappings.each do |_, mapping|
|
2013-06-19 07:17:54 +00:00
|
|
|
mapping.to.instance_variable_set(:@devise_parameter_filter, nil)
|
2011-11-10 12:14:02 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-04-18 01:14:56 +00:00
|
|
|
end
|