]> git.openstreetmap.org Git - rails.git/blob - test/controllers/api/old_ways_controller_test.rb
Add frozen_string_literal comments to ruby files
[rails.git] / test / controllers / api / old_ways_controller_test.rb
1 # frozen_string_literal: true
2
3 require "test_helper"
4
5 module Api
6   class OldWaysControllerTest < ActionDispatch::IntegrationTest
7     ##
8     # test all routes which lead to this controller
9     def test_routes
10       assert_routing(
11         { :path => "/api/0.6/way/1/history", :method => :get },
12         { :controller => "api/old_ways", :action => "index", :way_id => "1" }
13       )
14       assert_routing(
15         { :path => "/api/0.6/way/1/history.json", :method => :get },
16         { :controller => "api/old_ways", :action => "index", :way_id => "1", :format => "json" }
17       )
18       assert_routing(
19         { :path => "/api/0.6/way/1/2", :method => :get },
20         { :controller => "api/old_ways", :action => "show", :way_id => "1", :version => "2" }
21       )
22       assert_routing(
23         { :path => "/api/0.6/way/1/2.json", :method => :get },
24         { :controller => "api/old_ways", :action => "show", :way_id => "1", :version => "2", :format => "json" }
25       )
26     end
27
28     ##
29     # check that a visible way is returned properly
30     def test_index
31       way = create(:way, :with_history, :version => 2)
32
33       get api_way_versions_path(way)
34
35       assert_response :success
36       assert_dom "osm:root", 1 do
37         assert_dom "> way", 2 do |dom_ways|
38           assert_dom dom_ways[0], "> @id", way.id.to_s
39           assert_dom dom_ways[0], "> @version", "1"
40
41           assert_dom dom_ways[1], "> @id", way.id.to_s
42           assert_dom dom_ways[1], "> @version", "2"
43         end
44       end
45     end
46
47     ##
48     # check that an invisible way's history is returned properly
49     def test_index_invisible
50       get api_way_versions_path(create(:way, :with_history, :deleted))
51       assert_response :success
52     end
53
54     ##
55     # check chat a non-existent way is not returned
56     def test_index_invalid
57       get api_way_versions_path(0)
58       assert_response :not_found
59     end
60
61     ##
62     # test that redacted ways aren't visible in the history
63     def test_index_redacted_unauthorised
64       way = create(:way, :with_history, :version => 2)
65       way.old_ways.find_by(:version => 1).redact!(create(:redaction))
66
67       get api_way_versions_path(way)
68
69       assert_response :success, "Redaction shouldn't have stopped history working."
70       assert_dom "osm way[id='#{way.id}'][version='1']", 0,
71                  "redacted way #{way.id} version 1 shouldn't be present in the history."
72
73       get api_way_versions_path(way, :show_redactions => "true")
74
75       assert_response :success, "Redaction shouldn't have stopped history working."
76       assert_dom "osm way[id='#{way.id}'][version='1']", 0,
77                  "redacted way #{way.id} version 1 shouldn't be present in the history when passing flag."
78     end
79
80     def test_index_redacted_normal_user
81       way = create(:way, :with_history, :version => 2)
82       way.old_ways.find_by(:version => 1).redact!(create(:redaction))
83
84       get api_way_versions_path(way), :headers => bearer_authorization_header
85
86       assert_response :success, "Redaction shouldn't have stopped history working."
87       assert_dom "osm way[id='#{way.id}'][version='1']", 0,
88                  "redacted node #{way.id} version 1 shouldn't be present in the history, even when logged in."
89
90       get api_way_versions_path(way, :show_redactions => "true"), :headers => bearer_authorization_header
91
92       assert_response :success, "Redaction shouldn't have stopped history working."
93       assert_dom "osm way[id='#{way.id}'][version='1']", 0,
94                  "redacted node #{way.id} version 1 shouldn't be present in the history, even when logged in and passing flag."
95     end
96
97     def test_index_redacted_moderator
98       way = create(:way, :with_history, :version => 2)
99       way.old_ways.find_by(:version => 1).redact!(create(:redaction))
100       auth_header = bearer_authorization_header create(:moderator_user)
101
102       get api_way_versions_path(way), :headers => auth_header
103
104       assert_response :success, "Redaction shouldn't have stopped history working."
105       assert_dom "osm way[id='#{way.id}'][version='1']", 0,
106                  "way #{way.id} version 1 should not be present in the history for moderators when not passing flag."
107
108       get api_way_versions_path(way, :show_redactions => "true"), :headers => auth_header
109
110       assert_response :success, "Redaction shouldn't have stopped history working."
111       assert_dom "osm way[id='#{way.id}'][version='1']", 1,
112                  "way #{way.id} version 1 should still be present in the history for moderators when passing flag."
113     end
114
115     def test_show
116       way = create(:way, :with_history, :version => 2)
117
118       get api_way_version_path(way, 1)
119
120       assert_response :success
121       assert_dom "osm:root", 1 do
122         assert_dom "> way", 1 do
123           assert_dom "> @id", way.id.to_s
124           assert_dom "> @version", "1"
125         end
126       end
127
128       get api_way_version_path(way, 2)
129
130       assert_response :success
131       assert_dom "osm:root", 1 do
132         assert_dom "> way", 1 do
133           assert_dom "> @id", way.id.to_s
134           assert_dom "> @version", "2"
135         end
136       end
137     end
138
139     ##
140     # test that redacted ways aren't visible, regardless of
141     # authorisation except as moderator...
142     def test_show_redacted_unauthorised
143       way = create(:way, :with_history, :version => 2)
144       way.old_ways.find_by(:version => 1).redact!(create(:redaction))
145
146       get api_way_version_path(way, 1)
147
148       assert_response :forbidden, "Redacted way shouldn't be visible via the version API."
149
150       get api_way_version_path(way, 1, :show_redactions => "true")
151
152       assert_response :forbidden, "Redacted way shouldn't be visible via the version API when passing flag."
153     end
154
155     def test_show_redacted_normal_user
156       way = create(:way, :with_history, :version => 2)
157       way.old_ways.find_by(:version => 1).redact!(create(:redaction))
158
159       get api_way_version_path(way, 1), :headers => bearer_authorization_header
160
161       assert_response :forbidden, "Redacted way shouldn't be visible via the version API, even when logged in."
162
163       get api_way_version_path(way, 1, :show_redactions => "true"), :headers => bearer_authorization_header
164
165       assert_response :forbidden, "Redacted way shouldn't be visible via the version API, even when logged in and passing flag."
166     end
167
168     def test_show_redacted_moderator
169       way = create(:way, :with_history, :version => 2)
170       way.old_ways.find_by(:version => 1).redact!(create(:redaction))
171       auth_header = bearer_authorization_header create(:moderator_user)
172
173       get api_way_version_path(way, 1), :headers => auth_header
174
175       assert_response :forbidden, "Redacted node should be gone for moderator, when flag not passed."
176
177       get api_way_version_path(way, 1, :show_redactions => "true"), :headers => auth_header
178
179       assert_response :success, "Redacted node should not be gone for moderator, when flag passed."
180     end
181
182     ##
183     # check that returned history is the same as getting all
184     # versions of a way from the api.
185     def test_history_equals_versions
186       way = create(:way, :with_history)
187       used_way = create(:way, :with_history)
188       create(:relation_member, :member => used_way)
189       way_with_versions = create(:way, :with_history, :version => 4)
190
191       check_history_equals_versions(way.id)
192       check_history_equals_versions(used_way.id)
193       check_history_equals_versions(way_with_versions.id)
194     end
195
196     private
197
198     ##
199     # look at all the versions of the way in the history and get each version from
200     # the versions call. check that they're the same.
201     def check_history_equals_versions(way_id)
202       get api_way_versions_path(way_id)
203       assert_response :success, "can't get way #{way_id} from API"
204       history_doc = XML::Parser.string(@response.body).parse
205       assert_not_nil history_doc, "parsing way #{way_id} history failed"
206
207       history_doc.find("//osm/way").each do |way_doc|
208         history_way = Way.from_xml_node(way_doc)
209         assert_not_nil history_way, "parsing way #{way_id} version failed"
210
211         get api_way_version_path(way_id, history_way.version)
212         assert_response :success, "couldn't get way #{way_id}, v#{history_way.version}"
213         version_way = Way.from_xml(@response.body)
214         assert_not_nil version_way, "failed to parse #{way_id}, v#{history_way.version}"
215
216         assert_ways_are_equal history_way, version_way
217       end
218     end
219   end
220 end