]> git.openstreetmap.org Git - rails.git/blob - test/models/trace_test.rb
Merge remote-tracking branch 'openstreetmap/pull/1030' into next
[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   fixtures :users
7
8   def setup
9     @gpx_trace_dir = Object.send("remove_const", "GPX_TRACE_DIR")
10     Object.const_set("GPX_TRACE_DIR", Rails.root.join("test", "gpx", "traces"))
11
12     @gpx_image_dir = Object.send("remove_const", "GPX_IMAGE_DIR")
13     Object.const_set("GPX_IMAGE_DIR", Rails.root.join("test", "gpx", "images"))
14   end
15
16   def teardown
17     File.unlink(*Dir.glob(File.join(GPX_TRACE_DIR, "*.gpx")))
18     File.unlink(*Dir.glob(File.join(GPX_IMAGE_DIR, "*.gif")))
19
20     Object.send("remove_const", "GPX_TRACE_DIR")
21     Object.const_set("GPX_TRACE_DIR", @gpx_trace_dir)
22
23     Object.send("remove_const", "GPX_IMAGE_DIR")
24     Object.const_set("GPX_IMAGE_DIR", @gpx_image_dir)
25   end
26
27   def test_visible
28     public_trace_file = create(:trace)
29     create(:trace, :deleted)
30     check_query(Trace.visible, [public_trace_file])
31   end
32
33   def test_visible_to
34     public_trace_file = create(:trace, :visibility => "public", :user => users(:normal_user))
35     anon_trace_file = create(:trace, :visibility => "private", :user => users(:public_user))
36     identifiable_trace_file = create(:trace, :visibility => "identifiable", :user => users(:normal_user))
37     pending_trace_file = create(:trace, :visibility => "public", :user => users(:public_user), :inserted => false)
38     trackable_trace_file = create(:trace, :visibility => "trackable", :user => users(:public_user))
39     _other_trace_file = create(:trace, :visibility => "private", :user => users(:second_public_user))
40
41     check_query(Trace.visible_to(users(:normal_user).id), [
42                   public_trace_file, identifiable_trace_file, pending_trace_file
43                 ])
44     check_query(Trace.visible_to(users(:public_user)), [
45                   public_trace_file, anon_trace_file, trackable_trace_file,
46                   identifiable_trace_file, pending_trace_file
47                 ])
48     check_query(Trace.visible_to(users(:inactive_user)), [
49                   public_trace_file, identifiable_trace_file, pending_trace_file
50                 ])
51   end
52
53   def test_visible_to_all
54     public_trace_file = create(:trace, :visibility => "public")
55     _private_trace_file = create(:trace, :visibility => "private")
56     identifiable_trace_file = create(:trace, :visibility => "identifiable")
57     _trackable_trace_file = create(:trace, :visibility => "trackable")
58     deleted_trace_file = create(:trace, :deleted, :visibility => "public")
59     pending_trace_file = create(:trace, :visibility => "public", :inserted => false)
60
61     check_query(Trace.visible_to_all, [
62                   public_trace_file, identifiable_trace_file,
63                   deleted_trace_file, pending_trace_file
64                 ])
65   end
66
67   def test_tagged
68     london_trace_file = create(:trace) do |trace|
69       create(:tracetag, :trace => trace, :tag => "London")
70     end
71     birmingham_trace_file = create(:trace) do |trace|
72       create(:tracetag, :trace => trace, :tag => "Birmingham")
73     end
74     check_query(Trace.tagged("London"), [london_trace_file])
75     check_query(Trace.tagged("Birmingham"), [birmingham_trace_file])
76     check_query(Trace.tagged("Unknown"), [])
77   end
78
79   def test_validations
80     trace_valid({})
81     trace_valid({ :user_id => nil }, false)
82     trace_valid(:name => "a" * 255)
83     trace_valid({ :name => "a" * 256 }, false)
84     trace_valid({ :description => nil }, false)
85     trace_valid(:description => "a" * 255)
86     trace_valid({ :description => "a" * 256 }, false)
87     trace_valid(:visibility => "private")
88     trace_valid(:visibility => "public")
89     trace_valid(:visibility => "trackable")
90     trace_valid(:visibility => "identifiable")
91     trace_valid({ :visibility => "foo" }, false)
92   end
93
94   def test_tagstring
95     trace = build(:trace)
96     trace.tagstring = "foo bar baz"
97     assert trace.valid?
98     assert_equal 3, trace.tags.length
99     assert_equal "foo", trace.tags[0].tag
100     assert_equal "bar", trace.tags[1].tag
101     assert_equal "baz", trace.tags[2].tag
102     assert_equal "foo, bar, baz", trace.tagstring
103     trace.tagstring = "foo, bar baz ,qux"
104     assert trace.valid?
105     assert_equal 3, trace.tags.length
106     assert_equal "foo", trace.tags[0].tag
107     assert_equal "bar baz", trace.tags[1].tag
108     assert_equal "qux", trace.tags[2].tag
109     assert_equal "foo, bar baz, qux", trace.tagstring
110   end
111
112   def test_public?
113     assert_equal true, build(:trace, :visibility => "public").public?
114     assert_equal false, build(:trace, :visibility => "private").public?
115     assert_equal false, build(:trace, :visibility => "trackable").public?
116     assert_equal true, build(:trace, :visibility => "identifiable").public?
117     assert_equal true, build(:trace, :deleted, :visibility => "public").public?
118   end
119
120   def test_trackable?
121     assert_equal false, build(:trace, :visibility => "public").trackable?
122     assert_equal false, build(:trace, :visibility => "private").trackable?
123     assert_equal true, build(:trace, :visibility => "trackable").trackable?
124     assert_equal true, build(:trace, :visibility => "identifiable").trackable?
125     assert_equal false, build(:trace, :deleted, :visibility => "public").trackable?
126   end
127
128   def test_identifiable?
129     assert_equal false, build(:trace, :visibility => "public").identifiable?
130     assert_equal false, build(:trace, :visibility => "private").identifiable?
131     assert_equal false, build(:trace, :visibility => "trackable").identifiable?
132     assert_equal true, build(:trace, :visibility => "identifiable").identifiable?
133     assert_equal false, build(:trace, :deleted, :visibility => "public").identifiable?
134   end
135
136   def test_mime_type
137     # The ids refer to the .gpx fixtures in test/traces
138     check_mime_type("a", "application/gpx+xml")
139     check_mime_type("b", "application/gpx+xml")
140     check_mime_type("c", "application/x-bzip2")
141     check_mime_type("d", "application/x-gzip")
142     check_mime_type("f", "application/x-zip")
143     check_mime_type("g", "application/x-tar")
144     check_mime_type("h", "application/x-gzip")
145     check_mime_type("i", "application/x-bzip2")
146   end
147
148   def test_extension_name
149     # The ids refer to the .gpx fixtures in test/traces
150     check_extension_name("a", ".gpx")
151     check_extension_name("b", ".gpx")
152     check_extension_name("c", ".gpx.bz2")
153     check_extension_name("d", ".gpx.gz")
154     check_extension_name("f", ".zip")
155     check_extension_name("g", ".tar")
156     check_extension_name("h", ".tar.gz")
157     check_extension_name("i", ".tar.bz2")
158   end
159
160   def test_xml_file
161     check_xml_file("a", "848caa72f2f456d1bd6a0fdf228aa1b9")
162     check_xml_file("b", "66179ca44f1e93d8df62e2b88cbea732")
163     check_xml_file("c", "848caa72f2f456d1bd6a0fdf228aa1b9")
164     check_xml_file("d", "abd6675fdf3024a84fc0a1deac147c0d")
165     check_xml_file("f", "848caa72f2f456d1bd6a0fdf228aa1b9")
166     check_xml_file("g", "848caa72f2f456d1bd6a0fdf228aa1b9")
167     check_xml_file("h", "848caa72f2f456d1bd6a0fdf228aa1b9")
168     check_xml_file("i", "848caa72f2f456d1bd6a0fdf228aa1b9")
169   end
170
171   def test_large_picture
172     picture = File.read(Rails.root.join("test", "gpx", "fixtures", "a.gif"), :mode => "rb")
173
174     trace = Trace.create
175     trace.large_picture = picture
176     assert_equal "7c841749e084ee4a5d13f12cd3bef456", md5sum(File.new(trace.large_picture_name))
177     assert_equal picture, trace.large_picture
178
179     trace.destroy
180   end
181
182   def test_icon_picture
183     picture = File.read(Rails.root.join("test", "gpx", "fixtures", "a_icon.gif"), :mode => "rb")
184
185     trace = Trace.create
186     trace.icon_picture = picture
187     assert_equal "b47baf22ed0e85d77e808694fad0ee27", md5sum(File.new(trace.icon_picture_name))
188     assert_equal picture, trace.icon_picture
189
190     trace.destroy
191   end
192
193   private
194
195   def check_query(query, traces)
196     traces = traces.map(&:id).sort
197     assert_equal traces, query.order(:id).ids
198   end
199
200   def check_mime_type(id, mime_type)
201     assert_equal mime_type, create(:trace, :fixture => id).mime_type
202   end
203
204   def check_extension_name(id, extension_name)
205     assert_equal extension_name, create(:trace, :fixture => id).extension_name
206   end
207
208   def check_xml_file(id, md5sum)
209     assert_equal md5sum, md5sum(create(:trace, :fixture => id).xml_file)
210   end
211
212   def trace_valid(attrs, result = true)
213     entry = build(:trace)
214     entry.assign_attributes(attrs)
215     assert_equal result, entry.valid?, "Expected #{attrs.inspect} to be #{result}"
216   end
217
218   def md5sum(io)
219     io.each_with_object(Digest::MD5.new) { |l, d| d.update(l) }.hexdigest
220   end
221 end