dmails: fix bug preventing members from sending dmails.

Bug: sending dmails failed for members.

Cause: using lambdas with `rakismet_attrs` failed because unexpected
arguments are passed to the lambdas. Using procs works because the
arguments are ignored.

Also fix the tests to actually test akismet. We didn't catch this
because the tests mocked out the `spam?` call.
This commit is contained in:
evazion
2019-08-22 00:42:49 -05:00
parent 44653fb722
commit d05ebfe116
2 changed files with 14 additions and 12 deletions

View File

@@ -20,7 +20,7 @@ class Dmail < ApplicationRecord
after_create :update_recipient
after_commit :send_email, on: :create
rakismet_attrs author: -> { from.name }, author_email: -> { from.email }, content: -> { title + "\n\n" + body }, user_ip: -> { creator_ip_addr.to_s }
rakismet_attrs author: proc { from.name }, author_email: proc { from.email }, content: proc { title + "\n\n" + body }, user_ip: proc { creator_ip_addr.to_s }
concerning :SpamMethods do
class_methods do

View File

@@ -17,29 +17,31 @@ class DmailTest < ActiveSupport::TestCase
context "spam" do
setup do
Dmail.any_instance.stubs(:spam?).returns(true)
@recipient = FactoryBot.create(:user)
Dmail.any_instance.stubs(:spam?).returns(true) unless Danbooru.config.rakismet_key.present?
# viagra-test-123 is guaranteed to be flagged as spam.
# https://akismet.com/development/api/#detailed-docs
@spammer = create(:user, name: "viagra-test-123")
@recipient = create(:user)
end
should "not validate" do
assert_difference("Dmail.count", 2)do
Dmail.create_split(:to_id => @recipient.id, :title => "My video", :body => "hey Noneeditsonlyme. My webcam see here http://bit.ly/2vTv9Ki")
Dmail.create_split(from: @spammer, to: @recipient, title: "spam", body: "wonderful spam")
assert(@recipient.dmails.last.is_spam?)
end
end
should "autoban spammers after sending spam to N distinct users" do
Dmail.any_instance.expects(:spam?).returns(true)
users = FactoryBot.create_list(:user, Dmail::AUTOBAN_THRESHOLD)
users = create_list(:user, Dmail::AUTOBAN_THRESHOLD)
users.each do |user|
Dmail.create_split(from: @user, to: user, title: "spam", body: "wonderful spam")
Dmail.create_split(from: @spammer, to: user, title: "spam", body: "wonderful spam")
end
assert_equal(true, Dmail.is_spammer?(@user))
assert_equal(true, @user.reload.is_banned)
assert_equal(1, @user.bans.count)
assert_match(/Spambot./, @user.bans.last.reason)
assert_equal(true, Dmail.is_spammer?(@spammer))
assert_equal(true, @spammer.reload.is_banned)
assert_equal(1, @spammer.bans.count)
assert_match(/Spambot./, @spammer.bans.last.reason)
end
end