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