]> git.openstreetmap.org Git - rails.git/blob - test/models/trace_test.rb
Merge pull request #6394 from openstreetmap/dependabot/github_actions/ruby/setup...
[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, :visibility => "public", :user => first_user)
19     anon_trace_file = create(:trace, :visibility => "private", :user => second_user)
20     identifiable_trace_file = create(:trace, :visibility => "identifiable", :user => first_user)
21     pending_trace_file = create(:trace, :visibility => "public", :user => second_user, :inserted => false)
22     trackable_trace_file = create(:trace, :visibility => "trackable", :user => second_user)
23     _other_trace_file = create(:trace, :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, :visibility => "public")
39     _private_trace_file = create(:trace, :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, :visibility => "public")
43     pending_trace_file = create(:trace, :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 => "private" })
72     trace_valid({ :visibility => "public" })
73     trace_valid({ :visibility => "trackable" })
74     trace_valid({ :visibility => "identifiable" })
75     trace_valid({ :visibility => "foo" }, :valid => false)
76   end
77
78   def test_tagstring_handles_space_separated_tags
79     trace = build(:trace)
80     trace.tagstring = "foo bar baz"
81     assert_predicate trace, :valid?
82     assert_equal 3, trace.tags.length
83     assert_equal "foo", trace.tags[0].tag
84     assert_equal "bar", trace.tags[1].tag
85     assert_equal "baz", trace.tags[2].tag
86     assert_equal "foo, bar, baz", trace.tagstring
87   end
88
89   def test_tagstring_handles_comma_separated_tags
90     trace = build(:trace)
91     trace.tagstring = "foo, bar baz ,qux"
92     assert_predicate 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_tagstring_strips_whitespace
101     trace = build(:trace)
102     trace.tagstring = "   zero  ,  one , two  "
103     assert_predicate trace, :valid?
104     assert_equal 3, trace.tags.length
105     assert_equal "zero", trace.tags[0].tag
106     assert_equal "one", trace.tags[1].tag
107     assert_equal "two", trace.tags[2].tag
108     assert_equal "zero, one, two", trace.tagstring
109   end
110
111   def test_public?
112     assert_predicate build(:trace, :visibility => "public"), :public?
113     assert_not_predicate build(:trace, :visibility => "private"), :public?
114     assert_not_predicate build(:trace, :visibility => "trackable"), :public?
115     assert_predicate build(:trace, :visibility => "identifiable"), :public?
116     assert_predicate build(:trace, :deleted, :visibility => "public"), :public?
117   end
118
119   def test_trackable?
120     assert_not_predicate build(:trace, :visibility => "public"), :trackable?
121     assert_not_predicate build(:trace, :visibility => "private"), :trackable?
122     assert_predicate build(:trace, :visibility => "trackable"), :trackable?
123     assert_predicate build(:trace, :visibility => "identifiable"), :trackable?
124     assert_not_predicate build(:trace, :deleted, :visibility => "public"), :trackable?
125   end
126
127   def test_identifiable?
128     assert_not_predicate build(:trace, :visibility => "public"), :identifiable?
129     assert_not_predicate build(:trace, :visibility => "private"), :identifiable?
130     assert_not_predicate build(:trace, :visibility => "trackable"), :identifiable?
131     assert_predicate build(:trace, :visibility => "identifiable"), :identifiable?
132     assert_not_predicate build(:trace, :deleted, :visibility => "public"), :identifiable?
133   end
134
135   def test_mime_type
136     # The ids refer to the .gpx fixtures in test/traces
137     check_mime_type("a", "application/gpx+xml")
138     check_mime_type("b", "application/gpx+xml")
139     check_mime_type("c", "application/x-bzip2")
140     check_mime_type("d", "application/gzip")
141     check_mime_type("f", "application/zip")
142     check_mime_type("g", "application/x-tar")
143     check_mime_type("h", "application/x-tar+gzip")
144     check_mime_type("i", "application/x-tar+x-bzip2")
145   end
146
147   def test_extension_name
148     # The ids refer to the .gpx fixtures in test/traces
149     check_extension_name("a", ".gpx")
150     check_extension_name("b", ".gpx")
151     check_extension_name("c", ".gpx.bz2")
152     check_extension_name("d", ".gpx.gz")
153     check_extension_name("f", ".zip")
154     check_extension_name("g", ".tar")
155     check_extension_name("h", ".tar.gz")
156     check_extension_name("i", ".tar.bz2")
157   end
158
159   def test_xml_file
160     check_xml_file("a", "848caa72f2f456d1bd6a0fdf228aa1b9")
161     check_xml_file("b", "db4cb5ed2d7d2b627b3b504296c4f701")
162     check_xml_file("c", "848caa72f2f456d1bd6a0fdf228aa1b9")
163     check_xml_file("d", "abd6675fdf3024a84fc0a1deac147c0d")
164     check_xml_file("f", "a7c05d676c77dc14369c21be216a3713")
165     check_xml_file("g", "a7c05d676c77dc14369c21be216a3713")
166     check_xml_file("h", "a7c05d676c77dc14369c21be216a3713")
167     check_xml_file("i", "a7c05d676c77dc14369c21be216a3713")
168   end
169
170   def test_large_picture
171     picture = Rails.root.join("test/gpx/fixtures/a.gif").read(:mode => "rb")
172     trace = create(:trace, :fixture => "a")
173
174     assert_equal picture, trace.large_picture
175   end
176
177   def test_icon_picture
178     picture = Rails.root.join("test/gpx/fixtures/a_icon.gif").read(:mode => "rb")
179     trace = create(:trace, :fixture => "a")
180
181     assert_equal picture, trace.icon_picture
182   end
183
184   def test_import_removes_previous_tracepoints
185     trace = create(:trace, :fixture => "a")
186     # Tracepoints don't have a primary key, so we use a specific latitude to
187     # check for successful deletion
188     create(:tracepoint, :latitude => 54321, :trace => trace)
189     assert_equal 1, Tracepoint.where(:latitude => 54321).count
190
191     trace.import
192
193     assert_equal 0, Tracepoint.where(:latitude => 54321).count
194   end
195
196   def test_import_creates_tracepoints
197     trace = create(:trace, :fixture => "a")
198     assert_equal 0, Tracepoint.where(:trace => trace).count
199
200     trace.import
201
202     trace.reload
203     assert_equal 1, Tracepoint.where(:trace => trace).count
204
205     # Check that the tile has been set prior to the bulk import
206     # i.e. that the callbacks have been run correctly
207     assert_equal 3221331576, Tracepoint.find_by(:trace => trace).tile
208   end
209
210   def test_import_creates_icon
211     trace = create(:trace, :inserted => false, :fixture => "a")
212
213     assert_not_predicate trace.icon, :attached?
214
215     trace.import
216
217     assert_predicate trace.icon, :attached?
218   end
219
220   def test_import_creates_large_picture
221     trace = create(:trace, :inserted => false, :fixture => "a")
222
223     assert_not_predicate trace.image, :attached?
224
225     trace.import
226
227     assert_predicate trace.image, :attached?
228   end
229
230   def test_import_handles_bz2
231     trace = create(:trace, :inserted => false, :fixture => "c")
232
233     trace.import
234
235     assert_equal 1, trace.size
236   end
237
238   def test_import_handles_plain
239     trace = create(:trace, :inserted => false, :fixture => "a")
240
241     trace.import
242
243     assert_equal 1, trace.size
244   end
245
246   def test_import_handles_plain_with_bom
247     trace = create(:trace, :inserted => false, :fixture => "b")
248
249     trace.import
250
251     assert_equal 1, trace.size
252   end
253
254   def test_import_handles_gz
255     trace = create(:trace, :inserted => false, :fixture => "d")
256
257     trace.import
258
259     assert_equal 1, trace.size
260   end
261
262   def test_import_handles_zip
263     trace = create(:trace, :inserted => false, :fixture => "f")
264
265     trace.import
266
267     assert_equal 2, trace.size
268   end
269
270   def test_import_handles_tar
271     trace = create(:trace, :inserted => false, :fixture => "g")
272
273     trace.import
274
275     assert_equal 2, trace.size
276   end
277
278   def test_import_handles_tar_gz
279     trace = create(:trace, :inserted => false, :fixture => "h")
280
281     trace.import
282
283     assert_equal 2, trace.size
284   end
285
286   def test_import_handles_tar_bz2
287     trace = create(:trace, :inserted => false, :fixture => "i")
288
289     trace.import
290
291     assert_equal 2, trace.size
292   end
293
294   def test_import_enforces_limit
295     trace = create(:trace, :inserted => false, :fixture => "f")
296
297     with_settings(:max_trace_size => 1) do
298       assert_raise GPX::FileTooBigError do
299         trace.import
300       end
301     end
302
303     assert_not trace.inserted
304   end
305
306   private
307
308   def check_query(query, traces)
309     traces = traces.map(&:id).sort
310     assert_equal traces, query.order(:id).ids
311   end
312
313   def check_mime_type(id, mime_type)
314     assert_equal mime_type, create(:trace, :fixture => id).mime_type
315   end
316
317   def check_extension_name(id, extension_name)
318     assert_equal extension_name, create(:trace, :fixture => id).extension_name
319   end
320
321   def check_xml_file(id, md5sum)
322     assert_equal md5sum, md5sum(create(:trace, :fixture => id).xml_file)
323   end
324
325   def trace_valid(attrs, valid: true)
326     entry = build(:trace, attrs)
327     assert_equal valid, entry.valid?, "Expected #{attrs.inspect} to be #{valid}"
328   end
329
330   def md5sum(io)
331     io.each_with_object(Digest::MD5.new) { |l, d| d.update(l) }.hexdigest
332   end
333 end