/
poll_zencoder_job_spec.rb
151 lines (98 loc) · 5.07 KB
/
poll_zencoder_job_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
require 'spec_helper'
module Pageflow
describe PollZencoderJob do
let(:zencoder_options) do
Pageflow.config.zencoder_options
end
it 'passes job id of file to zencoder api' do
video_file = build(:video_file, :job_id => 43)
allow(ZencoderApi).to receive(:instance).and_return(ZencoderApiDouble.pending)
result = PollZencoderJob.perform_with_result(video_file, {})
expect(ZencoderApi.instance).to have_received(:get_info).with(43)
end
it 'returns pending when zencoder has not finished' do
video_file = build(:video_file)
allow(ZencoderApi).to receive(:instance).and_return(ZencoderApiDouble.pending)
result = PollZencoderJob.perform_with_result(video_file, {})
expect(result).to eq(:pending)
end
it 'assigns zencoder progress' do
video_file = build(:video_file)
allow(ZencoderApi).to receive(:instance).and_return(ZencoderApiDouble.pending(:progress => 30))
result = PollZencoderJob.perform_with_result(video_file, {})
expect(video_file.encoding_progress).to eq(30)
end
it 'returns pending if zencoder api raises recoverable error' do
video_file = build(:video_file)
allow(ZencoderApi).to receive(:instance).and_return(ZencoderApiDouble.recoverably_failing)
result = PollZencoderJob.perform_with_result(video_file, {})
expect(result).to eq(:pending)
end
it 'records error message if zencoder api raises recoverable error' do
video_file = create(:video_file)
allow(ZencoderApi).to receive(:instance).and_return(ZencoderApiDouble.recoverably_failing)
PollZencoderJob.perform_with_result(video_file, {})
expect(video_file.reload.encoding_error_message).to be_present
end
it 'records error message if zencoder api raises unrecoverable error' do
video_file = create(:video_file)
allow(ZencoderApi).to receive(:instance).and_return(ZencoderApiDouble.unrecoverably_failing)
PollZencoderJob.perform_with_result(video_file, {}) rescue :ignore
expect(video_file.reload.encoding_error_message).to be_present
end
it 'returns pending if thumbnail is not there yet' do
video_file = build(:video_file)
allow(ZencoderApi).to receive(:instance).and_return(ZencoderApiDouble.finished)
stub_request(:get, /.*amazonaws\.com/).to_return(:status => 404)
result = PollZencoderJob.perform_with_result(video_file, {})
expect(result).to eq(:pending)
end
it 'returns ok if thumbnail is not there yet but skip_thumbnail option is set' do
video_file = build(:video_file)
allow(ZencoderApi).to receive(:instance).and_return(ZencoderApiDouble.finished)
stub_request(:get, /.*amazonaws\.com/).to_return(:status => 404)
result = PollZencoderJob.perform_with_result(video_file, {:skip_thumbnail => true})
expect(result).to eq(:ok)
end
it 'returns ok if thumbnail can be downloaded' do
video_file = build(:video_file)
allow(ZencoderApi).to receive(:instance).and_return(ZencoderApiDouble.finished)
stub_request(:any, /#{zencoder_options[:s3_host_alias]}/)
.to_return(:status => 200, :body => File.read('spec/fixtures/image.jpg'))
result = PollZencoderJob.perform_with_result(video_file, {})
expect(result).to eq(:ok)
end
it 'returns ok if encoding is finished and file does not respond to thumbnail' do
audio_file = build(:audio_file)
allow(ZencoderApi).to receive(:instance).and_return(ZencoderApiDouble.finished)
result = PollZencoderJob.perform_with_result(audio_file, {})
expect(result).to eq(:ok)
end
it 'returns error if encoding is failed and file does not respond to thumbnail' do
audio_file = build(:audio_file)
allow(ZencoderApi).to receive(:instance).and_return(ZencoderApiDouble.finished_but_failed)
result = PollZencoderJob.perform_with_result(audio_file, {})
expect(result).to eq(:error)
end
it 'passes job id of file to get_details method of zencoder api' do
video_file = build(:video_file, :job_id => 43)
allow(ZencoderApi).to receive(:instance).and_return(ZencoderApiDouble.finished)
stub_request(:get, /#{zencoder_options[:s3_host_alias]}/)
.to_return(:status => 200, :body => File.read('spec/fixtures/image.jpg'))
result = PollZencoderJob.perform_with_result(video_file, {})
expect(ZencoderApi.instance).to have_received(:get_details).with(43)
end
it 'assigns meta data' do
video_file = build(:video_file, :job_id => 43)
zencoder_api = ZencoderApiDouble.finished
meta_data = {}
allow(ZencoderApi).to receive(:instance).and_return(zencoder_api)
allow(zencoder_api).to receive(:get_details).and_return(meta_data)
stub_request(:get, /#{zencoder_options[:s3_host_alias]}/)
.to_return(:status => 200, :body => File.read('spec/fixtures/image.jpg'))
allow(video_file).to receive(:meta_data_attributes=)
result = PollZencoderJob.perform_with_result(video_file, {})
expect(video_file).to have_received(:meta_data_attributes=).with(meta_data)
end
end
end