1 require File.dirname(__FILE__) + '/../test_helper'
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" }
20 # -------------------------------------
21 # Test reading old relations.
22 # -------------------------------------
24 # check that a visible relations is returned properly
25 get :history, :id => relations(:visible_relation).relation_id
26 assert_response :success
28 # check chat a non-existent relations is not returned
29 get :history, :id => 0
30 assert_response :not_found
34 # test the redaction of an old version of a relation, while not being
36 def test_redact_relation_unauthorised
37 do_redact_relation(relations(:relation_with_versions_v3),
39 assert_response :unauthorized, "should need to be authenticated to redact."
43 # test the redaction of an old version of a relation, while being
44 # authorised as a normal user.
45 def test_redact_relation_normal_user
46 basic_authorization(users(:public_user).email, "test")
48 do_redact_relation(relations(:relation_with_versions_v3),
50 assert_response :forbidden, "should need to be moderator to redact."
54 # test that, even as moderator, the current version of a relation
56 def test_redact_relation_current_version
57 basic_authorization(users(:moderator_user).email, "test")
59 do_redact_relation(relations(:relation_with_versions_v4),
61 assert_response :bad_request, "shouldn't be OK to redact current version as moderator."
65 # test that redacted relations aren't visible, regardless of
66 # authorisation except as moderator...
67 def test_version_redacted
68 relation = relations(:relation_with_redacted_versions_v2)
70 get :version, :id => relation.relation_id, :version => relation.version
71 assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
73 # not even to a logged-in user
74 basic_authorization(users(:public_user).email, "test")
75 get :version, :id => relation.relation_id, :version => relation.version
76 assert_response :forbidden, "Redacted node shouldn't be visible via the version API, even when logged in."
80 # test that redacted nodes aren't visible in the history
81 def test_history_redacted
82 relation = relations(:relation_with_redacted_versions_v2)
84 get :history, :id => relation.relation_id
85 assert_response :success, "Redaction shouldn't have stopped history working."
86 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."
88 # not even to a logged-in user
89 basic_authorization(users(:public_user).email, "test")
90 get :version, :id => relation.relation_id, :version => relation.version
91 get :history, :id => relation.relation_id
92 assert_response :success, "Redaction shouldn't have stopped history working."
93 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."
97 # test the redaction of an old version of a relation, while being
98 # authorised as a moderator.
99 def test_redact_relation_moderator
100 relation = relations(:relation_with_versions_v3)
101 basic_authorization(users(:moderator_user).email, "test")
103 do_redact_relation(relation, redactions(:example))
104 assert_response :success, "should be OK to redact old version as moderator."
106 # check moderator can still see the redacted data, when passing
107 # the appropriate flag
108 get :version, :id => relation.relation_id, :version => relation.version
109 assert_response :forbidden, "After redaction, node should be gone for moderator, when flag not passed."
110 get :version, :id => relation.relation_id, :version => relation.version, :show_redactions => 'true'
111 assert_response :success, "After redaction, node should not be gone for moderator, when flag passed."
113 # and when accessed via history
114 get :history, :id => relation.relation_id
115 assert_response :success, "Redaction shouldn't have stopped history working."
116 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."
117 get :history, :id => relation.relation_id, :show_redactions => 'true'
118 assert_response :success, "Redaction shouldn't have stopped history working."
119 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."
122 # testing that if the moderator drops auth, he can't see the
123 # redacted stuff any more.
124 def test_redact_relation_is_redacted
125 relation = relations(:relation_with_versions_v3)
126 basic_authorization(users(:moderator_user).email, "test")
128 do_redact_relation(relation, redactions(:example))
129 assert_response :success, "should be OK to redact old version as moderator."
131 # re-auth as non-moderator
132 basic_authorization(users(:public_user).email, "test")
134 # check can't see the redacted data
135 get :version, :id => relation.relation_id, :version => relation.version
136 assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
138 # and when accessed via history
139 get :history, :id => relation.relation_id
140 assert_response :success, "Redaction shouldn't have stopped history working."
141 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."
145 # check that the current version of a relation is equivalent to the
146 # version which we're getting from the versions call.
147 def check_current_version(relation_id)
148 # get the current version
149 current_relation = with_controller(RelationController.new) do
150 get :read, :id => relation_id
151 assert_response :success, "can't get current relation #{relation_id}"
152 Relation.from_xml(@response.body)
154 assert_not_nil current_relation, "getting relation #{relation_id} returned nil"
156 # get the "old" version of the relation from the version method
157 get :version, :id => relation_id, :version => current_relation.version
158 assert_response :success, "can't get old relation #{relation_id}, v#{current_relation.version}"
159 old_relation = Relation.from_xml(@response.body)
161 # check that the relations are identical
162 assert_relations_are_equal current_relation, old_relation
166 # look at all the versions of the relation in the history and get each version from
167 # the versions call. check that they're the same.
168 def check_history_equals_versions(relation_id)
169 get :history, :id => relation_id
170 assert_response :success, "can't get relation #{relation_id} from API"
171 history_doc = XML::Parser.string(@response.body).parse
172 assert_not_nil history_doc, "parsing relation #{relation_id} history failed"
174 history_doc.find("//osm/relation").each do |relation_doc|
175 history_relation = Relation.from_xml_node(relation_doc)
176 assert_not_nil history_relation, "parsing relation #{relation_id} version failed"
178 get :version, :id => relation_id, :version => history_relation.version
179 assert_response :success, "couldn't get relation #{relation_id}, v#{history_relation.version}"
180 version_relation = Relation.from_xml(@response.body)
181 assert_not_nil version_relation, "failed to parse #{relation_id}, v#{history_relation.version}"
183 assert_relations_are_equal history_relation, version_relation
187 def do_redact_relation(relation, redaction)
188 get :version, :id => relation.relation_id, :version => relation.version
189 assert_response :success, "should be able to get version #{relation.version} of node #{relation.relation_id}."
192 post :redact, :id => relation.relation_id, :version => relation.version, :redaction => redaction.id