]> git.openstreetmap.org Git - rails.git/blob - test/test_helper.rb
Fix tests to copy with new style mail objects
[rails.git] / test / test_helper.rb
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'
5
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
8 # to rails 3.
9 module ActionController
10   module Integration
11     class Session
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
19         status
20       end
21
22       alias_method_chain :process, :capture
23     end
24
25     module ControllerCapture
26       module ClassMethods
27         mattr_accessor :last_controller
28
29         def clear_last_instantiation!
30           self.last_controller = nil
31         end
32
33         def new_with_capture(*args)
34           controller = new_without_capture(*args)
35           self.last_controller ||= controller
36           controller
37         end
38       end
39     end
40   end
41 end
42
43 class ActiveSupport::TestCase
44   # Load standard fixtures needed to test API methods
45   def self.api_fixtures
46     #print "setting up the api_fixtures"
47     fixtures :users, :changesets, :changeset_tags
48
49     fixtures :current_nodes, :nodes
50     set_fixture_class :current_nodes => 'Node'
51     set_fixture_class :nodes => 'OldNode'
52
53     fixtures  :current_node_tags,:node_tags
54     set_fixture_class :current_node_tags => 'NodeTag'
55     set_fixture_class :node_tags => 'OldNodeTag'
56
57     fixtures :current_ways
58     set_fixture_class :current_ways => 'Way'
59
60     fixtures :current_way_nodes, :current_way_tags
61     set_fixture_class :current_way_nodes => 'WayNode'
62     set_fixture_class :current_way_tags => 'WayTag'
63
64     fixtures :ways
65     set_fixture_class :ways => 'OldWay'
66
67     fixtures :way_nodes, :way_tags
68     set_fixture_class :way_nodes => 'OldWayNode'
69     set_fixture_class :way_tags => 'OldWayTag'
70
71     fixtures :current_relations
72     set_fixture_class :current_relations => 'Relation'
73
74     fixtures :current_relation_members, :current_relation_tags
75     set_fixture_class :current_relation_members => 'RelationMember'
76     set_fixture_class :current_relation_tags => 'RelationTag'
77
78     fixtures :relations
79     set_fixture_class :relations => 'OldRelation'
80
81     fixtures :relation_members, :relation_tags
82     set_fixture_class :relation_members => 'OldRelationMember'
83     set_fixture_class :relation_tags => 'OldRelationTag'
84     
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'
89
90     fixtures :client_applications
91   end
92
93   ##
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
99     begin
100       @controller = new_controller
101       yield
102     ensure
103       @controller = controller_save
104     end
105   end
106
107   ##
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}"
119   end
120
121   ##
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}"
133   end
134
135   def basic_authorization(user, pass)
136     @request.env["HTTP_AUTHORIZATION"] = "Basic %s" % Base64.encode64("#{user}:#{pass}")
137   end
138
139   def error_format(format)
140     @request.env["HTTP_X_ERROR_FORMAT"] = format
141   end
142
143   def content(c)
144     @request.env["RAW_POST_DATA"] = c.to_s
145   end
146   
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"
152   end
153   
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'], ""
158   end
159   
160   def assert_no_missing_translations(msg="")
161     assert_select "span[class=translation_missing]", false, "Missing translation #{msg}"
162   end
163
164   # Set things up for OpenID testing
165   def openid_setup
166     begin
167       # Test if the ROTS (Ruby OpenID Test Server) is already running
168       rots_response = Net::HTTP.get_response(URI.parse("http://localhost:1123/"))
169     rescue
170       # It isn't, so start a new instance.
171       rots = IO.popen("#{Rails.root}/vendor/gems/rots-0.2.1/bin/rots --silent")
172
173       # Wait for up to 30 seconds for the server to start and respond before continuing
174       for i in (1 .. 30)
175         begin
176           sleep 1
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
179           break
180         rescue
181           # If the connection failed, do nothing and repeat the loop
182         end
183       end
184
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
187       Kernel.at_exit do
188         Process.kill("KILL", rots.pid)
189       end
190     end
191   end
192
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)
197
198     return openid_response_qs
199   end
200
201   
202   # Add more helper methods to be used by all tests here...
203 end