REFACTOR: admin email controller specs to requests (#5962)

This commit is contained in:
OsamaSayegh 2018-06-11 07:50:08 +03:00 committed by Guo Xiang Tan
parent c0776884dd
commit 4c8939d530
1 changed files with 58 additions and 66 deletions

View File

@ -1,26 +1,28 @@
require 'rails_helper' require 'rails_helper'
describe Admin::EmailController do describe Admin::EmailController do
let(:admin) { Fabricate(:admin) }
before do
sign_in(admin)
end
it "is a subclass of AdminController" do it "is a subclass of AdminController" do
expect(Admin::EmailController < Admin::AdminController).to eq(true) expect(Admin::EmailController < Admin::AdminController).to eq(true)
end end
let!(:user) { log_in(:admin) } describe '#index' do
context '.index' do
before do before do
subject Admin::EmailController.any_instance
.expects(:action_mailer_settings) .expects(:action_mailer_settings)
.returns( .returns(
username: 'username', username: 'username',
password: 'secret' password: 'secret'
) )
get :index, format: :json
end end
it 'does not include the password in the response' do it 'does not include the password in the response' do
get "/admin/email.json"
mail_settings = JSON.parse(response.body)['settings'] mail_settings = JSON.parse(response.body)['settings']
expect( expect(
@ -29,84 +31,74 @@ describe Admin::EmailController do
end end
end end
context '.sent' do describe '#sent' do
before do it "succeeds" do
get :sent, format: :json get "/admin/email/sent.json"
end
subject { response }
it { is_expected.to be_successful }
end
context '.skipped' do
before do
get :skipped, format: :json
end
subject { response }
it { is_expected.to be_successful }
end
context '.test' do
it 'raises an error without the email parameter' do
expect do
post :test, format: :json
end.to raise_error(ActionController::ParameterMissing)
end
context 'with an email address' do
it 'enqueues a test email job' do
job_mock = mock
Jobs::TestEmail.expects(:new).returns(job_mock)
job_mock.expects(:execute).with(to_address: 'eviltrout@test.domain')
post :test, params: { email_address: 'eviltrout@test.domain' }, format: :json
end
end
end
context '.preview_digest' do
it 'raises an error without the last_seen_at parameter' do
expect do
get :preview_digest, format: :json
end.to raise_error(ActionController::ParameterMissing)
end
it "previews the digest" do
get :preview_digest, params: {
last_seen_at: 1.week.ago, username: user.username
}, format: :json
expect(response.status).to eq(200) expect(response.status).to eq(200)
end end
end end
context '#handle_mail' do describe '#skipped' do
before do it "succeeds" do
log_in_user(Fabricate(:admin)) get "/admin/email/skipped.json"
end expect(response.status).to eq(200)
it 'should enqueue the right job' do
expect { post :handle_mail, params: { email: email('cc') }, format: :json }
.to change { Jobs::ProcessEmail.jobs.count }.by(1)
end end
end end
context '.rejected' do describe '#test' do
it 'raises an error without the email parameter' do
post "/admin/email/test.json"
expect(response.status).to eq(400)
end
context 'with an email address' do
it 'enqueues a test email job' do
post "/admin/email/test.json", params: { email_address: 'eviltrout@test.domain' }
expect(response.status).to eq(200)
expect(ActionMailer::Base.deliveries.map(&:to).flatten).to include('eviltrout@test.domain')
end
end
end
describe '#preview_digest' do
it 'raises an error without the last_seen_at parameter' do
get "/admin/email/preview-digest.json"
expect(response.status).to eq(400)
end
it "previews the digest" do
get "/admin/email/preview-digest.json", params: {
last_seen_at: 1.week.ago, username: admin.username
}
expect(response.status).to eq(200)
end
end
describe '#handle_mail' do
it 'should enqueue the right job' do
expect { post "/admin/email/handle_mail.json", params: { email: email('cc') } }
.to change { Jobs::ProcessEmail.jobs.count }.by(1)
expect(response.status).to eq(200)
end
end
describe '#rejected' do
it 'should provide a string for a blank error' do it 'should provide a string for a blank error' do
Fabricate(:incoming_email, error: "") Fabricate(:incoming_email, error: "")
get :rejected, format: :json get "/admin/email/rejected.json"
expect(response.status).to eq(200)
rejected = JSON.parse(response.body) rejected = JSON.parse(response.body)
expect(rejected.first['error']).to eq(I18n.t("emails.incoming.unrecognized_error")) expect(rejected.first['error']).to eq(I18n.t("emails.incoming.unrecognized_error"))
end end
end end
context '.incoming' do describe '#incoming' do
it 'should provide a string for a blank error' do it 'should provide a string for a blank error' do
incoming_email = Fabricate(:incoming_email, error: "") incoming_email = Fabricate(:incoming_email, error: "")
get :incoming, params: { id: incoming_email.id }, format: :json get "/admin/email/incoming/#{incoming_email.id}.json"
expect(response.status).to eq(200)
incoming = JSON.parse(response.body) incoming = JSON.parse(response.body)
expect(incoming['error']).to eq(I18n.t("emails.incoming.unrecognized_error")) expect(incoming['error']).to eq(I18n.t("emails.incoming.unrecognized_error"))
end end
end end
end end