From d05ebfe116cb8228f0ef752b9f4ba84c41fb591a Mon Sep 17 00:00:00 2001 From: evazion Date: Thu, 22 Aug 2019 00:42:49 -0500 Subject: [PATCH] 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. --- app/models/dmail.rb | 2 +- test/unit/dmail_test.rb | 24 +++++++++++++----------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/app/models/dmail.rb b/app/models/dmail.rb index 922109dd5..48831348c 100644 --- a/app/models/dmail.rb +++ b/app/models/dmail.rb @@ -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 diff --git a/test/unit/dmail_test.rb b/test/unit/dmail_test.rb index 1b334ebb6..bbdcc6cc1 100644 --- a/test/unit/dmail_test.rb +++ b/test/unit/dmail_test.rb @@ -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