]> git.openstreetmap.org Git - rails.git/blob - test/test_helper.rb
Mark all users which have seen the terms as having accepted them
[rails.git] / test / test_helper.rb
1 ENV["RAILS_ENV"] = "test"
2 require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
3 require '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   # 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.
48   #
49   # Read Mike Clark's excellent walkthrough at
50   #   http://clarkware.com/cgi/blosxom/2005/10/24#Rails10FastTesting
51   #
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
55   # is recommended.
56   self.use_transactional_fixtures = false
57
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
64
65
66   # Load standard fixtures needed to test API methods
67   def self.api_fixtures
68     #print "setting up the api_fixtures"
69     fixtures :users, :changesets, :changeset_tags
70
71     fixtures :current_nodes, :nodes
72     set_fixture_class :current_nodes => 'Node'
73     set_fixture_class :nodes => 'OldNode'
74
75     fixtures  :current_node_tags,:node_tags
76     set_fixture_class :current_node_tags => 'NodeTag'
77     set_fixture_class :node_tags => 'OldNodeTag'
78
79     fixtures :current_ways
80     set_fixture_class :current_ways => 'Way'
81
82     fixtures :current_way_nodes, :current_way_tags
83     set_fixture_class :current_way_nodes => 'WayNode'
84     set_fixture_class :current_way_tags => 'WayTag'
85
86     fixtures :ways
87     set_fixture_class :ways => 'OldWay'
88
89     fixtures :way_nodes, :way_tags
90     set_fixture_class :way_nodes => 'OldWayNode'
91     set_fixture_class :way_tags => 'OldWayTag'
92
93     fixtures :current_relations
94     set_fixture_class :current_relations => 'Relation'
95
96     fixtures :current_relation_members, :current_relation_tags
97     set_fixture_class :current_relation_members => 'RelationMember'
98     set_fixture_class :current_relation_tags => 'RelationTag'
99
100     fixtures :relations
101     set_fixture_class :relations => 'OldRelation'
102
103     fixtures :relation_members, :relation_tags
104     set_fixture_class :relation_members => 'OldRelationMember'
105     set_fixture_class :relation_tags => 'OldRelationTag'
106     
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'
111
112     fixtures :client_applications
113   end
114
115   ##
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
121     begin
122       @controller = new_controller
123       yield
124     ensure
125       @controller = controller_save
126     end
127   end
128
129   ##
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}"
141   end
142
143   ##
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}"
155   end
156
157   def basic_authorization(user, pass)
158     @request.env["HTTP_AUTHORIZATION"] = "Basic %s" % Base64.encode64("#{user}:#{pass}")
159   end
160
161   def error_format(format)
162     @request.env["HTTP_X_ERROR_FORMAT"] = format
163   end
164
165   def content(c)
166     @request.env["RAW_POST_DATA"] = c.to_s
167   end
168   
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"
174   end
175   
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'], ""
180   end
181   
182   def assert_no_missing_translations(msg="")
183     assert_select "span[class=translation_missing]", false, "Missing translation #{msg}"
184   end
185
186   # Set things up for OpenID testing
187   def openid_setup
188     begin
189       # Test if the ROTS (Ruby OpenID Test Server) is already running
190       rots_response = Net::HTTP.get_response(URI.parse("http://localhost:1123/"))
191     rescue
192       # It isn't, so start a new instance.
193       rots = IO.popen(RAILS_ROOT + "/vendor/gems/rots-0.2.1/bin/rots --silent")
194
195       # Wait for up to 30 seconds for the server to start and respond before continuing
196       for i in (1 .. 30)
197         begin
198           sleep 1
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
201           break
202         rescue
203           # If the connection failed, do nothing and repeat the loop
204         end
205       end
206
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
209       Kernel.at_exit do
210         Process.kill("KILL", rots.pid)
211       end
212     end
213   end
214
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)
219
220     return openid_response_qs
221   end
222
223   
224   # Add more helper methods to be used by all tests here...
225 end