require 'spec_helper' describe Campaign, 'associations' do let(:campaign){ attributes_for :campaign } it {should have_many(:videolistings) } it 'disallows duplicate listingcode' do one = Campaign.new(campaign) one.save two = Campaign.new(campaign) two.listingcode = one.listingcode expect(two.valid?).to be false end it 'shows how complete the videos are by percentage' do camp = Campaign.new(campaign) video_1 = FactoryGirl.create(:videolisting) video_2 = FactoryGirl.create(:videolisting) expect(camp.video_completion_percent).to eq(0) # no associations camp.videolistings << video_1 camp.videolistings << video_2 camp.save expect(camp.video_completion_percent).to eq(0) camp.videolistings.first.published = true camp.videolistings.first.save expect(camp.video_completion_percent).to eq(50) camp.videolistings.last.published = true camp.videolistings.last.save expect(camp.video_completion_percent).to eq(100) end it 'gets the proper better video format' do campaign = FactoryGirl.build(:campaign) campaign.save bv_fmt = campaign.to_bettervideo_format expect(JSON.parse(bv_fmt)['key']).to eq("PRO1234") expect(JSON.parse(bv_fmt)['campaigns'][0]["clientid"]).to eq(720) expect(JSON.parse(bv_fmt)['campaigns'][0]["videolistings"].length).to eq(1) end it 'sends to better video and returns success' do campaign = FactoryGirl.build(:campaign) campaign.save client = BetterVideo.new bv_response = client.addVideo(campaign: campaign.to_bettervideo_format) expect(bv_response[:success]).to eq(true) end it 'sends to better video and returns missing field' do campaign = FactoryGirl.build(:campaign) campaign.listingcode = "videolistingmissing" campaign.save client = BetterVideo.new bv_response = client.addVideo(campaign: campaign.to_bettervideo_format) expect(bv_response["errormessage"]).to eq("Required field [voiceoverselection] missing.") end it 'sends to better video and returns error' do campaign = FactoryGirl.build(:campaign) campaign.listingcode = "contactdev" campaign.save client = BetterVideo.new bv_response = client.addVideo(campaign: campaign.to_bettervideo_format) expect(bv_response[:message]).to include("Please Contact Dev") end end