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
50 basic_authorization(users(:public_user).email, "test")
52 do_redact_relation(relations(:relation_with_versions_v3),
54 assert_response :forbidden, "should need to be moderator to redact."
58 # test that, even as moderator, the current version of a relation
60 def test_redact_relation_current_version
61 basic_authorization(users(:moderator_user).email, "test")
63 do_redact_relation(relations(:relation_with_versions_v4),
65 assert_response :bad_request, "shouldn't be OK to redact current version as moderator."
69 # test that redacted relations aren't visible, regardless of
70 # authorisation except as moderator...
71 def test_version_redacted
72 relation = relations(:relation_with_redacted_versions_v2)
74 get :version, :id => relation.relation_id, :version => relation.version
75 assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
77 # not even to a logged-in user
78 basic_authorization(users(:public_user).email, "test")
79 get :version, :id => relation.relation_id, :version => relation.version
80 assert_response :forbidden, "Redacted node shouldn't be visible via the version API, even when logged in."
84 # test that redacted nodes aren't visible in the history
85 def test_history_redacted
86 relation = relations(:relation_with_redacted_versions_v2)
88 get :history, :id => relation.relation_id
89 assert_response :success, "Redaction shouldn't have stopped history working."
90 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."
92 # not even to a logged-in user
93 basic_authorization(users(:public_user).email, "test")
94 get :version, :id => relation.relation_id, :version => relation.version
95 get :history, :id => relation.relation_id
96 assert_response :success, "Redaction shouldn't have stopped history working."
97 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."
101 # test the redaction of an old version of a relation, while being
102 # authorised as a moderator.
103 def test_redact_relation_moderator
104 relation = relations(:relation_with_versions_v3)
105 basic_authorization(users(:moderator_user).email, "test")
107 do_redact_relation(relation, redactions(:example))
108 assert_response :success, "should be OK to redact old version as moderator."
110 # check moderator can still see the redacted data, when passing
111 # the appropriate flag
112 get :version, :id => relation.relation_id, :version => relation.version
113 assert_response :forbidden, "After redaction, node should be gone for moderator, when flag not passed."
114 get :version, :id => relation.relation_id, :version => relation.version, :show_redactions => 'true'
115 assert_response :success, "After redaction, node should not be gone for moderator, when flag passed."
117 # and when accessed via history
118 get :history, :id => relation.relation_id
119 assert_response :success, "Redaction shouldn't have stopped history working."
120 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."
121 get :history, :id => relation.relation_id, :show_redactions => 'true'
122 assert_response :success, "Redaction shouldn't have stopped history working."
123 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."
126 # testing that if the moderator drops auth, he can't see the
127 # redacted stuff any more.
128 def test_redact_relation_is_redacted
129 relation = relations(:relation_with_versions_v3)
130 basic_authorization(users(:moderator_user).email, "test")
132 do_redact_relation(relation, redactions(:example))
133 assert_response :success, "should be OK to redact old version as moderator."
135 # re-auth as non-moderator
136 basic_authorization(users(:public_user).email, "test")
138 # check can't see the redacted data
139 get :version, :id => relation.relation_id, :version => relation.version
140 assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
142 # and when accessed via history
143 get :history, :id => relation.relation_id
144 assert_response :success, "Redaction shouldn't have stopped history working."
145 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."
149 # check that the current version of a relation is equivalent to the
150 # version which we're getting from the versions call.
151 def check_current_version(relation_id)
152 # get the current version
153 current_relation = with_controller(RelationController.new) do
154 get :read, :id => relation_id
155 assert_response :success, "can't get current relation #{relation_id}"
156 Relation.from_xml(@response.body)
158 assert_not_nil current_relation, "getting relation #{relation_id} returned nil"
160 # get the "old" version of the relation from the version method
161 get :version, :id => relation_id, :version => current_relation.version
162 assert_response :success, "can't get old relation #{relation_id}, v#{current_relation.version}"
163 old_relation = Relation.from_xml(@response.body)
165 # check that the relations are identical
166 assert_relations_are_equal current_relation, old_relation
170 # look at all the versions of the relation in the history and get each version from
171 # the versions call. check that they're the same.
172 def check_history_equals_versions(relation_id)
173 get :history, :id => relation_id
174 assert_response :success, "can't get relation #{relation_id} from API"
175 history_doc = XML::Parser.string(@response.body).parse
176 assert_not_nil history_doc, "parsing relation #{relation_id} history failed"
178 history_doc.find("//osm/relation").each do |relation_doc|
179 history_relation = Relation.from_xml_node(relation_doc)
180 assert_not_nil history_relation, "parsing relation #{relation_id} version failed"
182 get :version, :id => relation_id, :version => history_relation.version
183 assert_response :success, "couldn't get relation #{relation_id}, v#{history_relation.version}"
184 version_relation = Relation.from_xml(@response.body)
185 assert_not_nil version_relation, "failed to parse #{relation_id}, v#{history_relation.version}"
187 assert_relations_are_equal history_relation, version_relation
191 def do_redact_relation(relation, redaction)
192 get :version, :id => relation.relation_id, :version => relation.version
193 assert_response :success, "should be able to get version #{relation.version} of node #{relation.relation_id}."
196 post :redact, :id => relation.relation_id, :version => relation.version, :redaction => redaction.id