2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-10-11 05:41:23 -04:00
|
|
|
require 'rails_helper'
|
2013-06-03 16:12:24 -04:00
|
|
|
|
|
|
|
describe Admin::EmailController do
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:admin) { Fabricate(:admin) }
|
|
|
|
fab!(:email_log) { Fabricate(:email_log) }
|
2018-06-11 00:50:08 -04:00
|
|
|
|
|
|
|
before do
|
|
|
|
sign_in(admin)
|
|
|
|
end
|
2013-06-03 16:12:24 -04:00
|
|
|
|
|
|
|
it "is a subclass of AdminController" do
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(Admin::EmailController < Admin::AdminController).to eq(true)
|
2013-06-03 16:12:24 -04:00
|
|
|
end
|
|
|
|
|
2018-06-11 00:50:08 -04:00
|
|
|
describe '#index' do
|
2013-06-03 16:12:24 -04:00
|
|
|
before do
|
2018-06-11 00:50:08 -04:00
|
|
|
Admin::EmailController.any_instance
|
2017-08-31 00:06:56 -04:00
|
|
|
.expects(:action_mailer_settings)
|
|
|
|
.returns(
|
|
|
|
username: 'username',
|
|
|
|
password: 'secret'
|
|
|
|
)
|
2013-06-03 16:12:24 -04:00
|
|
|
end
|
|
|
|
|
2013-06-11 19:00:13 -04:00
|
|
|
it 'does not include the password in the response' do
|
2018-06-11 00:50:08 -04:00
|
|
|
get "/admin/email.json"
|
2013-06-11 19:00:13 -04:00
|
|
|
mail_settings = JSON.parse(response.body)['settings']
|
|
|
|
|
|
|
|
expect(
|
|
|
|
mail_settings.select { |setting| setting['name'] == 'password' }
|
|
|
|
).to be_empty
|
|
|
|
end
|
2013-06-03 16:12:24 -04:00
|
|
|
end
|
|
|
|
|
2018-06-11 00:50:08 -04:00
|
|
|
describe '#sent' do
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:post) { Fabricate(:post) }
|
|
|
|
fab!(:email_log) { Fabricate(:email_log, post: post) }
|
2018-07-18 04:28:44 -04:00
|
|
|
|
|
|
|
let(:post_reply_key) do
|
|
|
|
Fabricate(:post_reply_key, post: post, user: email_log.user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should return the right response" do
|
|
|
|
email_log
|
|
|
|
get "/admin/email/sent.json"
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
log = JSON.parse(response.body).first
|
|
|
|
expect(log["id"]).to eq(email_log.id)
|
|
|
|
expect(log["reply_key"]).to eq(nil)
|
|
|
|
|
|
|
|
post_reply_key
|
|
|
|
|
2018-06-11 00:50:08 -04:00
|
|
|
get "/admin/email/sent.json"
|
2018-07-18 04:28:44 -04:00
|
|
|
|
2018-06-11 00:50:08 -04:00
|
|
|
expect(response.status).to eq(200)
|
2018-07-18 04:28:44 -04:00
|
|
|
log = JSON.parse(response.body).first
|
|
|
|
expect(log["id"]).to eq(email_log.id)
|
|
|
|
expect(log["reply_key"]).to eq(post_reply_key.reply_key)
|
2014-02-14 18:50:08 -05:00
|
|
|
end
|
2019-01-09 20:21:28 -05:00
|
|
|
|
|
|
|
it 'should be able to filter by reply key' do
|
|
|
|
email_log_2 = Fabricate(:email_log, post: post)
|
|
|
|
|
|
|
|
post_reply_key_2 = Fabricate(:post_reply_key,
|
|
|
|
post: post,
|
|
|
|
user: email_log_2.user,
|
|
|
|
reply_key: "2d447423-c625-4fb9-8717-ff04ac60eee8"
|
|
|
|
)
|
|
|
|
|
|
|
|
[
|
2019-01-09 20:56:03 -05:00
|
|
|
"17ff04",
|
|
|
|
"2d447423c6254fb98717ff04ac60eee8"
|
2019-01-09 20:21:28 -05:00
|
|
|
].each do |reply_key|
|
|
|
|
get "/admin/email/sent.json", params: {
|
|
|
|
reply_key: reply_key
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
logs = JSON.parse(response.body)
|
|
|
|
|
|
|
|
expect(logs.size).to eq(1)
|
|
|
|
expect(logs.first["reply_key"]).to eq(post_reply_key_2.reply_key)
|
|
|
|
end
|
|
|
|
end
|
2014-02-14 18:50:08 -05:00
|
|
|
end
|
|
|
|
|
2018-06-11 00:50:08 -04:00
|
|
|
describe '#skipped' do
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:user) { Fabricate(:user) }
|
|
|
|
fab!(:log1) { Fabricate(:skipped_email_log, user: user) }
|
|
|
|
fab!(:log2) { Fabricate(:skipped_email_log) }
|
2018-07-24 00:55:43 -04:00
|
|
|
|
2018-06-11 00:50:08 -04:00
|
|
|
it "succeeds" do
|
|
|
|
get "/admin/email/skipped.json"
|
2018-07-24 00:55:43 -04:00
|
|
|
|
2018-06-11 00:50:08 -04:00
|
|
|
expect(response.status).to eq(200)
|
2018-07-24 00:55:43 -04:00
|
|
|
|
|
|
|
logs = JSON.parse(response.body)
|
|
|
|
|
|
|
|
expect(logs.first["id"]).to eq(log2.id)
|
|
|
|
expect(logs.last["id"]).to eq(log1.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'when filtered by username' do
|
|
|
|
it 'should return the right response' do
|
|
|
|
get "/admin/email/skipped.json", params: {
|
|
|
|
user: user.username
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
logs = JSON.parse(response.body)
|
|
|
|
|
|
|
|
expect(logs.count).to eq(1)
|
|
|
|
expect(logs.first["id"]).to eq(log1.id)
|
|
|
|
end
|
2013-06-03 16:12:24 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-11 00:50:08 -04:00
|
|
|
describe '#test' do
|
2013-06-03 16:12:24 -04:00
|
|
|
it 'raises an error without the email parameter' do
|
2018-06-11 00:50:08 -04:00
|
|
|
post "/admin/email/test.json"
|
|
|
|
expect(response.status).to eq(400)
|
2013-06-03 16:12:24 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'with an email address' do
|
|
|
|
it 'enqueues a test email job' do
|
2018-06-11 00:50:08 -04:00
|
|
|
post "/admin/email/test.json", params: { email_address: 'eviltrout@test.domain' }
|
2018-07-24 00:55:43 -04:00
|
|
|
|
2018-06-11 00:50:08 -04:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(ActionMailer::Base.deliveries.map(&:to).flatten).to include('eviltrout@test.domain')
|
2013-06-03 16:12:24 -04:00
|
|
|
end
|
|
|
|
end
|
2018-08-29 17:14:16 -04:00
|
|
|
|
|
|
|
context 'with SiteSetting.disable_emails' do
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:eviltrout) { Fabricate(:evil_trout) }
|
|
|
|
fab!(:admin) { Fabricate(:admin) }
|
2018-08-29 17:14:16 -04:00
|
|
|
|
2019-03-21 17:57:09 -04:00
|
|
|
it 'bypasses disable when setting is "yes"' do
|
2018-08-29 17:14:16 -04:00
|
|
|
SiteSetting.disable_emails = 'yes'
|
|
|
|
post "/admin/email/test.json", params: { email_address: admin.email }
|
|
|
|
|
2019-03-21 17:57:09 -04:00
|
|
|
expect(ActionMailer::Base.deliveries.first.to).to contain_exactly(
|
|
|
|
admin.email
|
|
|
|
)
|
|
|
|
|
2018-08-29 17:14:16 -04:00
|
|
|
incoming = JSON.parse(response.body)
|
2019-03-21 17:57:09 -04:00
|
|
|
expect(incoming['sent_test_email_message']).to eq(I18n.t("admin.email.sent_test"))
|
2018-08-29 17:14:16 -04:00
|
|
|
end
|
|
|
|
|
2019-03-21 17:57:09 -04:00
|
|
|
it 'bypasses disable when setting is "non-staff"' do
|
2018-08-29 17:14:16 -04:00
|
|
|
SiteSetting.disable_emails = 'non-staff'
|
|
|
|
|
|
|
|
post "/admin/email/test.json", params: { email_address: eviltrout.email }
|
2019-03-21 17:57:09 -04:00
|
|
|
|
|
|
|
expect(ActionMailer::Base.deliveries.first.to).to contain_exactly(
|
|
|
|
eviltrout.email
|
|
|
|
)
|
|
|
|
|
2018-08-29 17:14:16 -04:00
|
|
|
incoming = JSON.parse(response.body)
|
2019-03-21 17:57:09 -04:00
|
|
|
expect(incoming['sent_test_email_message']).to eq(I18n.t("admin.email.sent_test"))
|
2018-08-29 17:14:16 -04:00
|
|
|
end
|
|
|
|
|
2019-03-21 17:57:09 -04:00
|
|
|
it 'works when setting is "no"' do
|
2018-08-29 17:14:16 -04:00
|
|
|
SiteSetting.disable_emails = 'no'
|
|
|
|
|
|
|
|
post "/admin/email/test.json", params: { email_address: eviltrout.email }
|
|
|
|
|
2019-03-21 17:57:09 -04:00
|
|
|
expect(ActionMailer::Base.deliveries.first.to).to contain_exactly(
|
|
|
|
eviltrout.email
|
|
|
|
)
|
|
|
|
|
2018-08-29 17:14:16 -04:00
|
|
|
incoming = JSON.parse(response.body)
|
|
|
|
expect(incoming['sent_test_email_message']).to eq(I18n.t("admin.email.sent_test"))
|
|
|
|
end
|
|
|
|
end
|
2013-06-03 16:12:24 -04:00
|
|
|
end
|
|
|
|
|
2018-06-11 00:50:08 -04:00
|
|
|
describe '#preview_digest' do
|
2013-06-03 16:12:24 -04:00
|
|
|
it 'raises an error without the last_seen_at parameter' do
|
2018-06-11 00:50:08 -04:00
|
|
|
get "/admin/email/preview-digest.json"
|
|
|
|
expect(response.status).to eq(400)
|
2013-06-03 16:12:24 -04:00
|
|
|
end
|
|
|
|
|
2019-01-30 03:04:47 -05:00
|
|
|
it "returns the right response when username is invalid" do
|
|
|
|
get "/admin/email/preview-digest.json", params: {
|
|
|
|
last_seen_at: 1.week.ago, username: "somerandomeusername"
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(400)
|
|
|
|
end
|
|
|
|
|
2013-06-03 16:12:24 -04:00
|
|
|
it "previews the digest" do
|
2018-06-11 00:50:08 -04:00
|
|
|
get "/admin/email/preview-digest.json", params: {
|
|
|
|
last_seen_at: 1.week.ago, username: admin.username
|
|
|
|
}
|
2018-06-07 04:11:09 -04:00
|
|
|
expect(response.status).to eq(200)
|
2013-06-03 16:12:24 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-11 00:50:08 -04:00
|
|
|
describe '#handle_mail' do
|
2017-04-24 00:06:28 -04:00
|
|
|
it 'should enqueue the right job' do
|
2018-06-11 00:50:08 -04:00
|
|
|
expect { post "/admin/email/handle_mail.json", params: { email: email('cc') } }
|
2017-04-24 00:06:28 -04:00
|
|
|
.to change { Jobs::ProcessEmail.jobs.count }.by(1)
|
2018-06-11 00:50:08 -04:00
|
|
|
expect(response.status).to eq(200)
|
2017-04-24 00:06:28 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-11 00:50:08 -04:00
|
|
|
describe '#rejected' do
|
2017-08-04 14:04:26 -04:00
|
|
|
it 'should provide a string for a blank error' do
|
|
|
|
Fabricate(:incoming_email, error: "")
|
2018-06-11 00:50:08 -04:00
|
|
|
get "/admin/email/rejected.json"
|
|
|
|
expect(response.status).to eq(200)
|
2017-08-04 14:04:26 -04:00
|
|
|
rejected = JSON.parse(response.body)
|
|
|
|
expect(rejected.first['error']).to eq(I18n.t("emails.incoming.unrecognized_error"))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-11 00:50:08 -04:00
|
|
|
describe '#incoming' do
|
2017-08-04 14:04:26 -04:00
|
|
|
it 'should provide a string for a blank error' do
|
|
|
|
incoming_email = Fabricate(:incoming_email, error: "")
|
2018-06-11 00:50:08 -04:00
|
|
|
get "/admin/email/incoming/#{incoming_email.id}.json"
|
|
|
|
expect(response.status).to eq(200)
|
2017-08-04 14:04:26 -04:00
|
|
|
incoming = JSON.parse(response.body)
|
|
|
|
expect(incoming['error']).to eq(I18n.t("emails.incoming.unrecognized_error"))
|
|
|
|
end
|
|
|
|
end
|
2018-12-03 06:51:59 -05:00
|
|
|
|
2019-03-26 12:59:56 -04:00
|
|
|
describe '#incoming_from_bounced' do
|
|
|
|
it 'raises an error when the email log entry does not exist' do
|
|
|
|
get "/admin/email/incoming_from_bounced/12345.json"
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
|
|
|
|
json = JSON.parse(response.body)
|
|
|
|
expect(json["errors"]).to include("Discourse::InvalidParameters")
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an error when the email log entry is not marked as bounced' do
|
|
|
|
get "/admin/email/incoming_from_bounced/#{email_log.id}.json"
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
|
|
|
|
json = JSON.parse(response.body)
|
|
|
|
expect(json["errors"]).to include("Discourse::InvalidParameters")
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'bounced email log entry exists' do
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:email_log) { Fabricate(:email_log, bounced: true, bounce_key: SecureRandom.hex) }
|
2019-03-26 12:59:56 -04:00
|
|
|
let(:error_message) { "Email::Receiver::BouncedEmailError" }
|
|
|
|
|
|
|
|
it 'returns an incoming email sent to the reply_by_email_address' do
|
|
|
|
SiteSetting.reply_by_email_address = "replies+%{reply_key}@example.com"
|
|
|
|
|
|
|
|
Fabricate(:incoming_email,
|
|
|
|
is_bounce: true,
|
|
|
|
error: error_message,
|
|
|
|
to_addresses: Email::Sender.bounce_address(email_log.bounce_key)
|
|
|
|
)
|
|
|
|
|
|
|
|
get "/admin/email/incoming_from_bounced/#{email_log.id}.json"
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
json = JSON.parse(response.body)
|
|
|
|
expect(json["error"]).to eq(error_message)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an incoming email sent to the notification_email address' do
|
|
|
|
Fabricate(:incoming_email,
|
|
|
|
is_bounce: true,
|
|
|
|
error: error_message,
|
|
|
|
to_addresses: SiteSetting.notification_email.sub("@", "+verp-#{email_log.bounce_key}@")
|
|
|
|
)
|
|
|
|
|
|
|
|
get "/admin/email/incoming_from_bounced/#{email_log.id}.json"
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
json = JSON.parse(response.body)
|
|
|
|
expect(json["error"]).to eq(error_message)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an error if the bounce_key is blank' do
|
|
|
|
email_log.update(bounce_key: nil)
|
|
|
|
|
|
|
|
get "/admin/email/incoming_from_bounced/#{email_log.id}.json"
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
|
|
|
|
json = JSON.parse(response.body)
|
|
|
|
expect(json["errors"]).to include("Discourse::InvalidParameters")
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an error if there is no incoming email' do
|
|
|
|
get "/admin/email/incoming_from_bounced/#{email_log.id}.json"
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
|
|
|
|
json = JSON.parse(response.body)
|
|
|
|
expect(json["errors"]).to include("Discourse::NotFound")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-12-03 06:51:59 -05:00
|
|
|
describe '#advanced_test' do
|
|
|
|
it 'should ...' do
|
|
|
|
email = <<~EMAIL
|
|
|
|
From: "somebody" <somebody@example.com>
|
|
|
|
To: someone@example.com
|
|
|
|
Date: Mon, 3 Dec 2018 00:00:00 -0000
|
|
|
|
Subject: This is some subject
|
|
|
|
Content-Type: text/plain; charset="UTF-8"
|
|
|
|
|
|
|
|
Hello, this is a test!
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
This part should be elided.
|
|
|
|
EMAIL
|
|
|
|
post "/admin/email/advanced-test.json", params: { email: email }
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
incoming = JSON.parse(response.body)
|
|
|
|
expect(incoming['format']).to eq(1)
|
|
|
|
expect(incoming['text']).to eq("Hello, this is a test!")
|
|
|
|
expect(incoming['elided']).to eq("---\n\nThis part should be elided.")
|
|
|
|
end
|
|
|
|
end
|
2013-06-03 16:12:24 -04:00
|
|
|
end
|