【问题标题】:ActionMailer test fails in Rails 3.2.8Rails 3.2.8 中的 ActionMailer 测试失败
【发布时间】:2012-08-22 00:23:39
【问题描述】:

我在 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 现在由于以下补丁而返回为\'

https://groups.google.com/forum/#!msg/rubyonrails-security/kKGNeMrnmiY/r2yM7xy-G48J 中描述的 Ruby on Rails 中的 CVE-2012-3464 潜在 XSS 漏洞

以相同方式对 I18n.t 返回的字符串进行编码,或者从我的邮件对象中获取不带撇号的文本的最聪明的方法是什么?

【问题讨论】:

    标签: ruby-on-rails testing actionmailer


    【解决方案1】:

    最后我通过使用来完成这项工作

    CGI.unescapeHTML(mail.body.encoded)

    这样就行了

    assert_match I18n.t('notifier.registered_client.email_greeting', name: firstname), CGI.unescapeHTML(mail.body.encoded), '电子邮件问候应该存在'

    通过测试

    【讨论】:

    • 此问题是否仅限于测试代码或生产电子邮件中是否还需要使用 CGI.unescapeHTML?
    • 感谢您的提问,抱歉,我回复缓慢。虽然我上面描述的操作确实解决了问题,但根本问题却是不同的。我正在使用翻译文件,并且没有用 _html 命名邮件正文的翻译字符串。我认为我的说法是正确的,因此 Rails 决定转义该字符串,因此我不得不取消转义以使测试通过并匹配翻译文件中的原始文本。
    猜你喜欢
    • 1970-01-01
    • 2015-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-16
    • 2015-11-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多