require 'spec_helper'

describe ApiScoringController, type: :controller do
  render_views

  BOGUS_CLIENT_ID = 'nobodyclientid'
  BOGUS_IP_ADDRESS = '1.2.3.4'
  BAD_IP_ADDRESS = 'a.b.c.d'

  MARY_IP_ADDRESS = '75.92.54.210' # 1264334546, 4B.5C.36.D2
  MARY_ADDR = 1264334546
  MARY_LOCIDISPID = 1807000004

  MIKE_IP_ADDRESS = '173.172.108.1' # 2913758209, AD.AC.6C.01
  MIKE_ADDR = 2913758209
  MIKE_LOCIDISPID = 1539000002

  JOHN_IP_ADDRESS = '255.255.1.2' # 4294902018, FF.FF.01.02
  JOHN_ADDR = 4294902018
  JOHN_LOCIDISPID = 98765432

  before do
    @mary = FactoryGirl.create(:user, first_name: 'mary')
    @mary_connection = FactoryGirl.create(:connection, user: @mary, ip_address: MARY_IP_ADDRESS, addr: MARY_ADDR, locidispid: MARY_LOCIDISPID)
    @mary_client_id = @mary_connection.client_id

    @mike = FactoryGirl.create(:user, first_name: 'mike')
    @mike_connection = FactoryGirl.create(:connection, user: @mike, ip_address: MIKE_IP_ADDRESS, addr: MIKE_ADDR, locidispid: MIKE_LOCIDISPID)
    @mike_client_id = @mike_connection.client_id

    @john = FactoryGirl.create(:user, first_name: 'john')
    @john_connection = FactoryGirl.create(:connection, user: @john, ip_address: JOHN_IP_ADDRESS, addr: JOHN_ADDR, locidispid: JOHN_LOCIDISPID)
    @john_client_id = @john_connection.client_id
  end

  after do
    @mary_connection.delete
    @mary.delete
    @mike_connection.delete
    @mike.delete
    @john_connection.delete
    @john.delete
  end

  before(:each) do
    #User.delete_all
    #Connection.delete_all
    Score.delete_all
    Score.connection.execute('delete from current_network_scores').check
  end

  describe 'work' do

    it 'try work with nobody login and mary' do
      controller.current_user = nil
      get :work, {clientid: @mary_client_id}
      response.should_not be_success
      json = JSON.parse(response.body, :symbolize_names => true)
      json.length.should == 1
      json[:message].should eql('not logged in')
    end

    it 'try work with mary login and nothing' do
      controller.current_user = @mary
      get :work, {clientid: ''}
      response.should_not be_success
      json = JSON.parse(response.body, :symbolize_names => true)
      json.length.should == 1
      json[:message].should eql('clientid not specified')
    end

    it 'try work with mary login and bogus' do
      controller.current_user = @mary
      get :work, {clientid: BOGUS_CLIENT_ID}
      response.should_not be_success
      json = JSON.parse(response.body, :symbolize_names => true)
      json.length.should == 1
      json[:message].should eql('session not found')
    end

    it 'try work with mary login and mary' do
      controller.current_user = @mary
      get :work, {clientid: @mary_client_id}
      response.should be_success
      json = JSON.parse(response.body, :symbolize_names => true)
      json.length.should == 1
      clientid = json[:clientid]
      clientid.should_not be_nil
      [@mike_client_id].should include(clientid)
    end

    it 'try work with mike login and mike' do
      controller.current_user = @mike
      get :work, {clientid: @mike_client_id}
      response.should be_success
      json = JSON.parse(response.body, :symbolize_names => true)
      json.length.should == 1
      clientid = json[:clientid]
      clientid.should_not be_nil
      [@mary_client_id].should include(clientid)
    end

    it 'try work with mary login and mike' do
      controller.current_user = @mary
      get :work, {clientid: @mike_client_id}
      response.should_not be_success
      json = JSON.parse(response.body, :symbolize_names => true)
      json.length.should == 1
      json[:message].should eql('session not found')
    end

    it 'try work with mike login and mary' do
      controller.current_user = @mike
      get :work, {clientid: @mary_client_id}
      response.should_not be_success
      json = JSON.parse(response.body, :symbolize_names => true)
      json.length.should == 1
      json[:message].should eql('session not found')
    end

  end

  describe 'worklist' do

    it 'try worklist with nobody login and nobody' do
      controller.current_user = nil
      get :worklist, {clientid: ''}
      response.should_not be_success
      json = JSON.parse(response.body, :symbolize_names => true)
      json.length.should == 1
      json[:message].should eql('not logged in')
    end

    it 'try worklist with mary login and nobody' do
      controller.current_user = @mary
      get :worklist, {clientid: ''}
      response.should_not be_success
      json = JSON.parse(response.body, :symbolize_names => true)
      json.length.should == 1
      json[:message].should eql('clientid not specified')
    end

    it 'try worklist with nobody login and mary' do
      controller.current_user = nil
      get :worklist, {clientid: @mary_client_id}
      response.should_not be_success
      json = JSON.parse(response.body, :symbolize_names => true)
      json.length.should == 1
      json[:message].should eql('not logged in')
    end

    it 'try worklist with mary login and mary' do
      controller.current_user = @mary
      get :worklist, {clientid: @mary_client_id}
      response.should be_success
      json = JSON.parse(response.body, :symbolize_names => true)
      json.length.should == 1
      clientids = json[:clientids]
      clientids.should_not be_nil
      clientids.should respond_to :length
      clientids.length.should == 1
      [@mike_client_id].should include(clientids[0])
    end

    it 'try worklist with mike login and mike' do
      controller.current_user = @mike
      get :worklist, {clientid: @mike_client_id}
      response.should be_success
      json = JSON.parse(response.body, :symbolize_names => true)
      json.length.should == 1
      clientids = json[:clientids]
      clientids.should_not be_nil
      clientids.should respond_to :length
      clientids.length.should == 1
      [@mary_client_id].should include(clientids[0])
    end

    it 'try worklist with mary login and mike' do
      controller.current_user = @mary
      get :worklist, {clientid: @mike_client_id}
      response.should_not be_success
      json = JSON.parse(response.body, :symbolize_names => true)
      json.length.should == 1
      json[:message].should eql('session not found')
    end

  end

  describe 'record' do

    it 'record with nobody login, mary, mary_ip_address, mike, mike_addr, score' do
      controller.current_user = nil
      post :record, {:format => 'json', :aclientid => @mary_client_id, :aAddr => MARY_IP_ADDRESS, :bclientid => @mike_client_id, :bAddr => MIKE_IP_ADDRESS, :score => 20}
      response.should_not be_success
      json = JSON.parse(response.body, :symbolize_names => true)
      json.length.should == 1
      json[:message].should eql('not logged in')
    end

    it 'record with mary login, nil, mary_addr, mike, mike_addr, score' do
      controller.current_user = @mary
      post :record, {:format => 'json', :aclientid => nil, :aAddr => MARY_IP_ADDRESS, :bclientid => @mike_client_id, :bAddr => MIKE_IP_ADDRESS, :score => 20}
      response.should_not be_success
      json = JSON.parse(response.body, :symbolize_names => true)
      json.length.should == 1
      json[:message].should eql('aclientid not specified')
    end

    it 'record with mary login, mary, nil, mike, mike_addr, score' do
      controller.current_user = @mary
      post :record, {:format => 'json', :aclientid => @mary_client_id, :aAddr => nil, :bclientid => @mike_client_id, :bAddr => MIKE_IP_ADDRESS, :score => 20}
      response.should_not be_success
      json = JSON.parse(response.body, :symbolize_names => true)
      json.length.should == 1
      json[:message].should eql('aAddr not specified')
    end

    it 'record with mary login, mary, mary_addr, nil, mike_addr, score' do
      controller.current_user = @mary
      post :record, {:format => 'json', :aclientid => @mary_client_id, :aAddr => MARY_IP_ADDRESS, :bclientid => nil, :bAddr => MIKE_IP_ADDRESS, :score => 20}
      response.should_not be_success
      json = JSON.parse(response.body, :symbolize_names => true)
      json.length.should == 1
      json[:message].should eql('bclientid not specified')
    end

    it 'record with mary login, mary, mary_addr, mike, nil, score' do
      controller.current_user = @mary
      post :record, {:format => 'json', :aclientid => @mary_client_id, :aAddr => MARY_IP_ADDRESS, :bclientid => @mike_client_id, :bAddr => nil, :score => 20}
      response.should_not be_success
      json = JSON.parse(response.body, :symbolize_names => true)
      json.length.should == 1
      json[:message].should eql('bAddr not specified')
    end

    it 'record with mary login, mary, mary_addr, mike, mike_addr, nil' do
      controller.current_user = @mary
      post :record, {:format => 'json', :aclientid => @mary_client_id, :aAddr => MARY_IP_ADDRESS, :bclientid => @mike_client_id, :bAddr => MIKE_IP_ADDRESS, :score => nil}
      response.should_not be_success
      json = JSON.parse(response.body, :symbolize_names => true)
      json.length.should == 1
      json[:message].should eql('score not specified or not numeric')
    end

    it 'record with mary login, bogus, mary_addr, mike, mike_addr, score' do
      controller.current_user = @mary
      post :record, {:format => 'json', :aclientid => BOGUS_CLIENT_ID, :aAddr => MARY_IP_ADDRESS, :bclientid => @mike_client_id, :bAddr => MIKE_IP_ADDRESS, :score => 20}
      response.should_not be_success
      json = JSON.parse(response.body, :symbolize_names => true)
      json.length.should == 1
      json[:message].should eql('a\'s session not found')
    end

    it 'record with mary login, mary, mary_addr, bogus, mike_addr, score' do
      controller.current_user = @mary
      post :record, {:format => 'json', :aclientid => @mary_client_id, :aAddr => MARY_IP_ADDRESS, :bclientid => BOGUS_CLIENT_ID, :bAddr => MIKE_IP_ADDRESS, :score => 20}
      response.should_not be_success
      json = JSON.parse(response.body, :symbolize_names => true)
      json.length.should == 1
      json[:message].should eql('b\'s session not found')
    end

    it 'record with mary login, mary, bogus, mike, mike_addr, score' do
      controller.current_user = @mary
      post :record, {:format => 'json', :aclientid => @mary_client_id, :aAddr => BAD_IP_ADDRESS, :bclientid => @mike_client_id, :bAddr => MIKE_IP_ADDRESS, :score => 20}
      response.should_not be_success
      json = JSON.parse(response.body, :symbolize_names => true)
      json.length.should == 1
      json[:message].should eql('aAddr not valid ip_address')
    end

    it 'record with mary login, mary, bogus, mike, mike_addr, score' do
      controller.current_user = @mary
      post :record, {:format => 'json', :aclientid => @mary_client_id, :aAddr => BOGUS_IP_ADDRESS, :bclientid => @mike_client_id, :bAddr => MIKE_IP_ADDRESS, :score => 20}
      response.should_not be_success
      json = JSON.parse(response.body, :symbolize_names => true)
      json.length.should == 1
      json[:message].should eql('a\'s session addr does not match aAddr')
    end

    it 'record with mary login, mary, mary_addr, mike, bogus, score' do
      controller.current_user = @mary
      post :record, {:format => 'json', :aclientid => @mary_client_id, :aAddr => MARY_IP_ADDRESS, :bclientid => @mike_client_id, :bAddr => BAD_IP_ADDRESS, :score => 20}
      response.should_not be_success
      json = JSON.parse(response.body, :symbolize_names => true)
      json.length.should == 1
      json[:message].should eql('bAddr not valid ip_address')
    end

    it 'record with mary login, mary, mary_addr, mike, bogus, score' do
      controller.current_user = @mary
      post :record, {:format => 'json', :aclientid => @mary_client_id, :aAddr => MARY_IP_ADDRESS, :bclientid => @mike_client_id, :bAddr => BOGUS_IP_ADDRESS, :score => 20}
      response.should_not be_success
      json = JSON.parse(response.body, :symbolize_names => true)
      json.length.should == 1
      json[:message].should eql('b\'s session addr does not match bAddr')
    end

    it 'record with mary login, mike, mike_addr, mary, mary_addr, score' do
      controller.current_user = @mary
      post :record, {:format => 'json', :aclientid => @mike_client_id, :aAddr => MIKE_IP_ADDRESS, :bclientid => @mary_client_id, :bAddr => MARY_IP_ADDRESS, :score => 20}
      response.should_not be_success
      json = JSON.parse(response.body, :symbolize_names => true)
      json.length.should == 1
      json[:message].should eql('a\'s session not found')
    end

    it 'record with mary login, mary, mary_addr, mary, mary_addr, score' do
      controller.current_user = @mary
      post :record, {:format => 'json', :aclientid => @mary_client_id, :aAddr => MARY_IP_ADDRESS, :bclientid => @mary_client_id, :bAddr => MARY_IP_ADDRESS, :score => 20}
      response.should_not be_success
      json = JSON.parse(response.body, :symbolize_names => true)
      json.length.should == 1
      json[:message].should eql('aclientid is same as bclientid')
    end

    it 'record with mary login, mary, mary_addr, mike, mary_addr, score' do
      controller.current_user = @mary
      post :record, {:format => 'json', :aclientid => @mary_client_id, :aAddr => MARY_IP_ADDRESS, :bclientid => @mike_client_id, :bAddr => MARY_IP_ADDRESS, :score => 20}
      response.should_not be_success
      json = JSON.parse(response.body, :symbolize_names => true)
      json.length.should == 1
      json[:message].should eql('aAddr and bAddr are the same (to=f)')
    end

    it 'record with mary login, mary, mary_addr, mike, mike_addr, -1' do
      controller.current_user = @mary
      post :record, {:format => 'json', :aclientid => @mary_client_id, :aAddr => MARY_IP_ADDRESS, :bclientid => @mike_client_id, :bAddr => MIKE_IP_ADDRESS, :score => -1}
      response.should_not be_success
      json = JSON.parse(response.body, :symbolize_names => true)
      json.length.should == 1
      json[:message].should eql('score < 0 or score > 999')
    end

    it 'record with mary login, mary, mary_addr, mike, mike_addr, 1000' do
      controller.current_user = @mary
      post :record, {:format => 'json', :aclientid => @mary_client_id, :aAddr => MARY_IP_ADDRESS, :bclientid => @mike_client_id, :bAddr => MIKE_IP_ADDRESS, :score => 1000}
      response.should_not be_success
      json = JSON.parse(response.body, :symbolize_names => true)
      json.length.should == 1
      json[:message].should eql('score < 0 or score > 999')
    end

    it 'record with mary login, mary, mary_addr, mike, mike_addr, bogus' do
      controller.current_user = @mary
      post :record, {:format => 'json', :aclientid => @mary_client_id, :aAddr => MARY_IP_ADDRESS, :bclientid => @mike_client_id, :bAddr => MIKE_IP_ADDRESS, :score => 'abc'}
      response.should_not be_success
      json = JSON.parse(response.body, :symbolize_names => true)
      json.length.should == 1
      json[:message].should eql('score not specified or not numeric')
    end

    it 'record with john login, john, john_addr, mike, mike_addr, bogus' do
      controller.current_user = @john
      post :record, {:format => 'json', :aclientid => @john_client_id, :aAddr => JOHN_IP_ADDRESS, :bclientid => @mike_client_id, :bAddr => MIKE_IP_ADDRESS, :score => 20}
      response.should_not be_success
      json = JSON.parse(response.body, :symbolize_names => true)
      json.length.should == 1
      json[:message].should eql('a\'s location or isp not found')
    end

    it 'record with mary login, mary, mary_addr, john, john_addr, bogus' do
      controller.current_user = @mary
      post :record, {:format => 'json', :aclientid => @mary_client_id, :aAddr => MARY_IP_ADDRESS, :bclientid => @john_client_id, :bAddr => JOHN_IP_ADDRESS, :score => 20}
      response.should_not be_success
      json = JSON.parse(response.body, :symbolize_names => true)
      json.length.should == 1
      json[:message].should eql('b\'s location or isp not found')
    end

    it 'record with mary login, mary, mary_addr, mike, mike_addr, score (integer)' do
      controller.current_user = @mary
      post :record, {:format => 'json', :aclientid => @mary_client_id, :aAddr => MARY_IP_ADDRESS, :bclientid => @mike_client_id, :bAddr => MIKE_IP_ADDRESS, :score => 20}
      response.should be_success
      json = JSON.parse(response.body, :symbolize_names => true)
      json.should == {message: 'OK (to=f)'}
      score = Score.findx(MARY_LOCIDISPID, MIKE_LOCIDISPID)
      score.should_not be_nil
      score.should eq(20)
      score = Score.where(:scorer => 0).first # there should only be 2 scores in the db; so grab the scorer = 0 one
      score.auserid.should == @mary.id
      score.aaddr.should == 1264334546
      score.anodeid.should == @mary_client_id
      score.alocidispid.should == MARY_LOCIDISPID
      score.alatencytestid.should be_nil
      score.buserid.should == @mike.id
      score.baddr.should == 2913758209
      score.bnodeid.should == @mike_client_id
      score.blocidispid.should == MIKE_LOCIDISPID
      score.blatencytestid.should be_nil
      score.score.should == 20
      score.score_dt.should_not be_nil
    end

    it 'record with mary login, mary, mary_addr, mike, mike_addr, score (floating pt)' do
      controller.current_user = @mary
      post :record, {:format => 'json', :aclientid => @mary_client_id, :aAddr => MARY_IP_ADDRESS, :bclientid => @mike_client_id, :bAddr => MIKE_IP_ADDRESS, :score => 21.234}
      response.should be_success
      json = JSON.parse(response.body, :symbolize_names => true)
      json.should == {message: 'OK (to=f)'}
      score = Score.findx(MARY_LOCIDISPID, MIKE_LOCIDISPID)
      score.should_not be_nil
      score.should eq(22)
    end

  end
end
