1 ENV["RAILS_ENV"] = "test"
2 require File.expand_path('../../config/environment', __FILE__)
3 require 'rails/test_help'
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 # Load standard fixtures needed to test API methods
46 #print "setting up the api_fixtures"
47 fixtures :users, :changesets, :changeset_tags
49 fixtures :current_nodes, :nodes
50 set_fixture_class :current_nodes => 'Node'
51 set_fixture_class :nodes => 'OldNode'
53 fixtures :current_node_tags,:node_tags
54 set_fixture_class :current_node_tags => 'NodeTag'
55 set_fixture_class :node_tags => 'OldNodeTag'
57 fixtures :current_ways
58 set_fixture_class :current_ways => 'Way'
60 fixtures :current_way_nodes, :current_way_tags
61 set_fixture_class :current_way_nodes => 'WayNode'
62 set_fixture_class :current_way_tags => 'WayTag'
65 set_fixture_class :ways => 'OldWay'
67 fixtures :way_nodes, :way_tags
68 set_fixture_class :way_nodes => 'OldWayNode'
69 set_fixture_class :way_tags => 'OldWayTag'
71 fixtures :current_relations
72 set_fixture_class :current_relations => 'Relation'
74 fixtures :current_relation_members, :current_relation_tags
75 set_fixture_class :current_relation_members => 'RelationMember'
76 set_fixture_class :current_relation_tags => 'RelationTag'
79 set_fixture_class :relations => 'OldRelation'
81 fixtures :relation_members, :relation_tags
82 set_fixture_class :relation_members => 'OldRelationMember'
83 set_fixture_class :relation_tags => 'OldRelationTag'
85 fixtures :gpx_files, :gps_points, :gpx_file_tags
86 set_fixture_class :gpx_files => 'Trace'
87 set_fixture_class :gps_points => 'Tracepoint'
88 set_fixture_class :gpx_file_tags => 'Tracetag'
90 fixtures :client_applications
94 # takes a block which is executed in the context of a different
95 # ActionController instance. this is used so that code can call methods
96 # on the node controller whilst testing the old_node controller.
97 def with_controller(new_controller)
98 controller_save = @controller
100 @controller = new_controller
103 @controller = controller_save
108 # for some reason assert_equal a, b fails when the ways are actually
109 # equal, so this method manually checks the fields...
110 def assert_ways_are_equal(a, b)
111 assert_not_nil a, "first way is not allowed to be nil"
112 assert_not_nil b, "second way #{a.id} is not allowed to be nil"
113 assert_equal a.id, b.id, "way IDs"
114 assert_equal a.changeset_id, b.changeset_id, "changeset ID on way #{a.id}"
115 assert_equal a.visible, b.visible, "visible on way #{a.id}, #{a.visible.inspect} != #{b.visible.inspect}"
116 assert_equal a.version, b.version, "version on way #{a.id}"
117 assert_equal a.tags, b.tags, "tags on way #{a.id}"
118 assert_equal a.nds, b.nds, "node references on way #{a.id}"
122 # for some reason a==b is false, but there doesn't seem to be any
123 # difference between the nodes, so i'm checking all the attributes
124 # manually and blaming it on ActiveRecord
125 def assert_nodes_are_equal(a, b)
126 assert_equal a.id, b.id, "node IDs"
127 assert_equal a.latitude, b.latitude, "latitude on node #{a.id}"
128 assert_equal a.longitude, b.longitude, "longitude on node #{a.id}"
129 assert_equal a.changeset_id, b.changeset_id, "changeset ID on node #{a.id}"
130 assert_equal a.visible, b.visible, "visible on node #{a.id}"
131 assert_equal a.version, b.version, "version on node #{a.id}"
132 assert_equal a.tags, b.tags, "tags on node #{a.id}"
135 def basic_authorization(user, pass)
136 @request.env["HTTP_AUTHORIZATION"] = "Basic %s" % Base64.encode64("#{user}:#{pass}")
139 def error_format(format)
140 @request.env["HTTP_X_ERROR_FORMAT"] = format
144 @request.env["RAW_POST_DATA"] = c.to_s
147 # Used to check that the error header and the forbidden responses are given
148 # when the owner of the changset has their data not marked as public
149 def assert_require_public_data(msg = "Shouldn't be able to use API when the user's data is not public")
150 assert_response :forbidden, msg
151 assert_equal @response.headers['Error'], "You must make your edits public to upload new data", "Wrong error message"
154 # Not sure this is the best response we could give
155 def assert_inactive_user(msg = "an inactive user shouldn't be able to access the API")
156 assert_response :unauthorized, msg
157 #assert_equal @response.headers['Error'], ""
160 def assert_no_missing_translations(msg="")
161 assert_select "span[class=translation_missing]", false, "Missing translation #{msg}"
164 # Set things up for OpenID testing
167 # Test if the ROTS (Ruby OpenID Test Server) is already running
168 rots_response = Net::HTTP.get_response(URI.parse("http://localhost:1123/"))
170 # It isn't, so start a new instance.
171 rots = IO.popen(RAILS_ROOT + "/vendor/gems/rots-0.2.1/bin/rots --silent")
173 # Wait for up to 30 seconds for the server to start and respond before continuing
177 rots_response = Net::HTTP.get_response(URI.parse("http://localhost:1123/"))
178 # If the rescue block doesn't fire, ROTS is up and running and we can continue
181 # If the connection failed, do nothing and repeat the loop
185 # Arrange to kill the process when we exit - note that we need
186 # to kill it really har due to a bug in ROTS
188 Process.kill("KILL", rots.pid)
193 def openid_request(openid_request_uri)
194 openid_response = Net::HTTP.get_response(URI.parse(openid_request_uri))
195 openid_response_uri = URI(openid_response['Location'])
196 openid_response_qs = Rack::Utils.parse_query(openid_response_uri.query)
198 return openid_response_qs
202 # Add more helper methods to be used by all tests here...