# # Author:: Christian Reiniger # License:: GNU GPL v2 or later, as described in the accompanying LICENSE file # Homepage: http://dsadmin.dotsrc.org/ # # ---- # Last change: # # CVS User:: $Author$ # Date:: $Date$ # Revision:: $Revision$ # ENV["RAILS_ENV"] = RAILS_ENV = "test" verbose_org, $VERBOSE = $VERBOSE, false require File.expand_path(File.dirname(__FILE__) + "/../config/environment") require 'test_help' $VERBOSE = verbose_org require 'stringio' require 'digest/md5' require 'pp' require 'user' require 'dsadmin/singleton_config_manager' require 'dsadmin/contractor' require 'dsadmin/request' require 'mocks/application_controller.rb' class Test::Unit::TestCase # Transactional fixtures accelerate your tests by wrapping each test method # in a transaction that's rolled back on completion. This ensures that the # test database remains unchanged so your fixtures don't have to be reloaded # between every test method. Fewer database queries means faster tests. # # Read Mike Clark's excellent walkthrough at # http://clarkware.com/cgi/blosxom/2005/10/24#Rails10FastTesting # # Every Active Record database supports transactions except MyISAM tables # in MySQL. Turn off transactional fixtures in this case; however, if you # don't care one way or the other, switching from MyISAM to InnoDB tables # is recommended. self.use_transactional_fixtures = true # Instantiated fixtures are slow, but give you @david where otherwise you # would need people(:david). If you don't want to migrate your existing # test cases which use the @david style and don't mind the speed hit (each # instantiated fixtures translates to a database query per test method), # then set this back to true. self.use_instantiated_fixtures = false # Add more helper methods to be used by all tests here... include Dsadmin::Contractor TEST_TMP_PATH = 'tmp/test' REGRESSION_DATA_PATH = 'test/regression-reference-data' protected # --------------------------------------------------------------------------------- # Create a fully qualified (absolute) path from the specified filename. # Context: Reference files for regression testing def getRegressionFilename(aFilename) cfg = Dsadmin::SingletonConfigManager.instance path = cfg.localpath(REGRESSION_DATA_PATH) return File.join(path, aFilename) end # getRegressionFilename # Get a +String+ containing the contents of the specified file # Context: Reference files for regression testing def getRegressionData(aFilename) filename = getRegressionFilename(aFilename) File.open(filename, 'r') { |file| return file.read } end # getRegressionData # Write the data in +aString+ to the specified file # Context: Reference files for regression testing def writeRegressionData(aFilename, aString) filename = getRegressionFilename(aFilename) File.open(filename, File::CREAT | File::TRUNC | File::WRONLY) { |file| file.write(aString) } end # writeRegressionData # Create a fully qualified (absolute) path from the specified filename. # Context: Temporary files used in testing def getTmpFilename(aFilename) cfg = Dsadmin::SingletonConfigManager.instance path = cfg.localpath(TEST_TMP_PATH) return File.join(path, aFilename) end # getTmpFilename # Get a +String+ containing the contents of the specified file # Context: Temporary files used in testing def getTmpData(aFilename) if (aFilename =~ %r{^/}) filename = aFilename else filename = getTmpFilename(aFilename) end File.open(filename, 'r') { |file| return file.read } end # getTmpFilename # Generate an Exception of the specified type, with the given message. # The backtrace is always the same, independent of the actual code. def genException(aType, aMessage) mycaller = ["admind/test/TestUtilities.rb:60: in 'genException'", "admind/test/TestUtilities.rb:42: in 'nonExistant'", "admind/main.rb:4711: in 'neverBelieveMe'"] begin raise aType, aMessage, mycaller rescue Exception => xcept return xcept end end # Test a set of accessor methods (setter and corresponding getter) against # "good" and "bad" values. # # +goodvals+:: Array containing values that should be accepted # +badvals+:: Array containing values that should be rejected (by throwing an ArgumentError) # +setter+:: +Method+ object of the setter method # +getter+:: +Method+ object of the getter method # def accessorTest(goodvals, badvals, setter, getter) requireKindOf(Array, goodvals) requireKindOf(Array, badvals) requireKindOf(Method, setter) requireKindOf(Method, getter) badvals.each { |name| begin assert_raises(ArgumentError) { setter.call(name) } rescue StandardError => xcept xcept2 = xcept.class.new(xcept.message + " (Value: '#{name}')") xcept2.set_backtrace(xcept.backtrace) raise xcept2 end } goodvals.each { |name| begin assert_nothing_raised { setter.call(name) } assert_equal(name, getter.call) rescue StandardError => xcept xcept2 = xcept.class.new(xcept.message + " (Value: '#{name}')") xcept2.set_backtrace(xcept.backtrace) raise xcept2 end } end # Check the status code of a Response against a desired value. # # If an error occurs, the detailed error message from the Response Object # (if any) is added to the thrown Exception def checkResponseStatus(aStatus, aResponse) begin assert_equal(aStatus, aResponse.status) rescue AssertionFailedError => xcept msg = aResponse.data.to_s msg.gsub!(/^.*?(.*)<\/message>.*/m, '\1') xcept2 = xcept.class.new(xcept.message + " (#{msg})") xcept2.set_backtrace(xcept.backtrace) raise xcept2 end end # checkResultStatus @@instanceid = 'bluebox' @@token = Digest::MD5.hexdigest('bluebox') # generate an internal test request with the specified action def req_internal(action) req = Dsadmin::Request.new_internal(:test, action, @@instanceid, @@token) req.internal = true req end # generate an unauthorized remote test request with the specified action def req_noauth(action) req = Dsadmin::Request.new_client(:test, action, 'foo', 'none') req.from_outside = true req.remote_host = "somewhere_else" req end # generate a remote user test request with the specified action/username/password def req_user(action, username, password) req = Dsadmin::Request.new_client(:test, action, username, password) req.user = User.find_by_credentials(username, password) req.from_outside = true req.remote_host = "somewhere_else" req end # generate a remote superuser test request with the specified action def req_superuser(action) req_user(action, 'cthulhu', 'secret') end # (Webfe) Set authentication info to a superuser def set_auth_superuser Thread.current[:auth_username] = 'cthulhu' Thread.current[:auth_passwd] = 'secret' end # (Webfe) clear authentication info def set_auth_none Thread.current[:auth_username] = nil Thread.current[:auth_passwd] = nil end public #---------------------------------------------------------------------------------------- # Set up the testing environment (creating a proper tmp dir if necessary, # clearing it of old files etc) def self.setupTestEnvironment # We can't count on a configmanager being present here, so we're # inlining its +localpath+ method path = TEST_TMP_PATH unless(path =~ %r{\A/}) path = File.join(::INSTALL_PATH, path) end Dir.mkdir(path) rescue nil Dir.glob(%Q{#{path}/*}) { |fname| File.delete(fname) } end end