Ivan Storck at University of Washington

Notes on entrepreneurship and developing software for the Center for Commercialization

Testing Ruby on Rails Obfuscated Email Addresses With RSpec

You may have known that Ruby on Rails can protect email addresses that you have on your web pages with the mail_to view helper. But how do you test email obfuscation in your RSpec view tests?

To obfuscate an email address in a rails view, you can simply add encode: “javascript” to your mail_to helper.

obfuscate and protect an email address
1
mail_to "you@example.com","email me", encode: "javascript"

To test this, I created a method in my spec/spec_helper.rb file. This method goes through each character (byte) of the string containing the email address, and calls the sprintf method to format the character in the way that the javascript decoder in rails can read.

email encoder to include in spec_helper.rb
1
2
3
4
5
6
7
def encode_email(email)
# copied escape method from:
#http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-mail_to
escaped = ""
email.each_byte { |c| escaped << sprintf("%%%x",c) }
return escaped
end

Having that method available makes my example very easy to read:

email encoding example
1
2
3
it "should obfuscate and display the contact email" do
render.should have_content(encode_email("contact@example.com"))
end

Do you know a better way? For example, hooking directly into the view helper code from ActionView::Helpers::UrlHelper#mail_to? Let me know in the comments.

Comments