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, "redacted relation #{relation_v1.relation_id} version #{relation_v1.version} shouldn't be present in the history."
108 # not even to a logged-in user
109 auth_header = basic_authorization_header create(:user).email, "test"
110 get relation_version_path(:id => relation_v1.relation_id, :version => relation_v1.version), :headers => auth_header
111 get api_relation_history_path(:id => relation_v1.relation_id), :headers => auth_header
112 assert_response :success, "Redaction shouldn't have stopped history working."
113 assert_select "osm relation[id='#{relation_v1.relation_id}'][version='#{relation_v1.version}']", 0, "redacted relation #{relation_v1.relation_id} version #{relation_v1.version} shouldn't be present in the history, even when logged in."
117 # test the redaction of an old version of a relation, while being
118 # authorised as a moderator.
119 def test_redact_relation_moderator
120 relation = create(:relation, :with_history, :version => 4)
121 relation_v3 = relation.old_relations.find_by(:version => 3)
123 auth_header = basic_authorization_header create(:moderator_user).email, "test"
125 do_redact_relation(relation_v3, create(:redaction), auth_header)
126 assert_response :success, "should be OK to redact old version as moderator."
128 # check moderator can still see the redacted data, when passing
129 # the appropriate flag
130 get relation_version_path(:id => relation_v3.relation_id, :version => relation_v3.version), :headers => auth_header
131 assert_response :forbidden, "After redaction, relation should be gone for moderator, when flag not passed."
132 get relation_version_path(:id => relation_v3.relation_id, :version => relation_v3.version), :params => { :show_redactions => "true" }, :headers => auth_header
133 assert_response :success, "After redaction, relation should not be gone for moderator, when flag passed."
135 # and when accessed via history
136 get api_relation_history_path(:id => relation_v3.relation_id), :headers => auth_header
137 assert_response :success, "Redaction shouldn't have stopped history working."
138 assert_select "osm relation[id='#{relation_v3.relation_id}'][version='#{relation_v3.version}']", 0, "relation #{relation_v3.relation_id} version #{relation_v3.version} should not be present in the history for moderators when not passing flag."
139 get api_relation_history_path(:id => relation_v3.relation_id), :params => { :show_redactions => "true" }, :headers => auth_header
140 assert_response :success, "Redaction shouldn't have stopped history working."
141 assert_select "osm relation[id='#{relation_v3.relation_id}'][version='#{relation_v3.version}']", 1, "relation #{relation_v3.relation_id} version #{relation_v3.version} should still be present in the history for moderators when passing flag."
144 # testing that if the moderator drops auth, he can't see the
145 # redacted stuff any more.
146 def test_redact_relation_is_redacted
147 relation = create(:relation, :with_history, :version => 4)
148 relation_v3 = relation.old_relations.find_by(:version => 3)
150 auth_header = basic_authorization_header create(:moderator_user).email, "test"
152 do_redact_relation(relation_v3, create(:redaction), auth_header)
153 assert_response :success, "should be OK to redact old version as moderator."
155 # re-auth as non-moderator
156 auth_header = basic_authorization_header create(:user).email, "test"
158 # check can't see the redacted data
159 get relation_version_path(:id => relation_v3.relation_id, :version => relation_v3.version), :headers => auth_header
160 assert_response :forbidden, "Redacted relation shouldn't be visible via the version API."
162 # and when accessed via history
163 get api_relation_history_path(:id => relation_v3.relation_id), :headers => auth_header
164 assert_response :success, "Redaction shouldn't have stopped history working."
165 assert_select "osm relation[id='#{relation_v3.relation_id}'][version='#{relation_v3.version}']", 0, "redacted relation #{relation_v3.relation_id} version #{relation_v3.version} shouldn't be present in the history."
169 # test the unredaction of an old version of a relation, while not being
171 def test_unredact_relation_unauthorised
172 relation = create(:relation, :with_history, :version => 2)
173 relation_v1 = relation.old_relations.find_by(:version => 1)
174 relation_v1.redact!(create(:redaction))
176 post relation_version_redact_path(:id => relation_v1.relation_id, :version => relation_v1.version)
177 assert_response :unauthorized, "should need to be authenticated to unredact."
181 # test the unredaction of an old version of a relation, while being
182 # authorised as a normal user.
183 def test_unredact_relation_normal_user
184 relation = create(:relation, :with_history, :version => 2)
185 relation_v1 = relation.old_relations.find_by(:version => 1)
186 relation_v1.redact!(create(:redaction))
188 auth_header = basic_authorization_header create(:user).email, "test"
190 post relation_version_redact_path(:id => relation_v1.relation_id, :version => relation_v1.version), :headers => auth_header
191 assert_response :forbidden, "should need to be moderator to unredact."
195 # test the unredaction of an old version of a relation, while being
196 # authorised as a moderator.
197 def test_unredact_relation_moderator
198 relation = create(:relation, :with_history, :version => 2)
199 relation_v1 = relation.old_relations.find_by(:version => 1)
200 relation_v1.redact!(create(:redaction))
202 auth_header = basic_authorization_header create(:moderator_user).email, "test"
204 post relation_version_redact_path(:id => relation_v1.relation_id, :version => relation_v1.version), :headers => auth_header
205 assert_response :success, "should be OK to unredact old version as moderator."
207 # check moderator can still see the redacted data, without passing
208 # the appropriate flag
209 get relation_version_path(:id => relation_v1.relation_id, :version => relation_v1.version), :headers => auth_header
210 assert_response :success, "After unredaction, relation should not be gone for moderator."
212 # and when accessed via history
213 get api_relation_history_path(:id => relation_v1.relation_id), :headers => auth_header
214 assert_response :success, "Redaction shouldn't have stopped history working."
215 assert_select "osm relation[id='#{relation_v1.relation_id}'][version='#{relation_v1.version}']", 1, "relation #{relation_v1.relation_id} version #{relation_v1.version} should still be present in the history for moderators."
217 auth_header = basic_authorization_header create(:user).email, "test"
219 # check normal user can now see the redacted data
220 get relation_version_path(:id => relation_v1.relation_id, :version => relation_v1.version), :headers => auth_header
221 assert_response :success, "After redaction, node should not be gone for normal user."
223 # and when accessed via history
224 get api_relation_history_path(:id => relation_v1.relation_id), :headers => auth_header
225 assert_response :success, "Redaction shouldn't have stopped history working."
226 assert_select "osm relation[id='#{relation_v1.relation_id}'][version='#{relation_v1.version}']", 1, "relation #{relation_v1.relation_id} version #{relation_v1.version} should still be present in the history for normal users."
232 # check that the current version of a relation is equivalent to the
233 # version which we're getting from the versions call.
234 def check_current_version(relation_id)
235 # get the current version
236 current_relation = with_controller(RelationsController.new) do
237 get :show, :params => { :id => relation_id }
238 assert_response :success, "can't get current relation #{relation_id}"
239 Relation.from_xml(@response.body)
241 assert_not_nil current_relation, "getting relation #{relation_id} returned nil"
243 # get the "old" version of the relation from the version method
244 get :version, :params => { :id => relation_id, :version => current_relation.version }
245 assert_response :success, "can't get old relation #{relation_id}, v#{current_relation.version}"
246 old_relation = Relation.from_xml(@response.body)
248 # check that the relations are identical
249 assert_relations_are_equal current_relation, old_relation
253 # look at all the versions of the relation in the history and get each version from
254 # the versions call. check that they're the same.
255 def check_history_equals_versions(relation_id)
256 get :history, :params => { :id => relation_id }
257 assert_response :success, "can't get relation #{relation_id} from API"
258 history_doc = XML::Parser.string(@response.body).parse
259 assert_not_nil history_doc, "parsing relation #{relation_id} history failed"
261 history_doc.find("//osm/relation").each do |relation_doc|
262 history_relation = Relation.from_xml_node(relation_doc)
263 assert_not_nil history_relation, "parsing relation #{relation_id} version failed"
265 get :version, :params => { :id => relation_id, :version => history_relation.version }
266 assert_response :success, "couldn't get relation #{relation_id}, v#{history_relation.version}"
267 version_relation = Relation.from_xml(@response.body)
268 assert_not_nil version_relation, "failed to parse #{relation_id}, v#{history_relation.version}"
270 assert_relations_are_equal history_relation, version_relation
274 def do_redact_relation(relation, redaction, headers = {})
275 get relation_version_path(:id => relation.relation_id, :version => relation.version)
276 assert_response :success, "should be able to get version #{relation.version} of relation #{relation.relation_id}."
279 post relation_version_redact_path(:id => relation.relation_id, :version => relation.version), :params => { :redaction => redaction.id }, :headers => headers