DEV: Apply syntax_tree formatting to spec/*

This commit is contained in:
David Taylor
2023-01-09 11:18:21 +00:00
parent 0cf6421716
commit cb932d6ee1
907 changed files with 58693 additions and 45909 deletions

View File

@ -1,18 +1,16 @@
# frozen_string_literal: true
require 'email/sender'
require "email/sender"
RSpec.describe Email::Sender do
before do
SiteSetting.secure_uploads_allow_embed_images_in_emails = false
end
before { SiteSetting.secure_uploads_allow_embed_images_in_emails = false }
fab!(:post) { Fabricate(:post) }
let(:mock_smtp_transaction_response) { "250 Ok: queued as 2l3Md07BObzB8kRyHZeoN0baSUAhzc7A-NviRioOr80=@mailhog.example" }
let(:mock_smtp_transaction_response) do
"250 Ok: queued as 2l3Md07BObzB8kRyHZeoN0baSUAhzc7A-NviRioOr80=@mailhog.example"
end
def stub_deliver_response(message)
message.stubs(:deliver!).returns(
Net::SMTP::Response.new("250", mock_smtp_transaction_response)
)
message.stubs(:deliver!).returns(Net::SMTP::Response.new("250", mock_smtp_transaction_response))
end
context "when disable_emails is enabled" do
@ -62,15 +60,16 @@ RSpec.describe Email::Sender do
it "delivers mail to staff user when confirming new email if user is provided" do
Mail::Message.any_instance.expects(:deliver!).once
Fabricate(:email_change_request, {
user: moderator,
new_email: "newemail@testmoderator.com",
old_email: moderator.email,
change_state: EmailChangeRequest.states[:authorizing_new]
})
message = Mail::Message.new(
to: "newemail@testmoderator.com", body: "hello"
Fabricate(
:email_change_request,
{
user: moderator,
new_email: "newemail@testmoderator.com",
old_email: moderator.email,
change_state: EmailChangeRequest.states[:authorizing_new],
},
)
message = Mail::Message.new(to: "newemail@testmoderator.com", body: "hello")
Email::Sender.new(message, :confirm_new_email, moderator).send
end
end
@ -88,20 +87,23 @@ RSpec.describe Email::Sender do
end
it "doesn't deliver when the to address is nil" do
message = Mail::Message.new(body: 'hello')
message = Mail::Message.new(body: "hello")
message.expects(:deliver!).never
Email::Sender.new(message, :hello).send
end
it "doesn't deliver when the to address uses the .invalid tld" do
message = Mail::Message.new(body: 'hello', to: 'myemail@example.invalid')
message = Mail::Message.new(body: "hello", to: "myemail@example.invalid")
message.expects(:deliver!).never
expect { Email::Sender.new(message, :hello).send }.
to change { SkippedEmailLog.where(reason_type: SkippedEmailLog.reason_types[:sender_message_to_invalid]).count }.by(1)
expect { Email::Sender.new(message, :hello).send }.to change {
SkippedEmailLog.where(
reason_type: SkippedEmailLog.reason_types[:sender_message_to_invalid],
).count
}.by(1)
end
it "doesn't deliver when the body is nil" do
message = Mail::Message.new(to: 'eviltrout@test.domain')
message = Mail::Message.new(to: "eviltrout@test.domain")
message.expects(:deliver!).never
Email::Sender.new(message, :hello).send
end
@ -122,69 +124,67 @@ RSpec.describe Email::Sender do
it "downcases hosts" do
expect(Email::Sender.host_for("http://ForumSite.com")).to eq("forumsite.com")
end
end
context 'with a valid message' do
context "with a valid message" do
let(:reply_key) { "abcd" * 8 }
let(:message) do
message = Mail::Message.new(
to: 'eviltrout@test.domain',
body: '**hello**'
)
message = Mail::Message.new(to: "eviltrout@test.domain", body: "**hello**")
stub_deliver_response(message)
message
end
let(:email_sender) { Email::Sender.new(message, :valid_type) }
it 'calls deliver' do
it "calls deliver" do
message.expects(:deliver!).once
email_sender.send
end
context "when no plus addressing" do
before { SiteSetting.reply_by_email_address = '%{reply_key}@test.com' }
before { SiteSetting.reply_by_email_address = "%{reply_key}@test.com" }
it 'should not set the return_path' do
it "should not set the return_path" do
email_sender.send
expect(message.header[:return_path].to_s).to eq("")
end
end
context "with plus addressing" do
before { SiteSetting.reply_by_email_address = 'replies+%{reply_key}@test.com' }
before { SiteSetting.reply_by_email_address = "replies+%{reply_key}@test.com" }
it 'should set the return_path' do
it "should set the return_path" do
email_sender.send
expect(message.header[:return_path].to_s).to eq("replies+verp-#{EmailLog.last.bounce_key}@test.com")
expect(message.header[:return_path].to_s).to eq(
"replies+verp-#{EmailLog.last.bounce_key}@test.com",
)
end
end
context "when topic id is present" do
fab!(:category) { Fabricate(:category, name: 'Name With Space') }
fab!(:category) { Fabricate(:category, name: "Name With Space") }
fab!(:topic) { Fabricate(:topic, category: category) }
fab!(:post) { Fabricate(:post, topic: topic) }
before do
message.header['X-Discourse-Post-Id'] = post.id
message.header['X-Discourse-Topic-Id'] = topic.id
message.header["X-Discourse-Post-Id"] = post.id
message.header["X-Discourse-Topic-Id"] = topic.id
end
it 'should add the right header' do
it "should add the right header" do
email_sender.send
expect(message.header['List-ID']).to be_present
expect(message.header['List-ID'].to_s).to match('name-with-space')
expect(message.header["List-ID"]).to be_present
expect(message.header["List-ID"].to_s).to match("name-with-space")
end
end
context "when topic id is not present" do
it 'should add the right header' do
it "should add the right header" do
email_sender.send
expect(message.header['Message-ID']).to be_present
expect(message.header["Message-ID"]).to be_present
end
end
@ -194,34 +194,37 @@ RSpec.describe Email::Sender do
let(:reply_key) { PostReplyKey.find_by!(post_id: post.id, user_id: user.id).reply_key }
before do
SiteSetting.reply_by_email_address = 'replies+%{reply_key}@test.com'
SiteSetting.email_custom_headers = 'Auto-Submitted: auto-generated|Mail-Reply-To: sender-name+%{reply_key}@domain.net'
SiteSetting.reply_by_email_address = "replies+%{reply_key}@test.com"
SiteSetting.email_custom_headers =
"Auto-Submitted: auto-generated|Mail-Reply-To: sender-name+%{reply_key}@domain.net"
message.header['X-Discourse-Post-Id'] = post.id
message.header["X-Discourse-Post-Id"] = post.id
end
it 'replaces headers with reply_key if present' do
message.header[Email::MessageBuilder::ALLOW_REPLY_BY_EMAIL_HEADER] = 'test-%{reply_key}@test.com'
message.header['Reply-To'] = 'Test <test-%{reply_key}@test.com>'
message.header['Auto-Submitted'] = 'auto-generated'
message.header['Mail-Reply-To'] = 'sender-name+%{reply_key}@domain.net'
it "replaces headers with reply_key if present" do
message.header[
Email::MessageBuilder::ALLOW_REPLY_BY_EMAIL_HEADER
] = "test-%{reply_key}@test.com"
message.header["Reply-To"] = "Test <test-%{reply_key}@test.com>"
message.header["Auto-Submitted"] = "auto-generated"
message.header["Mail-Reply-To"] = "sender-name+%{reply_key}@domain.net"
email_sender.send
expect(message.header['Reply-To'].to_s).to eq("Test <test-#{reply_key}@test.com>")
expect(message.header['Auto-Submitted'].to_s).to eq('auto-generated')
expect(message.header['Mail-Reply-To'].to_s).to eq("sender-name+#{reply_key}@domain.net")
expect(message.header["Reply-To"].to_s).to eq("Test <test-#{reply_key}@test.com>")
expect(message.header["Auto-Submitted"].to_s).to eq("auto-generated")
expect(message.header["Mail-Reply-To"].to_s).to eq("sender-name+#{reply_key}@domain.net")
end
it 'removes headers with reply_key if absent' do
message.header['Auto-Submitted'] = 'auto-generated'
message.header['Mail-Reply-To'] = 'sender-name+%{reply_key}@domain.net'
it "removes headers with reply_key if absent" do
message.header["Auto-Submitted"] = "auto-generated"
message.header["Mail-Reply-To"] = "sender-name+%{reply_key}@domain.net"
email_sender.send
expect(message.header['Reply-To'].to_s).to eq('')
expect(message.header['Auto-Submitted'].to_s). to eq('auto-generated')
expect(message.header['Mail-Reply-To'].to_s).to eq('')
expect(message.header["Reply-To"].to_s).to eq("")
expect(message.header["Auto-Submitted"].to_s).to eq("auto-generated")
expect(message.header["Mail-Reply-To"].to_s).to eq("")
end
end
@ -230,22 +233,22 @@ RSpec.describe Email::Sender do
fab!(:post) { Fabricate(:post, topic: topic) }
before do
message.header['X-Discourse-Post-Id'] = post.id
message.header['X-Discourse-Topic-Id'] = topic.id
message.header["X-Discourse-Post-Id"] = post.id
message.header["X-Discourse-Topic-Id"] = topic.id
end
it 'should add the right header' do
it "should add the right header" do
email_sender.send
expect(message.header['Precedence']).to be_present
expect(message.header["Precedence"]).to be_present
end
end
describe "removes custom Discourse headers from digest/registration/other mails" do
it 'should remove the right headers' do
it "should remove the right headers" do
email_sender.send
expect(message.header['X-Discourse-Topic-Id']).not_to be_present
expect(message.header['X-Discourse-Post-Id']).not_to be_present
expect(message.header['X-Discourse-Reply-Key']).not_to be_present
expect(message.header["X-Discourse-Topic-Id"]).not_to be_present
expect(message.header["X-Discourse-Post-Id"]).not_to be_present
expect(message.header["X-Discourse-Reply-Key"]).not_to be_present
end
end
@ -266,153 +269,175 @@ RSpec.describe Email::Sender do
let!(:post_reply_4_6) { PostReply.create(post: post_4, reply: post_6) }
let!(:post_reply_5_6) { PostReply.create(post: post_5, reply: post_6) }
before do
message.header['X-Discourse-Topic-Id'] = topic.id
end
before { message.header["X-Discourse-Topic-Id"] = topic.id }
it "doesn't set References or In-Reply-To headers on the first post, only generates a Message-ID and saves it against the post" do
message.header['X-Discourse-Post-Id'] = post_1.id
message.header["X-Discourse-Post-Id"] = post_1.id
email_sender.send
post_1.reload
expect(message.header['Message-Id'].to_s).to eq("<discourse/post/#{post_1.id}@test.localhost>")
expect(message.header["Message-Id"].to_s).to eq(
"<discourse/post/#{post_1.id}@test.localhost>",
)
expect(post_1.outbound_message_id).to eq("discourse/post/#{post_1.id}@test.localhost")
expect(message.header['In-Reply-To'].to_s).to be_blank
expect(message.header['References'].to_s).to be_blank
expect(message.header["In-Reply-To"].to_s).to be_blank
expect(message.header["References"].to_s).to be_blank
end
it "uses the existing Message-ID header from the incoming email when sending the first post email" do
incoming = Fabricate(
:incoming_email,
topic: topic,
post: post_1,
message_id: "blah1234@someemailprovider.com",
created_via: IncomingEmail.created_via_types[:handle_mail]
)
incoming =
Fabricate(
:incoming_email,
topic: topic,
post: post_1,
message_id: "blah1234@someemailprovider.com",
created_via: IncomingEmail.created_via_types[:handle_mail],
)
post_1.update!(outbound_message_id: incoming.message_id)
message.header['X-Discourse-Post-Id'] = post_1.id
message.header["X-Discourse-Post-Id"] = post_1.id
email_sender.send
expect(message.header['Message-Id'].to_s).to eq("<blah1234@someemailprovider.com>")
expect(message.header['In-Reply-To'].to_s).to be_blank
expect(message.header['References'].to_s).to be_blank
expect(message.header["Message-Id"].to_s).to eq("<blah1234@someemailprovider.com>")
expect(message.header["In-Reply-To"].to_s).to be_blank
expect(message.header["References"].to_s).to be_blank
end
it "if no post is directly replied to then the Message-ID of post 1 via outbound_message_id should be used" do
message.header['X-Discourse-Post-Id'] = post_2.id
message.header["X-Discourse-Post-Id"] = post_2.id
email_sender.send
expect(message.header['Message-Id'].to_s).to eq("<discourse/post/#{post_2.id}@test.localhost>")
expect(message.header['In-Reply-To'].to_s).to eq("<discourse/post/#{post_1.id}@test.localhost>")
expect(message.header['References'].to_s).to eq("<discourse/post/#{post_1.id}@test.localhost>")
expect(message.header["Message-Id"].to_s).to eq(
"<discourse/post/#{post_2.id}@test.localhost>",
)
expect(message.header["In-Reply-To"].to_s).to eq(
"<discourse/post/#{post_1.id}@test.localhost>",
)
expect(message.header["References"].to_s).to eq(
"<discourse/post/#{post_1.id}@test.localhost>",
)
end
it "sets the References header to the most recently created replied post, as well as the OP, if there are no other replies in the chain" do
message.header['X-Discourse-Post-Id'] = post_4.id
message.header["X-Discourse-Post-Id"] = post_4.id
email_sender.send
expect(message.header['Message-ID'].to_s).to eq("<discourse/post/#{post_4.id}@test.localhost>")
expect(message.header['References'].to_s).to eq("<discourse/post/#{post_1.id}@test.localhost> <discourse/post/#{post_3.id}@test.localhost>")
expect(message.header["Message-ID"].to_s).to eq(
"<discourse/post/#{post_4.id}@test.localhost>",
)
expect(message.header["References"].to_s).to eq(
"<discourse/post/#{post_1.id}@test.localhost> <discourse/post/#{post_3.id}@test.localhost>",
)
end
it "sets the In-Reply-To header to all the posts that the post is connected to via PostReply" do
message.header['X-Discourse-Post-Id'] = post_6.id
message.header["X-Discourse-Post-Id"] = post_6.id
email_sender.send
expect(message.header['Message-ID'].to_s).to eq("<discourse/post/#{post_6.id}@test.localhost>")
expect(message.header['In-Reply-To'].to_s).to eq("<discourse/post/#{post_4.id}@test.localhost> <discourse/post/#{post_5.id}@test.localhost>")
expect(message.header["Message-ID"].to_s).to eq(
"<discourse/post/#{post_6.id}@test.localhost>",
)
expect(message.header["In-Reply-To"].to_s).to eq(
"<discourse/post/#{post_4.id}@test.localhost> <discourse/post/#{post_5.id}@test.localhost>",
)
end
it "sets the In-Reply-To and References header to the most recently created replied post and includes the parents of that post in References, as well as the OP" do
message.header['X-Discourse-Post-Id'] = post_4.id
message.header["X-Discourse-Post-Id"] = post_4.id
PostReply.create(post: post_2, reply: post_3)
email_sender.send
expect(message.header['Message-ID'].to_s).to eq("<discourse/post/#{post_4.id}@test.localhost>")
expect(message.header['In-Reply-To'].to_s).to eq("<discourse/post/#{post_1.id}@test.localhost> <discourse/post/#{post_2.id}@test.localhost> <discourse/post/#{post_3.id}@test.localhost>")
expect(message.header["Message-ID"].to_s).to eq(
"<discourse/post/#{post_4.id}@test.localhost>",
)
expect(message.header["In-Reply-To"].to_s).to eq(
"<discourse/post/#{post_1.id}@test.localhost> <discourse/post/#{post_2.id}@test.localhost> <discourse/post/#{post_3.id}@test.localhost>",
)
references = [
"<discourse/post/#{post_1.id}@test.localhost>",
"<discourse/post/#{post_2.id}@test.localhost>",
"<discourse/post/#{post_3.id}@test.localhost>"
"<discourse/post/#{post_3.id}@test.localhost>",
]
expect(message.header['References'].to_s).to eq(references.join(" "))
expect(message.header["References"].to_s).to eq(references.join(" "))
end
it "handles a complex reply tree to the OP for References, only using one Message-ID if there are multiple parents for a post" do
message.header['X-Discourse-Post-Id'] = post_6.id
message.header["X-Discourse-Post-Id"] = post_6.id
PostReply.create(post: post_2, reply: post_6)
email_sender.send
expect(message.header['Message-ID'].to_s).to eq("<discourse/post/#{post_6.id}@test.localhost>")
expect(message.header['In-Reply-To'].to_s).to eq("<discourse/post/#{post_2.id}@test.localhost> <discourse/post/#{post_4.id}@test.localhost> <discourse/post/#{post_5.id}@test.localhost>")
expect(message.header["Message-ID"].to_s).to eq(
"<discourse/post/#{post_6.id}@test.localhost>",
)
expect(message.header["In-Reply-To"].to_s).to eq(
"<discourse/post/#{post_2.id}@test.localhost> <discourse/post/#{post_4.id}@test.localhost> <discourse/post/#{post_5.id}@test.localhost>",
)
references = [
"<discourse/post/#{post_1.id}@test.localhost>",
"<discourse/post/#{post_3.id}@test.localhost>",
"<discourse/post/#{post_4.id}@test.localhost>",
"<discourse/post/#{post_5.id}@test.localhost>"
"<discourse/post/#{post_5.id}@test.localhost>",
]
expect(message.header['References'].to_s).to eq(references.join(" "))
expect(message.header["References"].to_s).to eq(references.join(" "))
end
end
describe "merges custom mandrill header" do
before do
ActionMailer::Base.smtp_settings[:address] = "smtp.mandrillapp.com"
message.header['X-MC-Metadata'] = { foo: "bar" }.to_json
message.header["X-MC-Metadata"] = { foo: "bar" }.to_json
end
it 'should set the right header' do
it "should set the right header" do
email_sender.send
expect(message.header['X-MC-Metadata'].to_s).to match(message.message_id)
expect(message.header["X-MC-Metadata"].to_s).to match(message.message_id)
end
end
describe "merges custom sparkpost header" do
before do
ActionMailer::Base.smtp_settings[:address] = "smtp.sparkpostmail.com"
message.header['X-MSYS-API'] = { foo: "bar" }.to_json
message.header["X-MSYS-API"] = { foo: "bar" }.to_json
end
it 'should set the right header' do
it "should set the right header" do
email_sender.send
expect(message.header['X-MSYS-API'].to_s).to match(message.message_id)
expect(message.header["X-MSYS-API"].to_s).to match(message.message_id)
end
end
context 'with email logs' do
context "with email logs" do
let(:email_log) { EmailLog.last }
it 'should create the right log' do
expect do
email_sender.send
end.to_not change { PostReplyKey.count }
it "should create the right log" do
expect do email_sender.send end.to_not change { PostReplyKey.count }
expect(email_log).to be_present
expect(email_log.email_type).to eq('valid_type')
expect(email_log.to_address).to eq('eviltrout@test.domain')
expect(email_log.email_type).to eq("valid_type")
expect(email_log.to_address).to eq("eviltrout@test.domain")
expect(email_log.user_id).to be_blank
expect(email_log.raw).to eq(nil)
end
context 'when the email is sent using group SMTP credentials' do
let(:reply) { Fabricate(:post, topic: post.topic, reply_to_user: post.user, reply_to_post_number: post.post_number) }
let(:notification) { Fabricate(:posted_notification, user: post.user, post: reply) }
let(:message) do
GroupSmtpMailer.send_mail(
group,
post.user.email,
post
context "when the email is sent using group SMTP credentials" do
let(:reply) do
Fabricate(
:post,
topic: post.topic,
reply_to_user: post.user,
reply_to_post_number: post.post_number,
)
end
let(:notification) { Fabricate(:posted_notification, user: post.user, post: reply) }
let(:message) { GroupSmtpMailer.send_mail(group, post.user.email, post) }
let(:group) { Fabricate(:smtp_group) }
before do
@ -420,13 +445,13 @@ RSpec.describe Email::Sender do
stub_deliver_response(message)
end
it 'adds the group id and raw content to the email log' do
it "adds the group id and raw content to the email log" do
TopicAllowedGroup.create(topic: post.topic, group: group)
email_sender.send
expect(email_log).to be_present
expect(email_log.email_type).to eq('valid_type')
expect(email_log.email_type).to eq("valid_type")
expect(email_log.to_address).to eq(post.user.email)
expect(email_log.user_id).to be_blank
expect(email_log.smtp_group_id).to eq(group.id)
@ -437,17 +462,17 @@ RSpec.describe Email::Sender do
TopicAllowedGroup.create(topic: post.topic, group: group)
email_sender.send
expect(message.header['List-ID']).to eq(nil)
expect(message.header['List-Archive']).to eq(nil)
expect(message.header['Precedence']).to eq(nil)
expect(message.header['List-Unsubscribe']).to eq(nil)
expect(message.header["List-ID"]).to eq(nil)
expect(message.header["List-Archive"]).to eq(nil)
expect(message.header["Precedence"]).to eq(nil)
expect(message.header["List-Unsubscribe"]).to eq(nil)
end
it "removes the Auto-Submitted header" do
TopicAllowedGroup.create!(topic: post.topic, group: group)
email_sender.send
expect(message.header['Auto-Submitted']).to eq(nil)
expect(message.header["Auto-Submitted"]).to eq(nil)
end
end
end
@ -456,13 +481,13 @@ RSpec.describe Email::Sender do
let(:topic) { post.topic }
before do
message.header['X-Discourse-Post-Id'] = post.id
message.header['X-Discourse-Topic-Id'] = topic.id
message.header["X-Discourse-Post-Id"] = post.id
message.header["X-Discourse-Topic-Id"] = topic.id
end
let(:email_log) { EmailLog.last }
it 'should create the right log' do
it "should create the right log" do
email_sender.send
expect(email_log.post_id).to eq(post.id)
expect(email_log.topic_id).to eq(topic.id)
@ -470,13 +495,13 @@ RSpec.describe Email::Sender do
end
end
context 'with email parts' do
it 'should contain the right message' do
context "with email parts" do
it "should contain the right message" do
email_sender.send
expect(message).to be_multipart
expect(message.text_part.content_type).to eq('text/plain; charset=UTF-8')
expect(message.html_part.content_type).to eq('text/html; charset=UTF-8')
expect(message.text_part.content_type).to eq("text/plain; charset=UTF-8")
expect(message.html_part.content_type).to eq("text/html; charset=UTF-8")
expect(message.html_part.body.to_s).to match("<p><strong>hello</strong></p>")
end
end
@ -484,24 +509,28 @@ RSpec.describe Email::Sender do
context "with attachments" do
fab!(:small_pdf) do
SiteSetting.authorized_extensions = 'pdf'
UploadCreator.new(file_from_fixtures("small.pdf", "pdf"), "small.pdf")
.create_for(Discourse.system_user.id)
SiteSetting.authorized_extensions = "pdf"
UploadCreator.new(file_from_fixtures("small.pdf", "pdf"), "small.pdf").create_for(
Discourse.system_user.id,
)
end
fab!(:large_pdf) do
SiteSetting.authorized_extensions = 'pdf'
UploadCreator.new(file_from_fixtures("large.pdf", "pdf"), "large.pdf")
.create_for(Discourse.system_user.id)
SiteSetting.authorized_extensions = "pdf"
UploadCreator.new(file_from_fixtures("large.pdf", "pdf"), "large.pdf").create_for(
Discourse.system_user.id,
)
end
fab!(:csv_file) do
SiteSetting.authorized_extensions = 'csv'
UploadCreator.new(file_from_fixtures("words.csv", "csv"), "words.csv")
.create_for(Discourse.system_user.id)
SiteSetting.authorized_extensions = "csv"
UploadCreator.new(file_from_fixtures("words.csv", "csv"), "words.csv").create_for(
Discourse.system_user.id,
)
end
fab!(:image) do
SiteSetting.authorized_extensions = 'png'
UploadCreator.new(file_from_fixtures("logo.png", "images"), "logo.png")
.create_for(Discourse.system_user.id)
SiteSetting.authorized_extensions = "png"
UploadCreator.new(file_from_fixtures("logo.png", "images"), "logo.png").create_for(
Discourse.system_user.id,
)
end
fab!(:post) { Fabricate(:post) }
fab!(:reply) do
@ -522,7 +551,7 @@ RSpec.describe Email::Sender do
post.user,
post: reply,
notification_type: notification.notification_type,
notification_data_hash: notification.data_hash
notification_data_hash: notification.data_hash,
)
end
@ -531,8 +560,9 @@ RSpec.describe Email::Sender do
Email::Sender.new(message, :valid_type).send
expect(message.attachments.length).to eq(3)
expect(message.attachments.map(&:filename))
.to contain_exactly(*[small_pdf, large_pdf, csv_file].map(&:original_filename))
expect(message.attachments.map(&:filename)).to contain_exactly(
*[small_pdf, large_pdf, csv_file].map(&:original_filename),
)
end
it "changes the hashtags to the slug with a # symbol beforehand rather than the full name of the resource" do
@ -563,8 +593,8 @@ RSpec.describe Email::Sender do
CookedPostProcessor.any_instance.stubs(:get_size).returns([244, 66])
@secure_image_file = file_from_fixtures("logo.png", "images")
@secure_image = UploadCreator.new(@secure_image_file, "logo.png")
.create_for(Discourse.system_user.id)
@secure_image =
UploadCreator.new(@secure_image_file, "logo.png").create_for(Discourse.system_user.id)
@secure_image.update_secure_status(override: true)
@secure_image.update(access_control_post_id: reply.id)
reply.update(raw: reply.raw + "\n" + "#{UploadMarkdown.new(@secure_image).image_markdown}")
@ -577,17 +607,21 @@ RSpec.describe Email::Sender do
end
context "when embedding secure images in email is allowed" do
before do
SiteSetting.secure_uploads_allow_embed_images_in_emails = true
end
before { SiteSetting.secure_uploads_allow_embed_images_in_emails = true }
it "can inline images with duplicate names" do
@secure_image_2 = UploadCreator.new(file_from_fixtures("logo-dev.png", "images"), "logo.png").create_for(Discourse.system_user.id)
@secure_image_2 =
UploadCreator.new(file_from_fixtures("logo-dev.png", "images"), "logo.png").create_for(
Discourse.system_user.id,
)
@secure_image_2.update_secure_status(override: true)
@secure_image_2.update(access_control_post_id: reply.id)
Jobs::PullHotlinkedImages.any_instance.expects(:execute)
reply.update(raw: "#{UploadMarkdown.new(@secure_image).image_markdown}\n#{UploadMarkdown.new(@secure_image_2).image_markdown}")
reply.update(
raw:
"#{UploadMarkdown.new(@secure_image).image_markdown}\n#{UploadMarkdown.new(@secure_image_2).image_markdown}",
)
reply.rebake!
Email::Sender.new(message, :valid_type).send
@ -610,9 +644,12 @@ RSpec.describe Email::Sender do
it "uses the email styles to inline secure images and attaches the secure image upload to the email" do
Email::Sender.new(message, :valid_type).send
expect(message.attachments.length).to eq(4)
expect(message.attachments.map(&:filename))
.to contain_exactly(*[small_pdf, large_pdf, csv_file, @secure_image].map(&:original_filename))
expect(message.attachments["logo.png"].body.raw_source.force_encoding("UTF-8")).to eq(File.read(@secure_image_file))
expect(message.attachments.map(&:filename)).to contain_exactly(
*[small_pdf, large_pdf, csv_file, @secure_image].map(&:original_filename),
)
expect(message.attachments["logo.png"].body.raw_source.force_encoding("UTF-8")).to eq(
File.read(@secure_image_file),
)
expect(message.html_part.body).to include("cid:")
expect(message.html_part.body).to include("embedded-secure-image")
expect(message.attachments.length).to eq(4)
@ -631,16 +668,19 @@ RSpec.describe Email::Sender do
before do
url = Discourse.store.store_optimized_image(optimized_image_file, optimized)
optimized.update(url: Discourse.store.absolute_base_url + '/' + url)
optimized.update(url: Discourse.store.absolute_base_url + "/" + url)
Discourse.store.cache_file(optimized_image_file, File.basename("#{optimized.sha1}.png"))
end
it "uses the email styles and the optimized image to inline secure images and attaches the secure image upload to the email" do
Email::Sender.new(message, :valid_type).send
expect(message.attachments.length).to eq(4)
expect(message.attachments.map(&:filename))
.to contain_exactly(*[small_pdf, large_pdf, csv_file, @secure_image].map(&:original_filename))
expect(message.attachments["logo.png"].body.raw_source.force_encoding("UTF-8")).to eq(File.read(optimized_image_file))
expect(message.attachments.map(&:filename)).to contain_exactly(
*[small_pdf, large_pdf, csv_file, @secure_image].map(&:original_filename),
)
expect(message.attachments["logo.png"].body.raw_source.force_encoding("UTF-8")).to eq(
File.read(optimized_image_file),
)
expect(message.html_part.body).to include("cid:")
expect(message.html_part.body).to include("embedded-secure-image")
end
@ -649,7 +689,9 @@ RSpec.describe Email::Sender do
SiteSetting.email_total_attachment_size_limit_kb = 45
Email::Sender.new(message, :valid_type).send
expect(message.attachments.length).to eq(4)
expect(message.attachments["logo.png"].body.raw_source.force_encoding("UTF-8")).to eq(File.read(optimized_image_file))
expect(message.attachments["logo.png"].body.raw_source.force_encoding("UTF-8")).to eq(
File.read(optimized_image_file),
)
end
end
end
@ -660,8 +702,9 @@ RSpec.describe Email::Sender do
Email::Sender.new(message, :valid_type).send
expect(message.attachments.length).to eq(3)
expect(message.attachments.map(&:filename))
.to contain_exactly(*[small_pdf, large_pdf, csv_file].map(&:original_filename))
expect(message.attachments.map(&:filename)).to contain_exactly(
*[small_pdf, large_pdf, csv_file].map(&:original_filename),
)
expect(message.html_part.body).to include("<img src=\"#{Discourse.base_url}#{image.url}\"")
end
@ -670,8 +713,9 @@ RSpec.describe Email::Sender do
Email::Sender.new(message, :valid_type).send
expect(message.attachments.length).to eq(2)
expect(message.attachments.map(&:filename))
.to contain_exactly(*[small_pdf, csv_file].map(&:original_filename))
expect(message.attachments.map(&:filename)).to contain_exactly(
*[small_pdf, csv_file].map(&:original_filename),
)
end
it "structures the email as a multipart/mixed with a multipart/alternative first part" do
@ -692,13 +736,13 @@ RSpec.describe Email::Sender do
end
end
context 'with a deleted post' do
it 'should skip sending the email' do
context "with a deleted post" do
it "should skip sending the email" do
post = Fabricate(:post, deleted_at: 1.day.ago)
message = Mail::Message.new to: 'disc@ourse.org', body: 'some content'
message.header['X-Discourse-Post-Id'] = post.id
message.header['X-Discourse-Topic-Id'] = post.topic_id
message = Mail::Message.new to: "disc@ourse.org", body: "some content"
message.header["X-Discourse-Post-Id"] = post.id
message.header["X-Discourse-Topic-Id"] = post.topic_id
message.expects(:deliver!).never
email_sender = Email::Sender.new(message, :valid_type)
@ -707,16 +751,15 @@ RSpec.describe Email::Sender do
log = SkippedEmailLog.last
expect(log.reason_type).to eq(SkippedEmailLog.reason_types[:sender_post_deleted])
end
end
context 'with a deleted topic' do
it 'should skip sending the email' do
context "with a deleted topic" do
it "should skip sending the email" do
post = Fabricate(:post, topic: Fabricate(:topic, deleted_at: 1.day.ago))
message = Mail::Message.new to: 'disc@ourse.org', body: 'some content'
message.header['X-Discourse-Post-Id'] = post.id
message.header['X-Discourse-Topic-Id'] = post.topic_id
message = Mail::Message.new to: "disc@ourse.org", body: "some content"
message.header["X-Discourse-Post-Id"] = post.id
message.header["X-Discourse-Topic-Id"] = post.topic_id
message.expects(:deliver!).never
email_sender = Email::Sender.new(message, :valid_type)
@ -725,12 +768,11 @@ RSpec.describe Email::Sender do
log = SkippedEmailLog.last
expect(log.reason_type).to eq(SkippedEmailLog.reason_types[:sender_topic_deleted])
end
end
context 'with a user' do
context "with a user" do
let(:message) do
message = Mail::Message.new to: 'eviltrout@test.domain', body: 'test body'
message = Mail::Message.new to: "eviltrout@test.domain", body: "test body"
stub_deliver_response(message)
message
end
@ -743,11 +785,11 @@ RSpec.describe Email::Sender do
@email_log = EmailLog.last
end
it 'should have the current user_id' do
it "should have the current user_id" do
expect(@email_log.user_id).to eq(user.id)
end
it 'should have the smtp_transaction_response message' do
it "should have the smtp_transaction_response message" do
expect(@email_log.smtp_transaction_response).to eq(mock_smtp_transaction_response)
end
@ -755,29 +797,27 @@ RSpec.describe Email::Sender do
fab!(:post) { Fabricate(:post) }
before do
message.header['X-Discourse-Post-Id'] = post.id
message.header['Reply-To'] = "test-%{reply_key}@test.com"
message.header["X-Discourse-Post-Id"] = post.id
message.header["Reply-To"] = "test-%{reply_key}@test.com"
end
describe 'when allow reply by email header is not present' do
it 'should not create a post reply key' do
describe "when allow reply by email header is not present" do
it "should not create a post reply key" do
expect { email_sender.send }.to_not change { PostReplyKey.count }
end
end
describe 'when allow reply by email header is present' do
describe "when allow reply by email header is present" do
let(:header) { Email::MessageBuilder::ALLOW_REPLY_BY_EMAIL_HEADER }
before do
message.header[header] = "test-%{reply_key}@test.com"
end
before { message.header[header] = "test-%{reply_key}@test.com" }
it 'should create a post reply key' do
it "should create a post reply key" do
expect { email_sender.send }.to change { PostReplyKey.count }.by(1)
post_reply_key = PostReplyKey.last
expect(message.header['Reply-To'].value).to eq(
"test-#{post_reply_key.reply_key}@test.com"
expect(message.header["Reply-To"].value).to eq(
"test-#{post_reply_key.reply_key}@test.com",
)
expect(message.header[header]).to eq(nil)
@ -786,20 +826,22 @@ RSpec.describe Email::Sender do
expect { email_sender.send }.not_to change { PostReplyKey.count }
end
it 'should find existing key' do
it "should find existing key" do
existing_post_reply_key = PostReplyKey.create(post_id: post.id, user_id: user.id)
expect { email_sender.send }.not_to change { PostReplyKey.count }
post_reply_key = PostReplyKey.last
expect(post_reply_key).to eq(existing_post_reply_key)
end
end
end
end
context "with cc addresses" do
let(:message) do
message = Mail::Message.new to: 'eviltrout@test.domain', body: 'test body', cc: 'someguy@test.com;otherguy@xyz.com'
message =
Mail::Message.new to: "eviltrout@test.domain",
body: "test body",
cc: "someguy@test.com;otherguy@xyz.com"
stub_deliver_response(message)
message
end