]> git.openstreetmap.org Git - rails.git/blob - test/models/trace_test.rb
2ab1a3d5a4da50c3e770b1183c30d0b5de2f6c07
[rails.git] / test / models / trace_test.rb
1 require "test_helper"
2 require "minitest/mock"
3
4 class TraceTest < ActiveSupport::TestCase
5   def setup
6     @gpx_trace_dir = Object.send("remove_const", "GPX_TRACE_DIR")
7     Object.const_set("GPX_TRACE_DIR", Rails.root.join("test", "gpx", "traces"))
8
9     @gpx_image_dir = Object.send("remove_const", "GPX_IMAGE_DIR")
10     Object.const_set("GPX_IMAGE_DIR", Rails.root.join("test", "gpx", "images"))
11   end
12
13   def teardown
14     File.unlink(*Dir.glob(File.join(GPX_TRACE_DIR, "*.gpx")))
15     File.unlink(*Dir.glob(File.join(GPX_IMAGE_DIR, "*.gif")))
16
17     Object.send("remove_const", "GPX_TRACE_DIR")
18     Object.const_set("GPX_TRACE_DIR", @gpx_trace_dir)
19
20     Object.send("remove_const", "GPX_IMAGE_DIR")
21     Object.const_set("GPX_IMAGE_DIR", @gpx_image_dir)
22   end
23
24   def test_visible
25     public_trace_file = create(:trace)
26     create(:trace, :deleted)
27     check_query(Trace.visible, [public_trace_file])
28   end
29
30   def test_visible_to
31     first_user = create(:user)
32     second_user = create(:user)
33     third_user = create(:user)
34     fourth_user = create(:user)
35     public_trace_file = create(:trace, :visibility => "public", :user => first_user)
36     anon_trace_file = create(:trace, :visibility => "private", :user => second_user)
37     identifiable_trace_file = create(:trace, :visibility => "identifiable", :user => first_user)
38     pending_trace_file = create(:trace, :visibility => "public", :user => second_user, :inserted => false)
39     trackable_trace_file = create(:trace, :visibility => "trackable", :user => second_user)
40     _other_trace_file = create(:trace, :visibility => "private", :user => third_user)
41
42     check_query(Trace.visible_to(first_user), [
43                   public_trace_file, identifiable_trace_file, pending_trace_file
44                 ])
45     check_query(Trace.visible_to(second_user), [
46                   public_trace_file, anon_trace_file, trackable_trace_file,
47                   identifiable_trace_file, pending_trace_file
48                 ])
49     check_query(Trace.visible_to(fourth_user), [
50                   public_trace_file, identifiable_trace_file, pending_trace_file
51                 ])
52   end
53
54   def test_visible_to_all
55     public_trace_file = create(:trace, :visibility => "public")
56     _private_trace_file = create(:trace, :visibility => "private")
57     identifiable_trace_file = create(:trace, :visibility => "identifiable")
58     _trackable_trace_file = create(:trace, :visibility => "trackable")
59     deleted_trace_file = create(:trace, :deleted, :visibility => "public")
60     pending_trace_file = create(:trace, :visibility => "public", :inserted => false)
61
62     check_query(Trace.visible_to_all, [
63                   public_trace_file, identifiable_trace_file,
64                   deleted_trace_file, pending_trace_file
65                 ])
66   end
67
68   def test_tagged
69     london_trace_file = create(:trace) do |trace|
70       create(:tracetag, :trace => trace, :tag => "London")
71     end
72     birmingham_trace_file = create(:trace) do |trace|
73       create(:tracetag, :trace => trace, :tag => "Birmingham")
74     end
75     check_query(Trace.tagged("London"), [london_trace_file])
76     check_query(Trace.tagged("Birmingham"), [birmingham_trace_file])
77     check_query(Trace.tagged("Unknown"), [])
78   end
79
80   def test_validations
81     trace_valid({})
82     trace_valid({ :user_id => nil }, false)
83     trace_valid(:name => "a" * 255)
84     trace_valid({ :name => "a" * 256 }, false)
85     trace_valid({ :description => nil }, false)
86     trace_valid(:description => "a" * 255)
87     trace_valid({ :description => "a" * 256 }, false)
88     trace_valid(:visibility => "private")
89     trace_valid(:visibility => "public")
90     trace_valid(:visibility => "trackable")
91     trace_valid(:visibility => "identifiable")
92     trace_valid({ :visibility => "foo" }, false)
93   end
94
95   def test_tagstring
96     trace = build(:trace)
97     trace.tagstring = "foo bar baz"
98     assert trace.valid?
99     assert_equal 3, trace.tags.length
100     assert_equal "foo", trace.tags[0].tag
101     assert_equal "bar", trace.tags[1].tag
102     assert_equal "baz", trace.tags[2].tag
103     assert_equal "foo, bar, baz", trace.tagstring
104     trace.tagstring = "foo, bar baz ,qux"
105     assert trace.valid?
106     assert_equal 3, trace.tags.length
107     assert_equal "foo", trace.tags[0].tag
108     assert_equal "bar baz", trace.tags[1].tag
109     assert_equal "qux", trace.tags[2].tag
110     assert_equal "foo, bar baz, qux", trace.tagstring
111   end
112
113   def test_public?
114     assert_equal true, build(:trace, :visibility => "public").public?
115     assert_equal false, build(:trace, :visibility => "private").public?
116     assert_equal false, build(:trace, :visibility => "trackable").public?
117     assert_equal true, build(:trace, :visibility => "identifiable").public?
118     assert_equal true, build(:trace, :deleted, :visibility => "public").public?
119   end
120
121   def test_trackable?
122     assert_equal false, build(:trace, :visibility => "public").trackable?
123     assert_equal false, build(:trace, :visibility => "private").trackable?
124     assert_equal true, build(:trace, :visibility => "trackable").trackable?
125     assert_equal true, build(:trace, :visibility => "identifiable").trackable?
126     assert_equal false, build(:trace, :deleted, :visibility => "public").trackable?
127   end
128
129   def test_identifiable?
130     assert_equal false, build(:trace, :visibility => "public").identifiable?
131     assert_equal false, build(:trace, :visibility => "private").identifiable?
132     assert_equal false, build(:trace, :visibility => "trackable").identifiable?
133     assert_equal true, build(:trace, :visibility => "identifiable").identifiable?
134     assert_equal false, build(:trace, :deleted, :visibility => "public").identifiable?
135   end
136
137   def test_mime_type
138     # The ids refer to the .gpx fixtures in test/traces
139     check_mime_type("a", "application/gpx+xml")
140     check_mime_type("b", "application/gpx+xml")
141     check_mime_type("c", "application/x-bzip2")
142     check_mime_type("d", "application/x-gzip")
143     check_mime_type("f", "application/x-zip")
144     check_mime_type("g", "application/x-tar")
145     check_mime_type("h", "application/x-gzip")
146     check_mime_type("i", "application/x-bzip2")
147   end
148
149   def test_extension_name
150     # The ids refer to the .gpx fixtures in test/traces
151     check_extension_name("a", ".gpx")
152     check_extension_name("b", ".gpx")
153     check_extension_name("c", ".gpx.bz2")
154     check_extension_name("d", ".gpx.gz")
155     check_extension_name("f", ".zip")
156     check_extension_name("g", ".tar")
157     check_extension_name("h", ".tar.gz")
158     check_extension_name("i", ".tar.bz2")
159   end
160
161   def test_xml_file
162     check_xml_file("a", "848caa72f2f456d1bd6a0fdf228aa1b9")
163     check_xml_file("b", "66179ca44f1e93d8df62e2b88cbea732")
164     check_xml_file("c", "848caa72f2f456d1bd6a0fdf228aa1b9")
165     check_xml_file("d", "abd6675fdf3024a84fc0a1deac147c0d")
166     check_xml_file("f", "848caa72f2f456d1bd6a0fdf228aa1b9")
167     check_xml_file("g", "848caa72f2f456d1bd6a0fdf228aa1b9")
168     check_xml_file("h", "848caa72f2f456d1bd6a0fdf228aa1b9")
169     check_xml_file("i", "848caa72f2f456d1bd6a0fdf228aa1b9")
170   end
171
172   def test_large_picture
173     picture = File.read(Rails.root.join("test", "gpx", "fixtures", "a.gif"), :mode => "rb")
174
175     trace = Trace.create
176     trace.large_picture = picture
177     assert_equal "7c841749e084ee4a5d13f12cd3bef456", md5sum(File.new(trace.large_picture_name))
178     assert_equal picture, trace.large_picture
179
180     trace.destroy
181   end
182
183   def test_icon_picture
184     picture = File.read(Rails.root.join("test", "gpx", "fixtures", "a_icon.gif"), :mode => "rb")
185
186     trace = Trace.create
187     trace.icon_picture = picture
188     assert_equal "b47baf22ed0e85d77e808694fad0ee27", md5sum(File.new(trace.icon_picture_name))
189     assert_equal picture, trace.icon_picture
190
191     trace.destroy
192   end
193
194   # When testing the trace.import method, care needs to be taken regarding the icon
195   # fixture files, since the fixtures could be overwritten by newly generated files.
196   # We use FakeFS to temporarily protect the real fixture files from being overwritten.
197
198   def test_import_removes_previous_tracepoints
199     FakeFS do
200       FakeFS::FileSystem.clone(Rails.root.join("test", "gpx"))
201       trace = create(:trace, :fixture => "a")
202       # Tracepoints don't have a primary key, so we use a specific latitude to
203       # check for successful deletion
204       create(:tracepoint, :latitude => 54321, :trace => trace)
205       assert_equal 1, Tracepoint.where(:latitude => 54321).count
206
207       trace.import
208
209       assert_equal 0, Tracepoint.where(:latitude => 54321).count
210     end
211   end
212
213   def test_import_creates_tracepoints
214     FakeFS do
215       FakeFS::FileSystem.clone(Rails.root.join("test", "gpx"))
216       trace = create(:trace, :fixture => "a")
217       assert_equal 0, Tracepoint.where(:gpx_id => trace.id).count
218
219       trace.import
220
221       trace.reload
222       assert_equal 1, Tracepoint.where(:gpx_id => trace.id).count
223     end
224   end
225
226   def test_import_creates_icon
227     FakeFS do
228       FakeFS::FileSystem.clone(Rails.root.join("test", "gpx"))
229       trace = create(:trace, :fixture => "a")
230       icon_path = File.join(GPX_IMAGE_DIR, "#{trace.id}_icon.gif")
231       FileUtils.rm(icon_path)
232       assert_equal false, File.exist?(icon_path)
233
234       trace.import
235
236       assert_equal true, File.exist?(icon_path)
237     end
238   end
239
240   def test_import_creates_large_picture
241     FakeFS do
242       FakeFS::FileSystem.clone(Rails.root.join("test", "gpx"))
243       trace = create(:trace, :fixture => "a")
244       large_picture_path = File.join(GPX_IMAGE_DIR, "#{trace.id}.gif")
245       FileUtils.rm(large_picture_path)
246       assert_equal false, File.exist?(large_picture_path)
247
248       trace.import
249
250       assert_equal true, File.exist?(large_picture_path)
251     end
252   end
253
254   private
255
256   def check_query(query, traces)
257     traces = traces.map(&:id).sort
258     assert_equal traces, query.order(:id).ids
259   end
260
261   def check_mime_type(id, mime_type)
262     assert_equal mime_type, create(:trace, :fixture => id).mime_type
263   end
264
265   def check_extension_name(id, extension_name)
266     assert_equal extension_name, create(:trace, :fixture => id).extension_name
267   end
268
269   def check_xml_file(id, md5sum)
270     assert_equal md5sum, md5sum(create(:trace, :fixture => id).xml_file)
271   end
272
273   def trace_valid(attrs, result = true)
274     entry = build(:trace)
275     entry.assign_attributes(attrs)
276     assert_equal result, entry.valid?, "Expected #{attrs.inspect} to be #{result}"
277   end
278
279   def md5sum(io)
280     io.each_with_object(Digest::MD5.new) { |l, d| d.update(l) }.hexdigest
281   end
282 end