]> git.openstreetmap.org Git - rails.git/blob - test/controllers/api/old_ways_controller_test.rb
b00c43ca70fcf63d76bd56014154b49b189cbfb1
[rails.git] / test / controllers / api / old_ways_controller_test.rb
1 require "test_helper"
2
3 module Api
4   class OldWaysControllerTest < ActionDispatch::IntegrationTest
5     ##
6     # test all routes which lead to this controller
7     def test_routes
8       assert_routing(
9         { :path => "/api/0.6/way/1/history", :method => :get },
10         { :controller => "api/old_ways", :action => "index", :way_id => "1" }
11       )
12       assert_routing(
13         { :path => "/api/0.6/way/1/history.json", :method => :get },
14         { :controller => "api/old_ways", :action => "index", :way_id => "1", :format => "json" }
15       )
16       assert_routing(
17         { :path => "/api/0.6/way/1/2", :method => :get },
18         { :controller => "api/old_ways", :action => "show", :way_id => "1", :version => "2" }
19       )
20       assert_routing(
21         { :path => "/api/0.6/way/1/2.json", :method => :get },
22         { :controller => "api/old_ways", :action => "show", :way_id => "1", :version => "2", :format => "json" }
23       )
24       assert_routing(
25         { :path => "/api/0.6/way/1/2/redact", :method => :post },
26         { :controller => "api/old_ways", :action => "redact", :way_id => "1", :version => "2" }
27       )
28     end
29
30     ##
31     # check that a visible way is returned properly
32     def test_index
33       way = create(:way, :with_history, :version => 2)
34
35       get api_way_versions_path(way)
36
37       assert_response :success
38       assert_dom "osm:root", 1 do
39         assert_dom "> way", 2 do |dom_ways|
40           assert_dom dom_ways[0], "> @id", way.id.to_s
41           assert_dom dom_ways[0], "> @version", "1"
42
43           assert_dom dom_ways[1], "> @id", way.id.to_s
44           assert_dom dom_ways[1], "> @version", "2"
45         end
46       end
47     end
48
49     ##
50     # check that an invisible way's history is returned properly
51     def test_index_invisible
52       get api_way_versions_path(create(:way, :with_history, :deleted))
53       assert_response :success
54     end
55
56     ##
57     # check chat a non-existent way is not returned
58     def test_index_invalid
59       get api_way_versions_path(0)
60       assert_response :not_found
61     end
62
63     ##
64     # test that redacted ways aren't visible in the history
65     def test_index_redacted_unauthorised
66       way = create(:way, :with_history, :version => 2)
67       way.old_ways.find_by(:version => 1).redact!(create(:redaction))
68
69       get api_way_versions_path(way)
70
71       assert_response :success, "Redaction shouldn't have stopped history working."
72       assert_dom "osm way[id='#{way.id}'][version='1']", 0,
73                  "redacted way #{way.id} version 1 shouldn't be present in the history."
74     end
75
76     def test_index_redacted_normal_user
77       way = create(:way, :with_history, :version => 2)
78       way.old_ways.find_by(:version => 1).redact!(create(:redaction))
79
80       get api_way_versions_path(way), :headers => bearer_authorization_header
81
82       assert_response :success, "Redaction shouldn't have stopped history working."
83       assert_dom "osm way[id='#{way.id}'][version='1']", 0,
84                  "redacted node #{way.id} version 1 shouldn't be present in the history, even when logged in."
85     end
86
87     def test_show
88       way = create(:way, :with_history, :version => 2)
89
90       get api_way_version_path(way, 1)
91
92       assert_response :success
93       assert_dom "osm:root", 1 do
94         assert_dom "> way", 1 do
95           assert_dom "> @id", way.id.to_s
96           assert_dom "> @version", "1"
97         end
98       end
99
100       get api_way_version_path(way, 2)
101
102       assert_response :success
103       assert_dom "osm:root", 1 do
104         assert_dom "> way", 1 do
105           assert_dom "> @id", way.id.to_s
106           assert_dom "> @version", "2"
107         end
108       end
109     end
110
111     ##
112     # test that redacted ways aren't visible, regardless of
113     # authorisation except as moderator...
114     def test_show_redacted
115       way = create(:way, :with_history, :version => 2)
116       way_v1 = way.old_ways.find_by(:version => 1)
117       way_v1.redact!(create(:redaction))
118
119       get api_way_version_path(way_v1.way_id, way_v1.version)
120       assert_response :forbidden, "Redacted way shouldn't be visible via the version API."
121
122       # not even to a logged-in user
123       auth_header = bearer_authorization_header
124       get api_way_version_path(way_v1.way_id, way_v1.version), :headers => auth_header
125       assert_response :forbidden, "Redacted way shouldn't be visible via the version API, even when logged in."
126     end
127
128     ##
129     # check that returned history is the same as getting all
130     # versions of a way from the api.
131     def test_history_equals_versions
132       way = create(:way, :with_history)
133       used_way = create(:way, :with_history)
134       create(:relation_member, :member => used_way)
135       way_with_versions = create(:way, :with_history, :version => 4)
136
137       check_history_equals_versions(way.id)
138       check_history_equals_versions(used_way.id)
139       check_history_equals_versions(way_with_versions.id)
140     end
141
142     ##
143     # test the redaction of an old version of a way, while not being
144     # authorised.
145     def test_redact_way_unauthorised
146       way = create(:way, :with_history, :version => 4)
147       way_v3 = way.old_ways.find_by(:version => 3)
148
149       do_redact_way(way_v3, create(:redaction))
150       assert_response :unauthorized, "should need to be authenticated to redact."
151     end
152
153     ##
154     # test the redaction of an old version of a way, while being
155     # authorised as a normal user.
156     def test_redact_way_normal_user
157       auth_header = bearer_authorization_header
158       way = create(:way, :with_history, :version => 4)
159       way_v3 = way.old_ways.find_by(:version => 3)
160
161       do_redact_way(way_v3, create(:redaction), auth_header)
162       assert_response :forbidden, "should need to be moderator to redact."
163     end
164
165     ##
166     # test that, even as moderator, the current version of a way
167     # can't be redacted.
168     def test_redact_way_current_version
169       auth_header = bearer_authorization_header create(:moderator_user)
170       way = create(:way, :with_history, :version => 4)
171       way_latest = way.old_ways.last
172
173       do_redact_way(way_latest, create(:redaction), auth_header)
174       assert_response :bad_request, "shouldn't be OK to redact current version as moderator."
175     end
176
177     def test_redact_way_by_regular_without_write_redactions_scope
178       auth_header = bearer_authorization_header(create(:user), :scopes => %w[read_prefs write_api])
179       do_redact_redactable_way(auth_header)
180       assert_response :forbidden, "should need to be moderator to redact."
181     end
182
183     def test_redact_way_by_regular_with_write_redactions_scope
184       auth_header = bearer_authorization_header(create(:user), :scopes => %w[write_redactions])
185       do_redact_redactable_way(auth_header)
186       assert_response :forbidden, "should need to be moderator to redact."
187     end
188
189     def test_redact_way_by_moderator_without_write_redactions_scope
190       auth_header = bearer_authorization_header(create(:moderator_user), :scopes => %w[read_prefs write_api])
191       do_redact_redactable_way(auth_header)
192       assert_response :forbidden, "should need to have write_redactions scope to redact."
193     end
194
195     def test_redact_way_by_moderator_with_write_redactions_scope
196       auth_header = bearer_authorization_header(create(:moderator_user), :scopes => %w[write_redactions])
197       do_redact_redactable_way(auth_header)
198       assert_response :success, "should be OK to redact old version as moderator with write_redactions scope."
199     end
200
201     ##
202     # test the redaction of an old version of a way, while being
203     # authorised as a moderator.
204     def test_redact_way_moderator
205       way = create(:way, :with_history, :version => 4)
206       way_v3 = way.old_ways.find_by(:version => 3)
207       auth_header = bearer_authorization_header create(:moderator_user)
208
209       do_redact_way(way_v3, create(:redaction), auth_header)
210       assert_response :success, "should be OK to redact old version as moderator."
211
212       # check moderator can still see the redacted data, when passing
213       # the appropriate flag
214       get api_way_version_path(way_v3.way_id, way_v3.version), :headers => auth_header
215       assert_response :forbidden, "After redaction, node should be gone for moderator, when flag not passed."
216       get api_way_version_path(way_v3.way_id, way_v3.version, :show_redactions => "true"), :headers => auth_header
217       assert_response :success, "After redaction, node should not be gone for moderator, when flag passed."
218
219       # and when accessed via history
220       get api_way_versions_path(way), :headers => auth_header
221       assert_response :success, "Redaction shouldn't have stopped history working."
222       assert_select "osm way[id='#{way_v3.way_id}'][version='#{way_v3.version}']", 0,
223                     "way #{way_v3.way_id} version #{way_v3.version} should not be present in the history for moderators when not passing flag."
224       get api_way_versions_path(way, :show_redactions => "true"), :headers => auth_header
225       assert_response :success, "Redaction shouldn't have stopped history working."
226       assert_select "osm way[id='#{way_v3.way_id}'][version='#{way_v3.version}']", 1,
227                     "way #{way_v3.way_id} version #{way_v3.version} should still be present in the history for moderators when passing flag."
228     end
229
230     # testing that if the moderator drops auth, he can't see the
231     # redacted stuff any more.
232     def test_redact_way_is_redacted
233       way = create(:way, :with_history, :version => 4)
234       way_v3 = way.old_ways.find_by(:version => 3)
235       auth_header = bearer_authorization_header create(:moderator_user)
236
237       do_redact_way(way_v3, create(:redaction), auth_header)
238       assert_response :success, "should be OK to redact old version as moderator."
239
240       # re-auth as non-moderator
241       auth_header = bearer_authorization_header
242
243       # check can't see the redacted data
244       get api_way_version_path(way_v3.way_id, way_v3.version), :headers => auth_header
245       assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
246
247       # and when accessed via history
248       get api_way_versions_path(way), :headers => auth_header
249       assert_response :success, "Redaction shouldn't have stopped history working."
250       assert_select "osm way[id='#{way_v3.way_id}'][version='#{way_v3.version}']", 0,
251                     "redacted way #{way_v3.way_id} version #{way_v3.version} shouldn't be present in the history."
252     end
253
254     ##
255     # test the unredaction of an old version of a way, while not being
256     # authorised.
257     def test_unredact_way_unauthorised
258       way = create(:way, :with_history, :version => 2)
259       way_v1 = way.old_ways.find_by(:version => 1)
260       way_v1.redact!(create(:redaction))
261
262       post way_version_redact_path(way_v1.way_id, way_v1.version)
263       assert_response :unauthorized, "should need to be authenticated to unredact."
264     end
265
266     ##
267     # test the unredaction of an old version of a way, while being
268     # authorised as a normal user.
269     def test_unredact_way_normal_user
270       way = create(:way, :with_history, :version => 2)
271       way_v1 = way.old_ways.find_by(:version => 1)
272       way_v1.redact!(create(:redaction))
273
274       auth_header = bearer_authorization_header
275
276       post way_version_redact_path(way_v1.way_id, way_v1.version), :headers => auth_header
277       assert_response :forbidden, "should need to be moderator to unredact."
278     end
279
280     ##
281     # test the unredaction of an old version of a way, while being
282     # authorised as a moderator.
283     def test_unredact_way_moderator
284       moderator_user = create(:moderator_user)
285       way = create(:way, :with_history, :version => 2)
286       way_v1 = way.old_ways.find_by(:version => 1)
287       way_v1.redact!(create(:redaction))
288
289       auth_header = bearer_authorization_header moderator_user
290
291       post way_version_redact_path(way_v1.way_id, way_v1.version), :headers => auth_header
292       assert_response :success, "should be OK to unredact old version as moderator."
293
294       # check moderator can still see the unredacted data, without passing
295       # the appropriate flag
296       get api_way_version_path(way_v1.way_id, way_v1.version), :headers => auth_header
297       assert_response :success, "After unredaction, node should not be gone for moderator."
298
299       # and when accessed via history
300       get api_way_versions_path(way), :headers => auth_header
301       assert_response :success, "Unredaction shouldn't have stopped history working."
302       assert_select "osm way[id='#{way_v1.way_id}'][version='#{way_v1.version}']", 1,
303                     "way #{way_v1.way_id} version #{way_v1.version} should still be present in the history for moderators."
304
305       auth_header = bearer_authorization_header
306
307       # check normal user can now see the unredacted data
308       get api_way_version_path(way_v1.way_id, way_v1.version), :headers => auth_header
309       assert_response :success, "After redaction, node should not be gone for moderator, when flag passed."
310
311       # and when accessed via history
312       get api_way_versions_path(way), :headers => auth_header
313       assert_response :success, "Redaction shouldn't have stopped history working."
314       assert_select "osm way[id='#{way_v1.way_id}'][version='#{way_v1.version}']", 1,
315                     "way #{way_v1.way_id} version #{way_v1.version} should still be present in the history for normal users."
316     end
317
318     private
319
320     ##
321     # look at all the versions of the way in the history and get each version from
322     # the versions call. check that they're the same.
323     def check_history_equals_versions(way_id)
324       get api_way_versions_path(way_id)
325       assert_response :success, "can't get way #{way_id} from API"
326       history_doc = XML::Parser.string(@response.body).parse
327       assert_not_nil history_doc, "parsing way #{way_id} history failed"
328
329       history_doc.find("//osm/way").each do |way_doc|
330         history_way = Way.from_xml_node(way_doc)
331         assert_not_nil history_way, "parsing way #{way_id} version failed"
332
333         get api_way_version_path(way_id, history_way.version)
334         assert_response :success, "couldn't get way #{way_id}, v#{history_way.version}"
335         version_way = Way.from_xml(@response.body)
336         assert_not_nil version_way, "failed to parse #{way_id}, v#{history_way.version}"
337
338         assert_ways_are_equal history_way, version_way
339       end
340     end
341
342     def do_redact_redactable_way(headers = {})
343       way = create(:way, :with_history, :version => 4)
344       way_v3 = way.old_ways.find_by(:version => 3)
345       do_redact_way(way_v3, create(:redaction), headers)
346     end
347
348     def do_redact_way(way, redaction, headers = {})
349       get api_way_version_path(way.way_id, way.version)
350       assert_response :success, "should be able to get version #{way.version} of way #{way.way_id}."
351
352       # now redact it
353       post way_version_redact_path(way.way_id, way.version), :params => { :redaction => redaction.id }, :headers => headers
354     end
355   end
356 end