spec_helper.rb 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. require 'rubygems'
  2. require 'rack/test'
  3. require 'rspec'
  4. #require 'spec/autorun'
  5. #require 'spec/interop/test'
  6. require 'logger'
  7. require 'ostruct'
  8. require 'webmock/rspec'
  9. require 'capybara'
  10. require 'capybara/dsl'
  11. require 'casserver/authenticators/base'
  12. require 'casserver/core_ext.rb'
  13. CASServer::Authenticators.autoload :LDAP, 'casserver/authenticators/ldap.rb'
  14. CASServer::Authenticators.autoload :ActiveDirectoryLDAP, 'casserver/authenticators/active_directory_ldap.rb'
  15. CASServer::Authenticators.autoload :SQL, 'casserver/authenticators/sql.rb'
  16. CASServer::Authenticators.autoload :SQLEncrypted, 'lib/casserver/authenticators/sql_encrypted.rb'
  17. CASServer::Authenticators.autoload :Google, 'casserver/authenticators/google.rb'
  18. CASServer::Authenticators.autoload :ActiveResource, 'casserver/authenticators/active_resource.rb'
  19. CASServer::Authenticators.autoload :Test, 'casserver/authenticators/test.rb'
  20. # require builder because it doesn't pull in the version
  21. # info automatically...
  22. begin
  23. require 'builder'
  24. require 'builder/version'
  25. rescue LoadError
  26. puts "builder not found, testing ActiveRecord 2.3?"
  27. end
  28. if Dir.getwd =~ /\/spec$/
  29. # Avoid potential weirdness by changing the working directory to the CASServer root
  30. FileUtils.cd('..')
  31. end
  32. def silence_warnings
  33. old_verbose, $VERBOSE = $VERBOSE, nil
  34. yield
  35. ensure
  36. $VERBOSE = old_verbose
  37. end
  38. # Ugly monkeypatch to allow us to test for correct redirection to
  39. # external services.
  40. #
  41. # This will likely break in the future when Capybara or RackTest are upgraded.
  42. class Capybara::RackTest::Browser
  43. def current_url
  44. if @redirected_to_external_url
  45. @redirected_to_external_url
  46. else
  47. request.url rescue ""
  48. end
  49. end
  50. def follow_redirects!
  51. if last_response.redirect? && last_response['Location'] =~ /^http[s]?:/
  52. @redirected_to_external_url = last_response['Location']
  53. else
  54. 5.times do
  55. follow_redirect! if last_response.redirect?
  56. end
  57. raise Capybara::InfiniteRedirectError, "redirected more than 5 times, check for infinite redirects." if last_response.redirect?
  58. end
  59. end
  60. end
  61. # This called in specs' `before` block.
  62. # Due to the way Sinatra applications are loaded,
  63. # we're forced to delay loading of the server code
  64. # until the start of each test so that certain
  65. # configuraiton options can be changed (e.g. `uri_path`)
  66. def load_server(config_file = 'default_config')
  67. ENV['CONFIG_FILE'] = File.join(File.dirname(__FILE__),'config',"#{config_file}.yml")
  68. silence_warnings do
  69. load File.dirname(__FILE__) + '/../lib/casserver/server.rb'
  70. end
  71. # set test environment
  72. CASServer::Server.set :environment, :test
  73. CASServer::Server.set :run, false
  74. CASServer::Server.set :raise_errors, true
  75. CASServer::Server.set :logging, false
  76. CASServer::Server.enable(:raise_errors)
  77. CASServer::Server.disable(:show_exceptions)
  78. #Capybara.current_driver = :selenium
  79. Capybara.app = CASServer::Server
  80. def app
  81. CASServer::Server
  82. end
  83. end
  84. # Deletes the sqlite3 database specified in the app's config
  85. # and runs the db:migrate rake tasks to rebuild the database schema.
  86. def reset_spec_database
  87. raise "Cannot reset the spec database because config[:database][:database] is not defined." unless
  88. CASServer::Server.config[:database] && CASServer::Server.config[:database][:database]
  89. FileUtils.rm_f(CASServer::Server.config[:database][:database])
  90. ActiveRecord::Base.logger = Logger.new(STDOUT)
  91. ActiveRecord::Base.logger.level = Logger::ERROR
  92. ActiveRecord::Migration.verbose = false
  93. ActiveRecord::Migrator.migrate("db/migrate")
  94. end
  95. def get_ticket_for(service, username = 'spec_user', password = 'spec_password')
  96. visit "/login?service=#{CGI.escape(service)}"
  97. fill_in 'username', :with => username
  98. fill_in 'password', :with => password
  99. click_button 'login-submit'
  100. page.current_url.match(/ticket=(.*)$/)[1]
  101. end
  102. def gem_available?(name)
  103. if Gem::Specification.methods.include?(:find_all_by_name)
  104. not Gem::Specification.find_all_by_name(name).empty?
  105. else
  106. Gem.available?(name)
  107. end
  108. end