# frozen_string_literal: true RSpec.describe SoapyCake::AdminAddedit do subject(:admin_addedit) { described_class.new } around { |example| Timecop.freeze(Time.utc(2015, 2, 17, 12), &example) } let(:affiliate_id) { 1 } let(:advertiser_id) { 15886 } let(:contact_id) { 8814 } let(:vertical_id) { 41 } let(:offer_id) { 8910 } let(:offer_contract_id) { 1456 } let(:redirect_offer_contract_id) { 1392 } let(:tier_id) { 4 } describe 'affiliates', :vcr do it 'edits affiliates' do result = admin_addedit.edit_affiliate( affiliate_id: 1, vat_tax_required: false ) expect(result).to include(affiliate_id: 1) end end describe 'advertisers', :vcr do it 'creates advertisers' do result = admin_addedit.create_advertiser( advertiser_name: 'Foxy Fox', account_status_id: 1 ) expect(result).to include(advertiser_id: 15908) end end describe 'contacts', :vcr do it 'edits a contact' do result = admin_addedit.edit_contact( entity_id: advertiser_id, contact_id: contact_id, contact_email_address: 'fox@rabbit.com' ) expect(result).to include( success: true, contact_id: contact_id ) end end describe 'offers' do let(:update_params) do { offer_id: offer_id, advertiser_id: advertiser_id, vertical_id: vertical_id, postback_url_ms_delay: 50, offer_contract_hidden: false, price_format_id: :cpa, received: 2.0, received_percentage: false, payout: 1.5, tags: '' } end it 'creates an offer', :vcr do result = admin_addedit.add_offer( hidden: false, offer_status_id: :public, offer_type_id: :third_party, currency_id: :eur, ssl: false, click_cookie_days: 30, impression_cookie_days: 30, redirect_404: 'off', enable_view_thru_conversions: 'off', click_trumps_impression: 'off', disable_click_deduplication: 'off', last_touch: 'off', enable_transaction_id_deduplication: 'off', postbacks_only: 'off', fire_global_pixel: 'on', fire_pixel_on_non_paid_conversions: 'off', offer_link: 'http://www.example.com/', thankyou_link: 'http://www.example.com/', from_lines: 'from', subject_lines: 'subject', conversion_cap_behavior: :ignore, conversion_behavior_on_redirect: :ignore, advertiser_id: advertiser_id, vertical_id: vertical_id, postback_url_ms_delay: 60, offer_contract_hidden: false, price_format_id: :cpa, received: 2.0, received_percentage: false, payout: 1.5, tags: %w[some tags], offer_name: 'Test Offer', offer_description: 'TEST1', restrictions: 'TEST2', advertiser_extended_terms: 'TEST3', testing_instructions: 'TEST4' ) %i[creative_id offer_contract_id offer_id].each do |key| expect(result[key]).to be_positive end end it 'updates an offer', :vcr do result = admin_addedit.edit_offer(update_params) expect(result).to include(offer_id: offer_id) end context 'errors' do it 'fails when not enough params are given' do expect do admin_addedit.edit_offer(offer_id: 123) end.to raise_error(SoapyCake::Error, "Parameter 'advertiser_id' missing!") end it 'fails when invalid offer_id is given on edit' do expect do admin_addedit.edit_offer(offer_id: -1) end.to raise_error(SoapyCake::Error, "Parameter 'offer_id' must be > 0!") end context 'when writes not enabled' do before do allow(ENV).to receive(:fetch).and_call_original allow(ENV).to receive(:fetch).with('CAKE_WRITE_ENABLED', nil).and_return('no') end it 'fails with error' do expect do admin_addedit.edit_offer(update_params) end.to raise_error(SoapyCake::Error, /Writes not enabled/) end end end end describe 'geo targeting' do it 'creates geo targetings', :vcr do result = admin_addedit.edit_geo_targets( offer_contract_id: offer_contract_id, countries: %w[DE FR], allow_countries: true ) expect(result).to include(success: true, row_count: '2') result = admin_addedit.edit_geo_targets( offer_contract_id: offer_contract_id, countries: %w[AT CH], redirects: { 'AT' => redirect_offer_contract_id, 'CH' => redirect_offer_contract_id }, allow_countries: false ) expect(result).to include(success: true, row_count: '2') end it 'fails if it does not get a correct redirect hash' do expect do admin_addedit.edit_geo_targets( offer_contract_id: offer_contract_id, redirects: {}, allow_countries: false ) end.to raise_error( SoapyCake::Error, "Parameter 'redirects' must be a COUNTRY=>REDIRECT_OFFER_CONTRACT_ID hash!" ) end end describe 'offer contracts' do it 'creates an offer contract', :vcr do result = admin_addedit.add_offer_contract( offer_id: offer_id, offer_contract_name: 'Test Contract', price_format_id: :cpa, received: 3.2, received_percentage: false, payout: 2.5, offer_link: 'http://oc.example.com', thankyou_link: 'http://oc.example.com/thanks', offer_contract_hidden: true, offer_contract_is_default: false, use_fallback_targeting: true ) expect(result).to include(offer_contract_id: 1456) end it 'updates an offer contract', :vcr do result = admin_addedit.edit_offer_contract( offer_id: offer_id, offer_contract_id: offer_contract_id, offer_contract_name: 'Test Contract', price_format_id: :cpa, received: 3.2, received_percentage: false, payout: 2.5, offer_link: 'http://oc.example.com', thankyou_link: 'http://oc.example.com/thanks', offer_contract_hidden: true, offer_contract_is_default: false, use_fallback_targeting: true ) expect(result).to include(offer_contract_id: 1456) end context 'errors' do it 'fails when not enough params are given' do expect do admin_addedit.edit_offer_contract(offer_contract_id: 123) end.to raise_error(SoapyCake::Error, "Parameter 'offer_id' missing!") end it 'fails when invalid offer_id is given on edit' do expect do admin_addedit.edit_offer_contract(offer_contract_id: -1) end.to raise_error(SoapyCake::Error, "Parameter 'offer_contract_id' must be > 0!") end end end describe 'offer / offer contract caps' do it 'updates a cap for an offer contract', :vcr do result = admin_addedit.update_caps( offer_contract_id: offer_contract_id, cap_type_id: :conversion, cap_interval_id: :daily, cap_amount: 42, send_alert_only: false ) expect(result[:message]).to eq('Cap Updated') end it 'removes a cap for an offer contract', :vcr do result = admin_addedit.remove_caps( offer_contract_id: offer_contract_id, cap_type_id: :conversion ) expect(result[:message]).to eq('Cap Updated') end end describe 'offer tiers' do it 'adds an offer tier', :vcr do result = admin_addedit.add_offer_tier( offer_id: offer_id, offer_contract_id: offer_contract_id, tier_id: tier_id, price_format_id: :cpa, status_id: :public ) expect(result[:message]).to eq('Offer Tier Added') end it 'edits an offer tier', :vcr do result = admin_addedit.edit_offer_tier( offer_id: offer_id, offer_contract_id: offer_contract_id, tier_id: tier_id, price_format_id: :cpa, status_id: :public ) expect(result[:message]).to eq('Offer Tier Replaced') end end describe '#create_creatives' do it 'creates a creative and adds a file to it', :vcr do result = admin_addedit.create_creative( offer_id: 11428, creative_name: 'creative_name', creative_file_import_url: 'http://placehold.it/300x250' ) expect(result).to eq( creative_files: { creative_file: { creative_file_id: 8015, creative_file_name: '300x250', updated: false } }, creative_id: 12164, success: true ) end end end