3 class OldRelationsControllerTest < ActionController::TestCase
5 # test all routes which lead to this controller
8 { :path => "/api/0.6/relation/1/history", :method => :get },
9 { :controller => "old_relations", :action => "history", :id => "1" }
12 { :path => "/api/0.6/relation/1/2", :method => :get },
13 { :controller => "old_relations", :action => "version", :id => "1", :version => "2" }
16 { :path => "/api/0.6/relation/1/2/redact", :method => :post },
17 { :controller => "old_relations", :action => "redact", :id => "1", :version => "2" }
21 # -------------------------------------
22 # Test reading old relations.
23 # -------------------------------------
25 # check that a visible relations is returned properly
26 get :history, :params => { :id => create(:relation, :with_history).id }
27 assert_response :success
29 # check chat a non-existent relations is not returned
30 get :history, :params => { :id => 0 }
31 assert_response :not_found
35 # test the redaction of an old version of a relation, while not being
37 def test_redact_relation_unauthorised
38 relation = create(:relation, :with_history, :version => 4)
39 relation_v3 = relation.old_relations.find_by(:version => 3)
41 do_redact_relation(relation_v3, create(:redaction))
42 assert_response :unauthorized, "should need to be authenticated to redact."
46 # test the redaction of an old version of a relation, while being
47 # authorised as a normal user.
48 def test_redact_relation_normal_user
49 relation = create(:relation, :with_history, :version => 4)
50 relation_v3 = relation.old_relations.find_by(:version => 3)
52 basic_authorization create(:user).email, "test"
54 do_redact_relation(relation_v3, create(:redaction))
55 assert_response :forbidden, "should need to be moderator to redact."
59 # test that, even as moderator, the current version of a relation
61 def test_redact_relation_current_version
62 relation = create(:relation, :with_history, :version => 4)
63 relation_latest = relation.old_relations.last
65 basic_authorization create(:moderator_user).email, "test"
67 do_redact_relation(relation_latest, create(:redaction))
68 assert_response :bad_request, "shouldn't be OK to redact current version as moderator."
72 # test that redacted relations aren't visible, regardless of
73 # authorisation except as moderator...
74 def test_version_redacted
75 relation = create(:relation, :with_history, :version => 2)
76 relation_v1 = relation.old_relations.find_by(:version => 1)
77 relation_v1.redact!(create(:redaction))
79 get :version, :params => { :id => relation_v1.relation_id, :version => relation_v1.version }
80 assert_response :forbidden, "Redacted relation shouldn't be visible via the version API."
82 # not even to a logged-in user
83 basic_authorization create(:user).email, "test"
84 get :version, :params => { :id => relation_v1.relation_id, :version => relation_v1.version }
85 assert_response :forbidden, "Redacted relation shouldn't be visible via the version API, even when logged in."
89 # test that redacted relations aren't visible in the history
90 def test_history_redacted
91 relation = create(:relation, :with_history, :version => 2)
92 relation_v1 = relation.old_relations.find_by(:version => 1)
93 relation_v1.redact!(create(:redaction))
95 get :history, :params => { :id => relation_v1.relation_id }
96 assert_response :success, "Redaction shouldn't have stopped history working."
97 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."
99 # not even to a logged-in user
100 basic_authorization create(:user).email, "test"
101 get :version, :params => { :id => relation_v1.relation_id, :version => relation_v1.version }
102 get :history, :params => { :id => relation_v1.relation_id }
103 assert_response :success, "Redaction shouldn't have stopped history working."
104 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."
108 # test the redaction of an old version of a relation, while being
109 # authorised as a moderator.
110 def test_redact_relation_moderator
111 relation = create(:relation, :with_history, :version => 4)
112 relation_v3 = relation.old_relations.find_by(:version => 3)
114 basic_authorization create(:moderator_user).email, "test"
116 do_redact_relation(relation_v3, create(:redaction))
117 assert_response :success, "should be OK to redact old version as moderator."
119 # check moderator can still see the redacted data, when passing
120 # the appropriate flag
121 get :version, :params => { :id => relation_v3.relation_id, :version => relation_v3.version }
122 assert_response :forbidden, "After redaction, relation should be gone for moderator, when flag not passed."
123 get :version, :params => { :id => relation_v3.relation_id, :version => relation_v3.version, :show_redactions => "true" }
124 assert_response :success, "After redaction, relation should not be gone for moderator, when flag passed."
126 # and when accessed via history
127 get :history, :params => { :id => relation_v3.relation_id }
128 assert_response :success, "Redaction shouldn't have stopped history working."
129 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."
130 get :history, :params => { :id => relation_v3.relation_id, :show_redactions => "true" }
131 assert_response :success, "Redaction shouldn't have stopped history working."
132 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."
135 # testing that if the moderator drops auth, he can't see the
136 # redacted stuff any more.
137 def test_redact_relation_is_redacted
138 relation = create(:relation, :with_history, :version => 4)
139 relation_v3 = relation.old_relations.find_by(:version => 3)
141 basic_authorization create(:moderator_user).email, "test"
143 do_redact_relation(relation_v3, create(:redaction))
144 assert_response :success, "should be OK to redact old version as moderator."
146 # re-auth as non-moderator
147 basic_authorization create(:user).email, "test"
149 # check can't see the redacted data
150 get :version, :params => { :id => relation_v3.relation_id, :version => relation_v3.version }
151 assert_response :forbidden, "Redacted relation shouldn't be visible via the version API."
153 # and when accessed via history
154 get :history, :params => { :id => relation_v3.relation_id }
155 assert_response :success, "Redaction shouldn't have stopped history working."
156 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."
160 # test the unredaction of an old version of a relation, while not being
162 def test_unredact_relation_unauthorised
163 relation = create(:relation, :with_history, :version => 2)
164 relation_v1 = relation.old_relations.find_by(:version => 1)
165 relation_v1.redact!(create(:redaction))
167 post :redact, :params => { :id => relation_v1.relation_id, :version => relation_v1.version }
168 assert_response :unauthorized, "should need to be authenticated to unredact."
172 # test the unredaction of an old version of a relation, while being
173 # authorised as a normal user.
174 def test_unredact_relation_normal_user
175 relation = create(:relation, :with_history, :version => 2)
176 relation_v1 = relation.old_relations.find_by(:version => 1)
177 relation_v1.redact!(create(:redaction))
179 basic_authorization create(:user).email, "test"
181 post :redact, :params => { :id => relation_v1.relation_id, :version => relation_v1.version }
182 assert_response :forbidden, "should need to be moderator to unredact."
186 # test the unredaction of an old version of a relation, while being
187 # authorised as a moderator.
188 def test_unredact_relation_moderator
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 basic_authorization create(:moderator_user).email, "test"
195 post :redact, :params => { :id => relation_v1.relation_id, :version => relation_v1.version }
196 assert_response :success, "should be OK to unredact old version as moderator."
198 # check moderator can still see the redacted data, without passing
199 # the appropriate flag
200 get :version, :params => { :id => relation_v1.relation_id, :version => relation_v1.version }
201 assert_response :success, "After unredaction, relation should not be gone for moderator."
203 # and when accessed via history
204 get :history, :params => { :id => relation_v1.relation_id }
205 assert_response :success, "Redaction shouldn't have stopped history working."
206 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."
208 basic_authorization create(:user).email, "test"
210 # check normal user can now see the redacted data
211 get :version, :params => { :id => relation_v1.relation_id, :version => relation_v1.version }
212 assert_response :success, "After redaction, node should not be gone for normal user."
214 # and when accessed via history
215 get :history, :params => { :id => relation_v1.relation_id }
216 assert_response :success, "Redaction shouldn't have stopped history working."
217 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."
223 # check that the current version of a relation is equivalent to the
224 # version which we're getting from the versions call.
225 def check_current_version(relation_id)
226 # get the current version
227 current_relation = with_controller(RelationsController.new) do
228 get :show, :params => { :id => relation_id }
229 assert_response :success, "can't get current relation #{relation_id}"
230 Relation.from_xml(@response.body)
232 assert_not_nil current_relation, "getting relation #{relation_id} returned nil"
234 # get the "old" version of the relation from the version method
235 get :version, :params => { :id => relation_id, :version => current_relation.version }
236 assert_response :success, "can't get old relation #{relation_id}, v#{current_relation.version}"
237 old_relation = Relation.from_xml(@response.body)
239 # check that the relations are identical
240 assert_relations_are_equal current_relation, old_relation
244 # look at all the versions of the relation in the history and get each version from
245 # the versions call. check that they're the same.
246 def check_history_equals_versions(relation_id)
247 get :history, :params => { :id => relation_id }
248 assert_response :success, "can't get relation #{relation_id} from API"
249 history_doc = XML::Parser.string(@response.body).parse
250 assert_not_nil history_doc, "parsing relation #{relation_id} history failed"
252 history_doc.find("//osm/relation").each do |relation_doc|
253 history_relation = Relation.from_xml_node(relation_doc)
254 assert_not_nil history_relation, "parsing relation #{relation_id} version failed"
256 get :version, :params => { :id => relation_id, :version => history_relation.version }
257 assert_response :success, "couldn't get relation #{relation_id}, v#{history_relation.version}"
258 version_relation = Relation.from_xml(@response.body)
259 assert_not_nil version_relation, "failed to parse #{relation_id}, v#{history_relation.version}"
261 assert_relations_are_equal history_relation, version_relation
265 def do_redact_relation(relation, redaction)
266 get :version, :params => { :id => relation.relation_id, :version => relation.version }
267 assert_response :success, "should be able to get version #{relation.version} of relation #{relation.relation_id}."
270 post :redact, :params => { :id => relation.relation_id, :version => relation.version, :redaction => redaction.id }