require 'spec_helper'

describe ApiAffiliateController, type: :controller do
  render_views

  let(:partner1) {FactoryGirl.create(:affiliate_partner)}
  let(:user_partner1) { partner1.partner_user }
  let(:partner2) {FactoryGirl.create(:affiliate_partner)}
  let(:user_partner2) { partner2.partner_user }

  before(:each) do
    controller.current_user = user_partner1
  end

  describe "traffic_index" do
    it "empty" do
      get :traffic_index
      response.should be_success
      JSON.parse(response.body)['traffics'].should eq([])
    end

    it "single item" do
      traffic_total = FactoryGirl.create(:affiliate_traffic_total, affiliate_partner: partner1, visits: 5, signups: 0, day:Date.today)

      get :traffic_index
      response.should be_success
      JSON.parse(response.body)['traffics'].should eq([{"day" =>  Date.today.to_s, "visits" => 5, "signups" => 0, "affiliate_partner_id" => partner1.id}])
    end
  end

  describe "monthly_index" do
    it "empty" do
      get :monthly_index
      response.should be_success
      JSON.parse(response.body)['monthlies'].should eq([])
    end

    it "single item" do
      monthly = FactoryGirl.create(:affiliate_monthly_payment, affiliate_partner: partner1, closed: true, due_amount_in_cents: 20, month: 1, year: 2015)

      get :monthly_index
      response.should be_success
      JSON.parse(response.body)['monthlies'].should eq([{"closed" => true, "month" =>  1, "year" => 2015, "due_amount_in_cents" => 20, "affiliate_partner_id" => partner1.id}])
    end
  end


  describe "quarterly_index" do
    it "empty" do
      get :quarterly_index
      response.should be_success
      JSON.parse(response.body)['quarterlies'].should eq([])
    end

    it "single item" do
      quarterly = FactoryGirl.create(:affiliate_quarterly_payment, affiliate_partner: partner1, paid: true, closed: true, due_amount_in_cents: 20, quarter: 1, year: 2015)

      get :quarterly_index
      response.should be_success
      JSON.parse(response.body)['quarterlies'].should eq([{"closed" => true, "paid" => true, "quarter" =>  1, "year" => 2015, "due_amount_in_cents" => 20, "affiliate_partner_id" => partner1.id}])
    end


    it "not paid is excluded" do
      quarterly = FactoryGirl.create(:affiliate_quarterly_payment, affiliate_partner: partner1, paid: false, closed: true, due_amount_in_cents: 20, quarter: 1, year: 2015)

      get :quarterly_index
      response.should be_success
      JSON.parse(response.body)['quarterlies'].should eq([])
    end


    it "not closed is excluded" do
      quarterly = FactoryGirl.create(:affiliate_quarterly_payment, affiliate_partner: partner1, paid: true, closed: false, due_amount_in_cents: 20, quarter: 1, year: 2015)

      get :quarterly_index
      response.should be_success
      JSON.parse(response.body)['quarterlies'].should eq([])
    end
  end

end
