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