]> git.openstreetmap.org Git - rails.git/blob - test/models/trace_test.rb
Merge pull request #7302 from tomhughes/terms-locale
[rails.git] / test / models / trace_test.rb
1 # frozen_string_literal: true
2
3 require "test_helper"
4 require "gpx"
5
6 class TraceTest < ActiveSupport::TestCase
7   def test_visible
8     public_trace_file = create(:trace)
9     create(:trace, :deleted)
10     check_query(Trace.visible, [public_trace_file])
11   end
12
13   def test_visible_to
14     first_user = create(:user)
15     second_user = create(:user)
16     third_user = create(:user)
17     fourth_user = create(:user)
18     public_trace_file = create(:trace, :without_validations, :visibility => "public", :user => first_user)
19     anon_trace_file = create(:trace, :without_validations, :visibility => "private", :user => second_user)
20     identifiable_trace_file = create(:trace, :visibility => "identifiable", :user => first_user)
21     pending_trace_file = create(:trace, :without_validations, :visibility => "public", :user => second_user, :inserted => false)
22     trackable_trace_file = create(:trace, :visibility => "trackable", :user => second_user)
23     _other_trace_file = create(:trace, :without_validations, :visibility => "private", :user => third_user)
24
25     check_query(Trace.visible_to(first_user), [
26                   public_trace_file, identifiable_trace_file, pending_trace_file
27                 ])
28     check_query(Trace.visible_to(second_user), [
29                   public_trace_file, anon_trace_file, trackable_trace_file,
30                   identifiable_trace_file, pending_trace_file
31                 ])
32     check_query(Trace.visible_to(fourth_user), [
33                   public_trace_file, identifiable_trace_file, pending_trace_file
34                 ])
35   end
36
37   def test_visible_to_all
38     public_trace_file = create(:trace, :without_validations, :visibility => "public")
39     _private_trace_file = create(:trace, :without_validations, :visibility => "private")
40     identifiable_trace_file = create(:trace, :visibility => "identifiable")
41     _trackable_trace_file = create(:trace, :visibility => "trackable")
42     deleted_trace_file = create(:trace, :deleted, :without_validations, :visibility => "public")
43     pending_trace_file = create(:trace, :without_validations, :visibility => "public", :inserted => false)
44
45     check_query(Trace.visible_to_all, [
46                   public_trace_file, identifiable_trace_file,
47                   deleted_trace_file, pending_trace_file
48                 ])
49   end
50
51   def test_tagged
52     london_trace_file = create(:trace) do |trace|
53       create(:tracetag, :trace => trace, :tag => "London")
54     end
55     birmingham_trace_file = create(:trace) do |trace|
56       create(:tracetag, :trace => trace, :tag => "Birmingham")
57     end
58     check_query(Trace.tagged("London"), [london_trace_file])
59     check_query(Trace.tagged("Birmingham"), [birmingham_trace_file])
60     check_query(Trace.tagged("Unknown"), [])
61   end
62
63   def test_validations
64     trace_valid({})
65     trace_valid({ :user_id => nil }, :valid => false)
66     trace_valid({ :name => "a" * 255 })
67     trace_valid({ :name => "a" * 256 }, :valid => false)
68     trace_valid({ :description => nil }, :valid => false)
69     trace_valid({ :description => "a" * 255 })
70     trace_valid({ :description => "a" * 256 }, :valid => false)
71     trace_valid({ :visibility => "trackable" })
72     trace_valid({ :visibility => "identifiable" })
73     # Legacy visibilities are no longer allowed on new traces
74     trace_valid({ :visibility => "private" }, :valid => false)
75     trace_valid({ :visibility => "public" }, :valid => false)
76     trace_valid({ :visibility => "foo" }, :valid => false)
77   end
78
79   def test_visibility_cannot_change_to_legacy
80     trace = create(:trace, :visibility => "trackable")
81     trace.visibility = "public"
82     assert_not_predicate trace, :valid?
83
84     trace = create(:trace, :visibility => "identifiable")
85     trace.visibility = "private"
86     assert_not_predicate trace, :valid?
87   end
88
89   def test_visibility_can_change_from_legacy
90     trace = create(:trace, :without_validations, :visibility => "public")
91     trace.visibility = "trackable"
92     assert_predicate trace, :valid?
93
94     trace = create(:trace, :without_validations, :visibility => "private")
95     trace.visibility = "public"
96     assert_predicate trace, :valid?
97   end
98
99   def test_selectable_visibilities
100     trace = build(:trace, :visibility => "identifiable")
101     assert_equal %w[trackable identifiable], trace.selectable_visibilities
102
103     trace = build(:trace, :visibility => "public")
104     assert_equal %w[public trackable identifiable], trace.selectable_visibilities
105
106     trace = build(:trace, :visibility => "private")
107     assert_equal %w[private trackable identifiable], trace.selectable_visibilities
108   end
109
110   def test_tagstring_handles_space_separated_tags
111     trace = build(:trace)
112     trace.tagstring = "foo bar baz"
113     assert_predicate trace, :valid?
114     assert_equal 3, trace.tags.length
115     assert_equal "foo", trace.tags[0].tag
116     assert_equal "bar", trace.tags[1].tag
117     assert_equal "baz", trace.tags[2].tag
118     assert_equal "foo, bar, baz", trace.tagstring
119   end
120
121   def test_tagstring_handles_comma_separated_tags
122     trace = build(:trace)
123     trace.tagstring = "foo, bar baz ,qux"
124     assert_predicate trace, :valid?
125     assert_equal 3, trace.tags.length
126     assert_equal "foo", trace.tags[0].tag
127     assert_equal "bar baz", trace.tags[1].tag
128     assert_equal "qux", trace.tags[2].tag
129     assert_equal "foo, bar baz, qux", trace.tagstring
130   end
131
132   def test_tagstring_strips_whitespace
133     trace = build(:trace)
134     trace.tagstring = "   zero  ,  one , two  "
135     assert_predicate trace, :valid?
136     assert_equal 3, trace.tags.length
137     assert_equal "zero", trace.tags[0].tag
138     assert_equal "one", trace.tags[1].tag
139     assert_equal "two", trace.tags[2].tag
140     assert_equal "zero, one, two", trace.tagstring
141   end
142
143   def test_public?
144     assert_predicate build(:trace, :visibility => "public"), :public?
145     assert_not_predicate build(:trace, :visibility => "private"), :public?
146     assert_not_predicate build(:trace, :visibility => "trackable"), :public?
147     assert_predicate build(:trace, :visibility => "identifiable"), :public?
148     assert_predicate build(:trace, :deleted, :visibility => "public"), :public?
149   end
150
151   def test_trackable?
152     assert_not_predicate build(:trace, :visibility => "public"), :trackable?
153     assert_not_predicate build(:trace, :visibility => "private"), :trackable?
154     assert_predicate build(:trace, :visibility => "trackable"), :trackable?
155     assert_predicate build(:trace, :visibility => "identifiable"), :trackable?
156     assert_not_predicate build(:trace, :deleted, :visibility => "public"), :trackable?
157   end
158
159   def test_identifiable?
160     assert_not_predicate build(:trace, :visibility => "public"), :identifiable?
161     assert_not_predicate build(:trace, :visibility => "private"), :identifiable?
162     assert_not_predicate build(:trace, :visibility => "trackable"), :identifiable?
163     assert_predicate build(:trace, :visibility => "identifiable"), :identifiable?
164     assert_not_predicate build(:trace, :deleted, :visibility => "public"), :identifiable?
165   end
166
167   def test_mime_type
168     # The ids refer to the .gpx fixtures in test/traces
169     check_mime_type("a", "application/gpx+xml")
170     check_mime_type("b", "application/gpx+xml")
171     check_mime_type("c", "application/x-bzip2")
172     check_mime_type("d", "application/gzip")
173     check_mime_type("f", "application/zip")
174     check_mime_type("g", "application/x-tar")
175     check_mime_type("h", "application/x-tar+gzip")
176     check_mime_type("i", "application/x-tar+x-bzip2")
177   end
178
179   def test_extension_name
180     # The ids refer to the .gpx fixtures in test/traces
181     check_extension_name("a", ".gpx")
182     check_extension_name("b", ".gpx")
183     check_extension_name("c", ".gpx.bz2")
184     check_extension_name("d", ".gpx.gz")
185     check_extension_name("f", ".zip")
186     check_extension_name("g", ".tar")
187     check_extension_name("h", ".tar.gz")
188     check_extension_name("i", ".tar.bz2")
189   end
190
191   def test_xml_file
192     check_xml_file("a", "848caa72f2f456d1bd6a0fdf228aa1b9")
193     check_xml_file("b", "db4cb5ed2d7d2b627b3b504296c4f701")
194     check_xml_file("c", "848caa72f2f456d1bd6a0fdf228aa1b9")
195     check_xml_file("d", "abd6675fdf3024a84fc0a1deac147c0d")
196     check_xml_file("f", "a7c05d676c77dc14369c21be216a3713")
197     check_xml_file("g", "a7c05d676c77dc14369c21be216a3713")
198     check_xml_file("h", "a7c05d676c77dc14369c21be216a3713")
199     check_xml_file("i", "a7c05d676c77dc14369c21be216a3713")
200   end
201
202   def test_large_picture
203     picture = Rails.root.join("test/gpx/fixtures/a.gif").read(:mode => "rb")
204     trace = create(:trace, :fixture => "a")
205
206     assert_equal picture, trace.large_picture
207   end
208
209   def test_icon_picture
210     picture = Rails.root.join("test/gpx/fixtures/a_icon.gif").read(:mode => "rb")
211     trace = create(:trace, :fixture => "a")
212
213     assert_equal picture, trace.icon_picture
214   end
215
216   def test_import_removes_previous_tracepoints
217     trace = create(:trace, :fixture => "a")
218     # Tracepoints don't have a primary key, so we use a specific latitude to
219     # check for successful deletion
220     create(:tracepoint, :latitude => 54321, :trace => trace)
221     assert_equal 1, Tracepoint.where(:latitude => 54321).count
222
223     trace.import
224
225     assert_equal 0, Tracepoint.where(:latitude => 54321).count
226   end
227
228   def test_import_creates_tracepoints
229     trace = create(:trace, :fixture => "a")
230     assert_equal 0, Tracepoint.where(:trace => trace).count
231
232     trace.import
233
234     trace.reload
235     assert_equal 1, Tracepoint.where(:trace => trace).count
236
237     # Check that the tile has been set prior to the bulk import
238     # i.e. that the callbacks have been run correctly
239     assert_equal 3221331576, Tracepoint.find_by(:trace => trace).tile
240   end
241
242   def test_import_creates_icon
243     trace = create(:trace, :inserted => false, :fixture => "a")
244
245     assert_not_predicate trace.icon, :attached?
246
247     trace.import
248
249     assert_predicate trace.icon, :attached?
250   end
251
252   def test_import_creates_large_picture
253     trace = create(:trace, :inserted => false, :fixture => "a")
254
255     assert_not_predicate trace.image, :attached?
256
257     trace.import
258
259     assert_predicate trace.image, :attached?
260   end
261
262   def test_import_handles_bz2
263     trace = create(:trace, :inserted => false, :fixture => "c")
264
265     trace.import
266
267     assert_equal 1, trace.size
268   end
269
270   def test_import_handles_plain
271     trace = create(:trace, :inserted => false, :fixture => "a")
272
273     trace.import
274
275     assert_equal 1, trace.size
276   end
277
278   def test_import_handles_plain_with_bom
279     trace = create(:trace, :inserted => false, :fixture => "b")
280
281     trace.import
282
283     assert_equal 1, trace.size
284   end
285
286   def test_import_handles_gz
287     trace = create(:trace, :inserted => false, :fixture => "d")
288
289     trace.import
290
291     assert_equal 1, trace.size
292   end
293
294   def test_import_handles_zip
295     trace = create(:trace, :inserted => false, :fixture => "f")
296
297     trace.import
298
299     assert_equal 2, trace.size
300   end
301
302   def test_import_handles_tar
303     trace = create(:trace, :inserted => false, :fixture => "g")
304
305     trace.import
306
307     assert_equal 2, trace.size
308   end
309
310   def test_import_handles_tar_gz
311     trace = create(:trace, :inserted => false, :fixture => "h")
312
313     trace.import
314
315     assert_equal 2, trace.size
316   end
317
318   def test_import_handles_tar_bz2
319     trace = create(:trace, :inserted => false, :fixture => "i")
320
321     trace.import
322
323     assert_equal 2, trace.size
324   end
325
326   def test_import_enforces_limit
327     trace = create(:trace, :inserted => false, :fixture => "f")
328
329     with_settings(:max_trace_size => 1) do
330       assert_raise GPX::FileTooBigError do
331         trace.import
332       end
333     end
334
335     assert_not trace.inserted
336   end
337
338   private
339
340   def check_query(query, traces)
341     traces = traces.map(&:id).sort
342     assert_equal traces, query.order(:id).ids
343   end
344
345   def check_mime_type(id, mime_type)
346     assert_equal mime_type, create(:trace, :fixture => id).mime_type
347   end
348
349   def check_extension_name(id, extension_name)
350     assert_equal extension_name, create(:trace, :fixture => id).extension_name
351   end
352
353   def check_xml_file(id, md5sum)
354     assert_equal md5sum, md5sum(create(:trace, :fixture => id).xml_file)
355   end
356
357   def trace_valid(attrs, valid: true)
358     entry = build(:trace, attrs)
359     assert_equal valid, entry.valid?, "Expected #{attrs.inspect} to be #{valid}"
360   end
361
362   def md5sum(io)
363     io.each_with_object(Digest::MD5.new) { |l, d| d.update(l) }.hexdigest
364   end
365 end