contents_controller_spec.rb 5.39 KB
Newer Older
marcheing's avatar
marcheing committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
require 'rails_helper'

# This spec was generated by rspec-rails when you ran the scaffold generator.
# It demonstrates how one might use RSpec to specify the controller code that
# was generated by Rails when you ran the scaffold generator.
#
# It assumes that the implementation code is generated by the rails scaffold
# generator.  If you are using any extension libraries to generate different
# controller code, this generated spec may or may not pass.
#
# It only uses APIs available in rails and/or rspec-rails.  There are a number
# of tools you can use to make these specs even more expressive, but we're
# sticking to rails and rspec-rails APIs to keep things simple and stable.
#
# Compared to earlier versions of this generator, there is very limited use of
# stubs and message expectations in this spec.  Stubs are only used when there
# is no simpler way to get a handle on the object needed for the example.
# Message expectations are only used when there is no simpler way to specify
# that an instance is receiving a specific message.

RSpec.describe ContentsController, type: :controller do
22
  include Devise::TestHelpers
marcheing's avatar
marcheing committed
23

24 25
  let(:valid_request_parameters) {
    attributes_for(:video)
marcheing's avatar
marcheing committed
26 27
  }

28 29 30 31 32
  let(:user) { create(:user, confirmed_at: Time.zone.now) }

  before do
    sign_in user
  end
marcheing's avatar
marcheing committed
33

34
  describe 'GET #show' do
35
    let(:video) { build(:video, :with_file_name, id: 1) }
36 37
    let(:encoded_video) { build(:encoded_video) }

38 39 40
    context 'video found' do
      before do
        expect(Content).to receive(:find).with(video.id.to_s).and_return(video)
41 42 43 44 45 46 47 48 49
        expect(video).to receive(:encoded_contents).and_return [encoded_video]
      end

      context 'encoded video finished' do
        before do
          get :show, params: { id: video.id }
        end

        it { is_expected.to respond_with(:ok) }
50
      end
marcheing's avatar
marcheing committed
51

52 53 54 55 56 57 58 59
      context 'encoded video not finished' do
        before do
          expect(encoded_video).to receive(:state).and_return nil
          get :show, params: { id: video.id }
        end

        it { is_expected.to respond_with(:redirect) }
      end
marcheing's avatar
marcheing committed
60 61
    end

62 63 64 65 66 67 68 69 70
    context 'video not found' do
      before do
        expect(Content).to receive(:find).with(video.id.to_s).and_raise ActiveRecord::RecordNotFound
      end
      it 'responds with not found' do
        pending 'Handle not found errors'
        get :show, params: { id: video.id }
        it { is_expected.to respond_with(:not_found) }
      end
marcheing's avatar
marcheing committed
71 72 73
    end
  end

74
  describe 'GET #new' do
75 76 77 78
    before do
      expect_any_instance_of(User).to receive(:profile).and_return build :profile
    end

79 80 81 82 83
    it 'responds with ok' do
      get :new
      expect(response.status).to eq 200
    end
  end
marcheing's avatar
marcheing committed
84

85
  describe 'POST #create' do
86
    let(:video) { build(:video, with: :with_file_name) }
87 88 89 90 91

    before do
      expect_any_instance_of(User).to receive(:profile).and_return build :profile
    end

92 93 94 95
    context 'with valid params' do
      before do
        expect_any_instance_of(Content).to receive(:save).and_return true
        post :create, params: { content: valid_request_parameters }
marcheing's avatar
marcheing committed
96 97
      end

98 99
      it 'redirects to the home page' do
        expect(response).to redirect_to(root_path)
marcheing's avatar
marcheing committed
100 101 102
      end
    end

103 104 105 106
    context 'with invalid params' do
      before do
        expect_any_instance_of(Content).to receive(:save).and_return false
        post :create, params: { content: valid_request_parameters }
marcheing's avatar
marcheing committed
107 108
      end

109
      it { is_expected.to respond_with(:ok) }
marcheing's avatar
marcheing committed
110 111
    end
  end
112 113 114

  describe 'POST #zencoder_callback' do
    let!(:job_id) { '1' }
115
    let!(:thumb_output) { [ images: [ url: 'http://test.com' ] ] }
116
    let!(:output) { { id: '2', state: 'finished', width: '640', height: '320', duration_in_ms: '5000',
117
                      file_size_in_bytes: '1024', url: 'http://test.com', thumbnails: thumb_output } }
118 119
    let!(:outputs) { [output] }
    let!(:video_file_mock) { instance_double('video_file') }
120
    let!(:thumb_file_mock) { instance_double('thumb_file') }
121

122 123 124 125
    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)
126 127
        expect(URI).to receive(:parse).with(outputs.first[:url]).and_return(video_file_mock)
        expect(URI).to receive(:parse).with(thumb_output.first[:images].first[:url]).and_return(thumb_file_mock)
128
        expect(encoded_video).to receive(:save).and_return(true)
129 130 131 132 133 134 135
        expect(encoded_video).to receive(:output_id=).with(output[:id])
        expect(encoded_video).to receive(:state=).with(output[:state])
        expect(encoded_video).to receive(:width=).with(output[:width])
        expect(encoded_video).to receive(:height=).with(output[:height])
        expect(encoded_video).to receive(:duration=).with(output[:duration_in_ms])
        expect(encoded_video).to receive(:file_size=).with(output[:file_size_in_bytes])
        expect(encoded_video).to receive(:video=).with(video_file_mock)
136
        expect(encoded_video).to receive(:thumbnail=).with(thumb_file_mock)
137

138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
        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
marcheing's avatar
marcheing committed
153
end