2015-10-11 17:41:23 +08:00
|
|
|
require 'rails_helper'
|
2013-06-04 04:12:24 +08:00
|
|
|
|
|
|
|
describe Admin::EmailController do
|
2018-06-11 12:50:08 +08:00
|
|
|
let(:admin) { Fabricate(:admin) }
|
2018-07-18 16:28:44 +08:00
|
|
|
let(:email_log) { Fabricate(:email_log) }
|
2018-06-11 12:50:08 +08:00
|
|
|
|
|
|
|
before do
|
|
|
|
sign_in(admin)
|
|
|
|
end
|
2013-06-04 04:12:24 +08:00
|
|
|
|
|
|
|
it "is a subclass of AdminController" do
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(Admin::EmailController < Admin::AdminController).to eq(true)
|
2013-06-04 04:12:24 +08:00
|
|
|
end
|
|
|
|
|
2018-06-11 12:50:08 +08:00
|
|
|
describe '#index' do
|
2013-06-04 04:12:24 +08:00
|
|
|
before do
|
2018-06-11 12:50:08 +08:00
|
|
|
Admin::EmailController.any_instance
|
2017-08-31 12:06:56 +08:00
|
|
|
.expects(:action_mailer_settings)
|
|
|
|
.returns(
|
|
|
|
username: 'username',
|
|
|
|
password: 'secret'
|
|
|
|
)
|
2013-06-04 04:12:24 +08:00
|
|
|
end
|
|
|
|
|
2013-06-12 07:00:13 +08:00
|
|
|
it 'does not include the password in the response' do
|
2018-06-11 12:50:08 +08:00
|
|
|
get "/admin/email.json"
|
2013-06-12 07:00:13 +08:00
|
|
|
mail_settings = JSON.parse(response.body)['settings']
|
|
|
|
|
|
|
|
expect(
|
|
|
|
mail_settings.select { |setting| setting['name'] == 'password' }
|
|
|
|
).to be_empty
|
|
|
|
end
|
2013-06-04 04:12:24 +08:00
|
|
|
end
|
|
|
|
|
2018-06-11 12:50:08 +08:00
|
|
|
describe '#sent' do
|
2018-07-18 16:28:44 +08:00
|
|
|
let(:post) { Fabricate(:post) }
|
|
|
|
let(:email_log) { Fabricate(:email_log, post: post) }
|
|
|
|
|
|
|
|
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 12:50:08 +08:00
|
|
|
get "/admin/email/sent.json"
|
2018-07-18 16:28:44 +08:00
|
|
|
|
2018-06-11 12:50:08 +08:00
|
|
|
expect(response.status).to eq(200)
|
2018-07-18 16:28:44 +08: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-15 07:50:08 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-11 12:50:08 +08:00
|
|
|
describe '#skipped' do
|
2018-07-24 12:55:43 +08:00
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
let!(:log1) { Fabricate(:skipped_email_log, user: user) }
|
|
|
|
let!(:log2) { Fabricate(:skipped_email_log) }
|
|
|
|
|
2018-06-11 12:50:08 +08:00
|
|
|
it "succeeds" do
|
|
|
|
get "/admin/email/skipped.json"
|
2018-07-24 12:55:43 +08:00
|
|
|
|
2018-06-11 12:50:08 +08:00
|
|
|
expect(response.status).to eq(200)
|
2018-07-24 12:55:43 +08: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-04 04:12:24 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-11 12:50:08 +08:00
|
|
|
describe '#test' do
|
2013-06-04 04:12:24 +08:00
|
|
|
it 'raises an error without the email parameter' do
|
2018-06-11 12:50:08 +08:00
|
|
|
post "/admin/email/test.json"
|
|
|
|
expect(response.status).to eq(400)
|
2013-06-04 04:12:24 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'with an email address' do
|
|
|
|
it 'enqueues a test email job' do
|
2018-06-11 12:50:08 +08:00
|
|
|
post "/admin/email/test.json", params: { email_address: 'eviltrout@test.domain' }
|
2018-07-24 12:55:43 +08:00
|
|
|
|
2018-06-11 12:50:08 +08:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(ActionMailer::Base.deliveries.map(&:to).flatten).to include('eviltrout@test.domain')
|
2013-06-04 04:12:24 +08:00
|
|
|
end
|
|
|
|
end
|
2018-08-30 05:14:16 +08:00
|
|
|
|
|
|
|
context 'with SiteSetting.disable_emails' do
|
|
|
|
let(:eviltrout) { Fabricate(:evil_trout) }
|
|
|
|
let(:admin) { Fabricate(:admin) }
|
|
|
|
|
|
|
|
it 'does not sends mail to anyone when setting is "yes"' do
|
|
|
|
SiteSetting.disable_emails = 'yes'
|
|
|
|
|
|
|
|
post "/admin/email/test.json", params: { email_address: admin.email }
|
|
|
|
|
|
|
|
incoming = JSON.parse(response.body)
|
|
|
|
expect(incoming['sent_test_email_message']).to eq(I18n.t("admin.email.sent_test_disabled"))
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sends mail to staff only when setting is "non-staff"' do
|
|
|
|
SiteSetting.disable_emails = 'non-staff'
|
|
|
|
|
|
|
|
post "/admin/email/test.json", params: { email_address: admin.email }
|
|
|
|
incoming = JSON.parse(response.body)
|
|
|
|
expect(incoming['sent_test_email_message']).to eq(I18n.t("admin.email.sent_test"))
|
|
|
|
|
|
|
|
post "/admin/email/test.json", params: { email_address: eviltrout.email }
|
|
|
|
incoming = JSON.parse(response.body)
|
|
|
|
expect(incoming['sent_test_email_message']).to eq(I18n.t("admin.email.sent_test_disabled_for_non_staff"))
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sends mail to everyone when setting is "no"' do
|
|
|
|
SiteSetting.disable_emails = 'no'
|
|
|
|
|
|
|
|
post "/admin/email/test.json", params: { email_address: eviltrout.email }
|
|
|
|
|
|
|
|
incoming = JSON.parse(response.body)
|
|
|
|
expect(incoming['sent_test_email_message']).to eq(I18n.t("admin.email.sent_test"))
|
|
|
|
end
|
|
|
|
end
|
2013-06-04 04:12:24 +08:00
|
|
|
end
|
|
|
|
|
2018-06-11 12:50:08 +08:00
|
|
|
describe '#preview_digest' do
|
2013-06-04 04:12:24 +08:00
|
|
|
it 'raises an error without the last_seen_at parameter' do
|
2018-06-11 12:50:08 +08:00
|
|
|
get "/admin/email/preview-digest.json"
|
|
|
|
expect(response.status).to eq(400)
|
2013-06-04 04:12:24 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "previews the digest" do
|
2018-06-11 12:50:08 +08:00
|
|
|
get "/admin/email/preview-digest.json", params: {
|
|
|
|
last_seen_at: 1.week.ago, username: admin.username
|
|
|
|
}
|
2018-06-07 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2013-06-04 04:12:24 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-11 12:50:08 +08:00
|
|
|
describe '#handle_mail' do
|
2017-04-24 12:06:28 +08:00
|
|
|
it 'should enqueue the right job' do
|
2018-06-11 12:50:08 +08:00
|
|
|
expect { post "/admin/email/handle_mail.json", params: { email: email('cc') } }
|
2017-04-24 12:06:28 +08:00
|
|
|
.to change { Jobs::ProcessEmail.jobs.count }.by(1)
|
2018-06-11 12:50:08 +08:00
|
|
|
expect(response.status).to eq(200)
|
2017-04-24 12:06:28 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-11 12:50:08 +08:00
|
|
|
describe '#rejected' do
|
2017-08-05 02:04:26 +08:00
|
|
|
it 'should provide a string for a blank error' do
|
|
|
|
Fabricate(:incoming_email, error: "")
|
2018-06-11 12:50:08 +08:00
|
|
|
get "/admin/email/rejected.json"
|
|
|
|
expect(response.status).to eq(200)
|
2017-08-05 02:04:26 +08:00
|
|
|
rejected = JSON.parse(response.body)
|
|
|
|
expect(rejected.first['error']).to eq(I18n.t("emails.incoming.unrecognized_error"))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-11 12:50:08 +08:00
|
|
|
describe '#incoming' do
|
2017-08-05 02:04:26 +08:00
|
|
|
it 'should provide a string for a blank error' do
|
|
|
|
incoming_email = Fabricate(:incoming_email, error: "")
|
2018-06-11 12:50:08 +08:00
|
|
|
get "/admin/email/incoming/#{incoming_email.id}.json"
|
|
|
|
expect(response.status).to eq(200)
|
2017-08-05 02:04:26 +08:00
|
|
|
incoming = JSON.parse(response.body)
|
|
|
|
expect(incoming['error']).to eq(I18n.t("emails.incoming.unrecognized_error"))
|
|
|
|
end
|
|
|
|
end
|
2013-06-04 04:12:24 +08:00
|
|
|
end
|