Clean up api/salmon controller (#3449)

This commit is contained in:
Matt Jankowski 2017-05-30 16:28:58 -04:00 committed by GitHub
parent 22cf18e16f
commit 1dcfb90202
2 changed files with 45 additions and 26 deletions

View File

@ -5,10 +5,8 @@ class Api::SalmonController < ApiController
respond_to :txt respond_to :txt
def update def update
payload = request.body.read if verify_payload?
process_salmon
if !payload.nil? && verify?(payload)
SalmonWorker.perform_async(@account.id, payload.force_encoding('UTF-8'))
head 201 head 201
else else
head 202 head 202
@ -21,7 +19,15 @@ class Api::SalmonController < ApiController
@account = Account.find(params[:id]) @account = Account.find(params[:id])
end end
def verify?(payload) def payload
VerifySalmonService.new.call(payload) @_payload ||= request.body.read
end
def verify_payload?
payload.present? && VerifySalmonService.new.call(payload)
end
def process_salmon
SalmonWorker.perform_async(@account.id, payload.force_encoding('UTF-8'))
end end
end end

View File

@ -13,6 +13,7 @@ RSpec.describe Api::SalmonController, type: :controller do
end end
describe 'POST #update' do describe 'POST #update' do
context 'with valid post data' do
before do before do
request.env['RAW_POST_DATA'] = File.read(File.join(Rails.root, 'spec', 'fixtures', 'salmon', 'mention.xml')) request.env['RAW_POST_DATA'] = File.read(File.join(Rails.root, 'spec', 'fixtures', 'salmon', 'mention.xml'))
post :update, params: { id: account.id } post :update, params: { id: account.id }
@ -38,4 +39,16 @@ RSpec.describe Api::SalmonController, type: :controller do
expect(account.mentions.count).to eq 1 expect(account.mentions.count).to eq 1
end end
end end
context 'with invalid post data' do
before do
request.env['RAW_POST_DATA'] = ''
post :update, params: { id: account.id }
end
it 'returns http success' do
expect(response).to have_http_status(202)
end
end
end
end end