From d12e65549de5c7823ec19fdbf08d17d4af0b5ee3 Mon Sep 17 00:00:00 2001 From: Scott Comer Date: Tue, 25 Feb 2014 23:01:55 -0600 Subject: [PATCH] wire up api/scoring/record --- ruby/lib/jam_ruby/models/score.rb | 2 + web/app/controllers/api_scoring_controller.rb | 79 ++++- .../api_scoring_controller_spec.rb | 294 ++++++++++++++++-- 3 files changed, 332 insertions(+), 43 deletions(-) diff --git a/ruby/lib/jam_ruby/models/score.rb b/ruby/lib/jam_ruby/models/score.rb index 7bf736971..570d409f4 100644 --- a/ruby/lib/jam_ruby/models/score.rb +++ b/ruby/lib/jam_ruby/models/score.rb @@ -5,6 +5,8 @@ module JamRuby self.table_name = 'scores' + attr_accessible :alocidispid, :anodeid, :aaddr, :blocidispid, :bnodeid, :baddr, :score, :score_dt, :scorer + default_scope order('score_dt desc') def self.createx(alocidispid, anodeid, aaddr, blocidispid, bnodeid, baddr, score, score_dt) diff --git a/web/app/controllers/api_scoring_controller.rb b/web/app/controllers/api_scoring_controller.rb index fb56de1a9..483a9a7a0 100644 --- a/web/app/controllers/api_scoring_controller.rb +++ b/web/app/controllers/api_scoring_controller.rb @@ -1,24 +1,83 @@ class ApiScoringController < ApiController respond_to :json - # todo before_filter :api_signed_in_user + before_filter :api_signed_in_user + + def work # clientid; returns another clientid + client_id = params[:clientid] + if client_id.nil? then render :json => {message: 'client_id not specified'}, :status => 400; return end + + c = Connection.where(client_id: client_id).first + if c.nil? then render :json => {message: 'connection not found'}, :status => 404; return end + if !c.user.id.eql?(current_user.id) then render :json => {message: 'user does not own client_id'}, :status => 403; return end - def work # clientid returns another clientid - # todo clientid should come from the connection record of the signed in user # todo this method is a stub - render :json => {:clientid => [params[:clientid]+'peer']}, :status => 200 + result_client_id = client_id+'peer' + + render :json => {:clientid => result_client_id}, :status => 200 end - def worklist # clientid returns a list of clientids - # todo clientid should come from the connection record of the signed in user + def worklist # clientid; returns a list of clientid + client_id = params[:clientid] + if client_id.nil? then render :json => {message: 'client_id not specified'}, :status => 400; return end + + c = Connection.where(client_id: client_id).first + if c.nil? then render :json => {message: 'connection not found'}, :status => 404; return end + if !c.user.id.eql?(current_user.id) then render :json => {message: 'user does not own client_id'}, :status => 403; return end + # todo this method is a stub - render :json => {:clientids => [params[:clientid]+'1_peer', params[:clientid]+'2_peer']}, :status => 200 + result_client_ids = [client_id+'peer1', client_id+'peer2'] + + render :json => {:clientids => result_client_ids}, :status => 200 end def record # aclientid, aAddr, bclientid, bAddr, score returns nothing - # todo aclientid, aAddr should come from the connection record of the signed in user - # todo this method is a stub + + aclient_id = params[:aclientid] + aip_address = params[:aAddr] + bclient_id = params[:bclientid] + bip_address = params[:bAddr] + score = params[:score] + + if aclient_id.nil? then render :json => {message: 'aclient_id not specified'}, :status => 400; return end + if aip_address.nil? then render :json => {message: 'aAddr not specified'}, :status => 400; return end + if bclient_id.nil? then render :json => {message: 'bclient_id not specified'}, :status => 400; return end + if bip_address.nil? then render :json => {message: 'bAddr not specified'}, :status => 400; return end + if score.nil? then render :json => {message: 'score not specified'}, :status => 400; return end + + aaddr = JamRuby::JamIsp.ip_to_num(aip_address) + if aaddr.nil? then render :json => {message: 'aAddr not valid ip_address'}, :status => 400; return end + + baddr = JamRuby::JamIsp.ip_to_num(bip_address) + if baddr.nil? then render :json => {message: 'bAddr not valid ip_address'}, :status => 400; return end + + if aaddr == baddr then render :json => {message: 'aAddr and bAddr are the same'}, :status => 403; return end + + if !score.is_a? Numeric then render :json => {message: 'score not valid numeric'}, :status => 400; return end + + aconn = Connection.where(client_id: aclient_id).first + if aconn.nil? then render :json => {message: 'a\'s session not found'}, :status => 404; return end + if aaddr != aconn.addr then render :json => {message: 'a\'s session addr does not match aAddr'}, :status => 403; return end + if !current_user.id.eql?(aconn.user.id) then render :json => {message: 'a\' session not owned by user'}, :status => 403; return end + + bconn = Connection.where(client_id: bclient_id).first + if bconn.nil? then render :json => {message: 'b\'s session not found'}, :status => 404; return end + if baddr != bconn.addr then render :json => {message: 'b\'s session addr does not match bAddr'}, :status => 403; return end + + if score < 0 or score > 999 then render :json => {message: 'score < 0 or score > 999'}, :status => 403; return end + + aloc = JamRuby::GeoIpBlocks.lookup(aaddr) + aisp = JamRuby::JamIsp.lookup(aaddr) + if aisp.nil? or aloc.nil? then render :json => {message: 'a\'s location or isp not found'}, :status => 404; return end + alocidispid = aloc.locid*1000000+aisp.coid; + + bloc = JamRuby::GeoIpBlocks.lookup(baddr) + bisp = JamRuby::JamIsp.lookup(baddr) + blocidispid = bloc.locid*1000000+bisp.coid + + JamRuby::Score.createx(alocidispid, aclient_id, aaddr, blocidispid, bclient_id, baddr, score, nil) + render :json => {}, :status => 200 end -end \ No newline at end of file +end diff --git a/web/spec/controllers/api_scoring_controller_spec.rb b/web/spec/controllers/api_scoring_controller_spec.rb index 96b67905a..c285ea54b 100644 --- a/web/spec/controllers/api_scoring_controller_spec.rb +++ b/web/spec/controllers/api_scoring_controller_spec.rb @@ -3,74 +3,302 @@ require 'spec_helper' describe ApiScoringController do render_views - let(:user) { FactoryGirl.create(:user) } + BOGUS_CLIENT_ID = 'nobodyclientid' + BOGUS_IP_ADDRESS = '0.0.0.0' + + MARY_IP_ADDRESS = '75.92.54.210' # 1264334546, 4B.5C.36.D2 + MARY_ADDR = 1264334546 + + MIKE_IP_ADDRESS = '173.172.108.1' # 2913758209, AD.AC.6C.01 + MIKE_ADDR = 2913758209 + + MARY_LOCIDISPID = 17192008423 + MIKE_LOCIDISPID = 17192043640 + + 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 + end + + after do + @mary_connection.delete + @mary.delete + @mike_connection.delete + @mike.delete + end before(:each) do - # nothing + #User.delete_all + #Connection.delete_all + Score.delete_all end describe 'work' do - it 'try with abc' do - # todo this should be using logged in user instead of passing clientid - get :work, {:clientid => 'abc'} - response.should be_success + + it 'try work with nobody and nobody' do + controller.current_user = nil + get :work, {} + response.should_not be_success json = JSON.parse(response.body, :symbolize_names => true) json.length.should == 1 - json[:clientid].should_not be_nil - json[:clientid].should_receive :length - json[:clientid].length == 1 - json[:clientid][0].should eql('abcpeer') + json[:message].should_not be_nil end - it 'try with def' do - # todo this should be using logged in user instead of passing clientid - get :work, {:clientid => 'def'} + it 'try work with mary and nobody' do + controller.current_user = @mary + get :work, {} + response.should_not be_success + json = JSON.parse(response.body, :symbolize_names => true) + json.length.should == 1 + json[:message].should_not be_nil + end + + it 'try work with nobody 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_not be_nil + end + + it 'try work with mary 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 + json[:clientid].should_not be_nil + json[:clientid].should eql(@mary_client_id+'peer') + end + + it 'try work with mike 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 json[:clientid].should_not be_nil - json[:clientid].should_receive :length - json[:clientid].length == 1 - json[:clientid][0].should eql('defpeer') + json[:clientid].should eql(@mike_client_id+'peer') end + + it 'try work with mike 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_not be_nil + end + end describe 'worklist' do - it 'try with abc' do - # todo this should be using logged in user instead of passing clientid - get :worklist, {:clientid => 'abc'} - response.should be_success + + it 'try worklist with nobody and nobody' do + controller.current_user = nil + get :worklist, {} + response.should_not be_success json = JSON.parse(response.body, :symbolize_names => true) json.length.should == 1 - json[:clientids].should_not be_nil - json[:clientids].should_receive :length - json[:clientids].length == 2 - json[:clientids][0].should eql('abc1_peer') - json[:clientids][1].should eql('abc2_peer') + json[:message].should_not be_nil end - it 'try with def' do - # todo this should be using logged in user instead of passing clientid - get :worklist, {:clientid => 'def'} + it 'try worklist with mary and nobody' do + controller.current_user = @mary + get :worklist, {} + response.should_not be_success + json = JSON.parse(response.body, :symbolize_names => true) + json.length.should == 1 + json[:message].should_not be_nil + end + + it 'try worklist with nobody 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_not be_nil + end + + it 'try worklist with mary 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 json[:clientids].should_not be_nil json[:clientids].should_receive :length json[:clientids].length == 2 - json[:clientids][0].should eql('def1_peer') - json[:clientids][1].should eql('def2_peer') + json[:clientids][0].should eql(@mary_client_id+'peer1') + json[:clientids][1].should eql(@mary_client_id+'peer2') end + + it 'try worklist with mike 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 + json[:clientids].should_not be_nil + json[:clientids].should_receive :length + json[:clientids].length == 2 + json[:clientids][0].should eql(@mike_client_id+'peer1') + json[:clientids][1].should eql(@mike_client_id+'peer2') + end + + it 'try worklist with mary 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_not be_nil + end + end describe 'record' do - it 'try with abc, def' do - # todo this should be using logged in user instead of passing aclientid, aAddr - post :record, {:format => 'json', :aclientid => 'abc', :aAddr => 0x04030201, :bclientid => 'def', :bAddr => 0x05040302, :score => 20} + + it 'record with no 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_not be_nil + 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_not be_nil + 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_not be_nil + 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_not be_nil + 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_not be_nil + 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_not be_nil + 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_not be_nil + 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_not be_nil + 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_not be_nil + 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_not be_nil + 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_not be_nil + 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_not be_nil + 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_not be_nil + 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_not be_nil + end + + it 'record with mary login, mary, mary_addr, mike, mike_addr, score' 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.length.should == 0 end + end end