1 ENV["RAILS_ENV"] = "test"
 
   2 require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
 
   4 load 'composite_primary_keys/fixtures.rb'
 
   6 # This monkey patch is to make tests where a rack module alters
 
   7 # the response work with rails 2 - it can be dropped when we move
 
   9 module ActionController
 
  12       def process_with_capture(method, path, parameters = nil, headers = nil)
 
  13         status = process_without_capture(method, path, parameters, headers)
 
  14         @controller = ActionController::Base.last_controller
 
  15         @request = @controller.request
 
  16         @response.session = @controller.response.session
 
  17         @response.template = @controller.response.template
 
  18         @response.redirected_to = @response.location
 
  22       alias_method_chain :process, :capture
 
  25     module ControllerCapture
 
  27         mattr_accessor :last_controller
 
  29         def clear_last_instantiation!
 
  30           self.last_controller = nil
 
  33         def new_with_capture(*args)
 
  34           controller = new_without_capture(*args)
 
  35           self.last_controller ||= controller
 
  43 class ActiveSupport::TestCase
 
  44   # Transactional fixtures accelerate your tests by wrapping each test method
 
  45   # in a transaction that's rolled back on completion.  This ensures that the
 
  46   # test database remains unchanged so your fixtures don't have to be reloaded
 
  47   # between every test method.  Fewer database queries means faster tests.
 
  49   # Read Mike Clark's excellent walkthrough at
 
  50   #   http://clarkware.com/cgi/blosxom/2005/10/24#Rails10FastTesting
 
  52   # Every Active Record database supports transactions except MyISAM tables
 
  53   # in MySQL.  Turn off transactional fixtures in this case; however, if you
 
  54   # don't care one way or the other, switching from MyISAM to InnoDB tables
 
  56   self.use_transactional_fixtures = false
 
  58   # Instantiated fixtures are slow, but give you @david where otherwise you
 
  59   # would need people(:david).  If you don't want to migrate your existing
 
  60   # test cases which use the @david style and don't mind the speed hit (each
 
  61   # instantiated fixtures translates to a database query per test method),
 
  62   # then set this back to true.
 
  63   self.use_instantiated_fixtures  = false
 
  66   # Load standard fixtures needed to test API methods
 
  68     #print "setting up the api_fixtures"
 
  69     fixtures :users, :changesets, :changeset_tags
 
  71     fixtures :current_nodes, :nodes
 
  72     set_fixture_class :current_nodes => 'Node'
 
  73     set_fixture_class :nodes => 'OldNode'
 
  75     fixtures  :current_node_tags,:node_tags
 
  76     set_fixture_class :current_node_tags => 'NodeTag'
 
  77     set_fixture_class :node_tags => 'OldNodeTag'
 
  79     fixtures :current_ways
 
  80     set_fixture_class :current_ways => 'Way'
 
  82     fixtures :current_way_nodes, :current_way_tags
 
  83     set_fixture_class :current_way_nodes => 'WayNode'
 
  84     set_fixture_class :current_way_tags => 'WayTag'
 
  87     set_fixture_class :ways => 'OldWay'
 
  89     fixtures :way_nodes, :way_tags
 
  90     set_fixture_class :way_nodes => 'OldWayNode'
 
  91     set_fixture_class :way_tags => 'OldWayTag'
 
  93     fixtures :current_relations
 
  94     set_fixture_class :current_relations => 'Relation'
 
  96     fixtures :current_relation_members, :current_relation_tags
 
  97     set_fixture_class :current_relation_members => 'RelationMember'
 
  98     set_fixture_class :current_relation_tags => 'RelationTag'
 
 101     set_fixture_class :relations => 'OldRelation'
 
 103     fixtures :relation_members, :relation_tags
 
 104     set_fixture_class :relation_members => 'OldRelationMember'
 
 105     set_fixture_class :relation_tags => 'OldRelationTag'
 
 107     fixtures :gpx_files, :gps_points, :gpx_file_tags
 
 108     set_fixture_class :gpx_files => 'Trace'
 
 109     set_fixture_class :gps_points => 'Tracepoint'
 
 110     set_fixture_class :gpx_file_tags => 'Tracetag'
 
 112     fixtures :client_applications
 
 116   # takes a block which is executed in the context of a different 
 
 117   # ActionController instance. this is used so that code can call methods
 
 118   # on the node controller whilst testing the old_node controller.
 
 119   def with_controller(new_controller)
 
 120     controller_save = @controller
 
 122       @controller = new_controller
 
 125       @controller = controller_save
 
 130   # for some reason assert_equal a, b fails when the ways are actually
 
 131   # equal, so this method manually checks the fields...
 
 132   def assert_ways_are_equal(a, b)
 
 133     assert_not_nil a, "first way is not allowed to be nil"
 
 134     assert_not_nil b, "second way #{a.id} is not allowed to be nil"
 
 135     assert_equal a.id, b.id, "way IDs"
 
 136     assert_equal a.changeset_id, b.changeset_id, "changeset ID on way #{a.id}"
 
 137     assert_equal a.visible, b.visible, "visible on way #{a.id}, #{a.visible.inspect} != #{b.visible.inspect}"
 
 138     assert_equal a.version, b.version, "version on way #{a.id}"
 
 139     assert_equal a.tags, b.tags, "tags on way #{a.id}"
 
 140     assert_equal a.nds, b.nds, "node references on way #{a.id}"
 
 144   # for some reason a==b is false, but there doesn't seem to be any 
 
 145   # difference between the nodes, so i'm checking all the attributes 
 
 146   # manually and blaming it on ActiveRecord
 
 147   def assert_nodes_are_equal(a, b)
 
 148     assert_equal a.id, b.id, "node IDs"
 
 149     assert_equal a.latitude, b.latitude, "latitude on node #{a.id}"
 
 150     assert_equal a.longitude, b.longitude, "longitude on node #{a.id}"
 
 151     assert_equal a.changeset_id, b.changeset_id, "changeset ID on node #{a.id}"
 
 152     assert_equal a.visible, b.visible, "visible on node #{a.id}"
 
 153     assert_equal a.version, b.version, "version on node #{a.id}"
 
 154     assert_equal a.tags, b.tags, "tags on node #{a.id}"
 
 157   def basic_authorization(user, pass)
 
 158     @request.env["HTTP_AUTHORIZATION"] = "Basic %s" % Base64.encode64("#{user}:#{pass}")
 
 161   def error_format(format)
 
 162     @request.env["HTTP_X_ERROR_FORMAT"] = format
 
 166     @request.env["RAW_POST_DATA"] = c.to_s
 
 169   # Used to check that the error header and the forbidden responses are given
 
 170   # when the owner of the changset has their data not marked as public
 
 171   def assert_require_public_data(msg = "Shouldn't be able to use API when the user's data is not public")
 
 172     assert_response :forbidden, msg
 
 173     assert_equal @response.headers['Error'], "You must make your edits public to upload new data", "Wrong error message"
 
 176   # Not sure this is the best response we could give
 
 177   def assert_inactive_user(msg = "an inactive user shouldn't be able to access the API")
 
 178     assert_response :unauthorized, msg
 
 179     #assert_equal @response.headers['Error'], ""
 
 182   def assert_no_missing_translations(msg="")
 
 183     assert_select "span[class=translation_missing]", false, "Missing translation #{msg}"
 
 186   # Set things up for OpenID testing
 
 189       # Test if the ROTS (Ruby OpenID Test Server) is already running
 
 190       rots_response = Net::HTTP.get_response(URI.parse("http://localhost:1123/"))
 
 192       # It isn't, so start a new instance.
 
 193       rots = IO.popen(RAILS_ROOT + "/vendor/gems/rots-0.2.1/bin/rots --silent")
 
 195       # Wait for up to 30 seconds for the server to start and respond before continuing
 
 199           rots_response = Net::HTTP.get_response(URI.parse("http://localhost:1123/"))
 
 200           # If the rescue block doesn't fire, ROTS is up and running and we can continue
 
 203           # If the connection failed, do nothing and repeat the loop
 
 207       # Arrange to kill the process when we exit - note that we need
 
 208       # to kill it really har due to a bug in ROTS
 
 210         Process.kill("KILL", rots.pid)
 
 215   def openid_request(openid_request_uri)
 
 216     openid_response = Net::HTTP.get_response(URI.parse(openid_request_uri))
 
 217     openid_response_uri = URI(openid_response['Location'])
 
 218     openid_response_qs = Rack::Utils.parse_query(openid_response_uri.query)
 
 220     return openid_response_qs
 
 224   # Add more helper methods to be used by all tests here...