]> projects.mako.cc - selectricity/blob - vendor/plugins/geokit/test/ip_geocode_lookup_test.rb
improved dependency information
[selectricity] / vendor / plugins / geokit / test / ip_geocode_lookup_test.rb
1 require File.join(File.dirname(__FILE__), '../../../../config/environment')
2 require 'action_controller/test_process'
3 require 'test/unit'
4 require 'rubygems'
5 require 'mocha'
6
7
8 class LocationAwareController < ActionController::Base #:nodoc: all
9   geocode_ip_address
10   
11   def index
12     render :nothing => true
13   end
14 end
15
16 class ActionController::TestRequest #:nodoc: all
17   attr_accessor :remote_ip
18 end
19
20 # Re-raise errors caught by the controller.
21 class LocationAwareController #:nodoc: all
22   def rescue_action(e) raise e end; 
23 end
24
25 class IpGeocodeLookupTest < Test::Unit::TestCase #:nodoc: all
26   
27   def setup
28     @success = GeoKit::GeoLoc.new
29     @success.provider = "hostip"
30     @success.lat = 41.7696
31     @success.lng = -88.4588
32     @success.city = "Sugar Grove"
33     @success.state = "IL"
34     @success.country_code = "US"
35     @success.success = true
36     
37     @failure = GeoKit::GeoLoc.new
38     @failure.provider = "hostip"
39     @failure.city = "(Private Address)"
40     @failure.success = false
41     
42     @controller = LocationAwareController.new
43     @request = ActionController::TestRequest.new
44     @response = ActionController::TestResponse.new
45   end
46
47   def test_no_location_in_cookie_or_session
48     GeoKit::Geocoders::IpGeocoder.expects(:geocode).with("good ip").returns(@success)
49     @request.remote_ip = "good ip"
50     get :index
51     verify
52   end
53   
54   def test_location_in_cookie
55     @request.remote_ip = "good ip"
56     @request.cookies['geo_location'] = CGI::Cookie.new('geo_location', @success.to_yaml)
57     get :index
58     verify
59   end
60   
61   def test_location_in_session
62     @request.remote_ip = "good ip"
63     @request.session[:geo_location] = @success
64     @request.cookies['geo_location'] = CGI::Cookie.new('geo_location', @success.to_yaml)
65     get :index
66     verify
67   end
68   
69   def test_ip_not_located
70     GeoKit::Geocoders::IpGeocoder.expects(:geocode).with("bad ip").returns(@failure)
71     @request.remote_ip = "bad ip"
72     get :index
73     assert_nil @request.session[:geo_location]
74   end
75   
76   private
77   
78   def verify
79     assert_response :success    
80     assert_equal @success, @request.session[:geo_location]
81     assert_not_nil cookies['geo_location']
82     assert_equal @success, YAML.load(cookies['geo_location'].join)
83   end
84 end

Benjamin Mako Hill || Want to submit a patch?