Rails 3.2.8中ActionMailer测试失败。

4

我在notifier_test.rb中有一个测试,其中Notifier是我的邮件发送器类。

mail = Notifier.registered_client(client.firstname, client.emailaddress)

assert_match I18n.t('notifier.registered_client.email_body'), mail.body.encoded, 'Email contents should be correct'

这个测试在Rails 3.2.8之前是通过的,但现在测试失败了,错误信息如下:

NotifierTest
      FAIL (0:00:07.963) should create registration email in English
      Email contents should be correct.
      Expected /In\ order\ to\ activate\ your\ company's\ registration\ you\ must\ click\ on\ the\ following\ link:/ 
      to match 
      "\nDear First Name, thanks for registering.\n\nIn order to activate your company\'s registration you must click on the following link:\n\nhttp://localhost:3000/en/clients/activate?email=english%40email.com&key=7b6e6ed1-ac03-4d95-a0d5-6f2541092dd0\n".

错误似乎是因为我的邮件文本包含一个撇号,而 mail.body.encoded 现在返回的是 \',这是由于补丁引起的:
CVE-2012-3464 Ruby on Rails 中的潜在 XSS 漏洞,具体描述请见此处。
最聪明的方法是以相同方式对 I18n.t 返回的字符串进行编码,或从我的邮件对象中获取不含撇号的文本。
1个回答

1
最终,我通过使用某种方法使这个工作起来了。

CGI.unescapeHTML(mail.body.encoded)

为了让这行代码通过测试:

assert_match I18n.t('notifier.registered_client.email_greeting', name: firstname), CGI.unescapeHTML(mail.body.encoded), 'Email greeting should be present'

请注意


这个问题只限于测试代码还是在生产邮件中也需要使用CGI.unescapeHTML? - Isaac Betesh
感谢您的提问,很抱歉我回复得比较慢。 虽然我上面描述的操作确实解决了问题,但是根本问题实际上是另外一件事情。 我正在使用一个翻译文件,但是没有给邮件正文的翻译字符串命名为_html。 我认为我的理解是正确的,因为Rails决定对这个字符串进行转义,所以我必须对其进行反转义才能使测试通过并匹配翻译文件中的原始文本。 - Craig Miles

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接