require 'spec_helper' describe JamRuby::Connection do let(:user) { FactoryGirl.create(:user) } let (:music_session) { FactoryGirl.create(:active_music_session, :creator => user) } let (:conn) { FactoryGirl.create(:connection, :user => user, :music_session => music_session, :ip_address => "1.1.1.1", :client_id => "1") } it 'starts in the correct state' do connection = FactoryGirl.create(:connection, :user => user, :music_session => music_session, :ip_address => "1.1.1.1", :client_id => "1") connection.idle?.should be_true end it 'transitions properly' do connection = FactoryGirl.create(:connection, :user => user, :music_session => music_session, :ip_address => "1.1.1.1", :client_id => "1") connection.connect! connection.connected?.should be_true connection.state_message.should == 'Connected' connection.stale! connection.stale?.should be_true connection.state_message.should == 'Stale' connection.expire! connection.destroyed?.should be_true end it 'updates user lat/lng' do pending 'distance search changes' uu = FactoryGirl.create(:user) uu.lat.should == nil msess = FactoryGirl.create(:active_music_session, :creator => uu) geocode = FactoryGirl.create(:geocoder) connection = FactoryGirl.create(:connection, :user => uu, :music_session => msess, :ip_address => "1.1.1.1", :client_id => "1") user.lat.should == geocode.lat user.lng.should == geocode.lng end describe "audio latency" do it "allow update" do conn.last_jam_audio_latency = 1 conn.save! end it "prevent negative or 0" do conn.last_jam_audio_latency = 0 conn.save.should be_false conn.errors[:last_jam_audio_latency].should == ['must be greater than 0'] end it "prevent non numerical" do conn.last_jam_audio_latency = 'a' conn.save.should be_false conn.errors[:last_jam_audio_latency].should == ['is not a number'] end end describe "update_locidispids" do before(:each) do create_phony_database end after(:all) do create_phony_database end it "updates locidispid with valid maxmind data" do conn.locidispid.should == 0 # default in factory girl Connection.update_locidispids(false) conn.reload conn.locidispid.should == 17192 * 1000000 + JamIsp.lookup(conn.addr).coid end it "updates locidispid to 0 with no maxmind data" do # delete the ATX location info, and update. should be 0 conn.locidispid = 5 # make it not zero to start conn.save! GeoIpLocations.connection.execute("DELETE from geoiplocations where city = 'Austin'").check Connection.update_locidispids(false) conn.reload conn.locidispid.should == 0 end end end