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