4 class OldRelationsControllerTest < ActionController::TestCase
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/2/redact", :method => :post },
18 { :controller => "api/old_relations", :action => "redact", :id => "1", :version => "2" }
22 # -------------------------------------
23 # Test reading old relations.
24 # -------------------------------------
26 # check that a visible relations is returned properly
27 get :history, :params => { :id => create(:relation, :with_history).id }
28 assert_response :success
30 # check chat a non-existent relations is not returned
31 get :history, :params => { :id => 0 }
32 assert_response :not_found
36 # test the redaction of an old version of a relation, while not being
38 def test_redact_relation_unauthorised
39 relation = create(:relation, :with_history, :version => 4)
40 relation_v3 = relation.old_relations.find_by(:version => 3)
42 do_redact_relation(relation_v3, create(:redaction))
43 assert_response :unauthorized, "should need to be authenticated to redact."
47 # test the redaction of an old version of a relation, while being
48 # authorised as a normal user.
49 def test_redact_relation_normal_user
50 relation = create(:relation, :with_history, :version => 4)
51 relation_v3 = relation.old_relations.find_by(:version => 3)
53 basic_authorization create(:user).email, "test"
55 do_redact_relation(relation_v3, create(:redaction))
56 assert_response :forbidden, "should need to be moderator to redact."
60 # test that, even as moderator, the current version of a relation
62 def test_redact_relation_current_version
63 relation = create(:relation, :with_history, :version => 4)
64 relation_latest = relation.old_relations.last
66 basic_authorization create(:moderator_user).email, "test"
68 do_redact_relation(relation_latest, create(:redaction))
69 assert_response :bad_request, "shouldn't be OK to redact current version as moderator."
73 # test that redacted relations aren't visible, regardless of
74 # authorisation except as moderator...
75 def test_version_redacted
76 relation = create(:relation, :with_history, :version => 2)
77 relation_v1 = relation.old_relations.find_by(:version => 1)
78 relation_v1.redact!(create(:redaction))
80 get :version, :params => { :id => relation_v1.relation_id, :version => relation_v1.version }
81 assert_response :forbidden, "Redacted relation shouldn't be visible via the version API."
83 # not even to a logged-in user
84 basic_authorization create(:user).email, "test"
85 get :version, :params => { :id => relation_v1.relation_id, :version => relation_v1.version }
86 assert_response :forbidden, "Redacted relation shouldn't be visible via the version API, even when logged in."
90 # test that redacted relations aren't visible in the history
91 def test_history_redacted
92 relation = create(:relation, :with_history, :version => 2)
93 relation_v1 = relation.old_relations.find_by(:version => 1)
94 relation_v1.redact!(create(:redaction))
96 get :history, :params => { :id => relation_v1.relation_id }
97 assert_response :success, "Redaction shouldn't have stopped history working."
98 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."
100 # not even to a logged-in user
101 basic_authorization create(:user).email, "test"
102 get :version, :params => { :id => relation_v1.relation_id, :version => relation_v1.version }
103 get :history, :params => { :id => relation_v1.relation_id }
104 assert_response :success, "Redaction shouldn't have stopped history working."
105 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."
109 # test the redaction of an old version of a relation, while being
110 # authorised as a moderator.
111 def test_redact_relation_moderator
112 relation = create(:relation, :with_history, :version => 4)
113 relation_v3 = relation.old_relations.find_by(:version => 3)
115 basic_authorization create(:moderator_user).email, "test"
117 do_redact_relation(relation_v3, create(:redaction))
118 assert_response :success, "should be OK to redact old version as moderator."
120 # check moderator can still see the redacted data, when passing
121 # the appropriate flag
122 get :version, :params => { :id => relation_v3.relation_id, :version => relation_v3.version }
123 assert_response :forbidden, "After redaction, relation should be gone for moderator, when flag not passed."
124 get :version, :params => { :id => relation_v3.relation_id, :version => relation_v3.version, :show_redactions => "true" }
125 assert_response :success, "After redaction, relation should not be gone for moderator, when flag passed."
127 # and when accessed via history
128 get :history, :params => { :id => relation_v3.relation_id }
129 assert_response :success, "Redaction shouldn't have stopped history working."
130 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."
131 get :history, :params => { :id => relation_v3.relation_id, :show_redactions => "true" }
132 assert_response :success, "Redaction shouldn't have stopped history working."
133 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."
136 # testing that if the moderator drops auth, he can't see the
137 # redacted stuff any more.
138 def test_redact_relation_is_redacted
139 relation = create(:relation, :with_history, :version => 4)
140 relation_v3 = relation.old_relations.find_by(:version => 3)
142 basic_authorization create(:moderator_user).email, "test"
144 do_redact_relation(relation_v3, create(:redaction))
145 assert_response :success, "should be OK to redact old version as moderator."
147 # re-auth as non-moderator
148 basic_authorization create(:user).email, "test"
150 # check can't see the redacted data
151 get :version, :params => { :id => relation_v3.relation_id, :version => relation_v3.version }
152 assert_response :forbidden, "Redacted relation shouldn't be visible via the version API."
154 # and when accessed via history
155 get :history, :params => { :id => relation_v3.relation_id }
156 assert_response :success, "Redaction shouldn't have stopped history working."
157 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."
161 # test the unredaction of an old version of a relation, while not being
163 def test_unredact_relation_unauthorised
164 relation = create(:relation, :with_history, :version => 2)
165 relation_v1 = relation.old_relations.find_by(:version => 1)
166 relation_v1.redact!(create(:redaction))
168 post :redact, :params => { :id => relation_v1.relation_id, :version => relation_v1.version }
169 assert_response :unauthorized, "should need to be authenticated to unredact."
173 # test the unredaction of an old version of a relation, while being
174 # authorised as a normal user.
175 def test_unredact_relation_normal_user
176 relation = create(:relation, :with_history, :version => 2)
177 relation_v1 = relation.old_relations.find_by(:version => 1)
178 relation_v1.redact!(create(:redaction))
180 basic_authorization create(:user).email, "test"
182 post :redact, :params => { :id => relation_v1.relation_id, :version => relation_v1.version }
183 assert_response :forbidden, "should need to be moderator to unredact."
187 # test the unredaction of an old version of a relation, while being
188 # authorised as a moderator.
189 def test_unredact_relation_moderator
190 relation = create(:relation, :with_history, :version => 2)
191 relation_v1 = relation.old_relations.find_by(:version => 1)
192 relation_v1.redact!(create(:redaction))
194 basic_authorization create(:moderator_user).email, "test"
196 post :redact, :params => { :id => relation_v1.relation_id, :version => relation_v1.version }
197 assert_response :success, "should be OK to unredact old version as moderator."
199 # check moderator can still see the redacted data, without passing
200 # the appropriate flag
201 get :version, :params => { :id => relation_v1.relation_id, :version => relation_v1.version }
202 assert_response :success, "After unredaction, relation should not be gone for moderator."
204 # and when accessed via history
205 get :history, :params => { :id => relation_v1.relation_id }
206 assert_response :success, "Redaction shouldn't have stopped history working."
207 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."
209 basic_authorization create(:user).email, "test"
211 # check normal user can now see the redacted data
212 get :version, :params => { :id => relation_v1.relation_id, :version => relation_v1.version }
213 assert_response :success, "After redaction, node should not be gone for normal user."
215 # and when accessed via history
216 get :history, :params => { :id => relation_v1.relation_id }
217 assert_response :success, "Redaction shouldn't have stopped history working."
218 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."
224 # check that the current version of a relation is equivalent to the
225 # version which we're getting from the versions call.
226 def check_current_version(relation_id)
227 # get the current version
228 current_relation = with_controller(RelationsController.new) do
229 get :show, :params => { :id => relation_id }
230 assert_response :success, "can't get current relation #{relation_id}"
231 Relation.from_xml(@response.body)
233 assert_not_nil current_relation, "getting relation #{relation_id} returned nil"
235 # get the "old" version of the relation from the version method
236 get :version, :params => { :id => relation_id, :version => current_relation.version }
237 assert_response :success, "can't get old relation #{relation_id}, v#{current_relation.version}"
238 old_relation = Relation.from_xml(@response.body)
240 # check that the relations are identical
241 assert_relations_are_equal current_relation, old_relation
245 # look at all the versions of the relation in the history and get each version from
246 # the versions call. check that they're the same.
247 def check_history_equals_versions(relation_id)
248 get :history, :params => { :id => relation_id }
249 assert_response :success, "can't get relation #{relation_id} from API"
250 history_doc = XML::Parser.string(@response.body).parse
251 assert_not_nil history_doc, "parsing relation #{relation_id} history failed"
253 history_doc.find("//osm/relation").each do |relation_doc|
254 history_relation = Relation.from_xml_node(relation_doc)
255 assert_not_nil history_relation, "parsing relation #{relation_id} version failed"
257 get :version, :params => { :id => relation_id, :version => history_relation.version }
258 assert_response :success, "couldn't get relation #{relation_id}, v#{history_relation.version}"
259 version_relation = Relation.from_xml(@response.body)
260 assert_not_nil version_relation, "failed to parse #{relation_id}, v#{history_relation.version}"
262 assert_relations_are_equal history_relation, version_relation
266 def do_redact_relation(relation, redaction)
267 get :version, :params => { :id => relation.relation_id, :version => relation.version }
268 assert_response :success, "should be able to get version #{relation.version} of relation #{relation.relation_id}."
271 post :redact, :params => { :id => relation.relation_id, :version => relation.version, :redaction => redaction.id }