]> git.openstreetmap.org Git - rails.git/blob - test/test_helper.rb
Merge pull request #3009 from gravitystorm/svg_icons
[rails.git] / test / test_helper.rb
1 require "simplecov"
2 require "simplecov-lcov"
3
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)
7   module SimpleCov
8     def self.branch_coverage?
9       false
10     end
11   end
12 end
13
14 SimpleCov::Formatter::LcovFormatter.config do |config|
15   config.report_with_single_file = true
16   config.single_report_path = "coverage/lcov.info"
17 end
18
19 SimpleCov.formatters = SimpleCov::Formatter::MultiFormatter.new(
20   [
21     SimpleCov::Formatter::HTMLFormatter,
22     SimpleCov::Formatter::LcovFormatter
23   ]
24 )
25
26 SimpleCov.start("rails")
27
28 require "securerandom"
29 require "digest/sha1"
30
31 ENV["RAILS_ENV"] = "test"
32 require_relative "../config/environment"
33 require "rails/test_help"
34 require "webmock/minitest"
35
36 WebMock.disable_net_connect!(:allow_localhost => true)
37
38 module ActiveSupport
39   class TestCase
40     include FactoryBot::Syntax::Methods
41     include ActiveJob::TestHelper
42
43     # Run tests in parallel with specified workers
44     parallelize(:workers => :number_of_processors)
45
46     ##
47     # takes a block which is executed in the context of a different
48     # ActionController instance. this is used so that code can call methods
49     # on the node controller whilst testing the old_node controller.
50     def with_controller(new_controller)
51       controller_save = @controller
52       begin
53         @controller = new_controller
54         yield
55       ensure
56         @controller = controller_save
57       end
58     end
59
60     ##
61     # execute a block with missing translation exceptions suppressed
62     def without_i18n_exceptions
63       exception_handler = I18n.exception_handler
64       begin
65         I18n.exception_handler = nil
66         yield
67       ensure
68         I18n.exception_handler = exception_handler
69       end
70     end
71
72     ##
73     # work round minitest insanity that causes it to tell you
74     # to use assert_nil to test for nil, which is fine if you're
75     # comparing to a nil constant but not if you're comparing
76     # an expression that might be nil sometimes
77     def assert_equal_allowing_nil(exp, act, msg = nil)
78       if exp.nil?
79         assert_nil act, msg
80       else
81         assert_equal exp, act, msg
82       end
83     end
84
85     ##
86     # for some reason assert_equal a, b fails when the relations are
87     # actually equal, so this method manually checks the fields...
88     def assert_relations_are_equal(a, b)
89       assert_not_nil a, "first relation is not allowed to be nil"
90       assert_not_nil b, "second relation #{a.id} is not allowed to be nil"
91       assert_equal a.id, b.id, "relation IDs"
92       assert_equal a.changeset_id, b.changeset_id, "changeset ID on relation #{a.id}"
93       assert_equal a.visible, b.visible, "visible on relation #{a.id}, #{a.visible.inspect} != #{b.visible.inspect}"
94       assert_equal a.version, b.version, "version on relation #{a.id}"
95       assert_equal a.tags, b.tags, "tags on relation #{a.id}"
96       assert_equal a.members, b.members, "member references on relation #{a.id}"
97     end
98
99     ##
100     # for some reason assert_equal a, b fails when the ways are actually
101     # equal, so this method manually checks the fields...
102     def assert_ways_are_equal(a, b)
103       assert_not_nil a, "first way is not allowed to be nil"
104       assert_not_nil b, "second way #{a.id} is not allowed to be nil"
105       assert_equal a.id, b.id, "way IDs"
106       assert_equal a.changeset_id, b.changeset_id, "changeset ID on way #{a.id}"
107       assert_equal a.visible, b.visible, "visible on way #{a.id}, #{a.visible.inspect} != #{b.visible.inspect}"
108       assert_equal a.version, b.version, "version on way #{a.id}"
109       assert_equal a.tags, b.tags, "tags on way #{a.id}"
110       assert_equal a.nds, b.nds, "node references on way #{a.id}"
111     end
112
113     ##
114     # for some reason a==b is false, but there doesn't seem to be any
115     # difference between the nodes, so i'm checking all the attributes
116     # manually and blaming it on ActiveRecord
117     def assert_nodes_are_equal(a, b)
118       assert_equal a.id, b.id, "node IDs"
119       assert_equal a.latitude, b.latitude, "latitude on node #{a.id}"
120       assert_equal a.longitude, b.longitude, "longitude on node #{a.id}"
121       assert_equal a.changeset_id, b.changeset_id, "changeset ID on node #{a.id}"
122       assert_equal a.visible, b.visible, "visible on node #{a.id}"
123       assert_equal a.version, b.version, "version on node #{a.id}"
124       assert_equal a.tags, b.tags, "tags on node #{a.id}"
125     end
126
127     ##
128     # return request header for HTTP Basic Authorization
129     def basic_authorization_header(user, pass)
130       { "Authorization" => format("Basic %<auth>s", :auth => Base64.encode64("#{user}:#{pass}")) }
131     end
132
133     ##
134     # make an OAuth signed request
135     def signed_request(method, uri, options = {})
136       uri = URI.parse(uri)
137       uri.scheme ||= "http"
138       uri.host ||= "www.example.com"
139
140       oauth = options.delete(:oauth)
141       params = options.fetch(:params, {}).transform_keys(&:to_s)
142
143       oauth[:consumer] ||= oauth[:token].client_application
144
145       helper = OAuth::Client::Helper.new(nil, oauth)
146
147       request = OAuth::RequestProxy.proxy(
148         "method" => method.to_s.upcase,
149         "uri" => uri,
150         "parameters" => params.merge(helper.oauth_parameters)
151       )
152
153       request.sign!(oauth)
154
155       method(method).call(request.signed_uri, options)
156     end
157
158     ##
159     # make an OAuth signed GET request
160     def signed_get(uri, options = {})
161       signed_request(:get, uri, options)
162     end
163
164     ##
165     # make an OAuth signed POST request
166     def signed_post(uri, options = {})
167       signed_request(:post, uri, options)
168     end
169
170     ##
171     # return request header for HTTP Accept
172     def accept_format_header(format)
173       { "Accept" => format }
174     end
175
176     ##
177     # return request header to ask for a particular error format
178     def error_format_header(f)
179       { "X-Error-Format" => f }
180     end
181
182     ##
183     # Used to check that the error header and the forbidden responses are given
184     # when the owner of the changset has their data not marked as public
185     def assert_require_public_data(msg = "Shouldn't be able to use API when the user's data is not public")
186       assert_response :forbidden, msg
187       assert_equal("You must make your edits public to upload new data", @response.headers["Error"], "Wrong error message")
188     end
189
190     ##
191     # Not sure this is the best response we could give
192     def assert_inactive_user(msg = "an inactive user shouldn't be able to access the API")
193       assert_response :unauthorized, msg
194       # assert_equal @response.headers['Error'], ""
195     end
196
197     ##
198     # Check for missing translations in an HTML response
199     def assert_no_missing_translations(msg = "")
200       assert_select "span[class=translation_missing]", false, "Missing translation #{msg}"
201     end
202
203     ##
204     # execute a block with a given set of HTTP responses stubbed
205     def with_http_stubs(stubs_file)
206       stubs = YAML.load_file(File.expand_path("../http/#{stubs_file}.yml", __FILE__))
207       stubs.each do |url, response|
208         stub_request(:get, Regexp.new(Regexp.quote(url))).to_return(:status => response["code"], :body => response["body"])
209       end
210
211       yield
212     end
213
214     def stub_gravatar_request(email, status = 200, body = nil)
215       hash = ::Digest::MD5.hexdigest(email.downcase)
216       url = "https://www.gravatar.com/avatar/#{hash}?d=404"
217       stub_request(:get, url).and_return(:status => status, :body => body)
218     end
219
220     def email_text_parts(message)
221       message.parts.each_with_object([]) do |part, text_parts|
222         if part.content_type.start_with?("text/")
223           text_parts.push(part)
224         elsif part.multipart?
225           text_parts.concat(email_text_parts(part))
226         end
227       end
228     end
229
230     def sign_in_as(user)
231       visit login_path
232       fill_in "username", :with => user.email
233       fill_in "password", :with => "test"
234       click_on "Login", :match => :first
235     end
236
237     def session_for(user)
238       post login_path, :params => { :username => user.display_name, :password => "test" }
239       follow_redirect!
240     end
241
242     def xml_for_node(node)
243       doc = OSM::API.new.get_xml_doc
244       doc.root << xml_node_for_node(node)
245       doc
246     end
247
248     def xml_node_for_node(node)
249       el = XML::Node.new "node"
250       el["id"] = node.id.to_s
251
252       OMHelper.add_metadata_to_xml_node(el, node, {}, {})
253
254       if node.visible?
255         el["lat"] = node.lat.to_s
256         el["lon"] = node.lon.to_s
257       end
258
259       OMHelper.add_tags_to_xml_node(el, node.node_tags)
260
261       el
262     end
263
264     def xml_for_way(way)
265       doc = OSM::API.new.get_xml_doc
266       doc.root << xml_node_for_way(way)
267       doc
268     end
269
270     def xml_node_for_way(way)
271       el = XML::Node.new "way"
272       el["id"] = way.id.to_s
273
274       OMHelper.add_metadata_to_xml_node(el, way, {}, {})
275
276       # make sure nodes are output in sequence_id order
277       ordered_nodes = []
278       way.way_nodes.each do |nd|
279         ordered_nodes[nd.sequence_id] = nd.node_id.to_s if nd.node&.visible?
280       end
281
282       ordered_nodes.each do |nd_id|
283         next unless nd_id && nd_id != "0"
284
285         node_el = XML::Node.new "nd"
286         node_el["ref"] = nd_id
287         el << node_el
288       end
289
290       OMHelper.add_tags_to_xml_node(el, way.way_tags)
291
292       el
293     end
294
295     def xml_for_relation(relation)
296       doc = OSM::API.new.get_xml_doc
297       doc.root << xml_node_for_relation(relation)
298       doc
299     end
300
301     def xml_node_for_relation(relation)
302       el = XML::Node.new "relation"
303       el["id"] = relation.id.to_s
304
305       OMHelper.add_metadata_to_xml_node(el, relation, {}, {})
306
307       relation.relation_members.each do |member|
308         member_el = XML::Node.new "member"
309         member_el["type"] = member.member_type.downcase
310         member_el["ref"] = member.member_id.to_s
311         member_el["role"] = member.member_role
312         el << member_el
313       end
314
315       OMHelper.add_tags_to_xml_node(el, relation.relation_tags)
316
317       el
318     end
319
320     class OMHelper
321       extend ObjectMetadata
322     end
323   end
324 end