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.
This commit is contained in:
Herve Saint-Amand 2017-01-11 23:17:24 +00:00
parent 31778fd155
commit 0449024ca0
2 changed files with 26 additions and 14 deletions

View 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

View 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