Modify tests to search only text parts
authorHerve Saint-Amand <git@saintamh.org>
Wed, 11 Jan 2017 23:17:24 +0000 (23:17 +0000)
committerHerve Saint-Amand <git@saintamh.org>
Wed, 11 Jan 2017 23:17:24 +0000 (23:17 +0000)
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
test/test_helper.rb

index db35be5f350e8db3bd7b223cab7d8f6c6a8ca56d..09d2cc1fde39b3821afbca76f3d2c760d765b422 100644 (file)
@@ -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
index 9633989cda4d109278d828b049688545b832159b..33a1c92dacbb24398ffec9ddbd987f823c91cabe 100644 (file)
@@ -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