# This file is copied to ~/spec when you run 'ruby script/generate rspec' # from the project root directory. ENV["RAILS_ENV"] = 'test' require File.expand_path(File.join('..', '..', 'config', 'environment'), __FILE__) require 'spec/autorun' require 'spec/rails' # set a default username and password so we can test config = MySociety::Config.load_default() config['ADMIN_USERNAME'] = 'foo' config['ADMIN_PASSWORD'] = 'baz' # tests assume 20 days config['REPLY_LATE_AFTER_DAYS'] = 20 # register a fake Varnish server require 'fakeweb' FakeWeb.register_uri(:purge, %r|varnish.localdomain|, :body => "OK") # Uncomment the next line to use webrat's matchers #require 'webrat/integrations/rspec-rails' # Use test-specific translations FastGettext.add_text_domain 'app', :path => File.join(File.dirname(__FILE__), 'fixtures', 'locale'), :type => :po FastGettext.default_text_domain = 'app' Spec::Runner.configure do |config| # If you're not using ActiveRecord you should remove these # lines, delete config/database.yml and disable :active_record # in your config/boot.rb # fixture_path must end in a separator config.fixture_path = File.join(Rails.root, 'spec', 'fixtures') + File::SEPARATOR config.global_fixtures = :users, :public_bodies, :public_body_translations, :public_body_versions, :info_requests, :raw_emails, :incoming_messages, :outgoing_messages, :comments, :info_request_events, :track_things, :foi_attachments, :has_tag_string_tags, :holidays, :track_things_sent_emails # == Fixtures # # You can declare fixtures for each example_group like this: # describe "...." do # fixtures :table_a, :table_b # # Alternatively, if you prefer to declare them only once, you can # do so right here. Just uncomment the next line and replace the fixture # names with your fixtures. # # config.global_fixtures = :table_a, :table_b # # If you declare global fixtures, be aware that they will be declared # for all of your examples, even those that don't use them. # # You can also declare which fixtures to use (for example fixtures for test/fixtures): # # config.fixture_path = Rails.root + '/spec/fixtures/' # # == Mock Framework # # RSpec uses its own mocking framework by default. If you prefer to # use mocha, flexmock or RR, uncomment the appropriate line: # # config.mock_with :mocha # config.mock_with :flexmock # config.mock_with :rr # # == Notes # # For more information take a look at Spec::Runner::Configuration and Spec::Runner end # XXX No idea what namespace/class/module to put this in def receive_incoming_mail(email_name, email_to, email_from = 'geraldinequango@localhost') email_name = file_fixture_name(email_name) content = File.read(email_name) content.gsub!('EMAIL_TO', email_to) content.gsub!('EMAIL_FROM', email_from) RequestMailer.receive(content) end def file_fixture_name(file_name) return File.join(Spec::Runner.configuration.fixture_path, "files", file_name) end def load_file_fixture(file_name) file_name = file_fixture_name(file_name) content = File.read(file_name) return content end def parse_all_incoming_messages IncomingMessage.find(:all).each{ |x| x.parse_raw_email! } end def load_raw_emails_data raw_emails_yml = File.join(Spec::Runner.configuration.fixture_path, "raw_emails.yml") for raw_email_id in YAML::load_file(raw_emails_yml).map{|k,v| v["id"]} do raw_email = RawEmail.find(raw_email_id) raw_email.data = load_file_fixture("raw_emails/%d.email" % [raw_email_id]) end end # Rebuild the current xapian index def rebuild_xapian_index(terms = true, values = true, texts = true, dropfirst = true) if dropfirst begin ActsAsXapian.readable_init FileUtils.rm_r(ActsAsXapian.db_path) rescue RuntimeError end ActsAsXapian.writable_init ActsAsXapian.writable_db.close end parse_all_incoming_messages # safe_rebuild=true, which involves forking to avoid memory leaks, doesn't work well with rspec. # unsafe is significantly faster, and we can afford possible memory leaks while testing. models = [PublicBody, User, InfoRequestEvent] ActsAsXapian.rebuild_index(models, verbose=false, terms, values, texts, safe_rebuild=false) end # Create a clean xapian index based on the fixture files and the raw_email data. def create_fixtures_xapian_index load_raw_emails_data rebuild_xapian_index end def update_xapian_index ActsAsXapian.update_index(flush_to_disk=false, verbose=false) end # Copy the xapian index created in create_fixtures_xapian_index to a temporary # copy at the same level and point xapian at the copy def get_fixtures_xapian_index() # Create a base index for the fixtures if not already created $existing_xapian_db ||= create_fixtures_xapian_index # Store whatever the xapian db path is originally $original_xapian_path ||= ActsAsXapian.db_path path_array = $original_xapian_path.split(File::Separator) path_array.pop temp_path = File.join(path_array, 'test.temp') FileUtils.remove_entry_secure(temp_path, force=true) FileUtils.cp_r($original_xapian_path, temp_path) ActsAsXapian.db_path = temp_path end # Validate an entire HTML page def validate_html(html) $tempfilecount = $tempfilecount + 1 tempfilename = File.join(Dir::tmpdir, "railshtmlvalidate."+$$.to_s+"."+$tempfilecount.to_s+".html") File.open(tempfilename, "w+") do |f| f.puts html end if not system($html_validation_script, *($html_validation_script_options +[tempfilename])) raise "HTML validation error in " + tempfilename + " HTTP status: " + @response.response_code.to_s end File.unlink(tempfilename) return true end # Validate HTML fragment by wrapping it as the
of a page def validate_as_body(html) validate_html('' + "