From eb40b8d70d301586cf42a93ca6572e34f4157fa9 Mon Sep 17 00:00:00 2001 From: Henare Degan Date: Tue, 11 Dec 2012 16:49:47 +1100 Subject: Reintroduce the scary, I-can't-believe-I'm-copy-and-pasting-this HTML validation code back in via it's own file --- spec/support/validate_html.rb | 65 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 spec/support/validate_html.rb (limited to 'spec/support') diff --git a/spec/support/validate_html.rb b/spec/support/validate_html.rb new file mode 100644 index 000000000..ba40b395a --- /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 of a page +def validate_as_body(html) + validate_html('' + + "Test#{html}") +end + +# Monkeypatch! Validate HTML in tests. +$html_validation_script_found = false +Configuration::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.query_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 unless @response.template.controller.instance_eval { render_views? } + # 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 -- cgit v1.2.3 From 6e63699743941457962d53827e683fc085555aae Mon Sep 17 00:00:00 2001 From: Henare Degan Date: Wed, 12 Dec 2012 13:47:12 +1100 Subject: safe_mock_model doesn't seem to be needed, also tested in Ruby 1.9 --- spec/support/load_file_fixtures.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 spec/support/load_file_fixtures.rb (limited to 'spec/support') 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 -- cgit v1.2.3 From 0a5b470e28f67c191d962706cf5b1be039d770bf Mon Sep 17 00:00:00 2001 From: Henare Degan Date: Wed, 12 Dec 2012 14:18:33 +1100 Subject: Allow the FoiAttachment specs to run --- spec/support/load_raw_emails.rb | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 spec/support/load_raw_emails.rb (limited to 'spec/support') diff --git a/spec/support/load_raw_emails.rb b/spec/support/load_raw_emails.rb new file mode 100644 index 000000000..db9e2884e --- /dev/null +++ b/spec/support/load_raw_emails.rb @@ -0,0 +1,7 @@ +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 -- cgit v1.2.3 From 1fc3bea6cb0a2fc5e26c171dd7f83dc155c962f9 Mon Sep 17 00:00:00 2001 From: Henare Degan Date: Wed, 12 Dec 2012 14:21:26 +1100 Subject: Allow the IncomingMessage model specs to run --- spec/support/load_email_fixtures.rb | 19 +++++++++++++++++++ spec/support/load_raw_emails.rb | 7 ------- 2 files changed, 19 insertions(+), 7 deletions(-) create mode 100644 spec/support/load_email_fixtures.rb delete mode 100644 spec/support/load_raw_emails.rb (limited to 'spec/support') diff --git a/spec/support/load_email_fixtures.rb b/spec/support/load_email_fixtures.rb new file mode 100644 index 000000000..d0575f029 --- /dev/null +++ b/spec/support/load_email_fixtures.rb @@ -0,0 +1,19 @@ +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 diff --git a/spec/support/load_raw_emails.rb b/spec/support/load_raw_emails.rb deleted file mode 100644 index db9e2884e..000000000 --- a/spec/support/load_raw_emails.rb +++ /dev/null @@ -1,7 +0,0 @@ -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 -- cgit v1.2.3 From b9d7b5e15db08113161aea660ad76ee678c704c1 Mon Sep 17 00:00:00 2001 From: Henare Degan Date: Wed, 12 Dec 2012 16:05:47 +1100 Subject: Add more spec helper methods and update acts_as_xapian to make info_request model specs pass --- spec/support/email_helpers.rb | 23 +++++++++++++++++++++++ spec/support/load_email_fixtures.rb | 19 ------------------- spec/support/xapian_index.rb | 21 +++++++++++++++++++++ 3 files changed, 44 insertions(+), 19 deletions(-) create mode 100644 spec/support/email_helpers.rb delete mode 100644 spec/support/load_email_fixtures.rb create mode 100644 spec/support/xapian_index.rb (limited to 'spec/support') 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_email_fixtures.rb b/spec/support/load_email_fixtures.rb deleted file mode 100644 index d0575f029..000000000 --- a/spec/support/load_email_fixtures.rb +++ /dev/null @@ -1,19 +0,0 @@ -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 diff --git a/spec/support/xapian_index.rb b/spec/support/xapian_index.rb new file mode 100644 index 000000000..21f6192ef --- /dev/null +++ b/spec/support/xapian_index.rb @@ -0,0 +1,21 @@ +# 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 -- cgit v1.2.3 From 50dd3ad01371e73912f8c2bf3a9fad70c8aa94fb Mon Sep 17 00:00:00 2001 From: Henare Degan Date: Tue, 18 Dec 2012 18:31:33 +1100 Subject: Add some more xapian methods for models/xapian_spec.rb --- spec/support/xapian_index.rb | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'spec/support') diff --git a/spec/support/xapian_index.rb b/spec/support/xapian_index.rb index 21f6192ef..344c28ebb 100644 --- a/spec/support/xapian_index.rb +++ b/spec/support/xapian_index.rb @@ -19,3 +19,24 @@ 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 -- cgit v1.2.3 From 2860c5f3637a31c5d5748688d82411e9e15d4648 Mon Sep 17 00:00:00 2001 From: Henare Degan Date: Sun, 27 Jan 2013 12:50:46 +1100 Subject: This API seems to have changed so just check if there's a body --- spec/support/validate_html.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'spec/support') diff --git a/spec/support/validate_html.rb b/spec/support/validate_html.rb index ba40b395a..558e562e2 100644 --- a/spec/support/validate_html.rb +++ b/spec/support/validate_html.rb @@ -45,7 +45,7 @@ if $tempfilecount.nil? # 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 unless @response.template.controller.instance_eval { render_views? } + return if @response.body.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 -- cgit v1.2.3 From 39e879cfe57a61132c4e2dbc57a0f578e3ba1a3e Mon Sep 17 00:00:00 2001 From: Henare Degan Date: Sun, 27 Jan 2013 12:52:40 +1100 Subject: Fix indentation only --- spec/support/validate_html.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'spec/support') diff --git a/spec/support/validate_html.rb b/spec/support/validate_html.rb index 558e562e2..09c415936 100644 --- a/spec/support/validate_html.rb +++ b/spec/support/validate_html.rb @@ -48,12 +48,12 @@ if $tempfilecount.nil? return if @response.body.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 + if !is_fragment + validate_html(@response.body) + else + # it's a partial + validate_as_body(@response.body) + end end end end -- cgit v1.2.3 From 04817186f63e9a55cd70133fea98c6ab5e6823bc Mon Sep 17 00:00:00 2001 From: Matthew Landauer Date: Mon, 28 Jan 2013 16:54:49 +1100 Subject: Correctly recognise a fragment for html validation --- spec/support/validate_html.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'spec/support') diff --git a/spec/support/validate_html.rb b/spec/support/validate_html.rb index 09c415936..93031ef46 100644 --- a/spec/support/validate_html.rb +++ b/spec/support/validate_html.rb @@ -38,7 +38,7 @@ if $tempfilecount.nil? alias :original_process :process def is_fragment # XXX there must be a better way of doing this! - return @request.query_parameters["action"] == "search_typeahead" + 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) -- cgit v1.2.3 From d85a11d1a6072abe48ff75cc47624e104fa42c4a Mon Sep 17 00:00:00 2001 From: Matthew Landauer Date: Thu, 31 Jan 2013 12:44:29 +1100 Subject: Fix rendering of atom feed and set correct content type --- spec/support/validate_html.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'spec/support') diff --git a/spec/support/validate_html.rb b/spec/support/validate_html.rb index 93031ef46..56f384537 100644 --- a/spec/support/validate_html.rb +++ b/spec/support/validate_html.rb @@ -45,7 +45,7 @@ if $tempfilecount.nil? # 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.empty? + 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 -- cgit v1.2.3 From cbdff06aa95a7987b54c712dc6729e138f608eca Mon Sep 17 00:00:00 2001 From: Henare Degan Date: Sun, 3 Mar 2013 14:52:30 +1100 Subject: Rename Configuration class to avoid conflict with ActiveSupport::Configurable --- spec/support/validate_html.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'spec/support') diff --git a/spec/support/validate_html.rb b/spec/support/validate_html.rb index 56f384537..403865c74 100644 --- a/spec/support/validate_html.rb +++ b/spec/support/validate_html.rb @@ -20,7 +20,7 @@ end # Monkeypatch! Validate HTML in tests. $html_validation_script_found = false -Configuration::utility_search_path.each do |d| +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 -- cgit v1.2.3 From 476890e21eb325cebf44b069c73682d05d189d89 Mon Sep 17 00:00:00 2001 From: Henare Degan Date: Thu, 14 Mar 2013 15:04:26 +1100 Subject: HTML validation was removed in 25ad21e0c93c7e79a5324067401f6c2b0340b9d6 --- spec/support/validate_html.rb | 65 ------------------------------------------- 1 file changed, 65 deletions(-) delete mode 100644 spec/support/validate_html.rb (limited to 'spec/support') diff --git a/spec/support/validate_html.rb b/spec/support/validate_html.rb deleted file mode 100644 index 403865c74..000000000 --- a/spec/support/validate_html.rb +++ /dev/null @@ -1,65 +0,0 @@ -# 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('' + - "Test#{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 -- cgit v1.2.3 From 9a5ef164ba29a8f56b3e2f42edf83d732613738f Mon Sep 17 00:00:00 2001 From: Mark Longair Date: Thu, 9 May 2013 14:19:50 +0100 Subject: Switch to just loading fixtures in tests as binary strings The data loaded with load_file_fixture is generally used in the tests as if it were binary data received from the MTA, so just load it as binary data - in Ruby 1.9 this means that it will end up with the encoding ASCII-8BIT. The same is the case when reading data from a fixture file in receive_incoming_mail. --- spec/support/email_helpers.rb | 2 +- spec/support/load_file_fixtures.rb | 10 ++-------- 2 files changed, 3 insertions(+), 9 deletions(-) (limited to 'spec/support') diff --git a/spec/support/email_helpers.rb b/spec/support/email_helpers.rb index 7e98c39f6..252b1f137 100644 --- a/spec/support/email_helpers.rb +++ b/spec/support/email_helpers.rb @@ -8,7 +8,7 @@ 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 = File.open(email_name, 'rb') { |f| f.read } content.gsub!('EMAIL_TO', email_to) content.gsub!('EMAIL_FROM', email_from) RequestMailer.receive(content) diff --git a/spec/support/load_file_fixtures.rb b/spec/support/load_file_fixtures.rb index 08079f654..a54505e99 100644 --- a/spec/support/load_file_fixtures.rb +++ b/spec/support/load_file_fixtures.rb @@ -2,13 +2,7 @@ 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) +def load_file_fixture(file_name) 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 + return File.open(file_name, 'rb') { |f| f.read } end -- cgit v1.2.3