2 require "old_relation_controller"
4 class OldRelationControllerTest < ActionController::TestCase
8 # test all routes which lead to this controller
11 { :path => "/api/0.6/relation/1/history", :method => :get },
12 { :controller => "old_relation", :action => "history", :id => "1" }
15 { :path => "/api/0.6/relation/1/2", :method => :get },
16 { :controller => "old_relation", :action => "version", :id => "1", :version => "2" }
19 { :path => "/api/0.6/relation/1/2/redact", :method => :post },
20 { :controller => "old_relation", :action => "redact", :id => "1", :version => "2" }
24 # -------------------------------------
25 # Test reading old relations.
26 # -------------------------------------
28 # check that a visible relations is returned properly
29 get :history, :id => relations(:visible_relation).relation_id
30 assert_response :success
32 # check chat a non-existent relations is not returned
33 get :history, :id => 0
34 assert_response :not_found
38 # test the redaction of an old version of a relation, while not being
40 def test_redact_relation_unauthorised
41 do_redact_relation(relations(:relation_with_versions_v3),
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
51 basic_authorization(user.email, "test")
53 do_redact_relation(relations(:relation_with_versions_v3),
54 create(:redaction, :user => user))
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 moderator_user = create(:moderator_user)
63 basic_authorization(moderator_user.email, "test")
65 do_redact_relation(relations(:relation_with_versions_v4),
66 create(:redaction, :user => moderator_user))
67 assert_response :bad_request, "shouldn't be OK to redact current version as moderator."
71 # test that redacted relations aren't visible, regardless of
72 # authorisation except as moderator...
73 def test_version_redacted
74 relation = relations(:relation_with_redacted_versions_v2)
76 get :version, :id => relation.relation_id, :version => relation.version
77 assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
79 # not even to a logged-in user
80 basic_authorization(users(:public_user).email, "test")
81 get :version, :id => relation.relation_id, :version => relation.version
82 assert_response :forbidden, "Redacted node shouldn't be visible via the version API, even when logged in."
86 # test that redacted nodes aren't visible in the history
87 def test_history_redacted
88 relation = relations(:relation_with_redacted_versions_v2)
90 get :history, :id => relation.relation_id
91 assert_response :success, "Redaction shouldn't have stopped history working."
92 assert_select "osm relation[id='#{relation.relation_id}'][version='#{relation.version}']", 0, "redacted relation #{relation.relation_id} version #{relation.version} shouldn't be present in the history."
94 # not even to a logged-in user
95 basic_authorization(users(:public_user).email, "test")
96 get :version, :id => relation.relation_id, :version => relation.version
97 get :history, :id => relation.relation_id
98 assert_response :success, "Redaction shouldn't have stopped history working."
99 assert_select "osm relation[id='#{relation.relation_id}'][version='#{relation.version}']", 0, "redacted node #{relation.relation_id} version #{relation.version} shouldn't be present in the history, even when logged in."
103 # test the redaction of an old version of a relation, while being
104 # authorised as a moderator.
105 def test_redact_relation_moderator
106 moderator_user = create(:moderator_user)
107 relation = relations(:relation_with_versions_v3)
108 basic_authorization(moderator_user.email, "test")
110 do_redact_relation(relation, create(:redaction, :user => moderator_user))
111 assert_response :success, "should be OK to redact old version as moderator."
113 # check moderator can still see the redacted data, when passing
114 # the appropriate flag
115 get :version, :id => relation.relation_id, :version => relation.version
116 assert_response :forbidden, "After redaction, node should be gone for moderator, when flag not passed."
117 get :version, :id => relation.relation_id, :version => relation.version, :show_redactions => "true"
118 assert_response :success, "After redaction, node should not be gone for moderator, when flag passed."
120 # and when accessed via history
121 get :history, :id => relation.relation_id
122 assert_response :success, "Redaction shouldn't have stopped history working."
123 assert_select "osm relation[id='#{relation.relation_id}'][version='#{relation.version}']", 0, "relation #{relation.relation_id} version #{relation.version} should not be present in the history for moderators when not passing flag."
124 get :history, :id => relation.relation_id, :show_redactions => "true"
125 assert_response :success, "Redaction shouldn't have stopped history working."
126 assert_select "osm relation[id='#{relation.relation_id}'][version='#{relation.version}']", 1, "relation #{relation.relation_id} version #{relation.version} should still be present in the history for moderators when passing flag."
129 # testing that if the moderator drops auth, he can't see the
130 # redacted stuff any more.
131 def test_redact_relation_is_redacted
132 moderator_user = create(:moderator_user)
133 relation = relations(:relation_with_versions_v3)
134 basic_authorization(moderator_user.email, "test")
136 do_redact_relation(relation, create(:redaction, :user => moderator_user))
137 assert_response :success, "should be OK to redact old version as moderator."
139 # re-auth as non-moderator
140 basic_authorization(users(:public_user).email, "test")
142 # check can't see the redacted data
143 get :version, :id => relation.relation_id, :version => relation.version
144 assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
146 # and when accessed via history
147 get :history, :id => relation.relation_id
148 assert_response :success, "Redaction shouldn't have stopped history working."
149 assert_select "osm relation[id='#{relation.relation_id}'][version='#{relation.version}']", 0, "redacted relation #{relation.relation_id} version #{relation.version} shouldn't be present in the history."
153 # test the unredaction of an old version of a relation, while not being
155 def test_unredact_relation_unauthorised
156 relation = relations(:relation_with_redacted_versions_v3)
158 post :redact, :id => relation.relation_id, :version => relation.version
159 assert_response :unauthorized, "should need to be authenticated to unredact."
163 # test the unredaction of an old version of a relation, while being
164 # authorised as a normal user.
165 def test_unredact_relation_normal_user
166 relation = relations(:relation_with_redacted_versions_v3)
167 basic_authorization(users(:public_user).email, "test")
169 post :redact, :id => relation.relation_id, :version => relation.version
170 assert_response :forbidden, "should need to be moderator to unredact."
174 # test the unredaction of an old version of a relation, while being
175 # authorised as a moderator.
176 def test_unredact_relation_moderator
177 relation = relations(:relation_with_redacted_versions_v3)
178 basic_authorization(users(:moderator_user).email, "test")
180 post :redact, :id => relation.relation_id, :version => relation.version
181 assert_response :success, "should be OK to unredact old version as moderator."
183 # check moderator can still see the redacted data, without passing
184 # the appropriate flag
185 get :version, :id => relation.relation_id, :version => relation.version
186 assert_response :success, "After redaction, node should not be gone for moderator, when flag passed."
188 # and when accessed via history
189 get :history, :id => relation.relation_id
190 assert_response :success, "Redaction shouldn't have stopped history working."
191 assert_select "osm relation[id='#{relation.relation_id}'][version='#{relation.version}']", 1, "relation #{relation.relation_id} version #{relation.version} should still be present in the history for moderators when passing flag."
193 basic_authorization(users(:normal_user).email, "test")
195 # check normal user can now see the redacted data
196 get :version, :id => relation.relation_id, :version => relation.version
197 assert_response :success, "After redaction, node should not be gone for moderator, when flag passed."
199 # and when accessed via history
200 get :history, :id => relation.relation_id
201 assert_response :success, "Redaction shouldn't have stopped history working."
202 assert_select "osm relation[id='#{relation.relation_id}'][version='#{relation.version}']", 1, "relation #{relation.relation_id} version #{relation.version} should still be present in the history for moderators when passing flag."
208 # check that the current version of a relation is equivalent to the
209 # version which we're getting from the versions call.
210 def check_current_version(relation_id)
211 # get the current version
212 current_relation = with_controller(RelationController.new) do
213 get :read, :id => relation_id
214 assert_response :success, "can't get current relation #{relation_id}"
215 Relation.from_xml(@response.body)
217 assert_not_nil current_relation, "getting relation #{relation_id} returned nil"
219 # get the "old" version of the relation from the version method
220 get :version, :id => relation_id, :version => current_relation.version
221 assert_response :success, "can't get old relation #{relation_id}, v#{current_relation.version}"
222 old_relation = Relation.from_xml(@response.body)
224 # check that the relations are identical
225 assert_relations_are_equal current_relation, old_relation
229 # look at all the versions of the relation in the history and get each version from
230 # the versions call. check that they're the same.
231 def check_history_equals_versions(relation_id)
232 get :history, :id => relation_id
233 assert_response :success, "can't get relation #{relation_id} from API"
234 history_doc = XML::Parser.string(@response.body).parse
235 assert_not_nil history_doc, "parsing relation #{relation_id} history failed"
237 history_doc.find("//osm/relation").each do |relation_doc|
238 history_relation = Relation.from_xml_node(relation_doc)
239 assert_not_nil history_relation, "parsing relation #{relation_id} version failed"
241 get :version, :id => relation_id, :version => history_relation.version
242 assert_response :success, "couldn't get relation #{relation_id}, v#{history_relation.version}"
243 version_relation = Relation.from_xml(@response.body)
244 assert_not_nil version_relation, "failed to parse #{relation_id}, v#{history_relation.version}"
246 assert_relations_are_equal history_relation, version_relation
250 def do_redact_relation(relation, redaction)
251 get :version, :id => relation.relation_id, :version => relation.version
252 assert_response :success, "should be able to get version #{relation.version} of relation #{relation.relation_id}."
255 post :redact, :id => relation.relation_id, :version => relation.version, :redaction => redaction.id