aboutsummaryrefslogtreecommitdiffstats
path: root/spec/support
diff options
context:
space:
mode:
Diffstat (limited to 'spec/support')
-rw-r--r--spec/support/email_helpers.rb23
-rw-r--r--spec/support/load_file_fixtures.rb14
-rw-r--r--spec/support/validate_html.rb65
-rw-r--r--spec/support/xapian_index.rb42
4 files changed, 144 insertions, 0 deletions
diff --git a/spec/support/email_helpers.rb b/spec/support/email_helpers.rb
new file mode 100644
index 000000000..7e98c39f6
--- /dev/null
+++ b/spec/support/email_helpers.rb
@@ -0,0 +1,23 @@
+def load_raw_emails_data
+ raw_emails_yml = File.join(RSpec.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
+
+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 get_fixture_mail(filename)
+ MailHandler.mail_from_raw_email(load_file_fixture(filename))
+end
+
+def parse_all_incoming_messages
+ IncomingMessage.find(:all).each{ |x| x.parse_raw_email! }
+end
diff --git a/spec/support/load_file_fixtures.rb b/spec/support/load_file_fixtures.rb
new file mode 100644
index 000000000..08079f654
--- /dev/null
+++ b/spec/support/load_file_fixtures.rb
@@ -0,0 +1,14 @@
+def file_fixture_name(file_name)
+ return File.join(RSpec.configuration.fixture_path, "files", file_name)
+end
+
+def load_file_fixture(file_name, as_binary=false)
+ file_name = file_fixture_name(file_name)
+ content = File.open(file_name, 'r') do |file|
+ if as_binary
+ file.set_encoding(Encoding::BINARY) if file.respond_to?(:set_encoding)
+ end
+ file.read
+ end
+ return content
+end
diff --git a/spec/support/validate_html.rb b/spec/support/validate_html.rb
new file mode 100644
index 000000000..403865c74
--- /dev/null
+++ b/spec/support/validate_html.rb
@@ -0,0 +1,65 @@
+# 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 <body> of a page
+def validate_as_body(html)
+ validate_html('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">' +
+ "<html><head><title>Test</title></head><body>#{html}</body></html>")
+end
+
+# Monkeypatch! Validate HTML in tests.
+$html_validation_script_found = false
+AlaveteliConfiguration::utility_search_path.each do |d|
+ $html_validation_script = File.join(d, "validate")
+ $html_validation_script_options = ["--charset=utf-8"]
+ if File.file? $html_validation_script and File.executable? $html_validation_script
+ $html_validation_script_found = true
+ break
+ end
+end
+if $tempfilecount.nil?
+ $tempfilecount = 0
+ if $html_validation_script_found
+ module ActionController
+ class TestCase
+ module Behavior
+ # Hook into the process function, so can automatically get HTML after each request
+ alias :original_process :process
+ def is_fragment
+ # XXX there must be a better way of doing this!
+ return @request.path_parameters["action"] == "search_typeahead"
+ end
+ def process(action, parameters = nil, session = nil, flash = nil, http_method = 'GET')
+ self.original_process(action, parameters, session, flash, http_method)
+ # don't validate auto-generated HTML
+ return if @request.query_parameters["action"] == "get_attachment_as_html"
+ # XXX Is there a better way to check this than calling a private method?
+ return if @response.body.strip.empty?
+ # And then if HTML, not a redirect (302, 301)
+ if @response.content_type == "text/html" && ! [301,302,401].include?(@response.response_code)
+ if !is_fragment
+ validate_html(@response.body)
+ else
+ # it's a partial
+ validate_as_body(@response.body)
+ end
+ end
+ end
+ end
+ end
+ end
+ else
+ puts "WARNING: HTML validation script " + $html_validation_script + " not found"
+ end
+end
diff --git a/spec/support/xapian_index.rb b/spec/support/xapian_index.rb
new file mode 100644
index 000000000..344c28ebb
--- /dev/null
+++ b/spec/support/xapian_index.rb
@@ -0,0 +1,42 @@
+# 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
+
+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
+
+# 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