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