dmails: set sender name and ip address explicitly.

Set the sender name and IP addresses explicitly in the controller rather
than implicitly in the model.

Fixes cases where automated dmails from DanbooruBot had their IP
addresses set to the person who triggered the dmail, even though they
didn't actually send the dmail themselves.
This commit is contained in:
evazion
2020-02-23 01:41:54 -06:00
parent 7855e36d17
commit 3a018ee9f7
4 changed files with 17 additions and 35 deletions

View File

@@ -166,12 +166,10 @@ class DmailsControllerTest < ActionDispatch::IntegrationTest
@sender = create(:user)
@recipient = create(:user)
as(@sender) do
@dmail1 = Dmail.create_split(title: "test1", body: "test", to: @recipient)
@dmail2 = Dmail.create_split(title: "test2", body: "test", to: @recipient)
@dmail3 = Dmail.create_split(title: "test3", body: "test", to: @recipient, is_read: true)
@dmail4 = Dmail.create_split(title: "test4", body: "test", to: @recipient, is_deleted: true)
end
@dmail1 = create(:dmail, from: @sender, owner: @recipient, to: @recipient)
@dmail2 = create(:dmail, from: @sender, owner: @recipient, to: @recipient)
@dmail3 = create(:dmail, from: @sender, owner: @recipient, to: @recipient, is_read: true)
@dmail4 = create(:dmail, from: @sender, owner: @recipient, to: @recipient, is_deleted: true)
end
should "mark all unread, undeleted dmails as read" do
@@ -182,7 +180,7 @@ class DmailsControllerTest < ActionDispatch::IntegrationTest
assert_response :success
assert_equal(0, @recipient.reload.unread_dmail_count)
assert_equal(true, [@dmail1, @dmail2, @dmail3, @dmail4].all?(&:is_read))
assert_equal(true, [@dmail1, @dmail2, @dmail3, @dmail4].map(&:reload).all?(&:is_read))
end
end

View File

@@ -29,14 +29,11 @@ class DmailTest < ActiveSupport::TestCase
end
context "from a banned user" do
setup do
@user.update_attribute(:is_banned, true)
end
should "not validate" do
dmail = FactoryBot.build(:dmail, :title => "xxx", :owner => @user)
dmail.save
assert_equal(1, dmail.errors.size)
user = create(:banned_user)
dmail = build(:dmail, owner: user, from: user)
assert_equal(false, dmail.valid?)
assert_equal(["Sender is banned and cannot send messages"], dmail.errors.full_messages)
end
end
@@ -85,15 +82,10 @@ class DmailTest < ActiveSupport::TestCase
should "create a copy for each user" do
@new_user = FactoryBot.create(:user)
assert_difference("Dmail.count", 2) do
Dmail.create_split(:to_id => @new_user.id, :title => "foo", :body => "foo")
Dmail.create_split(from: CurrentUser.user, creator_ip_addr: "127.0.0.1", to_id: @new_user.id, title: "foo", body: "foo")
end
end
should "record the creator's ip addr" do
dmail = FactoryBot.create(:dmail, owner: @user)
assert_equal(CurrentUser.ip_addr, dmail.creator_ip_addr.to_s)
end
should "send an email if the user wants it" do
user = create(:user, receive_email_notifications: true)
assert_difference("ActionMailer::Base.deliveries.size", 1) do
@@ -104,14 +96,14 @@ class DmailTest < ActiveSupport::TestCase
should "create only one message for a split response" do
user = FactoryBot.create(:user, :receive_email_notifications => true)
assert_difference("ActionMailer::Base.deliveries.size", 1) do
Dmail.create_split(:to_id => user.id, :title => "foo", :body => "foo")
Dmail.create_split(from: CurrentUser.user, creator_ip_addr: "127.0.0.1", to_id: user.id, title: "foo", body: "foo")
end
end
should "notify the recipient he has mail" do
recipient = create(:user)
Dmail.create_split(title: "hello", body: "hello", to_id: recipient.id)
create(:dmail, owner: recipient, to: recipient)
assert_equal(1, recipient.reload.unread_dmail_count)
recipient.dmails.unread.last.update!(is_read: true)