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