From 0449024ca0f53307615993e9a480a28355153bc7 Mon Sep 17 00:00:00 2001 From: Herve Saint-Amand Date: Wed, 11 Jan 2017 23:17:24 +0000 Subject: [PATCH] Modify tests to search only text parts The tests assumed that every part in the multipart emails that we send were either plain text or HTML. Now we have image attachments, against whose contents the tests were still trying to match regexes. The tests have been modified to only run regexes on the text parts of the mails. --- test/integration/user_creation_test.rb | 28 +++++++++++++------------- test/test_helper.rb | 12 +++++++++++ 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/test/integration/user_creation_test.rb b/test/integration/user_creation_test.rb index db35be5f3..09d2cc1fd 100644 --- a/test/integration/user_creation_test.rb +++ b/test/integration/user_creation_test.rb @@ -135,10 +135,10 @@ class UserCreationTest < ActionDispatch::IntegrationTest assert_equal register_email.to[0], new_email # Check that the confirm account url is correct confirm_regex = Regexp.new("/user/redirect_tester/confirm\\?confirm_string=([a-zA-Z0-9]*)") - register_email.parts.each do |part| + email_text_parts(register_email).each do |part| assert_match confirm_regex, part.body.to_s end - confirm_string = register_email.parts[0].body.match(confirm_regex)[1] + confirm_string = email_text_parts(register_email)[0].body.match(confirm_regex)[1] # Check the page assert_response :success @@ -248,10 +248,10 @@ class UserCreationTest < ActionDispatch::IntegrationTest assert_equal register_email.to[0], new_email # Check that the confirm account url is correct confirm_regex = Regexp.new("/user/redirect_tester_openid/confirm\\?confirm_string=([a-zA-Z0-9]*)") - register_email.parts.each do |part| + email_text_parts(register_email).each do |part| assert_match confirm_regex, part.body.to_s end - confirm_string = register_email.parts[0].body.match(confirm_regex)[1] + confirm_string = email_text_parts(register_email)[0].body.match(confirm_regex)[1] # Check the page assert_response :success @@ -365,10 +365,10 @@ class UserCreationTest < ActionDispatch::IntegrationTest assert_equal register_email.to[0], new_email # Check that the confirm account url is correct confirm_regex = Regexp.new("/user/redirect_tester_google/confirm\\?confirm_string=([a-zA-Z0-9]*)") - register_email.parts.each do |part| + email_text_parts(register_email).each do |part| assert_match confirm_regex, part.body.to_s end - confirm_string = register_email.parts[0].body.match(confirm_regex)[1] + confirm_string = email_text_parts(register_email)[0].body.match(confirm_regex)[1] # Check the page assert_response :success @@ -478,10 +478,10 @@ class UserCreationTest < ActionDispatch::IntegrationTest assert_equal register_email.to[0], new_email # Check that the confirm account url is correct confirm_regex = Regexp.new("/user/redirect_tester_facebook/confirm\\?confirm_string=([a-zA-Z0-9]*)") - register_email.parts.each do |part| + email_text_parts(register_email).each do |part| assert_match confirm_regex, part.body.to_s end - confirm_string = register_email.parts[0].body.match(confirm_regex)[1] + confirm_string = email_text_parts(register_email)[0].body.match(confirm_regex)[1] # Check the page assert_response :success @@ -591,10 +591,10 @@ class UserCreationTest < ActionDispatch::IntegrationTest assert_equal register_email.to[0], new_email # Check that the confirm account url is correct confirm_regex = Regexp.new("/user/redirect_tester_windowslive/confirm\\?confirm_string=([a-zA-Z0-9]*)") - register_email.parts.each do |part| + email_text_parts(register_email).each do |part| assert_match confirm_regex, part.body.to_s end - confirm_string = register_email.parts[0].body.match(confirm_regex)[1] + confirm_string = email_text_parts(register_email)[0].body.match(confirm_regex)[1] # Check the page assert_response :success @@ -704,10 +704,10 @@ class UserCreationTest < ActionDispatch::IntegrationTest assert_equal register_email.to[0], new_email # Check that the confirm account url is correct confirm_regex = Regexp.new("/user/redirect_tester_github/confirm\\?confirm_string=([a-zA-Z0-9]*)") - register_email.parts.each do |part| + email_text_parts(register_email).each do |part| assert_match confirm_regex, part.body.to_s end - confirm_string = register_email.parts[0].body.match(confirm_regex)[1] + confirm_string = email_text_parts(register_email)[0].body.match(confirm_regex)[1] # Check the page assert_response :success @@ -817,10 +817,10 @@ class UserCreationTest < ActionDispatch::IntegrationTest assert_equal register_email.to[0], new_email # Check that the confirm account url is correct confirm_regex = Regexp.new("/user/redirect_tester_wikipedia/confirm\\?confirm_string=([a-zA-Z0-9]*)") - register_email.parts.each do |part| + email_text_parts(register_email).each do |part| assert_match confirm_regex, part.body.to_s end - confirm_string = register_email.parts[0].body.match(confirm_regex)[1] + confirm_string = email_text_parts(register_email)[0].body.match(confirm_regex)[1] # Check the page assert_response :success diff --git a/test/test_helper.rb b/test/test_helper.rb index 9633989cd..33a1c92da 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -185,5 +185,17 @@ module ActiveSupport stub_request(:get, "http://api.hostip.info/country.php?ip=0.0.0.0") stub_request(:get, "http://api.hostip.info/country.php?ip=127.0.0.1") end + + def email_text_parts(message) + text_parts = [] + message.parts.each do |part| + if part.content_type.start_with?("text/") + text_parts.push(part) + elsif part.multipart? + text_parts.concat(email_text_parts(part)) + end + end + return text_parts + end end end -- 2.39.5