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