require 'spec_helper'

describe User do

=begin
X If user provides profile location data, that will be used for lat/lng lookup
X If the user changes their profile location, we update their lat/lng address
X If no profile location is provided, then we populate lat/lng from their IP address
X If no profile location is provided, and the user creates/joins a music session, then we update their lat/lng from the IP address
=end

  before do
    # @geocode1 = FactoryGirl.create(:geocoder)
    # @geocode2 = FactoryGirl.create(:geocoder)
    # @user = User.new(first_name: "Example", last_name: "User", email: "user@example.com",
    #                  password: "foobar", password_confirmation: "foobar",
    #                  city: "Apex", state: "NC", country: "US",
    #                  terms_of_service: true, musician: true)
    # @user.save!
  end

  describe "with profile location data" do
    it "should have lat/lng values" do
      pending 'distance search changes'
      geo = GeoIpLocations.find_by_city(@user.city).blocks.first
      @user.lat.should == geo.latitude
      @user.lng.should == geo.longitude
    end

    it "should have updated lat/lng values" do
      pending 'distance search changes'
      @user.update_attributes({ :city => @geocode2.city,
                                :state => @geocode2.region,
                                :country => @geocode2.country,
                              })
      geo = GeoIpLocations.find_by_city(@user.city).blocks.first
      @user.lat.should == geo.latitude
      @user.lng.should == geo.longitude
    end
  end

  describe "without profile location data" do
    it "should have lat/lng values from ip_address" do
      pending 'distance search changes'
      @user.update_attributes({ :city => nil, 
                                :state => nil,
                                :country => nil,
                              })
      @user.lat.should == nil
      @user.lng.should == nil
      geo = GeoIpBlocks.ip_lookup('1.1.0.0')
      geo.should_not be_nil
      geo = GeoIpBlocks.ip_lookup('1.1.0.255')
      geo.should_not be_nil
      @user.update_lat_lng('1.1.0.255')
      @user.lat.should == geo.latitude
      @user.lng.should == geo.longitude
    end
  end

end
