1 # frozen_string_literal: true
6 class TraceTest < ActiveSupport::TestCase
8 public_trace_file = create(:trace)
9 create(:trace, :deleted)
10 check_query(Trace.visible, [public_trace_file])
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)
25 check_query(Trace.visible_to(first_user), [
26 public_trace_file, identifiable_trace_file, pending_trace_file
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
32 check_query(Trace.visible_to(fourth_user), [
33 public_trace_file, identifiable_trace_file, pending_trace_file
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)
45 check_query(Trace.visible_to_all, [
46 public_trace_file, identifiable_trace_file,
47 deleted_trace_file, pending_trace_file
52 london_trace_file = create(:trace) do |trace|
53 create(:tracetag, :trace => trace, :tag => "London")
55 birmingham_trace_file = create(:trace) do |trace|
56 create(:tracetag, :trace => trace, :tag => "Birmingham")
58 check_query(Trace.tagged("London"), [london_trace_file])
59 check_query(Trace.tagged("Birmingham"), [birmingham_trace_file])
60 check_query(Trace.tagged("Unknown"), [])
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)
79 def test_visibility_cannot_change_to_legacy
80 trace = create(:trace, :visibility => "trackable")
81 trace.visibility = "public"
82 assert_not_predicate trace, :valid?
84 trace = create(:trace, :visibility => "identifiable")
85 trace.visibility = "private"
86 assert_not_predicate trace, :valid?
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?
94 trace = create(:trace, :without_validations, :visibility => "private")
95 trace.visibility = "public"
96 assert_predicate trace, :valid?
99 def test_selectable_visibilities
100 trace = build(:trace, :visibility => "identifiable")
101 assert_equal %w[trackable identifiable], trace.selectable_visibilities
103 trace = build(:trace, :visibility => "public")
104 assert_equal %w[public trackable identifiable], trace.selectable_visibilities
106 trace = build(:trace, :visibility => "private")
107 assert_equal %w[private trackable identifiable], trace.selectable_visibilities
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
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
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
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?
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?
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?
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")
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")
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")
202 def test_large_picture
203 picture = Rails.root.join("test/gpx/fixtures/a.gif").read(:mode => "rb")
204 trace = create(:trace, :fixture => "a")
206 assert_equal picture, trace.large_picture
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")
213 assert_equal picture, trace.icon_picture
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
225 assert_equal 0, Tracepoint.where(:latitude => 54321).count
228 def test_import_creates_tracepoints
229 trace = create(:trace, :fixture => "a")
230 assert_equal 0, Tracepoint.where(:trace => trace).count
235 assert_equal 1, Tracepoint.where(:trace => trace).count
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
242 def test_import_creates_icon
243 trace = create(:trace, :inserted => false, :fixture => "a")
245 assert_not_predicate trace.icon, :attached?
249 assert_predicate trace.icon, :attached?
252 def test_import_creates_large_picture
253 trace = create(:trace, :inserted => false, :fixture => "a")
255 assert_not_predicate trace.image, :attached?
259 assert_predicate trace.image, :attached?
262 def test_import_handles_bz2
263 trace = create(:trace, :inserted => false, :fixture => "c")
267 assert_equal 1, trace.size
270 def test_import_handles_plain
271 trace = create(:trace, :inserted => false, :fixture => "a")
275 assert_equal 1, trace.size
278 def test_import_handles_plain_with_bom
279 trace = create(:trace, :inserted => false, :fixture => "b")
283 assert_equal 1, trace.size
286 def test_import_handles_gz
287 trace = create(:trace, :inserted => false, :fixture => "d")
291 assert_equal 1, trace.size
294 def test_import_handles_zip
295 trace = create(:trace, :inserted => false, :fixture => "f")
299 assert_equal 2, trace.size
302 def test_import_handles_tar
303 trace = create(:trace, :inserted => false, :fixture => "g")
307 assert_equal 2, trace.size
310 def test_import_handles_tar_gz
311 trace = create(:trace, :inserted => false, :fixture => "h")
315 assert_equal 2, trace.size
318 def test_import_handles_tar_bz2
319 trace = create(:trace, :inserted => false, :fixture => "i")
323 assert_equal 2, trace.size
326 def test_import_enforces_limit
327 trace = create(:trace, :inserted => false, :fixture => "f")
329 with_settings(:max_trace_size => 1) do
330 assert_raise GPX::FileTooBigError do
335 assert_not trace.inserted
340 def check_query(query, traces)
341 traces = traces.map(&:id).sort
342 assert_equal traces, query.order(:id).ids
345 def check_mime_type(id, mime_type)
346 assert_equal mime_type, create(:trace, :fixture => id).mime_type
349 def check_extension_name(id, extension_name)
350 assert_equal extension_name, create(:trace, :fixture => id).extension_name
353 def check_xml_file(id, md5sum)
354 assert_equal md5sum, md5sum(create(:trace, :fixture => id).xml_file)
357 def trace_valid(attrs, valid: true)
358 entry = build(:trace, attrs)
359 assert_equal valid, entry.valid?, "Expected #{attrs.inspect} to be #{valid}"
363 io.each_with_object(Digest::MD5.new) { |l, d| d.update(l) }.hexdigest