Add unit tests for Content#zencoder_callback

Create a factory for EncodedContents and refactor the action
Signed-off-by: marcheing's avatarHeitor Reis <marcheing@gmail.com>
parent e40c024d
...@@ -31,14 +31,18 @@ class ContentsController < ApplicationController ...@@ -31,14 +31,18 @@ class ContentsController < ApplicationController
def zencoder_callback def zencoder_callback
@encoded_content = EncodedContent.find_by(job_id: params[:job][:id]) @encoded_content = EncodedContent.find_by(job_id: params[:job][:id])
@encoded_content.output_id = params[:outputs].first[:id] unless @encoded_content.nil?
@encoded_content.state = params[:outputs].first[:state] @encoded_content.output_id = params[:outputs].first[:id]
@encoded_content.width = params[:outputs].first[:width] @encoded_content.state = params[:outputs].first[:state]
@encoded_content.height = params[:outputs].first[:height] @encoded_content.width = params[:outputs].first[:width]
@encoded_content.duration = params[:outputs].first[:duration_in_ms] @encoded_content.height = params[:outputs].first[:height]
@encoded_content.file_size = params[:outputs].first[:file_size_in_bytes] @encoded_content.duration = params[:outputs].first[:duration_in_ms]
@encoded_content.video = open(params[:outputs].first[:url]) @encoded_content.file_size = params[:outputs].first[:file_size_in_bytes]
@encoded_content.save @encoded_content.video = open(params[:outputs].first[:url])
@encoded_content.save
end
head :no_content
end end
private private
......
...@@ -83,4 +83,30 @@ RSpec.describe ContentsController, type: :controller do ...@@ -83,4 +83,30 @@ RSpec.describe ContentsController, type: :controller do
it { is_expected.to respond_with(:ok) } it { is_expected.to respond_with(:ok) }
end end
end end
describe 'POST #zencoder_callback' do
let!(:job_id) { '1' }
let!(:outputs) { [{id: '2', state: 'finished', width: '640', height: '320', duration_in_ms: '5000',
file_size_in_bytes: '1024', url: 'http://test.com'}] }
context 'when the content is found' do
let(:encoded_video) { build(:encoded_video) }
before do
expect(EncodedContent).to receive(:find_by).with(job_id: job_id).and_return(encoded_video)
expect(subject).to receive(:open).with(outputs.first[:url])
expect(encoded_video).to receive(:save).and_return(true)
post :zencoder_callback, params: { job: { id: job_id}, outputs: outputs }
end
it { is_expected.to respond_with(:no_content) }
end
context 'when the content is not found' do
before do
expect(EncodedContent).to receive(:find_by).with(job_id: job_id).and_return(nil)
post :zencoder_callback, params: { job: { id: job_id}, outputs: outputs }
end
it { is_expected.to respond_with(:no_content) }
end
end
end end
FactoryGirl.define do
factory :encoded_video, class: EncodedContent do
output_id 1
state 'finished'
width 640
height 320
duration 5000
file_size 1024
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment