Skip to content

Commit

Permalink
Re-enabled and fixed up tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
ryankopf committed Jan 21, 2023
1 parent ffcc42e commit 3a84bd9
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 43 deletions.
7 changes: 4 additions & 3 deletions geokit-rails.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "bundler", "> 1.0"
spec.add_development_dependency "simplecov", ">= 0.16.1"
spec.add_development_dependency "simplecov-rcov"
spec.add_development_dependency 'net-http'
spec.add_development_dependency 'rake'
spec.add_development_dependency 'test-unit'
spec.add_development_dependency "mocha", "~> 0.9"
spec.add_development_dependency 'coveralls_reborn'
spec.add_development_dependency "mysql2", "~> 0.2"
spec.add_development_dependency "activerecord-mysql2spatial-adapter"
spec.add_development_dependency "pg", "~> 0.10"
spec.add_development_dependency "mysql2", ">= 0.2"
# spec.add_development_dependency "activerecord-mysql2spatial-adapter"
spec.add_development_dependency "pg", ">= 0.10"
spec.add_development_dependency "sqlite3"
end
6 changes: 1 addition & 5 deletions lib/geokit-rails/ip_geocode_lookup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@ module IpGeocodeLookup
# Class method to mix into active record.
module ClassMethods # :nodoc:
def geocode_ip_address(filter_options = {})
if respond_to? :before_action
before_action :store_ip_location, filter_options
else
before_filter :store_ip_location, filter_options
end
before_action :store_ip_location, filter_options
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/geokit-rails/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module GeokitRails
VERSION = "2.4.0.pre"
VERSION = "2.5.0"
end
36 changes: 35 additions & 1 deletion test/dummy/app/controllers/location_aware_controller.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,43 @@
class LocationAwareController < ApplicationController #:nodoc: all
before_action :set_ip, only: [:index,:cookietest,:sessiontest]
before_action :set_ip_bad, only: [:failtest]
before_action :setup, only: [:cookietest,:sessiontest]
geocode_ip_address

def index
render :nothing => true
render plain: ''
end

def cookietest
cookies[:geo_location] = @success.to_json
render plain: ''
end

def sessiontest
session[:geo_location] = @success.to_json
render plain: ''
end

def failtest
render plain: ''
end

def rescue_action(e) raise e end;
private
def set_ip
request.remote_ip = "good ip"
end
def set_ip_bad
request.remote_ip = "bad ip"
end
def setup
@success = Geokit::GeoLoc.new
@success.provider = "hostip"
@success.lat = 41.7696
@success.lng = -88.4588
@success.city = "Sugar Grove"
@success.state = "IL"
@success.country_code = "US"
@success.success = true
end
end
10 changes: 4 additions & 6 deletions test/dummy/config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
Rails.application.routes.draw do
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

# Defines the root path route ("/")
# root "articles#index"
# get "/", controller: :LocationAware, action: :index
match ':controller(/:action(/:id(.:format)))', via: [:get, :post]
get "/", to: "location_aware#index"
get "/cookietest", to: "location_aware#cookietest"
get "/sessiontest", to: "location_aware#sessiontest"
get "/failtest", to: "location_aware#failtest"
end
44 changes: 17 additions & 27 deletions test/ip_geocode_lookup_test.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
require 'test_helper'

class IpGeocodeLookupTest < ActiveSupport::TestCase
tests LocationAwareController
class IpGeocodeLookupTest < ActionDispatch::IntegrationTest#ActiveSupport::TestCase#ActionController::TestCase
# tests LocationAwareController

def setup
# @controller = LocationAwareController
@success = Geokit::GeoLoc.new
@success.provider = "hostip"
@success.lat = 41.7696
Expand All @@ -21,39 +22,28 @@ def setup

def test_no_location_in_cookie_or_session
Geokit::Geocoders::MultiGeocoder.expects(:geocode).with("good ip").returns(@success)
@request.remote_ip = "good ip"
get :index
verify
get '/'
assert_response :success
assert_equal @success, @request.session[:geo_location]
assert_not_nil cookies[:geo_location]
assert_equal @success.to_json, cookies[:geo_location]
end

def test_location_in_cookie
@request.remote_ip = "good ip"
@request.cookies['geo_location'] = @success.to_yaml
get :index
verify
get '/cookietest'
assert_not_nil cookies[:geo_location]
assert_equal @success.to_json, cookies[:geo_location]
end

def test_location_in_session
@request.remote_ip = "good ip"
@request.session[:geo_location] = @success
@request.cookies['geo_location'] = CGI::Cookie.new('geo_location', @success.to_yaml)
get :index
verify
get '/sessiontest'
assert_response :success
assert_equal @success, Geokit::GeoLoc.new(JSON.parse(session[:geo_location]).transform_keys(&:to_sym))
end

def test_ip_not_located
Geokit::Geocoders::MultiGeocoder.expects(:geocode).with("bad ip").returns(@failure)
@request.remote_ip = "bad ip"
get :index
get '/failtest'
assert_nil @request.session[:geo_location]
end

private

def verify
assert_response :success
assert_equal @success, @request.session[:geo_location]
assert_not_nil cookies['geo_location']
assert_equal @success, YAML.load(cookies['geo_location'])
end
end

0 comments on commit 3a84bd9

Please sign in to comment.