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