]> git.openstreetmap.org Git - rails.git/blob - test/controllers/api/old_ways_controller_test.rb
Split api old element redacted show tests
[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_unauthorised
115       way = create(:way, :with_history, :version => 2)
116       way.old_ways.find_by(:version => 1).redact!(create(:redaction))
117
118       get api_way_version_path(way, 1)
119
120       assert_response :forbidden, "Redacted way shouldn't be visible via the version API."
121     end
122
123     def test_show_redacted_normal_user
124       way = create(:way, :with_history, :version => 2)
125       way.old_ways.find_by(:version => 1).redact!(create(:redaction))
126
127       get api_way_version_path(way, 1), :headers => bearer_authorization_header
128
129       assert_response :forbidden, "Redacted way shouldn't be visible via the version API, even when logged in."
130     end
131
132     ##
133     # check that returned history is the same as getting all
134     # versions of a way from the api.
135     def test_history_equals_versions
136       way = create(:way, :with_history)
137       used_way = create(:way, :with_history)
138       create(:relation_member, :member => used_way)
139       way_with_versions = create(:way, :with_history, :version => 4)
140
141       check_history_equals_versions(way.id)
142       check_history_equals_versions(used_way.id)
143       check_history_equals_versions(way_with_versions.id)
144     end
145
146     ##
147     # test the redaction of an old version of a way, while not being
148     # authorised.
149     def test_redact_way_unauthorised
150       way = create(:way, :with_history, :version => 4)
151       way_v3 = way.old_ways.find_by(:version => 3)
152
153       do_redact_way(way_v3, create(:redaction))
154       assert_response :unauthorized, "should need to be authenticated to redact."
155     end
156
157     ##
158     # test the redaction of an old version of a way, while being
159     # authorised as a normal user.
160     def test_redact_way_normal_user
161       auth_header = bearer_authorization_header
162       way = create(:way, :with_history, :version => 4)
163       way_v3 = way.old_ways.find_by(:version => 3)
164
165       do_redact_way(way_v3, create(:redaction), auth_header)
166       assert_response :forbidden, "should need to be moderator to redact."
167     end
168
169     ##
170     # test that, even as moderator, the current version of a way
171     # can't be redacted.
172     def test_redact_way_current_version
173       auth_header = bearer_authorization_header create(:moderator_user)
174       way = create(:way, :with_history, :version => 4)
175       way_latest = way.old_ways.last
176
177       do_redact_way(way_latest, create(:redaction), auth_header)
178       assert_response :bad_request, "shouldn't be OK to redact current version as moderator."
179     end
180
181     def test_redact_way_by_regular_without_write_redactions_scope
182       auth_header = bearer_authorization_header(create(:user), :scopes => %w[read_prefs write_api])
183       do_redact_redactable_way(auth_header)
184       assert_response :forbidden, "should need to be moderator to redact."
185     end
186
187     def test_redact_way_by_regular_with_write_redactions_scope
188       auth_header = bearer_authorization_header(create(:user), :scopes => %w[write_redactions])
189       do_redact_redactable_way(auth_header)
190       assert_response :forbidden, "should need to be moderator to redact."
191     end
192
193     def test_redact_way_by_moderator_without_write_redactions_scope
194       auth_header = bearer_authorization_header(create(:moderator_user), :scopes => %w[read_prefs write_api])
195       do_redact_redactable_way(auth_header)
196       assert_response :forbidden, "should need to have write_redactions scope to redact."
197     end
198
199     def test_redact_way_by_moderator_with_write_redactions_scope
200       auth_header = bearer_authorization_header(create(:moderator_user), :scopes => %w[write_redactions])
201       do_redact_redactable_way(auth_header)
202       assert_response :success, "should be OK to redact old version as moderator with write_redactions scope."
203     end
204
205     ##
206     # test the redaction of an old version of a way, while being
207     # authorised as a moderator.
208     def test_redact_way_moderator
209       way = create(:way, :with_history, :version => 4)
210       way_v3 = way.old_ways.find_by(:version => 3)
211       auth_header = bearer_authorization_header create(:moderator_user)
212
213       do_redact_way(way_v3, create(:redaction), auth_header)
214       assert_response :success, "should be OK to redact old version as moderator."
215
216       # check moderator can still see the redacted data, when passing
217       # the appropriate flag
218       get api_way_version_path(way_v3.way_id, way_v3.version), :headers => auth_header
219       assert_response :forbidden, "After redaction, node should be gone for moderator, when flag not passed."
220       get api_way_version_path(way_v3.way_id, way_v3.version, :show_redactions => "true"), :headers => auth_header
221       assert_response :success, "After redaction, node should not be gone for moderator, when flag passed."
222
223       # and when accessed via history
224       get api_way_versions_path(way), :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}']", 0,
227                     "way #{way_v3.way_id} version #{way_v3.version} should not be present in the history for moderators when not passing flag."
228       get api_way_versions_path(way, :show_redactions => "true"), :headers => auth_header
229       assert_response :success, "Redaction shouldn't have stopped history working."
230       assert_select "osm way[id='#{way_v3.way_id}'][version='#{way_v3.version}']", 1,
231                     "way #{way_v3.way_id} version #{way_v3.version} should still be present in the history for moderators when passing flag."
232     end
233
234     # testing that if the moderator drops auth, he can't see the
235     # redacted stuff any more.
236     def test_redact_way_is_redacted
237       way = create(:way, :with_history, :version => 4)
238       way_v3 = way.old_ways.find_by(:version => 3)
239       auth_header = bearer_authorization_header create(:moderator_user)
240
241       do_redact_way(way_v3, create(:redaction), auth_header)
242       assert_response :success, "should be OK to redact old version as moderator."
243
244       # re-auth as non-moderator
245       auth_header = bearer_authorization_header
246
247       # check can't see the redacted data
248       get api_way_version_path(way_v3.way_id, way_v3.version), :headers => auth_header
249       assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
250
251       # and when accessed via history
252       get api_way_versions_path(way), :headers => auth_header
253       assert_response :success, "Redaction shouldn't have stopped history working."
254       assert_select "osm way[id='#{way_v3.way_id}'][version='#{way_v3.version}']", 0,
255                     "redacted way #{way_v3.way_id} version #{way_v3.version} shouldn't be present in the history."
256     end
257
258     ##
259     # test the unredaction of an old version of a way, while not being
260     # authorised.
261     def test_unredact_way_unauthorised
262       way = create(:way, :with_history, :version => 2)
263       way_v1 = way.old_ways.find_by(:version => 1)
264       way_v1.redact!(create(:redaction))
265
266       post way_version_redact_path(way_v1.way_id, way_v1.version)
267       assert_response :unauthorized, "should need to be authenticated to unredact."
268     end
269
270     ##
271     # test the unredaction of an old version of a way, while being
272     # authorised as a normal user.
273     def test_unredact_way_normal_user
274       way = create(:way, :with_history, :version => 2)
275       way_v1 = way.old_ways.find_by(:version => 1)
276       way_v1.redact!(create(:redaction))
277
278       auth_header = bearer_authorization_header
279
280       post way_version_redact_path(way_v1.way_id, way_v1.version), :headers => auth_header
281       assert_response :forbidden, "should need to be moderator to unredact."
282     end
283
284     ##
285     # test the unredaction of an old version of a way, while being
286     # authorised as a moderator.
287     def test_unredact_way_moderator
288       moderator_user = create(:moderator_user)
289       way = create(:way, :with_history, :version => 2)
290       way_v1 = way.old_ways.find_by(:version => 1)
291       way_v1.redact!(create(:redaction))
292
293       auth_header = bearer_authorization_header moderator_user
294
295       post way_version_redact_path(way_v1.way_id, way_v1.version), :headers => auth_header
296       assert_response :success, "should be OK to unredact old version as moderator."
297
298       # check moderator can still see the unredacted data, without passing
299       # the appropriate flag
300       get api_way_version_path(way_v1.way_id, way_v1.version), :headers => auth_header
301       assert_response :success, "After unredaction, node should not be gone for moderator."
302
303       # and when accessed via history
304       get api_way_versions_path(way), :headers => auth_header
305       assert_response :success, "Unredaction shouldn't have stopped history working."
306       assert_select "osm way[id='#{way_v1.way_id}'][version='#{way_v1.version}']", 1,
307                     "way #{way_v1.way_id} version #{way_v1.version} should still be present in the history for moderators."
308
309       auth_header = bearer_authorization_header
310
311       # check normal user can now see the unredacted data
312       get api_way_version_path(way_v1.way_id, way_v1.version), :headers => auth_header
313       assert_response :success, "After redaction, node should not be gone for moderator, when flag passed."
314
315       # and when accessed via history
316       get api_way_versions_path(way), :headers => auth_header
317       assert_response :success, "Redaction shouldn't have stopped history working."
318       assert_select "osm way[id='#{way_v1.way_id}'][version='#{way_v1.version}']", 1,
319                     "way #{way_v1.way_id} version #{way_v1.version} should still be present in the history for normal users."
320     end
321
322     private
323
324     ##
325     # look at all the versions of the way in the history and get each version from
326     # the versions call. check that they're the same.
327     def check_history_equals_versions(way_id)
328       get api_way_versions_path(way_id)
329       assert_response :success, "can't get way #{way_id} from API"
330       history_doc = XML::Parser.string(@response.body).parse
331       assert_not_nil history_doc, "parsing way #{way_id} history failed"
332
333       history_doc.find("//osm/way").each do |way_doc|
334         history_way = Way.from_xml_node(way_doc)
335         assert_not_nil history_way, "parsing way #{way_id} version failed"
336
337         get api_way_version_path(way_id, history_way.version)
338         assert_response :success, "couldn't get way #{way_id}, v#{history_way.version}"
339         version_way = Way.from_xml(@response.body)
340         assert_not_nil version_way, "failed to parse #{way_id}, v#{history_way.version}"
341
342         assert_ways_are_equal history_way, version_way
343       end
344     end
345
346     def do_redact_redactable_way(headers = {})
347       way = create(:way, :with_history, :version => 4)
348       way_v3 = way.old_ways.find_by(:version => 3)
349       do_redact_way(way_v3, create(:redaction), headers)
350     end
351
352     def do_redact_way(way, redaction, headers = {})
353       get api_way_version_path(way.way_id, way.version)
354       assert_response :success, "should be able to get version #{way.version} of way #{way.way_id}."
355
356       # now redact it
357       post way_version_redact_path(way.way_id, way.version), :params => { :redaction => redaction.id }, :headers => headers
358     end
359   end
360 end