require 'spec_helper'

describe "Affiliate Reports", :type => :api do

  let!(:user) { FactoryGirl.create(:user) }
  let!(:partner) { 
    AffiliatePartner.create_with_web_params(user, {:partner_name => Faker::Company.name, entity_type:'Individual'})
  }

  it "valid score" do
    FactoryGirl.create(:user, :created_at => Time.now - 5.days, :affiliate_referral_id => partner.id)
    FactoryGirl.create(:user, :created_at => Time.now - 6.days, :affiliate_referral_id => partner.id)
    FactoryGirl.create(:user, :created_at => Time.now - 6.days, :affiliate_referral_id => partner.id)
    FactoryGirl.create(:user, :created_at => Time.now - 7.days, :affiliate_referral_id => partner.id)
    FactoryGirl.create(:user, :created_at => Time.now - 7.days, :affiliate_referral_id => partner.id)
    FactoryGirl.create(:user, :created_at => Time.now - 7.days, :affiliate_referral_id => partner.id)
    FactoryGirl.create(:user, :created_at => Time.now - 7.days, :affiliate_referral_id => partner.id)

    post('/api/auth_session.json', 
         { :email => user.email, :password => user.password }.to_json, 
         "CONTENT_TYPE" => 'application/json')
    last_response.status.should == 200
    expect(JSON.parse(last_response.body)).to eq({ "success" => true })

    get "/api/users/#{user.id}/affiliate"

    expect(last_response.status).to eq(200)
    json = JSON.parse(last_response.body)

    expect(json['total_count']).to eq(7)
    by_date = json['by_date']
    expect(by_date.count).to eq(3)
    expect(by_date.first.last).to eq(1)
    expect(by_date.last.last).to eq(4)
  end

end
