2 require "simplecov-lcov"
4 # Fix incompatibility of simplecov-lcov with older versions of simplecov that are not expresses in its gemspec.
5 # https://github.com/fortissimo1997/simplecov-lcov/pull/25
6 unless SimpleCov.respond_to?(:branch_coverage)
8 def self.branch_coverage?
14 SimpleCov::Formatter::LcovFormatter.config do |config|
15 config.report_with_single_file = true
16 config.single_report_path = "coverage/lcov.info"
19 SimpleCov.formatters = SimpleCov::Formatter::MultiFormatter.new(
21 SimpleCov::Formatter::HTMLFormatter,
22 SimpleCov::Formatter::LcovFormatter
26 SimpleCov.start("rails")
28 require "securerandom"
31 ENV["RAILS_ENV"] = "test"
32 require_relative "../config/environment"
33 require "rails/test_help"
34 require "webmock/minitest"
35 require "minitest/focus" unless ENV["CI"]
37 WebMock.disable_net_connect!(:allow_localhost => true)
41 include FactoryBot::Syntax::Methods
42 include ActiveJob::TestHelper
44 # Run tests in parallel with specified workers
45 parallelize(:workers => :number_of_processors)
47 parallelize_setup do |worker|
48 SimpleCov.command_name "#{SimpleCov.command_name}-#{worker}"
51 parallelize_teardown do
56 # takes a block which is executed in the context of a different
57 # ActionController instance. this is used so that code can call methods
58 # on the node controller whilst testing the old_node controller.
59 def with_controller(new_controller)
60 controller_save = @controller
62 @controller = new_controller
65 @controller = controller_save
70 # execute a block with missing translation exceptions suppressed
71 def without_i18n_exceptions
72 exception_handler = I18n.exception_handler
74 I18n.exception_handler = nil
77 I18n.exception_handler = exception_handler
82 # work round minitest insanity that causes it to tell you
83 # to use assert_nil to test for nil, which is fine if you're
84 # comparing to a nil constant but not if you're comparing
85 # an expression that might be nil sometimes
86 def assert_equal_allowing_nil(exp, act, msg = nil)
90 assert_equal exp, act, msg
95 # for some reason assert_equal a, b fails when the relations are
96 # actually equal, so this method manually checks the fields...
97 def assert_relations_are_equal(a, b)
98 assert_not_nil a, "first relation is not allowed to be nil"
99 assert_not_nil b, "second relation #{a.id} is not allowed to be nil"
100 assert_equal a.id, b.id, "relation IDs"
101 assert_equal a.changeset_id, b.changeset_id, "changeset ID on relation #{a.id}"
102 assert_equal a.visible, b.visible, "visible on relation #{a.id}, #{a.visible.inspect} != #{b.visible.inspect}"
103 assert_equal a.version, b.version, "version on relation #{a.id}"
104 assert_equal a.tags, b.tags, "tags on relation #{a.id}"
105 assert_equal a.members, b.members, "member references on relation #{a.id}"
109 # for some reason assert_equal a, b fails when the ways are actually
110 # equal, so this method manually checks the fields...
111 def assert_ways_are_equal(a, b)
112 assert_not_nil a, "first way is not allowed to be nil"
113 assert_not_nil b, "second way #{a.id} is not allowed to be nil"
114 assert_equal a.id, b.id, "way IDs"
115 assert_equal a.changeset_id, b.changeset_id, "changeset ID on way #{a.id}"
116 assert_equal a.visible, b.visible, "visible on way #{a.id}, #{a.visible.inspect} != #{b.visible.inspect}"
117 assert_equal a.version, b.version, "version on way #{a.id}"
118 assert_equal a.tags, b.tags, "tags on way #{a.id}"
119 assert_equal a.nds, b.nds, "node references on way #{a.id}"
123 # for some reason a==b is false, but there doesn't seem to be any
124 # difference between the nodes, so i'm checking all the attributes
125 # manually and blaming it on ActiveRecord
126 def assert_nodes_are_equal(a, b)
127 assert_equal a.id, b.id, "node IDs"
128 assert_equal a.latitude, b.latitude, "latitude on node #{a.id}"
129 assert_equal a.longitude, b.longitude, "longitude on node #{a.id}"
130 assert_equal a.changeset_id, b.changeset_id, "changeset ID on node #{a.id}"
131 assert_equal a.visible, b.visible, "visible on node #{a.id}"
132 assert_equal a.version, b.version, "version on node #{a.id}"
133 assert_equal a.tags, b.tags, "tags on node #{a.id}"
137 # return request header for HTTP Basic Authorization
138 def basic_authorization_header(user, pass)
139 { "Authorization" => format("Basic %<auth>s", :auth => Base64.encode64("#{user}:#{pass}")) }
143 # return request header for HTTP Bearer Authorization
144 def bearer_authorization_header(token)
145 { "Authorization" => "Bearer #{token}" }
149 # make an OAuth signed request
150 def signed_request(method, uri, options = {})
152 uri.scheme ||= "http"
153 uri.host ||= "www.example.com"
155 oauth = options.delete(:oauth)
156 params = options.fetch(:params, {}).transform_keys(&:to_s)
158 oauth[:consumer] ||= oauth[:token].client_application
160 helper = OAuth::Client::Helper.new(nil, oauth)
162 request = OAuth::RequestProxy.proxy(
163 "method" => method.to_s.upcase,
165 "parameters" => params.merge(helper.oauth_parameters)
170 method(method).call(request.signed_uri, **options)
174 # make an OAuth signed GET request
175 def signed_get(uri, options = {})
176 signed_request(:get, uri, options)
180 # make an OAuth signed POST request
181 def signed_post(uri, options = {})
182 signed_request(:post, uri, options)
186 # return request header for HTTP Accept
187 def accept_format_header(format)
188 { "Accept" => format }
192 # return request header to ask for a particular error format
193 def error_format_header(f)
194 { "X-Error-Format" => f }
198 # Used to check that the error header and the forbidden responses are given
199 # when the owner of the changeset has their data not marked as public
200 def assert_require_public_data(msg = "Shouldn't be able to use API when the user's data is not public")
201 assert_response :forbidden, msg
202 assert_equal("You must make your edits public to upload new data", @response.headers["Error"], "Wrong error message")
206 # Not sure this is the best response we could give
207 def assert_inactive_user(msg = "an inactive user shouldn't be able to access the API")
208 assert_response :unauthorized, msg
209 # assert_equal @response.headers['Error'], ""
213 # Check for missing translations in an HTML response
214 def assert_no_missing_translations(msg = "")
215 assert_select "span[class=translation_missing]", false, "Missing translation #{msg}"
219 # execute a block with a given set of HTTP responses stubbed
220 def with_http_stubs(stubs_file)
221 stubs = YAML.load_file(File.expand_path("../http/#{stubs_file}.yml", __FILE__))
222 stubs.each do |url, response|
223 stub_request(:get, Regexp.new(Regexp.quote(url))).to_return(:status => response["code"], :body => response["body"])
229 def stub_gravatar_request(email, status = 200, body = nil)
230 hash = ::Digest::MD5.hexdigest(email.downcase)
231 url = "https://www.gravatar.com/avatar/#{hash}?d=404"
232 stub_request(:get, url).and_return(:status => status, :body => body)
235 def email_text_parts(message)
236 message.parts.each_with_object([]) do |part, text_parts|
237 if part.content_type.start_with?("text/")
238 text_parts.push(part)
239 elsif part.multipart?
240 text_parts.concat(email_text_parts(part))
247 within "form", :text => "Email Address or Username" do
248 fill_in "username", :with => user.email
249 fill_in "password", :with => "test"
254 def session_for(user)
256 post login_path, :params => { :username => user.display_name, :password => "test" }
260 def xml_for_node(node)
261 doc = OSM::API.new.xml_doc
262 doc.root << xml_node_for_node(node)
266 def xml_node_for_node(node)
267 el = XML::Node.new "node"
268 el["id"] = node.id.to_s
270 add_metadata_to_xml_node(el, node, {}, {})
273 el["lat"] = node.lat.to_s
274 el["lon"] = node.lon.to_s
277 add_tags_to_xml_node(el, node.node_tags)
283 doc = OSM::API.new.xml_doc
284 doc.root << xml_node_for_way(way)
288 def xml_node_for_way(way)
289 el = XML::Node.new "way"
290 el["id"] = way.id.to_s
292 add_metadata_to_xml_node(el, way, {}, {})
294 # make sure nodes are output in sequence_id order
296 way.way_nodes.each do |nd|
297 ordered_nodes[nd.sequence_id] = nd.node_id.to_s if nd.node&.visible?
300 ordered_nodes.each do |nd_id|
301 next unless nd_id && nd_id != "0"
303 node_el = XML::Node.new "nd"
304 node_el["ref"] = nd_id
308 add_tags_to_xml_node(el, way.way_tags)
313 def xml_for_relation(relation)
314 doc = OSM::API.new.xml_doc
315 doc.root << xml_node_for_relation(relation)
319 def xml_node_for_relation(relation)
320 el = XML::Node.new "relation"
321 el["id"] = relation.id.to_s
323 add_metadata_to_xml_node(el, relation, {}, {})
325 relation.relation_members.each do |member|
326 member_el = XML::Node.new "member"
327 member_el["type"] = member.member_type.downcase
328 member_el["ref"] = member.member_id.to_s
329 member_el["role"] = member.member_role
333 add_tags_to_xml_node(el, relation.relation_tags)
338 def add_metadata_to_xml_node(el, osm, changeset_cache, user_display_name_cache)
339 el["changeset"] = osm.changeset_id.to_s
340 el["redacted"] = osm.redaction.id.to_s if osm.redacted?
341 el["timestamp"] = osm.timestamp.xmlschema
342 el["version"] = osm.version.to_s
343 el["visible"] = osm.visible.to_s
345 if changeset_cache.key?(osm.changeset_id)
346 # use the cache if available
348 changeset_cache[osm.changeset_id] = osm.changeset.user_id
351 user_id = changeset_cache[osm.changeset_id]
353 if user_display_name_cache.key?(user_id)
354 # use the cache if available
355 elsif osm.changeset.user.data_public?
356 user_display_name_cache[user_id] = osm.changeset.user.display_name
358 user_display_name_cache[user_id] = nil
361 unless user_display_name_cache[user_id].nil?
362 el["user"] = user_display_name_cache[user_id]
363 el["uid"] = user_id.to_s
367 def add_tags_to_xml_node(el, tags)
369 tag_el = XML::Node.new("tag")
378 def with_settings(settings)
379 saved_settings = Settings.to_hash.slice(*settings.keys)
381 Settings.merge!(settings)
385 Settings.merge!(saved_settings)
388 def with_user_account_deletion_delay(value, &block)
391 with_settings(:user_account_deletion_delay => value, &block)