1 require File.dirname(__FILE__) + '/../test_helper'
2 require 'old_way_controller'
4 class OldWayControllerTest < ActionController::TestCase
8 # test all routes which lead to this controller
11 { :path => "/api/0.6/way/1/history", :method => :get },
12 { :controller => "old_way", :action => "history", :id => "1" }
15 { :path => "/api/0.6/way/1/2", :method => :get },
16 { :controller => "old_way", :action => "version", :id => "1", :version => "2" }
20 # -------------------------------------
21 # Test reading old ways.
22 # -------------------------------------
24 def test_history_visible
25 # check that a visible way is returned properly
26 get :history, :id => ways(:visible_way).way_id
27 assert_response :success
30 def test_history_invisible
31 # check that an invisible way's history is returned properly
32 get :history, :id => ways(:invisible_way).way_id
33 assert_response :success
36 def test_history_invalid
37 # check chat a non-existent way is not returned
38 get :history, :id => 0
39 assert_response :not_found
43 # check that we can retrieve versions of a way
45 check_current_version(current_ways(:visible_way).id)
46 check_current_version(current_ways(:used_way).id)
47 check_current_version(current_ways(:way_with_versions).id)
51 # check that returned history is the same as getting all
52 # versions of a way from the api.
53 def test_history_equals_versions
54 check_history_equals_versions(current_ways(:visible_way).id)
55 check_history_equals_versions(current_ways(:used_way).id)
56 check_history_equals_versions(current_ways(:way_with_versions).id)
60 # test the redaction of an old version of a way, while not being
62 def test_redact_way_unauthorised
63 do_redact_way(ways(:way_with_versions),
65 assert_response :unauthorized, "should need to be authenticated to redact."
69 # test the redaction of an old version of a way, while being
70 # authorised as a normal user.
71 def test_redact_way_normal_user
72 basic_authorization(users(:public_user).email, "test")
74 do_redact_way(ways(:way_with_versions),
76 assert_response :forbidden, "should need to be moderator to redact."
80 # test that, even as moderator, the current version of a way
82 def test_redact_way_current_version
83 basic_authorization(users(:moderator_user).email, "test")
85 do_redact_way(ways(:way_with_versions_v4),
87 assert_response :forbidden, "shouldn't be OK to redact current version as moderator."
91 # test that redacted ways aren't visible, regardless of
92 # authorisation except as moderator...
93 def test_version_redacted
94 way = ways(:way_with_redacted_versions_v2)
96 get :version, :id => way.way_id, :version => way.version
97 assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
99 # not even to a logged-in user
100 basic_authorization(users(:public_user).email, "test")
101 get :version, :id => way.way_id, :version => way.version
102 assert_response :forbidden, "Redacted node shouldn't be visible via the version API, even when logged in."
106 # test that redacted nodes aren't visible in the history
107 def test_history_redacted
108 way = ways(:way_with_redacted_versions_v2)
110 get :history, :id => way.way_id
111 assert_response :success, "Redaction shouldn't have stopped history working."
112 assert_select "osm way[id=#{way.way_id}][version=#{way.version}]", 0, "redacted way #{way.way_id} version #{way.version} shouldn't be present in the history."
114 # not even to a logged-in user
115 basic_authorization(users(:public_user).email, "test")
116 get :version, :id => way.way_id, :version => way.version
117 get :history, :id => way.way_id
118 assert_response :success, "Redaction shouldn't have stopped history working."
119 assert_select "osm way[id=#{way.way_id}][version=#{way.version}]", 0, "redacted node #{way.way_id} version #{way.version} shouldn't be present in the history, even when logged in."
123 # test the redaction of an old version of a way, while being
124 # authorised as a moderator.
125 def test_redact_way_moderator
126 way = ways(:way_with_versions)
127 basic_authorization(users(:moderator_user).email, "test")
129 do_redact_way(way, redactions(:example))
130 assert_response :success, "should be OK to redact old version as moderator."
132 # check moderator can still see the redacted data
133 get :version, :id => way.way_id, :version => way.version
134 assert_response :success, "After redaction, node should not be gone for moderator."
136 # and when accessed via history
137 get :history, :id => way.way_id
138 assert_response :success, "Redaction shouldn't have stopped history working."
139 assert_select "osm way[id=#{way.way_id}][version=#{way.version}]", 1, "way #{way.way_id} version #{way.version} should still be present in the history for moderators."
142 # testing that if the moderator drops auth, he can't see the
143 # redacted stuff any more.
144 def test_redact_way_is_redacted
145 way = ways(:way_with_versions)
146 basic_authorization(users(:moderator_user).email, "test")
148 do_redact_way(way, redactions(:example))
149 assert_response :success, "should be OK to redact old version as moderator."
151 # re-auth as non-moderator
152 basic_authorization(users(:public_user).email, "test")
154 # check can't see the redacted data
155 get :version, :id => way.way_id, :version => way.version
156 assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
158 # and when accessed via history
159 get :version, :id => way.node_id, :version => way.version
160 get :history, :id => way.node_id
161 assert_response :success, "Redaction shouldn't have stopped history working."
162 assert_select "osm way[id=#{way.way_id}][version=#{way.version}]", 0, "redacted way #{way.way_id} version #{way.version} shouldn't be present in the history."
166 # check that the current version of a way is equivalent to the
167 # version which we're getting from the versions call.
168 def check_current_version(way_id)
169 # get the current version
170 current_way = with_controller(WayController.new) do
171 get :read, :id => way_id
172 assert_response :success, "can't get current way #{way_id}"
173 Way.from_xml(@response.body)
175 assert_not_nil current_way, "getting way #{way_id} returned nil"
177 # get the "old" version of the way from the version method
178 get :version, :id => way_id, :version => current_way.version
179 assert_response :success, "can't get old way #{way_id}, v#{current_way.version}"
180 old_way = Way.from_xml(@response.body)
182 # check that the ways are identical
183 assert_ways_are_equal current_way, old_way
187 # look at all the versions of the way in the history and get each version from
188 # the versions call. check that they're the same.
189 def check_history_equals_versions(way_id)
190 get :history, :id => way_id
191 assert_response :success, "can't get way #{way_id} from API"
192 history_doc = XML::Parser.string(@response.body).parse
193 assert_not_nil history_doc, "parsing way #{way_id} history failed"
195 history_doc.find("//osm/way").each do |way_doc|
196 history_way = Way.from_xml_node(way_doc)
197 assert_not_nil history_way, "parsing way #{way_id} version failed"
199 get :version, :id => way_id, :version => history_way.version
200 assert_response :success, "couldn't get way #{way_id}, v#{history_way.version}"
201 version_way = Way.from_xml(@response.body)
202 assert_not_nil version_way, "failed to parse #{way_id}, v#{history_way.version}"
204 assert_ways_are_equal history_way, version_way
208 def do_redact_way(way, redaction)
209 get :version, :id => way.way_id, :version => way.version
210 assert_response :success, "should be able to get version #{way.version} of node #{way.way_id}."
213 post :redact, :id => way.way_id, :version => way.version, :redaction => redaction.id