4 class OldRelationsControllerTest < ActionDispatch::IntegrationTest
6 # test all routes which lead to this controller
9 { :path => "/api/0.6/relation/1/history", :method => :get },
10 { :controller => "api/old_relations", :action => "history", :id => "1" }
13 { :path => "/api/0.6/relation/1/2", :method => :get },
14 { :controller => "api/old_relations", :action => "version", :id => "1", :version => "2" }
17 { :path => "/api/0.6/relation/1/history.json", :method => :get },
18 { :controller => "api/old_relations", :action => "history", :id => "1", :format => "json" }
21 { :path => "/api/0.6/relation/1/2.json", :method => :get },
22 { :controller => "api/old_relations", :action => "version", :id => "1", :version => "2", :format => "json" }
25 { :path => "/api/0.6/relation/1/2/redact", :method => :post },
26 { :controller => "api/old_relations", :action => "redact", :id => "1", :version => "2" }
30 # -------------------------------------
31 # Test reading old relations.
32 # -------------------------------------
34 # check that a visible relations is returned properly
35 get api_relation_history_path(create(:relation, :with_history))
36 assert_response :success
38 # check chat a non-existent relations is not returned
39 get api_relation_history_path(:id => 0)
40 assert_response :not_found
44 # test the redaction of an old version of a relation, while not being
46 def test_redact_relation_unauthorised
47 relation = create(:relation, :with_history, :version => 4)
48 relation_v3 = relation.old_relations.find_by(:version => 3)
50 do_redact_relation(relation_v3, create(:redaction))
51 assert_response :unauthorized, "should need to be authenticated to redact."
55 # test the redaction of an old version of a relation, while being
56 # authorised as a normal user.
57 def test_redact_relation_normal_user
58 relation = create(:relation, :with_history, :version => 4)
59 relation_v3 = relation.old_relations.find_by(:version => 3)
61 auth_header = basic_authorization_header create(:user).email, "test"
63 do_redact_relation(relation_v3, create(:redaction), auth_header)
64 assert_response :forbidden, "should need to be moderator to redact."
68 # test that, even as moderator, the current version of a relation
70 def test_redact_relation_current_version
71 relation = create(:relation, :with_history, :version => 4)
72 relation_latest = relation.old_relations.last
74 auth_header = basic_authorization_header create(:moderator_user).email, "test"
76 do_redact_relation(relation_latest, create(:redaction), auth_header)
77 assert_response :bad_request, "shouldn't be OK to redact current version as moderator."
81 # test that redacted relations aren't visible, regardless of
82 # authorisation except as moderator...
83 def test_version_redacted
84 relation = create(:relation, :with_history, :version => 2)
85 relation_v1 = relation.old_relations.find_by(:version => 1)
86 relation_v1.redact!(create(:redaction))
88 get relation_version_path(:id => relation_v1.relation_id, :version => relation_v1.version)
89 assert_response :forbidden, "Redacted relation shouldn't be visible via the version API."
91 # not even to a logged-in user
92 auth_header = basic_authorization_header create(:user).email, "test"
93 get relation_version_path(:id => relation_v1.relation_id, :version => relation_v1.version), :headers => auth_header
94 assert_response :forbidden, "Redacted relation shouldn't be visible via the version API, even when logged in."
98 # test that redacted relations aren't visible in the history
99 def test_history_redacted
100 relation = create(:relation, :with_history, :version => 2)
101 relation_v1 = relation.old_relations.find_by(:version => 1)
102 relation_v1.redact!(create(:redaction))
104 get api_relation_history_path(:id => relation_v1.relation_id)
105 assert_response :success, "Redaction shouldn't have stopped history working."
106 assert_select "osm relation[id='#{relation_v1.relation_id}'][version='#{relation_v1.version}']", 0,
107 "redacted relation #{relation_v1.relation_id} version #{relation_v1.version} shouldn't be present in the history."
109 # not even to a logged-in user
110 auth_header = basic_authorization_header create(:user).email, "test"
111 get relation_version_path(:id => relation_v1.relation_id, :version => relation_v1.version), :headers => auth_header
112 get api_relation_history_path(:id => relation_v1.relation_id), :headers => auth_header
113 assert_response :success, "Redaction shouldn't have stopped history working."
114 assert_select "osm relation[id='#{relation_v1.relation_id}'][version='#{relation_v1.version}']", 0,
115 "redacted relation #{relation_v1.relation_id} version #{relation_v1.version} shouldn't be present in the history, even when logged in."
119 # test the redaction of an old version of a relation, while being
120 # authorised as a moderator.
121 def test_redact_relation_moderator
122 relation = create(:relation, :with_history, :version => 4)
123 relation_v3 = relation.old_relations.find_by(:version => 3)
125 auth_header = basic_authorization_header create(:moderator_user).email, "test"
127 do_redact_relation(relation_v3, create(:redaction), auth_header)
128 assert_response :success, "should be OK to redact old version as moderator."
130 # check moderator can still see the redacted data, when passing
131 # the appropriate flag
132 get relation_version_path(:id => relation_v3.relation_id, :version => relation_v3.version), :headers => auth_header
133 assert_response :forbidden, "After redaction, relation should be gone for moderator, when flag not passed."
134 get relation_version_path(:id => relation_v3.relation_id, :version => relation_v3.version), :params => { :show_redactions => "true" }, :headers => auth_header
135 assert_response :success, "After redaction, relation should not be gone for moderator, when flag passed."
137 # and when accessed via history
138 get api_relation_history_path(:id => relation_v3.relation_id), :headers => auth_header
139 assert_response :success, "Redaction shouldn't have stopped history working."
140 assert_select "osm relation[id='#{relation_v3.relation_id}'][version='#{relation_v3.version}']", 0,
141 "relation #{relation_v3.relation_id} version #{relation_v3.version} should not be present in the history for moderators when not passing flag."
142 get api_relation_history_path(:id => relation_v3.relation_id), :params => { :show_redactions => "true" }, :headers => auth_header
143 assert_response :success, "Redaction shouldn't have stopped history working."
144 assert_select "osm relation[id='#{relation_v3.relation_id}'][version='#{relation_v3.version}']", 1,
145 "relation #{relation_v3.relation_id} version #{relation_v3.version} should still be present in the history for moderators when passing flag."
148 # testing that if the moderator drops auth, he can't see the
149 # redacted stuff any more.
150 def test_redact_relation_is_redacted
151 relation = create(:relation, :with_history, :version => 4)
152 relation_v3 = relation.old_relations.find_by(:version => 3)
154 auth_header = basic_authorization_header create(:moderator_user).email, "test"
156 do_redact_relation(relation_v3, create(:redaction), auth_header)
157 assert_response :success, "should be OK to redact old version as moderator."
159 # re-auth as non-moderator
160 auth_header = basic_authorization_header create(:user).email, "test"
162 # check can't see the redacted data
163 get relation_version_path(:id => relation_v3.relation_id, :version => relation_v3.version), :headers => auth_header
164 assert_response :forbidden, "Redacted relation shouldn't be visible via the version API."
166 # and when accessed via history
167 get api_relation_history_path(:id => relation_v3.relation_id), :headers => auth_header
168 assert_response :success, "Redaction shouldn't have stopped history working."
169 assert_select "osm relation[id='#{relation_v3.relation_id}'][version='#{relation_v3.version}']", 0,
170 "redacted relation #{relation_v3.relation_id} version #{relation_v3.version} shouldn't be present in the history."
174 # test the unredaction of an old version of a relation, while not being
176 def test_unredact_relation_unauthorised
177 relation = create(:relation, :with_history, :version => 2)
178 relation_v1 = relation.old_relations.find_by(:version => 1)
179 relation_v1.redact!(create(:redaction))
181 post relation_version_redact_path(:id => relation_v1.relation_id, :version => relation_v1.version)
182 assert_response :unauthorized, "should need to be authenticated to unredact."
186 # test the unredaction of an old version of a relation, while being
187 # authorised as a normal user.
188 def test_unredact_relation_normal_user
189 relation = create(:relation, :with_history, :version => 2)
190 relation_v1 = relation.old_relations.find_by(:version => 1)
191 relation_v1.redact!(create(:redaction))
193 auth_header = basic_authorization_header create(:user).email, "test"
195 post relation_version_redact_path(:id => relation_v1.relation_id, :version => relation_v1.version), :headers => auth_header
196 assert_response :forbidden, "should need to be moderator to unredact."
200 # test the unredaction of an old version of a relation, while being
201 # authorised as a moderator.
202 def test_unredact_relation_moderator
203 relation = create(:relation, :with_history, :version => 2)
204 relation_v1 = relation.old_relations.find_by(:version => 1)
205 relation_v1.redact!(create(:redaction))
207 auth_header = basic_authorization_header create(:moderator_user).email, "test"
209 post relation_version_redact_path(:id => relation_v1.relation_id, :version => relation_v1.version), :headers => auth_header
210 assert_response :success, "should be OK to unredact old version as moderator."
212 # check moderator can still see the redacted data, without passing
213 # the appropriate flag
214 get relation_version_path(:id => relation_v1.relation_id, :version => relation_v1.version), :headers => auth_header
215 assert_response :success, "After unredaction, relation should not be gone for moderator."
217 # and when accessed via history
218 get api_relation_history_path(:id => relation_v1.relation_id), :headers => auth_header
219 assert_response :success, "Redaction shouldn't have stopped history working."
220 assert_select "osm relation[id='#{relation_v1.relation_id}'][version='#{relation_v1.version}']", 1,
221 "relation #{relation_v1.relation_id} version #{relation_v1.version} should still be present in the history for moderators."
223 auth_header = basic_authorization_header create(:user).email, "test"
225 # check normal user can now see the redacted data
226 get relation_version_path(:id => relation_v1.relation_id, :version => relation_v1.version), :headers => auth_header
227 assert_response :success, "After redaction, node should not be gone for normal user."
229 # and when accessed via history
230 get api_relation_history_path(:id => relation_v1.relation_id), :headers => auth_header
231 assert_response :success, "Redaction shouldn't have stopped history working."
232 assert_select "osm relation[id='#{relation_v1.relation_id}'][version='#{relation_v1.version}']", 1,
233 "relation #{relation_v1.relation_id} version #{relation_v1.version} should still be present in the history for normal users."
239 # check that the current version of a relation is equivalent to the
240 # version which we're getting from the versions call.
241 def check_current_version(relation_id)
242 # get the current version
243 current_relation = with_controller(RelationsController.new) do
244 get :show, :params => { :id => relation_id }
245 assert_response :success, "can't get current relation #{relation_id}"
246 Relation.from_xml(@response.body)
248 assert_not_nil current_relation, "getting relation #{relation_id} returned nil"
250 # get the "old" version of the relation from the version method
251 get :version, :params => { :id => relation_id, :version => current_relation.version }
252 assert_response :success, "can't get old relation #{relation_id}, v#{current_relation.version}"
253 old_relation = Relation.from_xml(@response.body)
255 # check that the relations are identical
256 assert_relations_are_equal current_relation, old_relation
260 # look at all the versions of the relation in the history and get each version from
261 # the versions call. check that they're the same.
262 def check_history_equals_versions(relation_id)
263 get :history, :params => { :id => relation_id }
264 assert_response :success, "can't get relation #{relation_id} from API"
265 history_doc = XML::Parser.string(@response.body).parse
266 assert_not_nil history_doc, "parsing relation #{relation_id} history failed"
268 history_doc.find("//osm/relation").each do |relation_doc|
269 history_relation = Relation.from_xml_node(relation_doc)
270 assert_not_nil history_relation, "parsing relation #{relation_id} version failed"
272 get :version, :params => { :id => relation_id, :version => history_relation.version }
273 assert_response :success, "couldn't get relation #{relation_id}, v#{history_relation.version}"
274 version_relation = Relation.from_xml(@response.body)
275 assert_not_nil version_relation, "failed to parse #{relation_id}, v#{history_relation.version}"
277 assert_relations_are_equal history_relation, version_relation
281 def do_redact_relation(relation, redaction, headers = {})
282 get relation_version_path(:id => relation.relation_id, :version => relation.version)
283 assert_response :success, "should be able to get version #{relation.version} of relation #{relation.relation_id}."
286 post relation_version_redact_path(:id => relation.relation_id, :version => relation.version), :params => { :redaction => redaction.id }, :headers => headers