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