require 'spec_helper'

describe DownloadTracker do

  let(:user1) {FactoryGirl.create(:user)}
  let(:user2) {FactoryGirl.create(:user)}
  let(:user3) {FactoryGirl.create(:user)}
  let(:jam_track) {FactoryGirl.create(:jam_track)}

  describe "check_user_sharer" do
    describe "with max 1" do
      it "and there is one row that is not paid for" do
        tracker1 = FactoryGirl.create(:download_tracker, user: user1, paid: false, remote_ip: '1.1.1.1')

        results = DownloadTracker.check_user_sharer(1)
        results.all.length.should eq 1
        results[0]['user_id'].should eql (user1.id)
      end
    end

    describe "with max 2" do

      it "and there is one row that is not paid for" do
        tracker1 = FactoryGirl.create(:download_tracker, user: user1, paid: false, remote_ip: '1.1.1.1')

        results = DownloadTracker.check_user_sharer(2)
        results.all.length.should eq 0

        # and then add that same user at different IP, and see that something shows up

        tracker1 = FactoryGirl.create(:download_tracker, user: user1, paid: false, remote_ip: '2.2.2.2')
        results = DownloadTracker.check_user_sharer(2)
        results.all.length.should eq 1
      end

      it "and there are two rows from different IP address, different jam tracks" do
        tracker1 = FactoryGirl.create(:download_tracker, user: user1, paid: false, remote_ip: '1.1.1.1')
        tracker1 = FactoryGirl.create(:download_tracker, user: user1, paid: false, remote_ip: '2.2.2.2')

        results = DownloadTracker.check_user_sharer(2)
        results.all.length.should eq 1
        results[0]['user_id'].should eql(user1.id)
        results[0]['count'].should eql 2

        # now add a second user with one of the previous IP addresses; shouldn't matter yet
        tracker1 = FactoryGirl.create(:download_tracker, user: user2, paid: false, remote_ip: '1.1.1.1')

        results = DownloadTracker.check_user_sharer(2)
        results.all.length.should eq 1
        results[0]['user_id'].should eql(user1.id)
        results[0]['count'].should eql 2
      end

      it "and there are two rows from same IP adresss, same jam track" do
        tracker1 = FactoryGirl.create(:download_tracker, user: user1, paid: false, remote_ip: '1.1.1.1', jam_track: jam_track)
        tracker1 = FactoryGirl.create(:download_tracker, user: user1, paid: false, remote_ip: '1.1.1.1', jam_track: jam_track)

        results = DownloadTracker.check_user_sharer(2)
        results.all.length.should eq 0

        tracker1 = FactoryGirl.create(:download_tracker, user: user2, paid: false, remote_ip: '1.1.1.1')

        results = DownloadTracker.check_user_sharer(2)
        results.all.length.should eq 0

        tracker1 = FactoryGirl.create(:download_tracker, user: user1, paid: false, remote_ip: '2.2.2.2')

        results = DownloadTracker.check_user_sharer(2)
        results.all.length.should eq 1
        results[0]['user_id'].should eql(user1.id)
        results[0]['count'].should eql 3
      end

      it "and there are two rows from same user one paid for, one not" do
        tracker1 = FactoryGirl.create(:download_tracker, user: user1, paid: true, remote_ip: '1.1.1.1')
        tracker1 = FactoryGirl.create(:download_tracker, user: user1, paid: false, remote_ip: '2.2.2.2')

        results = DownloadTracker.check_user_sharer(2)
        results.all.length.should eq 1
      end
    end
  end

  describe "check_freebie_snarfer" do

    describe "with max 1" do

      it "and there is one row that is not paid for" do
        tracker1 = FactoryGirl.create(:download_tracker, user: user1, paid: false, remote_ip: '1.1.1.1')

        results = DownloadTracker.check_freebie_snarfer(1)
        results.all.length.should eq 1
      end

      it "and there is one row that is paid for" do
        tracker1 = FactoryGirl.create(:download_tracker, user: user1, paid: true, remote_ip: '1.1.1.1')

        results = DownloadTracker.check_freebie_snarfer(1)
        results.all.length.should eq 0
      end
    end
    describe "with max 2" do

      it "and there is one row that is not paid for" do
        tracker1 = FactoryGirl.create(:download_tracker, user: user1, paid: false, remote_ip: '1.1.1.1')

        results = DownloadTracker.check_freebie_snarfer(2)
        results.all.length.should eq 0

        # and then add a second user at same IP, and see that something shows up

        tracker1 = FactoryGirl.create(:download_tracker, user: user2, paid: false, remote_ip: '1.1.1.1')
        results = DownloadTracker.check_freebie_snarfer(2)
        results.all.length.should eq 1
      end

      it "and there are two rows from same user, different jam tracks" do
        tracker1 = FactoryGirl.create(:download_tracker, user: user1, paid: false, remote_ip: '1.1.1.1')
        tracker1 = FactoryGirl.create(:download_tracker, user: user1, paid: false, remote_ip: '1.1.1.1')

        results = DownloadTracker.check_freebie_snarfer(2)
        results.all.length.should eq 1
        results[0]['remote_ip'].should eql('1.1.1.1')
        results[0]['count'].should eql 2

        tracker1 = FactoryGirl.create(:download_tracker, user: user2, paid: false, remote_ip: '2.2.2.2')

        results = DownloadTracker.check_freebie_snarfer(2)
        results.all.length.should eq 1
        results[0]['remote_ip'].should eql('1.1.1.1')
        results[0]['count'].should eql 2
      end

      it "and there are two rows from same user, same jam track" do
        tracker1 = FactoryGirl.create(:download_tracker, user: user1, paid: false, remote_ip: '1.1.1.1', jam_track: jam_track)
        tracker1 = FactoryGirl.create(:download_tracker, user: user1, paid: false, remote_ip: '1.1.1.1', jam_track: jam_track)

        results = DownloadTracker.check_freebie_snarfer(2)
        results.all.length.should eq 0

        tracker1 = FactoryGirl.create(:download_tracker, user: user1, paid: false, remote_ip: '2.2.2.2')

        results = DownloadTracker.check_freebie_snarfer(2)
        results.all.length.should eq 0
        tracker1 = FactoryGirl.create(:download_tracker, user: user1, paid: false, remote_ip: '1.1.1.1')

        results = DownloadTracker.check_freebie_snarfer(2)
        results.all.length.should eq 1
        results[0]['remote_ip'].should eql('1.1.1.1')
        results[0]['count'].should eql 3
      end

      it "and there are two rows from same user one paid for, one not" do
        tracker1 = FactoryGirl.create(:download_tracker, user: user1, paid: true, remote_ip: '1.1.1.1')
        tracker1 = FactoryGirl.create(:download_tracker, user: user1, paid: false, remote_ip: '1.1.1.1')

        results = DownloadTracker.check_freebie_snarfer(2)
        results.all.length.should eq 0
      end
    end
  end
end
