I am using the MailForm Gem to create a contact form for my app and everything seems to be working just fine so I decided to write some tests to make sure it stays that way.
class ContactsControllerTest < ActionDispatch::IntegrationTest
def setup
ActionMailer::Base.deliveries.clear
end
test "should send contact email" do
get contact_path
post contacts_path, params: { contact: {
name: "interested customer",
email: "interested@customer.com",
subject: "we are interested!",
message: "we are so interested!!!"
}}
assert_equal 1, ActionMailer::Base.deliveries.size
assert_redirected_to root_path
end
test "should not send invalid contact email" do
get contact_path
post contacts_path, params: { contact: {
name: "",
email: "",
subject: "",
message: ""
}}
assert_equal 0, ActionMailer::Base.deliveries.size
assert_template 'contacts/new'
end
test "should not send contact email with captcha filled" do
get contact_path
post contacts_path, params: { contact: {
name: "interested customer",
email: "interested@customer.com",
subject: "we are interested!",
message: "we are so interested!!!",
nickname: "not_blank"
}}
assert_equal 0, ActionMailer::Base.deliveries.size
assert_template 'contacts/new'
end
the first two tests pass while the third fails with the message
FAIL["test_should_not_send_contact_email_with_captcha_filled", ContactsControllerTest, 5.2574800989968935]
test_should_not_send_contact_email_with_captcha_filled#ContactsControllerTest (5.26s)
Expected: 0
Actual: 1
test/controllers/contacts_controller_test.rb:35:in `block in <class:ContactsControllerTest>'
My model looks like this.
class Contact < MailForm::Base
attribute :name, :validate => true
attribute :email, :validate => /\A([\w\.%\+\-]+)@([\w\-]+\.)+ ([\w]{2,})\z/i
attribute :subject
attribute :message
attribute :nickname, :captcha => true
def headers
{
:subject => "Contact: #{subject}" ,
:to => "myemail@mail.com",
:from => %("#{name}" <#{email}>)
}
end
end
My first thought was that the captcha validation is not stopping the mail from being sent. If someone could point out what I am missing I would appreciate it.
The mailform model returns valid, even if the nickname is given. But you can check if it is spam, which prohibits the mail from being sent. I use this to check if the spam detection works(with rspec):
it 'should be marked spam if it contains :nickname' do
expect(FactoryGirl.build(:contact_with_nickname)).to be_spam
end